Lisp lệnh MT trong chủ đề “vẽ đường chú thích thanh thép (đường mũi tên chỉ)”
Sorry cdhn !
Mấy hôm nay do mình phải nhảy như sóc trong công việc & việc học lisp nên không chú tâm lắm trong code
Nên mình bỏ sót mất trường hợp khi bản vẽ đã bị thay đổi UCS (không còn là World), hoặc khi đường dóng không cắt thanh thép
Dẫn đến các sai sót về tọa đọ điểm ... >>> kết quả là các lỗi mà bạn đã nêu trên.
Nay mình test lại, đã phát hiện lỗi & fix dưới đây:
p/s: Nếu bạn chạy lisp trên bản vẽ bạn đã từng chạy nó mà vẫn xảy ra lỗi (ko thấy có mũi tên - chính xác là mũi tên insert ko đúng vị trí) thì tìm và xóa hết block có tên "mui_ten_hiep" >>> dùng lệnh PU để xóa block này rồi chạy lại lisp nhé !
;lisp ve mui ten ghi chu thep
(defun c:MT( / temperr errorTrap lst_va old lay lay_thep ss pt1 pt2 tl ent1 pt lst_pt)
;ham bay loi
(setq temperr *error*)
(defun errorTrap (msg)
(if old (mapcar 'setvar lst_va old))
(if lay (setvar 'clayer lay))
(cond
((tblsearch "ucs" "save_ucs")
(command "ucs" "na" "r" "save_ucs")
(command "ucs" "na" "d" "save_ucs")
)
)
(setq *error* temperr)
(princ "\n*** Da set lai bien, OK ! ***")
(princ)
)
(setq *error* errorTrap)
;======het ham bay loi = P1 ============================
(setq lst_va '("osmode" "cmdecho" "AUNITS")
old (mapcar 'getvar lst_va))
;=================
(if (not(tblsearch "layer" "DONG_MAU_DO")) (MakeLayer "DONG_MAU_DO" 1 nil nil T))
(setq lay (getvar 'clayer))
(setvar 'clayer "DONG_MAU_DO")
;=================
(command "ucs" "na" "s" "save_ucs")
(command "ucs" "w")
(cond ((not(tblsearch "block" "mui_ten_hiep"))
(entmake (list
'(0 . "TRACE")
'(100 . "AcDbEntity")
;(cons 8 "0")
'(100 . "AcDbTrace")
'(10 0 0 0)
'(11 0 0 0)
'(12 -2.5 -0.5 0)
'(13 -2.5 0.5 0)
)
)
(command "-block" "mui_ten_hiep" '(0 0 0) (entlast) "")
))
;=================
(setq lay_thep (assoc 8 (entget (car (entsel "\nChon thanh thep mau: ")))))
(prompt "\nChon cac thanh thep can ghi chu thich !")
(setq ss (ssget (append '((0 . "LINE,LWPOLYLINE")) (list lay_thep)))
pt1 (getpoint "\nXac dinh 2 diem tren duong dong ghi chu !\nChon diem goc: ")
pt2 (getpoint pt1 "\nChon diem phia ngon mui ten: ")
)
(mapcar 'setvar lst_va '(0 0 3))
(setq #tl# (NGT #tl# 1.0 getreal "Nhap ti le "))
(if (and ss pt1 pt2 #tl#)
(progn
(MAKELINE pt1 pt2 nil nil nil nil nil)
(setq ent1 (entlast))
(foreach elem (ss2lst ss)
(setq lst1 (acet-geom-intersectwith ent1 elem 3)
pt (car (vl-sort lst1 '(lambda (x y) (< (distance pt1 x) (distance pt1 y)))))
lst_pt (cons pt lst_pt))
(command "-insert" "mui_ten_hiep" "S" (* 1.5 #tl#) "R" (angle pt1 pt2) pt)
) ;for
(setq lst_pt (vl-sort lst_pt '(lambda (x y) (> (distance pt1 x) (distance pt1 y)))))
(entmod (subst (cons 11 (car lst_pt)) (assoc 11 (setq info (entget ent1))) info))
)
(princ "\n*** Dau vao chu hop ly ***")
) ;if
(command "ucs" "na" "r" "save_ucs")
(command "ucs" "na" "d" "save_ucs")
(setq *error* temperr)
(setvar 'clayer lay)
(mapcar 'setvar lst_va old)
(princ)
)
;=================================
(defun MakeLine (PT1 PT2 Linetype LTScale Layer Color xdata)
(entmakex (list '(0 . "LINE")
(cons 8 (if Layer Layer (getvar "Clayer")))
(cons 6 (if Linetype Linetype "bylayer"))
(cons 48 (if LTScale LTScale 1))
(cons 62 (if Color Color 256))
(cons 10 PT1) (cons 11 PT2)))
)
;===================================
(defun ss2lst (ss / ename i lst)
;chuyen ss thanh list
(setq i 0)
(repeat (sslength ss)
(setq ename (ssname ss i)
i (1+ i)
lst (cons ename lst))
)
(reverse lst)
)
;================================
(defun MakeLayer (name color linetype lineWeight plot)
(entmakex (list '(0 . "LAYER")
(cons 100 "AcDbSymbolTableRecord")
(cons 100 "AcDbLayerTableRecord")
(cons 2 name)
(cons 70 0)
(cons 62 (if color color 7))
(cons 6 (if linetype linetype "Continuous"))
(cons 290 (if plot 1 0))
(cons 370 (if lineWeight (fix (* 100 lineWeight)) -3))))
)
;=================================
(defun NGT(a mac_dinh ham str_nhac / modul)
;;Nhan gia tri
(or a (setq a mac_dinh))
(setq a (cond
((= "" (setq modul (ham (strcat "\n" str_nhac " <" (vl-princ-to-string a) ">: ")))) a)
(modul)
(a)
)
)
)
- Tải lisp
- 318273_mt.lsp
Cách dùng
- Tải tệp 318273_mt.lsp về máy.
- 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ẽ.
- Gõ lệnh MT để chạy lisp.
Mã nguồn 318273_mt.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/112275-v%E1%BA%BD-%C4%91%C6%B0%E1%BB%9Dng-ch%C3%BA-th%C3%ADch-thanh-th%C3%A9p-%C4%91%C6%B0%E1%BB%9Dng-m%C5%A9i-t%C3%AAn-ch%E1%BB%89/
;lisp ve mui ten ghi chu thep
(defun c:MT( / temperr errorTrap lst_va old lay lay_thep ss pt1 pt2 tl ent1 pt lst_pt)
;ham bay loi
(setq temperr *error*)
(defun errorTrap (msg)
(if old (mapcar 'setvar lst_va old))
(if lay (setvar 'clayer lay))
(cond
((tblsearch "ucs" "save_ucs")
(command "ucs" "na" "r" "save_ucs")
(command "ucs" "na" "d" "save_ucs")
)
)
(setq *error* temperr)
(princ "\n*** Da set lai bien, OK ! ***")
(princ)
)
(setq *error* errorTrap)
;======het ham bay loi = P1 ============================
(setq lst_va '("osmode" "cmdecho" "AUNITS")
old (mapcar 'getvar lst_va))
;=================
(if (not(tblsearch "layer" "DONG_MAU_DO")) (MakeLayer "DONG_MAU_DO" 1 nil nil T))
(setq lay (getvar 'clayer))
(setvar 'clayer "DONG_MAU_DO")
;=================
(command "ucs" "na" "s" "save_ucs")
(command "ucs" "w")
(cond ((not(tblsearch "block" "mui_ten_hiep"))
(entmake (list
'(0 . "TRACE")
'(100 . "AcDbEntity")
;(cons 8 "0")
'(100 . "AcDbTrace")
'(10 0 0 0)
'(11 0 0 0)
'(12 -2.5 -0.5 0)
'(13 -2.5 0.5 0)
)
)
(command "-block" "mui_ten_hiep" '(0 0 0) (entlast) "")
))
;=================
(setq lay_thep (assoc 8 (entget (car (entsel "\nChon thanh thep mau: ")))))
(prompt "\nChon cac thanh thep can ghi chu thich !")
(setq ss (ssget (append '((0 . "LINE,LWPOLYLINE")) (list lay_thep)))
pt1 (getpoint "\nXac dinh 2 diem tren duong dong ghi chu !\nChon diem goc: ")
pt2 (getpoint pt1 "\nChon diem phia ngon mui ten: ")
)
(mapcar 'setvar lst_va '(0 0 3))
(setq #tl# (NGT #tl# 1.0 getreal "Nhap ti le "))
(if (and ss pt1 pt2 #tl#)
(progn
(MAKELINE pt1 pt2 nil nil nil nil nil)
(setq ent1 (entlast))
(foreach elem (ss2lst ss)
(setq lst1 (acet-geom-intersectwith ent1 elem 3)
pt (car (vl-sort lst1 '(lambda (x y) (< (distance pt1 x) (distance pt1 y)))))
lst_pt (cons pt lst_pt))
(command "-insert" "mui_ten_hiep" "S" (* 1.5 #tl#) "R" (angle pt1 pt2) pt)
) ;for
(setq lst_pt (vl-sort lst_pt '(lambda (x y) (> (distance pt1 x) (distance pt1 y)))))
(entmod (subst (cons 11 (car lst_pt)) (assoc 11 (setq info (entget ent1))) info))
)
(princ "\n*** Dau vao chu hop ly ***")
) ;if
(command "ucs" "na" "r" "save_ucs")
(command "ucs" "na" "d" "save_ucs")
(setq *error* temperr)
(setvar 'clayer lay)
(mapcar 'setvar lst_va old)
(princ)
)
;=================================
(defun MakeLine (PT1 PT2 Linetype LTScale Layer Color xdata)
(entmakex (list '(0 . "LINE")
(cons 8 (if Layer Layer (getvar "Clayer")))
(cons 6 (if Linetype Linetype "bylayer"))
(cons 48 (if LTScale LTScale 1))
(cons 62 (if Color Color 256))
(cons 10 PT1) (cons 11 PT2)))
)
;===================================
(defun ss2lst (ss / ename i lst)
;chuyen ss thanh list
(setq i 0)
(repeat (sslength ss)
(setq ename (ssname ss i)
i (1+ i)
lst (cons ename lst))
)
(reverse lst)
)
;================================
(defun MakeLayer (name color linetype lineWeight plot)
(entmakex (list '(0 . "LAYER")
(cons 100 "AcDbSymbolTableRecord")
(cons 100 "AcDbLayerTableRecord")
(cons 2 name)
(cons 70 0)
(cons 62 (if color color 7))
(cons 6 (if linetype linetype "Continuous"))
(cons 290 (if plot 1 0))
(cons 370 (if lineWeight (fix (* 100 lineWeight)) -3))))
)
;=================================
(defun NGT(a mac_dinh ham str_nhac / modul)
;;Nhan gia tri
(or a (setq a mac_dinh))
(setq a (cond
((= "" (setq modul (ham (strcat "\n" str_nhac " <" (vl-princ-to-string a) ">: ")))) a)
(modul)
(a)
)
)
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề vẽ đường chú thích thanh thép (đường mũi tên chỉ). Xem đúng bài viết #318273 để đọc hướng dẫn và trao đổi thêm.