AutoLISP: Attribute to text (mtext)

Here is a rare LISP routine. there are plenty of routines out there that take an exploded attribute and turn it into either text or mtext. But this routine allows you to make mtext objects out from attributes without exploding any blocks.

To use this routine:

  • ATTMT <enter> to start
  • Specify the height that you want the mtext to be.
  • Select the blocks with attributes
  • <enter> to make the mtext objects and end the command

NOTE: The mtext that is made will show the tag and the tag value. So if you want a very fast way to extract data, this will save you a lot of time.

:;;;; written by Smirnoff

;;;;; found @


(defun c:attmt(/ aDoc aSp oSiz bSet aLst cLst tStr nTxt bCtr Tags TextInsert)

; *****************************************************************************


; (Modify it to adjust for your own requirements) ;

; *****************************************************************************

(setq Tags T) ; - if T add tags to MText if Nil not

(setq TextInsert T) ; - Text insertion point. If T center of Bounding Box

; of block, if Nil Block insertion point.

; ******************************* END ADJUSTMENTS *****************************


(defun GetBoundingCenter (vlaObj / blPt trPt cnPt)

(vla-GetBoundingBox vlaObj 'minPt 'maxPt)

(setq blPt(vlax-safearray->list minPt)

trPt(vlax-safearray->list maxPt)



(+(car blPt)(/(-(car trPt)(car blPt))2))

(+(cadr blPt)(/(-(cadr trPt)(cadr blPt))2))


); end list

); end vlax-3D-point

); end setq

); end of GetBoundingCenter

(if(not attmt:Size)(setq attmt:Size(getvar "TEXTSIZE")))

(setq oSiz attmt:Size

attmt:Size(getreal(strcat "\nText size <"(rtos attmt:Size)">: ")))

(if(null attmt:Size)(setq attmt:Size oSiz))

(setq aDoc(vla-get-ActiveDocument(vlax-get-acad-object)))

(if(= 1(vla-get-ActiveSpace aDoc))

(setq aSp(vla-get-ModelSpace aDoc))

(setq aSp(vla-get-PaperSpace aDoc))

); end if

(princ "\n<<< Select text to extract attributes to MText >>> ")

(if(setq bSet(ssget '((0 . "INSERT"))))


(foreach b(mapcar 'vlax-ename->vla-object

(vl-remove-if 'listp

(mapcar 'cadr(ssnamex bSet))))

(setq aLst '()

tStr "") ; end setq

(if TextInsert

(setq bCtr(GetBoundingCenter b))

(setq bCtr(vla-get-InsertionPoint b))

); end if

(if(= :vlax-true(vla-get-HasAttributes b))


(setq aLst

(mapcar '(lambda (a)

(list (vla-get-TagString a)

(vla-get-TextString a)))


(vlax-variant-value(vla-GetAttributes b)))))

(foreach i(reverse aLst)

(setq tStr(strcat tStr(if Tags(strcat(car i) ": ")"")(last i)"\\P"))

); end foreach

(if(/= "" tStr)


(setq nTxt(vla-AddMText aSp bCtr (* attmt:Size 30.0) tStr))

(vla-put-Height nTxt attmt:Size)

); end progn

); end if

); end progn

); end if

); end foreach

(vla-EndUndoMark aDoc)

); end progn

); end if


); end of c:attmt

About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in Attributes, AutoLISP, Blocks, Text, TIPS. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s