I think it's a wrong way, if you will lost your block instance say remove them etc,
so your field will be unreadable, use instead direct way to populate mleader with
attribute value, though Tom's suggestion has a reason of course
Just from a scratch:
Using textstring value
Code:
(defun c:mlb (/ attribs blk_obj mlead p1 p2 ptlist pts sset txtstring)
(vl-load-com)
(prompt "\n Select block:")
(while
(setq sset (ssget "_+.:s:l"
(list (cons 0 "insert")
(cons 66 1)
(cons 2 "`*U*,MM_Duct_Equipment_VAV") ; <-- your block name,added "`*u*" if dynamiic block
) ;_ end of list
) ;_ end of ssget
) ;_ end of setq
(progn
(setq blk_obj (vlax-ename->vla-object (ssname sset 0)))
(setq attribs (vlax-invoke blk_obj 'getattributes))
(foreach att_obj attribs
(cond ((eq "DESIGNATION" (vla-get-tagstring att_obj) )
(setq txtstring (vla-get-textstring att_obj))
)
(T nil));_ end of cond
)
(setq p1 (cadr (last (last (ssnamex sset))))
p2 (getpoint p1 "\nPick mleader text line point: ")
ptlist (list p1 p2)
pts (vlax-make-variant
(vlax-safearray-fill
(vlax-make-safearray
vlax-vbdouble
(cons 0 (1- (* 3 (length ptlist))))
) ;_ end of vlax-make-safearray
(apply 'append ptlist)
) ;_ end of vlax-safearray-fill
) ;_ end of vlax-make-variant
) ;_ end of setq
(setq mlead (vla-addmleader
(vla-get-modelspace
(vla-get-activedocument (vlax-get-acad-object))) ;get modelspace object
pts
0
) ;_ end of vla-addmleader
);_ end of setq
(vla-put-textstring mlead txtstring)
) ;_ end of progn
) ;_ end of while
(princ)
) ;_ end of defun
(prompt "\n")
(prompt "\t\t<<< start command with MLB ...\t>>> ")
(prin1)
Here is code to use field if you wanted be
Code:
(defun c:mlf (/ attribs blk_obj Get-ObjectID-x86-x64 mlead p1 p2 ptlist pts sset txtstring)
(vl-load-com)
;; from http://forum.dwg.ru/showthread.php?t=51822&highlight=GetObjectIdString
;;--------------------------------------------------------
;; get ObjectID irrespective of the version
;; for AutoCAD x86 or x64
;; source: https://discussion.autodesk.com/forums/message.jspa?messageID=6172961
;;--------------------------------------------------------
(defun Get-ObjectID-x86-x64 (obj / util)
(setq util (vla-get-Utility
(vla-get-activedocument (vlax-get-acad-object))))
(if (= (type obj) 'ENAME)
(setq obj (vlax-ename->vla-object obj)))
(if (= (type obj) 'VLA-OBJECT)
(if (> (vl-string-search "x64" (getvar "platform")) 0)
(vlax-invoke-method
util
"GetObjectIdString"
obj
:vlax-False)
(rtos (vla-get-objectid obj) 2 0)
)
)
)
(prompt "\n Select block:")
(while
(setq sset (ssget "_+.:s:l"
(list (cons 0 "insert")
(cons 66 1)
(cons 2 "`*U*,MM_Duct_Equipment_VAV") ; <-- your block name,added "`*u*" if dynamiic block
) ;_ end of list
) ;_ end of ssget
) ;_ end of setq
(progn
(setq blk_obj (vlax-ename->vla-object (ssname sset 0)))
(setq attribs (vlax-invoke blk_obj 'getattributes))
(foreach att_obj attribs
(cond ((eq "DESIGNATION" (vla-get-tagstring att_obj) )
(setq att_id (Get-ObjectID-x86-x64 att_obj))
)
(T nil);_ end of cond
)
);_ end of foreach
(setq p1 (cadr (last (last (ssnamex sset))))
p2 (getpoint p1 "\nPick mleader text line point: ")
ptlist (list p1 p2)
pts (vlax-make-variant
(vlax-safearray-fill
(vlax-make-safearray
vlax-vbdouble
(cons 0 (1- (* 3 (length ptlist))))
) ;_ end of vlax-make-safearray
(apply 'append ptlist)
) ;_ end of vlax-safearray-fill
) ;_ end of vlax-make-variant
) ;_ end of setq
(setq mlead (vla-addmleader
(vla-get-modelspace
(vla-get-activedocument (vlax-get-acad-object))) ;get modelspace object
pts
0
) ;_ end of vla-addmleader
);_ end of setq
;(vla-update mlead)
(vla-put-textstring mlead (strcat "%<\\AcObjProp Object(%<\\_ObjId " att_id ">%).TextString>%"))
) ;_ end of progn
(command "ddedit" "_L" );; just to open text editor to force updateing field
(command \U+001B); perform Cancel command
) ;_ end of while
(princ)
) ;_ end of defun
(prompt "\n")
(prompt "\t\t<<< Start command with MLF ...\t>>> ")
(prin1)