Lisp lệnh GG trong chủ đề “Xin Trợ Giúp Về Lisp Ghi Độ Dài Đường Thẳng Ra Block Att”
Tôi mạn phép sửa code của bác Trịnh Công Sơn cho bạn vuonghung108 nhé!
;;;------------------------------
;;;-----by MENZI ENGINEERING ----
(defun SetAtt (obj lst / attval)
(mapcar '(lambda (att)
(if (setq attval (cdr (assoc (vla-get-TagString att) lst)))
(vla-put-TextString att attval)
)
)
(vlax-invoke obj 'GetAttributes)
)
(vla-update obj)
)
;;;------------------------------
(defun TS:select (/ ent)
(while
(progn
(setvar 'errno 0)
(setq ent (entsel "\nCh\U+1ECDn Line,Pline,SPline \U+0111\U+1EC3 l\U+1EA5y chi\U+1EC1u d\U+00E0i :"))
(cond
((= 7 (getvar 'errno))
(princ "\nCh\U+1ECDn l\U+1ED7i.Vui l\U+00F2ng l\U+1EA1i.")
)
((= 'ename (type (car ent)))
(if (wcmatch (cdr (assoc 0 (entget (car ent))))
"*LINE"
)
(progn (setq ent (car ent))
nil
)
(princ
"\n\U+0110\U+1ED1i t\U+01B0\U+1EE3ng ch\U+1ECDn kh\U+00F4ng ph\U+1EA3i LINE,PLINE,SPLINE."
)
)
)
)
)
)
ent
)
;;;-------------------------------
(defun GetLen (ent / len)
(setq len (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent)))
)
;;;-------------------------------
(defun GetDxf (n elist) (cdr (assoc n elist)))
;;;-------------------------------
(defun c:GG (/ *error* blkatt ent len)
(vl-load-com)
(setvar "Modemacro" "@ Tr\U+1EA7n C\U+00F4ng S\U+01A1n _ XDDD & CN")
(defun *error* (msg)
(if ent
(redraw ent 4)
)
(if (not (wcmatch (strcase msg) "*BREAK,*EXIT*,*CANCEL*"))
(princ (strcat "\n** Error: " msg " **"))
)
(princ)
)
(while (setq ent (TS:select))
(redraw ent 3)
(setq Len (rtos (GetLen ent) 2 2))
(if (and (setq BlkATT (car (entsel "\nChon Block ATT:")))
(wcmatch (GetDxf 0 (entget BlkATT)) "INSERT")
(= (GetDxf 66 (entget BlkATT)) 1)
)
(SetAtt (vlax-ename->Vla-Object BlkATT) (list (cons "CHIEUDAI" Len)))
(alert
"\n\U+0110\U+1ED1i t\U+01B0\U+1EE3ng ch\U+1ECDn kh\U+00F4ng ph\U+1EA3i Block ATTribute."
)
)
(redraw ent 4)
)
(princ)
)
Tôi mặc định 2 số thập phân. Bạn có thể thay đổi tùy ý trong hàm (rtos (GetLen ent) 2 2) ở trên.
VD: Muốn kết quả la 3 sô thập phân thì sửa nó lại thành (rtos (GetLen ent) 2 3)
- Tải lisp
- 407430_gg.lsp
Cách dùng
- Tải tệp 407430_gg.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 GG để chạy lisp.
Mã nguồn 407430_gg.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/147900-xin-tr%E1%BB%A3-gi%C3%BAp-v%E1%BB%81-lisp-ghi-%C4%91%E1%BB%99-d%C3%A0i-%C4%91%C6%B0%E1%BB%9Dng-th%E1%BA%B3ng-ra-block-att/
;;;------------------------------;;;-----by MENZI ENGINEERING ----(defun SetAtt (obj lst / attval)(mapcar '(lambda (att)(if (setq attval (cdr (assoc (vla-get-TagString att) lst)))(vla-put-TextString att attval)))(vlax-invoke obj 'GetAttributes))(vla-update obj));;;------------------------------(defun TS:select (/ ent)(while(progn(setvar 'errno 0)(setq ent (entsel "\nCh\U+1ECDn Line,Pline,SPline \U+0111\U+1EC3 l\U+1EA5y chi\U+1EC1u d\U+00E0i :"))(cond((= 7 (getvar 'errno))(princ "\nCh\U+1ECDn l\U+1ED7i.Vui l\U+00F2ng l\U+1EA1i."))((= 'ename (type (car ent)))(if (wcmatch (cdr (assoc 0 (entget (car ent))))"*LINE")(progn (setq ent (car ent))nil)(princ"\n\U+0110\U+1ED1i t\U+01B0\U+1EE3ng ch\U+1ECDn kh\U+00F4ng ph\U+1EA3i LINE,PLINE,SPLINE."))))))ent);;;-------------------------------(defun GetLen (ent / len)(setq len (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent))));;;-------------------------------(defun GetDxf (n elist) (cdr (assoc n elist)));;;-------------------------------(defun c:GG (/ *error* blkatt ent len)(vl-load-com)(setvar "Modemacro" "@ Tr\U+1EA7n C\U+00F4ng S\U+01A1n _ XDDD & CN")(defun *error* (msg)(if ent(redraw ent 4))(if (not (wcmatch (strcase msg) "*BREAK,*EXIT*,*CANCEL*"))(princ (strcat "\n** Error: " msg " **")))(princ))(while (setq ent (TS:select))(redraw ent 3)(setq Len (rtos (GetLen ent) 2 2))(if (and (setq BlkATT (car (entsel "\nChon Block ATT:")))(wcmatch (GetDxf 0 (entget BlkATT)) "INSERT")(= (GetDxf 66 (entget BlkATT)) 1))(SetAtt (vlax-ename->Vla-Object BlkATT) (list (cons "CHIEUDAI" Len)))(alert"\n\U+0110\U+1ED1i t\U+01B0\U+1EE3ng ch\U+1ECDn kh\U+00F4ng ph\U+1EA3i Block ATTribute."))(redraw ent 4))(princ))
Thảo luận gốc
Lisp được chia sẻ trong chủ đề Xin Trợ Giúp Về Lisp Ghi Độ Dài Đường Thẳng Ra Block Att. Xem đúng bài viết #407430 để đọc hướng dẫn và trao đổi thêm.