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 @ http://www.cadtutor.net/forum/showthread.php?56833-Display-ATTRIBUTES-as-Text-from-multiple-blocks&highlight=attmt

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

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

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

; ADJUSTMENTS ;

; (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 *****************************

(vl-load-com)

(defun GetBoundingCenter (vlaObj / blPt trPt cnPt)

(vla-GetBoundingBox vlaObj 'minPt 'maxPt)

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

trPt(vlax-safearray->list maxPt)

cnPt(vlax-3D-point

(list

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

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

0.0

); 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"))))

(progn

(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))

(progn

(setq aLst

(mapcar '(lambda (a)

(list (vla-get-TagString a)

(vla-get-TextString a)))

(vlax-safearray->list

(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)

(progn

(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

(princ)

); 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.

2 Responses to AutoLISP: Attribute to text (mtext)

  1. gp says:

    hello,
    i know thats a very old post, but I try to ask anyway.
    could you can explain me how to modify this lisp to show the attribute in the right order and not reversed?

    thanks

    gp

  2. 2bikerblog says:

    hello,
    i know thats a very old post, but I try to ask anyway.
    could you can explain me how to modify this lisp to show the attribute in the right order and not reversed?

    thanks

    gp

Leave a comment