Once you create or insert the block with the attribute inside the drawing that I call "RoomNumber"
Code:
;___________________________________________________________________________________________________________|
;
; Written By: Peter Jamtgaard C.E., P.E., S.E. copyright 2020 All Rights Reserved
;___________________________________________________________________________________________________________|
;___________________________________________________________________________________________________________|
;
; Command Line Function Header List
;___________________________________________________________________________________________
; Function and Description
;* C:RoomNumber
;* Command line Function to convert mtext entities to a block with an attribute
;___________________________________________________________________________________________________________|
;
; General Function Header List
;___________________________________________________________________________________________________________|
; Function, Arguments and Description
;* (Attributes objBlock)
;* Function to get a list of attributes from a block object
;* (ErrorTrap symFunction)
;* Function to capture error in symfunction expression
;* (ObjectMiddlePoint objSelection)
;* Function to get the middle of a bounding box from a vla-object
;* (OwnerObject objDocument objSelection)
;* Function to get the vla-object owner of a vla-object give the document and initial object
;* (SelectionSetToList ssSelections)
;* Function to convert a lisp selection set to a list of vla-objects
;___________________________________________________________________________________________________________|
;___________________________________________________________________________________________________________|
;$ Header End
;___________________________________________________________________________________________________________|
;
; Command line Function to convert mtext entities to a block with an attribute
;___________________________________________________________________________________________________________|
(defun C:RoomNumber (/ intOwnerID lstAttributes lstInsertion lstSelections objActiveDocument
objAttribute objBlock ssSelections strTextString)
(if (and (setq objActiveDocument (vla-get-activedocument (vlax-get-acad-object)))
(setq ssSelections (ssget (list (cons 0 "MTEXT"))))
(setq lstSelections (SelectionSetToList ssSelections))
)
(foreach objMtext lstSelections
(and
(setq lstInsertion (ObjectMiddlePoint objMtext))
(errortrap '(setq sngRotation (vla-get-rotation objMText)))
(setq strTextString (vla-get-textstring objMtext))
(setq objOwner (ownerObject objActiveDocument objMtext))
(errortrap '(setq objBlock (vlax-invoke objOwner "InsertBlock" lstInsertion "RoomNumber" 1.0 1.0 1.0 sngRotation)))
(setq lstAttributes (attributes objBlock))
(setq objAttribute (car lstAttributes))
(errortrap '(vla-put-textstring objAttribute strTextString))
(errortrap '(vla-put-layer objBlock (vla-get-layer objMtext)))
(errortrap '(vla-delete objMtext))
)
)
)
)
;___________________________________________________________________________________________________________|
;
; Function to get a list of attributes from a block object
;___________________________________________________________________________________________________________|
(defun Attributes (objBlock)
(if (and (vlax-property-available-p objBlock "hasattributes")
(= (vla-get-hasattributes objBlock) :vlax-true)
)
(vlax-invoke objBlock "getattributes")
)
)
;___________________________________________________________________________________________________________|
;
; Function to capture error in symfunction expression
;___________________________________________________________________________________________________________|
(defun ErrorTrap (symFunction / objError result)
(if (not
(vl-catch-all-error-p
(setq objError (vl-catch-all-apply
'(lambda (X)(set X (eval symFunction)))
(list 'result)))))
(if result result 'T)
)
)
;___________________________________________________________________________________________________________|
;
; Function to get the middle of a bounding box from a vla-object
;___________________________________________________________________________________________________________|
(defun ObjectMiddlePoint (objSelection / lstPoint1 lstPoint2 safPoint1 safPoint2)
(if (and (errortrap '(vla-getboundingbox objSelection 'safPoint1 'safPoint2))
(errortrap '(setq lstPoint1 (vlax-safearray->list safPoint1)))
(errortrap '(setq lstPoint2 (vlax-safearray->list safPoint2)))
)
(mapcar '(lambda (X Y)(/ (+ X Y) 2.0)) lstPoint1 lstPoint2)
)
)
;___________________________________________________________________________________________________________|
;
; Function to get the vla-object owner of a vla-object give the document and initial object
;___________________________________________________________________________________________________________|
(defun OwnerObject (objDocument objSelection / intOwnderID objActiveDocument objOwner)
(if (and (errortrap '(setq intOwnerID (vla-get-ownerid objSelection)))
(errortrap '(setq objOwner (vla-objectidtoobject objDocument intOwnerID)))
)
objOwner
)
)
;___________________________________________________________________________________________________________|
;
; Function to convert a lisp selection set to a list of vla-objects
;___________________________________________________________________________________________________________|
(defun SelectionSetToList (ssSelections / entSelection intCount objSelection lstObjects)
(repeat (setq intCount (sslength ssSelections))
(and (setq intCount (1- intCount))
(setq entSelection (ssname ssSelections intCount))
(setq objSelection (vlax-ename->vla-object entSelection))
(setq lstObjects (cons objSelection lstObjects))
)
)
lstObjects
)
(princ "!")
(vl-load-com)