Jump to content

Lisp lệnh CHL CHL trong chủ đề “Xử lý bản vẽ bị lẻ số”

- Bạn đang vẽ một bản cad lẻ toét, các số cứ sêm sêm nhau cỡ: 8399.9899 thay vì 8400. Mặc dù bạn đánh kích thước thì vẫn có kết quả là 8400 nhưng rất 'ngứa ngáy'. Số lẻ này làm một lệnh (như pedit, hatch)không hoạt động tốt. Làm gì để các con số này chẵn với nhau? làm sao để các kích thước không còn lẻ?

- Đơn giản, bạn sử dụng chương trình làm chẵn của cadviet, appload chương trình lên, sau đó sử dụng lệnh chl chương trình sẽ yêu cầu bạn chọn các đối tượng muốn làm chẵn và điểm gốc chẵn (để các tọa độ sẽ chẵn với điểm gốc này), và mức làm chẵn. Ví dụ bạn muốn làm chẵn đến đơn vị mét (là 1000) thì bạn nhập mức 5..


(defun c:chl (/ p ss x y solt mlt)
(defun lamtron (so / t1)
(setq
t1 (abs so)
t2 (* solt (float (fix (/ t1 solt))))
t1 (cond
((< (- t1 t2) (* 0.5 solt)) t2)
(t (+ t2 (* 1.0 solt)))
)
)
(if (> so 0.0)
t1
(* -1.0 t1)
)
)
(defun chlone (ent / p1 p2 dx dy tt x1 y1 z1 x2 y2 z2)
(setq
tt (entget ent)
)
(foreach pp tt
(if (or (= (car pp) 10)
(= (car pp) 11)
)
(progn
(setq
p1 (cdr pp)

x1 (car p1)
y1 (cadr p1)
z1 (cond
((= (length p1) 3) (caddr p1))
(t nil)
)
dx1 (- x1 x)
dy1 (- y1 y)
dx1 (lamtron dx1)
dy1 (lamtron dy1)
x1 (+ x dx1)
y1 (+ y dy1)

p1m (cond
(z1 (list x1 y1 z1))
(t (list x1 y1))
)

tt (subst (cons (car pp) p1m) pp tt)
)
(entmod tt)
)
)
)

)
(H:init)
(setq ss (ssget) ;'((0 . "LINE")))
muctron (list 1.0 5.0 10.0 100.0 500.0 1000.0 2000.0 5000.0
100000.0)
p (getpoint "\nDiem chen: ")
temp (getint
"\nMuc lam tron: 0=1, 1=5, 2=10, 3=100, 4=500, 5=1000, 6=2000, 7=5000, 8=100000 <0>: "
)
mlt (cond
(temp temp)
(t 0)
)
x (car p)
y (cadr p)
solt 1.0
)
(setq solt (nth mlt muctron))
(H:sudung chlone ss)
(H:done)
)

;;------------------------------------------------------------
(defun H:newerror (msg)
(if (and (/= msg "Function cancelled")
(/= msg "quit / exit abort")
)
(princ (strcat "\n" msg))
)
(H:done)
)
;;----------
(defun H:init ()
(setq
H:VAR_CMDECHO
(getvar "CMDECHO")
H:VAR_OLDERROR
*error*
*error* H:newerror

)
(setvar "CMDECHO" 0)
(command ".undo" "BE")
)
;;----------
(defun H:done ()
(if (null command-s)
(command ".redraw")
(command-s ".redraw")
)
(if (null command-s)
(command ".undo" "E")
(command-s ".undo" "E")
)
(if H:VAR_CMDECHO
(setvar "CMDECHO" H:VAR_CMDECHO)
)
(if H:VAR_OLDERROR
(setq *error* H:VAR_OLDERROR)
)
(princ)
)
;;----------
(defun H:sudung (hamsudung ss /)
(length
(vl-remove-if
'null
(mapcar 'hamsudung
(vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
)
)
)
)

(defun c:chl (/ p ss x y solt mlt)
(defun lamtron (so / t1)
(setq
t1 (abs so)
t2 (* solt (float (fix (/ t1 solt))))
t1 (cond
((< (- t1 t2) (* 0.5 solt)) t2)
(t (+ t2 (* 1.0 solt)))
)
)
(if (> so 0.0)
t1
(* -1.0 t1)
)
)
(defun chlone (ent / p1 p2 dx dy tt x1 y1 z1 x2 y2 z2)
(setq
tt (entget ent)
)
(foreach pp tt
(if (or (= (car pp) 10)
(= (car pp) 11)
)
(progn
(setq
p1 (cdr pp)

x1 (car p1)
y1 (cadr p1)
z1 (cond
((= (length p1) 3) (caddr p1))
(t nil)
)
dx1 (- x1 x)
dy1 (- y1 y)
dx1 (lamtron dx1)
dy1 (lamtron dy1)
x1 (+ x dx1)
y1 (+ y dy1)

p1m (cond
(z1 (list x1 y1 z1))
(t (list x1 y1))
)

tt (subst (cons (car pp) p1m) pp tt)
)
(entmod tt)
)
)
)

)
(H:init)
(setq ss (ssget) ;'((0 . "LINE")))
muctron (list 1.0 5.0 10.0 100.0 500.0 1000.0 2000.0 5000.0
100000.0)
p (getpoint "\nDiem chen: ")
temp (getint
"\nMuc lam tron: 0=1, 1=5, 2=10, 3=100, 4=500, 5=1000, 6=2000, 7=5000, 8=100000 <0>: "
)
mlt (cond
(temp temp)
(t 0)
)
x (car p)
y (cadr p)
solt 1.0
)
(setq solt (nth mlt muctron))
(H:sudung chlone ss)
(H:done)
)

Cách dùng

  1. Tải tệp 132_chl_chl.lsp về máy.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh CHL CHL để chạy lisp.

Mã nguồn 132_chl_chl.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/56-x%E1%BB%AD-l%C3%BD-b%E1%BA%A3n-v%E1%BA%BD-b%E1%BB%8B-l%E1%BA%BB-s%E1%BB%91/

(defun c:chl (/ p ss x y solt mlt)
(defun lamtron (so / t1)
(setq
t1 (abs so)
t2 (* solt (float (fix (/ t1 solt))))
t1 (cond
((< (- t1 t2) (* 0.5 solt)) t2)
(t (+ t2 (* 1.0 solt)))
)
)
(if (> so 0.0)
t1
(* -1.0 t1)
)
)
(defun chlone (ent / p1 p2 dx dy tt x1 y1 z1 x2 y2 z2)
(setq
tt (entget ent)
)
(foreach pp tt
(if (or (= (car pp) 10)
(= (car pp) 11)
)
(progn
(setq
p1 (cdr pp) 
x1 (car p1)
y1 (cadr p1)
z1 (cond
((= (length p1) 3) (caddr p1))
(t nil)
)
dx1 (- x1 x)
dy1 (- y1 y)
dx1 (lamtron dx1)
dy1 (lamtron dy1)
x1 (+ x dx1)
y1 (+ y dy1) 
p1m (cond
(z1 (list x1 y1 z1))
(t (list x1 y1))
) 
tt (subst (cons (car pp) p1m) pp tt)
)
(entmod tt)
)
)
) 
)
(H:init)
(setq ss (ssget) ;'((0 . "LINE")))
muctron (list 1.0 5.0 10.0 100.0 500.0 1000.0 2000.0 5000.0
100000.0)
p (getpoint "\nDiem chen: ")
temp (getint
"\nMuc lam tron: 0=1, 1=5, 2=10, 3=100, 4=500, 5=1000, 6=2000, 7=5000, 8=100000 <0>: "
)
mlt (cond
(temp temp)
(t 0)
)
x (car p)
y (cadr p)
solt 1.0
)
(setq solt (nth mlt muctron))
(H:sudung chlone ss)
(H:done)
) 
;;------------------------------------------------------------
(defun H:newerror (msg)
(if (and (/= msg "Function cancelled")
(/= msg "quit / exit abort")
)
(princ (strcat "\n" msg))
)
(H:done)
)
;;----------
(defun H:init ()
(setq
H:VAR_CMDECHO
(getvar "CMDECHO")
H:VAR_OLDERROR
*error*
*error* H:newerror 
)
(setvar "CMDECHO" 0)
(command ".undo" "BE")
)
;;----------
(defun H:done ()
(if (null command-s)
(command ".redraw")
(command-s ".redraw")
)
(if (null command-s)
(command ".undo" "E")
(command-s ".undo" "E")
)
(if H:VAR_CMDECHO
(setvar "CMDECHO" H:VAR_CMDECHO)
)
(if H:VAR_OLDERROR
(setq *error* H:VAR_OLDERROR)
)
(princ)
)
;;----------
(defun H:sudung (hamsudung ss /)
(length
(vl-remove-if
'null
(mapcar 'hamsudung
(vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
)
)
)
) 
(defun c:chl (/ p ss x y solt mlt)
(defun lamtron (so / t1)
(setq
t1 (abs so)
t2 (* solt (float (fix (/ t1 solt))))
t1 (cond
((< (- t1 t2) (* 0.5 solt)) t2)
(t (+ t2 (* 1.0 solt)))
)
)
(if (> so 0.0)
t1
(* -1.0 t1)
)
)
(defun chlone (ent / p1 p2 dx dy tt x1 y1 z1 x2 y2 z2)
(setq
tt (entget ent)
)
(foreach pp tt
(if (or (= (car pp) 10)
(= (car pp) 11)
)
(progn
(setq
p1 (cdr pp) 
x1 (car p1)
y1 (cadr p1)
z1 (cond
((= (length p1) 3) (caddr p1))
(t nil)
)
dx1 (- x1 x)
dy1 (- y1 y)
dx1 (lamtron dx1)
dy1 (lamtron dy1)
x1 (+ x dx1)
y1 (+ y dy1) 
p1m (cond
(z1 (list x1 y1 z1))
(t (list x1 y1))
) 
tt (subst (cons (car pp) p1m) pp tt)
)
(entmod tt)
)
)
) 
)
(H:init)
(setq ss (ssget) ;'((0 . "LINE")))
muctron (list 1.0 5.0 10.0 100.0 500.0 1000.0 2000.0 5000.0
100000.0)
p (getpoint "\nDiem chen: ")
temp (getint
"\nMuc lam tron: 0=1, 1=5, 2=10, 3=100, 4=500, 5=1000, 6=2000, 7=5000, 8=100000 <0>: "
)
mlt (cond
(temp temp)
(t 0)
)
x (car p)
y (cadr p)
solt 1.0
)
(setq solt (nth mlt muctron))
(H:sudung chlone ss)
(H:done)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Xử lý bản vẽ bị lẻ số. Xem đúng bài viết #132 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 220)