Jump to content

Lisp lệnh DILEADER DIL DILEDIT trong chủ đề “Hỏi về Lisp (thuật toán, ý tưởng, coding,...)”

Các bác cho em hỏi, khi entmake Mleader thì làm sao để user Block (ở content) hiện hành ạ? Mtext thì ok, nhưng block thì phải vào lại mleaderstyle để chọn lại blk (kể cả block của CAD). Mong các bác chỉ giáo!

Nhoc có sưu tầm lisp này, anh quocmanh04tt có mót ji thì mót nhé :P

(princ "\nDILEADER (DIL) v. 1.2 by Andrea Andreetti -Loaded-")
(defun c:DILEADER () (C:DIL)) (vl-load-com)
(defun c:DIL (/ Llead Ltext MainDLactive DQobject DragMess PointMethod ASPoint
DQLcursorPosition OKaccept Ltext Input DLactive DL_textsize VLAo DXItemHandle 
DQL_llpoint DQL_urpoint cen)
(setq DL_textsize (getvar "DIMTXT")) (setvar "CMDECHO" 0)
(while (not DQobject) (setq DQobject (nentsel "\nSelect Object...")))
(if DQobject (progn (princ "\nPick Text location...")
(DleaderWhile nil (vlax-ename->vla-object (car DQobject)) nil)
(while (and (/= (car input) 25)		;RIGHT CLICK
(/= (car input) 11) (/= (car input) 12) (/= (car input) 3)		;LEFT CLICK
(not (and (= (car input) 2) (= (cadr input) 32))) ;ESCAPE
(not (and (= (car input) 2) (= (cadr input) 13)))	) ;ENTER
(DleaderWhile nil (vlax-ename->vla-object (car DQobject)) nil)	)))
(princ "\nDleader Finish.") (princ))
;;DLEADER While Loop;;
(defun DleaderWhile (nnn VLAo Lleadprop	/ NewMod Ltext) 
;TEXTEname;VLAobject;LeaderPropreties
(vla-StartUndoMark (vla-get-ActiveDocument (vlax-get-ACAD-Object)))
(setq DXItemHandle (vla-get-Handle VLAo)) (Setq DLactive T)
(while (and DLactive (setq input (grread t 4 4))
(or (= (car input) 5) (= (car input) 3))	) ;Cursor;PickPoint
(if (= (car input) 5) (setq DQLcursorPosition (cadr input)))
(if (and VLAo DQLcursorPosition)
(DLEADER_C&M_OBJECT DQLcursorPosition nnn VLAo cen Lleadprop)	)
(if (= (car input) 3) (progn (setq DLactive nil) (if (and Ltext (not nnn))
(command "._MTEDIT" (cdar (entmod (subst (cons 1 "") (assoc 1 (entget Ltext)) (entget Ltext)))))	)))) ;while
(if (and Ltext DXLeaderHandle DXItemHandle)
(DLEADERPUTXDATA Ltext "DLEADER_TEXT"
(vl-prin1-to-string (list DXLeaderHandle DXItemHandle))	))
(if (and NewMod DXLeaderHandle DXItemHandle)
(DLEADERPUTXDATA NewMod "DLEADER_TEXT"
(vl-prin1-to-string (list DXLeaderHandle DXItemHandle))	))
(vla-EndUndoMark (vla-get-ActiveDocument (vlax-get-ACAD-Object)))
(setq DLactive nil)	)
;;DLEADER While Loop ;;DLEADER UPDATE for REACTOR
(defun Dleader_Updated_By_reactor (cursp cenpoint Lleadprop TXTobj)
(setq int (vlax-curve-getClosestPointTo VLAo cursp))
(setq DL_textsize (vla-get-Height (vlax-ename->vla-object TXTobj)))
(if (and int Lleadprop) (progn (if (> (car cursp) (car int))
(setq v71 1 co1 (list (- (car cursp) DL_textsize) (- (nth 1 cursp) (/ DL_textsize 2))
(nth 2 cursp)) co2 (list (- (car cursp) (/ DL_textsize 3)) (- (nth 1 cursp) (/ DL_textsize 2))
(nth 2 cursp)) LR 1)
(setq v71 3 co1 (list (+ (car cursp) DL_textsize) (- (nth 1 cursp) (/ DL_textsize 2)) 
(nth 2 cursp)) co2 (list (+ (car cursp) (/ DL_textsize 3)) (- (nth 1 cursp) (/ DL_textsize 2)) 
(nth 2 cursp)) LR 3)	)
(repeat 3 (setq Lleadprop (vl-remove (assoc 10 Lleadprop) Lleadprop)))
(setq Lleadprop (entmod (append Lleadprop (list (cons 10 int)) (list (cons 10 co1)) 
(list (cons 10 co2)) )))
(setq Llead (cdar (entmod (subst (cons 71 v71) (assoc 71 Lleadprop) Lleadprop))))
(setq TXTobjdata (entget TXTobj))
(setq Ltext (cdar (entmod (subst (cons 71 LR) (assoc 71 TXTobjdata) TXTobjdata))))
(setq DXLeaderHandle (cdr (assoc 5 (entget Llead)))) (setq int nil Lleadprop nil)	)))
;;DLEADER UPDATE for REACTOR;;DLEADER CREATE & MODIFY OBJECT
(defun DLEADER_C&M_OBJECT (cursp DLtext VLAo cenpoint Lleadprop / FicLine)
(setq int (vlax-curve-getClosestPointTo VLAo cursp))   
;;Create Mtext
(if Ltext (progn (command "._erase" Ltext "") (setq Ltext nil)))
(if (> (car cursp) (car int)) (setq v71 1
co1 (list (- (car cursp) DL_textsize) (- (nth 1 cursp) (/ DL_textsize 2))(nth 2 cursp))
co2 (list (- (car cursp) (/ DL_textsize 3)) (- (nth 1 cursp) (/ DL_textsize 2)) (nth 2 cursp)) )
(setq v71 3
co1 (list (+ (car cursp) DL_textsize) (- (nth 1 cursp) (/ DL_textsize 2)) (nth 2 cursp))
co2 (list (+ (car cursp) (/ DL_textsize 3)) (- (nth 1 cursp) (/ DL_textsize 2)) (nth 2 cursp)) ))  
(if (and int (not DLtext)) (progn
(setq Ltext (entmakex (list (cons 0 "MTEXT") (cons 100 "AcDbEntity")
(cons 100 "AcDbMText") (cons 1 "NOTE") (cons 10 cursp) (cons 40 DL_textsize)
(cons 50 0.0) (cons 71 v71) (cons 72 5))))	))
(if (and int DLtext) (progn
(setq NewMod (entmod(subst (cons 10 cursp)(assoc 10 DLtext) DLtext)))
(setq NewMod (cdar (entmod (subst (cons 71 v71)(assoc 71 NewMod) NewMod))))	))
;;Create Leader
(if (and int (not Lleadprop)) (progn
(if Llead (progn (command "._erase" Llead "") (setq Llead nil)))
(setq Llead (entmakex (list (cons 0 "LEADER") (cons 100 "AcDbEntity")
(cons 100 "AcDbLeader")	(cons 10 int) (cons 10 co1) (cons 10 co2))))
(vla-put-ArrowheadSize (vlax-ename->vla-object Llead) (getvar "DIMASZ"))
(setq DXLeaderHandle (cdr (assoc 5 (entget Llead))))	))  
(if (and int Lleadprop) (progn
(repeat 3 (setq Lleadprop (vl-remove (assoc 10 Lleadprop) Lleadprop)))
(setq Lleadprop (entmod (append Lleadprop (list (cons 10 int)) (list (cons 10 co1)) 
(list (cons 10 co2)) )))
(setq Llead (cdar (entmod (subst (cons 71 v71)(assoc 71 Lleadprop) Lleadprop))))
(setq DXLeaderHandle (cdr (assoc 5 (entget Llead))))	)) )
;;DLEADER CREATE & MODIFY OBJECT;;X D A T A;;
(defun DLEADERPUTXDATA (item xdataname tag / ent type1 valeur)
(setq ent (vlax-ename->vla-object item) type1 (vlax-make-safearray vlax-vbInteger 
(cons 0 1)) Valeur (vlax-make-safearray vlax-vbVariant (cons 0 1)))
(vlax-safearray-put-element type1 0 1001) 
(vlax-safearray-put-element valeur 0 xdataname)
(vlax-safearray-put-element type1 1 1000) (vlax-safearray-put-element valeur 1 tag)
(setq type1  (vlax-make-variant type1) valeur (vlax-make-variant valeur))
(vla-setxdata ent type1 valeur) )
;;X D A T A;;E D I T O R;;
(defun c:DILEDIT (/ sbs Llead Lleadprop vla-QDLitem LtextData valeur type1 Ltexti 
FLtext ent)
(setq sbs nil) (while (not sbs)
(setq Ltexti (nentsel "\nSelect TEXT Object..."))
(if Ltexti (progn (setq ent (vlax-ename->vla-object (setq FLtext (car Ltexti))))
(vla-getxdata ent "DLEADER_TEXT" 'type1 'valeur)
(if valeur (setq sbs (vlax-variant-value (nth 1 (vlax-safearray->list valeur)))))	)))
(if sbs (progn (setq DL_textsize (vla-get-Height (vlax-ename->vla-object FLtext)))
(setq LtextData (entget FLtext)) (setq Llead (handent (car (read sbs))))
(setq Lleadprop (entget Llead))
(setq vla-QDLitem (vlax-ename->vla-object (handent (cadr (read sbs)))))
(DleaderWhile (entget FLtext) vla-QDLitem Lleadprop)	)))
;R E A C T O R S
(defun Dleader_ObjectWasEdited (/ DLobj DLobj_data Llead)
(setq DLobj (cadr (ssgetfirst)))
(if DLobj (progn (setq DLobj (ssname DLobj 0)) (setvar "CMDECHO" 0)
(setq DLobj_data (entget DLobj)) (setq DLobj_5 (cdr (assoc 5 DLobj_data)))
(setq VLAo (vlax-ename->vla-object DLobj)) (if VLAo (progn
(vla-getboundingbox VLAo 'x 'y) (setq DQL_llpoint (vlax-safearray->list x))
(setq DQL_urpoint (vlax-safearray->list y))
(setq cen (polar DQL_llpoint (angle DQL_llpoint DQL_urpoint)
(/ (distance DQL_llpoint DQL_urpoint) 2)))
(setq cen (list (nth 0 cen) (nth 1 cen) (getvar "ELEVATION")))	))
(setq DLallText (ssget "X" '((0 . "MTEXT") (100 . "AcDbMText") (-3 ("DLEADER_TEXT") )))) (if DLallText (progn
(setq sscount (sslength DLallText)) (setq val1 (- sscount 1))
(repeat sscount (setq ent (vlax-ename->vla-object (ssname DLallText val1)))
(Setq cursp (vlax-safearray->list (vlax-variant-value (vla-get-InsertionPoint ent))))
(vla-getxdata ent "DLEADER_TEXT" 'type1 'valeur)
(setq sbs (vlax-variant-value (nth 1 (vlax-safearray->list valeur))))
(setq Llead (handent (car (read sbs)))) (setq ObjHandle (cadr (read sbs)))
(if (and Llead (eq ObjHandle DLobj_5))
(Dleader_Updated_By_reactor cursp cen (entget Llead) (ssname DLallText val1))	)
(setq val1 (1- val1))	))))))
;;Reactor on ended MOVE
(defun *Dleader_object_Modification* (call-reactor scI /)
(if (member (car scI) (list "GRIP_MOVE" "GRIP_STRETCH" "GRIP_SCALE" 
"GRIP_ROTATE")) (Dleader_ObjectWasEdited)	)) ;;;;;
(defun DLEADER_run_reac ()
(if Dleader_object_Modification (progn (vlr-remove Dleader_object_Modification)
(setq Dleader_object_Modification nil)	))
(setq Dleader_object_Modification (vlr-command-reactor nil
'((:vlr-commandEnded . *Dleader_object_Modification*))	))) ;;;;;
(DLEADER_run_reac)

Tệp .lsp gốc không còn trên máy chủ. Hãy chép mã nguồn bên dưới (lấy từ bài viết gốc) vào một tệp văn bản, lưu với đuôi .lsp.

Cách dùng

  1. Chép mã nguồn, lưu thành tệp 363401_dileader_dil_diledit.lsp.
  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 DILEADER DIL DILEDIT để chạy lisp.

Mã nguồn 363401_dileader_dil_diledit.lsp (trích từ bài viết gốc)

(princ "\nDILEADER (DIL) v. 1.2 by Andrea Andreetti -Loaded-")
(defun c:DILEADER () (C:DIL)) (vl-load-com)
(defun c:DIL (/ Llead Ltext MainDLactive DQobject DragMess PointMethod ASPoint
DQLcursorPosition OKaccept Ltext Input DLactive DL_textsize VLAo DXItemHandle 
DQL_llpoint DQL_urpoint cen)
(setq DL_textsize (getvar "DIMTXT")) (setvar "CMDECHO" 0)
(while (not DQobject) (setq DQobject (nentsel "\nSelect Object...")))
(if DQobject (progn (princ "\nPick Text location...")
(DleaderWhile nil (vlax-ename->vla-object (car DQobject)) nil)
(while (and (/= (car input) 25)		;RIGHT CLICK
(/= (car input) 11) (/= (car input) 12) (/= (car input) 3)		;LEFT CLICK
(not (and (= (car input) 2) (= (cadr input) 32))) ;ESCAPE
(not (and (= (car input) 2) (= (cadr input) 13)))	) ;ENTER
(DleaderWhile nil (vlax-ename->vla-object (car DQobject)) nil)	)))
(princ "\nDleader Finish.") (princ))
;;DLEADER While Loop;;
(defun DleaderWhile (nnn VLAo Lleadprop	/ NewMod Ltext) 
;TEXTEname;VLAobject;LeaderPropreties
(vla-StartUndoMark (vla-get-ActiveDocument (vlax-get-ACAD-Object)))
(setq DXItemHandle (vla-get-Handle VLAo)) (Setq DLactive T)
(while (and DLactive (setq input (grread t 4 4))
(or (= (car input) 5) (= (car input) 3))	) ;Cursor;PickPoint
(if (= (car input) 5) (setq DQLcursorPosition (cadr input)))
(if (and VLAo DQLcursorPosition)
(DLEADER_C&M_OBJECT DQLcursorPosition nnn VLAo cen Lleadprop)	)
(if (= (car input) 3) (progn (setq DLactive nil) (if (and Ltext (not nnn))
(command "._MTEDIT" (cdar (entmod (subst (cons 1 "") (assoc 1 (entget Ltext)) (entget Ltext)))))	)))) ;while
(if (and Ltext DXLeaderHandle DXItemHandle)
(DLEADERPUTXDATA Ltext "DLEADER_TEXT"
(vl-prin1-to-string (list DXLeaderHandle DXItemHandle))	))
(if (and NewMod DXLeaderHandle DXItemHandle)
(DLEADERPUTXDATA NewMod "DLEADER_TEXT"
(vl-prin1-to-string (list DXLeaderHandle DXItemHandle))	))
(vla-EndUndoMark (vla-get-ActiveDocument (vlax-get-ACAD-Object)))
(setq DLactive nil)	)
;;DLEADER While Loop ;;DLEADER UPDATE for REACTOR
(defun Dleader_Updated_By_reactor (cursp cenpoint Lleadprop TXTobj)
(setq int (vlax-curve-getClosestPointTo VLAo cursp))
(setq DL_textsize (vla-get-Height (vlax-ename->vla-object TXTobj)))
(if (and int Lleadprop) (progn (if (> (car cursp) (car int))
(setq v71 1 co1 (list (- (car cursp) DL_textsize) (- (nth 1 cursp) (/ DL_textsize 2))
(nth 2 cursp)) co2 (list (- (car cursp) (/ DL_textsize 3)) (- (nth 1 cursp) (/ DL_textsize 2))
(nth 2 cursp)) LR 1)
(setq v71 3 co1 (list (+ (car cursp) DL_textsize) (- (nth 1 cursp) (/ DL_textsize 2)) 
(nth 2 cursp)) co2 (list (+ (car cursp) (/ DL_textsize 3)) (- (nth 1 cursp) (/ DL_textsize 2)) 
(nth 2 cursp)) LR 3)	)
(repeat 3 (setq Lleadprop (vl-remove (assoc 10 Lleadprop) Lleadprop)))
(setq Lleadprop (entmod (append Lleadprop (list (cons 10 int)) (list (cons 10 co1)) 
(list (cons 10 co2)) )))
(setq Llead (cdar (entmod (subst (cons 71 v71) (assoc 71 Lleadprop) Lleadprop))))
(setq TXTobjdata (entget TXTobj))
(setq Ltext (cdar (entmod (subst (cons 71 LR) (assoc 71 TXTobjdata) TXTobjdata))))
(setq DXLeaderHandle (cdr (assoc 5 (entget Llead)))) (setq int nil Lleadprop nil)	)))
;;DLEADER UPDATE for REACTOR;;DLEADER CREATE & MODIFY OBJECT
(defun DLEADER_C&M_OBJECT (cursp DLtext VLAo cenpoint Lleadprop / FicLine)
(setq int (vlax-curve-getClosestPointTo VLAo cursp))   
;;Create Mtext
(if Ltext (progn (command "._erase" Ltext "") (setq Ltext nil)))
(if (> (car cursp) (car int)) (setq v71 1
co1 (list (- (car cursp) DL_textsize) (- (nth 1 cursp) (/ DL_textsize 2))(nth 2 cursp))
co2 (list (- (car cursp) (/ DL_textsize 3)) (- (nth 1 cursp) (/ DL_textsize 2)) (nth 2 cursp)) )
(setq v71 3
co1 (list (+ (car cursp) DL_textsize) (- (nth 1 cursp) (/ DL_textsize 2)) (nth 2 cursp))
co2 (list (+ (car cursp) (/ DL_textsize 3)) (- (nth 1 cursp) (/ DL_textsize 2)) (nth 2 cursp)) ))  
(if (and int (not DLtext)) (progn
(setq Ltext (entmakex (list (cons 0 "MTEXT") (cons 100 "AcDbEntity")
(cons 100 "AcDbMText") (cons 1 "NOTE") (cons 10 cursp) (cons 40 DL_textsize)
(cons 50 0.0) (cons 71 v71) (cons 72 5))))	))
(if (and int DLtext) (progn
(setq NewMod (entmod(subst (cons 10 cursp)(assoc 10 DLtext) DLtext)))
(setq NewMod (cdar (entmod (subst (cons 71 v71)(assoc 71 NewMod) NewMod))))	))
;;Create Leader
(if (and int (not Lleadprop)) (progn
(if Llead (progn (command "._erase" Llead "") (setq Llead nil)))
(setq Llead (entmakex (list (cons 0 "LEADER") (cons 100 "AcDbEntity")
(cons 100 "AcDbLeader")	(cons 10 int) (cons 10 co1) (cons 10 co2))))
(vla-put-ArrowheadSize (vlax-ename->vla-object Llead) (getvar "DIMASZ"))
(setq DXLeaderHandle (cdr (assoc 5 (entget Llead))))	))  
(if (and int Lleadprop) (progn
(repeat 3 (setq Lleadprop (vl-remove (assoc 10 Lleadprop) Lleadprop)))
(setq Lleadprop (entmod (append Lleadprop (list (cons 10 int)) (list (cons 10 co1)) 
(list (cons 10 co2)) )))
(setq Llead (cdar (entmod (subst (cons 71 v71)(assoc 71 Lleadprop) Lleadprop))))
(setq DXLeaderHandle (cdr (assoc 5 (entget Llead))))	)) )
;;DLEADER CREATE & MODIFY OBJECT;;X D A T A;;
(defun DLEADERPUTXDATA (item xdataname tag / ent type1 valeur)
(setq ent (vlax-ename->vla-object item) type1 (vlax-make-safearray vlax-vbInteger 
(cons 0 1)) Valeur (vlax-make-safearray vlax-vbVariant (cons 0 1)))
(vlax-safearray-put-element type1 0 1001) 
(vlax-safearray-put-element valeur 0 xdataname)
(vlax-safearray-put-element type1 1 1000) (vlax-safearray-put-element valeur 1 tag)
(setq type1  (vlax-make-variant type1) valeur (vlax-make-variant valeur))
(vla-setxdata ent type1 valeur) )
;;X D A T A;;E D I T O R;;
(defun c:DILEDIT (/ sbs Llead Lleadprop vla-QDLitem LtextData valeur type1 Ltexti 
FLtext ent)
(setq sbs nil) (while (not sbs)
(setq Ltexti (nentsel "\nSelect TEXT Object..."))
(if Ltexti (progn (setq ent (vlax-ename->vla-object (setq FLtext (car Ltexti))))
(vla-getxdata ent "DLEADER_TEXT" 'type1 'valeur)
(if valeur (setq sbs (vlax-variant-value (nth 1 (vlax-safearray->list valeur)))))	)))
(if sbs (progn (setq DL_textsize (vla-get-Height (vlax-ename->vla-object FLtext)))
(setq LtextData (entget FLtext)) (setq Llead (handent (car (read sbs))))
(setq Lleadprop (entget Llead))
(setq vla-QDLitem (vlax-ename->vla-object (handent (cadr (read sbs)))))
(DleaderWhile (entget FLtext) vla-QDLitem Lleadprop)	)))
;R E A C T O R S
(defun Dleader_ObjectWasEdited (/ DLobj DLobj_data Llead)
(setq DLobj (cadr (ssgetfirst)))
(if DLobj (progn (setq DLobj (ssname DLobj 0)) (setvar "CMDECHO" 0)
(setq DLobj_data (entget DLobj)) (setq DLobj_5 (cdr (assoc 5 DLobj_data)))
(setq VLAo (vlax-ename->vla-object DLobj)) (if VLAo (progn
(vla-getboundingbox VLAo 'x 'y) (setq DQL_llpoint (vlax-safearray->list x))
(setq DQL_urpoint (vlax-safearray->list y))
(setq cen (polar DQL_llpoint (angle DQL_llpoint DQL_urpoint)
(/ (distance DQL_llpoint DQL_urpoint) 2)))
(setq cen (list (nth 0 cen) (nth 1 cen) (getvar "ELEVATION")))	))
(setq DLallText (ssget "X" '((0 . "MTEXT") (100 . "AcDbMText") (-3 ("DLEADER_TEXT") )))) (if DLallText (progn
(setq sscount (sslength DLallText)) (setq val1 (- sscount 1))
(repeat sscount (setq ent (vlax-ename->vla-object (ssname DLallText val1)))
(Setq cursp (vlax-safearray->list (vlax-variant-value (vla-get-InsertionPoint ent))))
(vla-getxdata ent "DLEADER_TEXT" 'type1 'valeur)
(setq sbs (vlax-variant-value (nth 1 (vlax-safearray->list valeur))))
(setq Llead (handent (car (read sbs)))) (setq ObjHandle (cadr (read sbs)))
(if (and Llead (eq ObjHandle DLobj_5))
(Dleader_Updated_By_reactor cursp cen (entget Llead) (ssname DLallText val1))	)
(setq val1 (1- val1))	))))))
;;Reactor on ended MOVE
(defun *Dleader_object_Modification* (call-reactor scI /)
(if (member (car scI) (list "GRIP_MOVE" "GRIP_STRETCH" "GRIP_SCALE" 
"GRIP_ROTATE")) (Dleader_ObjectWasEdited)	)) ;;;;;
(defun DLEADER_run_reac ()
(if Dleader_object_Modification (progn (vlr-remove Dleader_object_Modification)
(setq Dleader_object_Modification nil)	))
(setq Dleader_object_Modification (vlr-command-reactor nil
'((:vlr-commandEnded . *Dleader_object_Modification*))	))) ;;;;;
(DLEADER_run_reac)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Hỏi về Lisp (thuật toán, ý tưởng, coding,...). Xem đúng bài viết #363401 để đọc hướng dẫn và trao đổi thêm.

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