Re: Renaming Attribute Tags?
Quote:
Originally Posted by
rkingery
Does anyone know of a way, or have a routine, that will edit the tag name (not the value) of an attribute in an existing block? I'm aware of BATTMAN's capability here but I have a HUGE number of drawings that require some tag renaming and I'm hoping to create a script to run overnight.
Thanks,
Randy
Randy,
Give this a try:
Code:
(defun MKSxAttTagSet
(obj ;;variable - object to use
att ;;variable - attribute to set
str ;;variable - new tag name
/
atts ;;variable - list of attributes
bool ;;variable - returns t if set, returns nil if not
index ;;variable - index/counter
)
(setq atts (vlax-safeArray->list (variant-value (vla-getAttributes obj)))
index 0
)
(repeat (length atts)
(if (= (vla-get-tagString (nth index atts)) att)
(progn
(vla-put-tagString (nth index atts) str)
(vla-update (nth index atts))
(setq bool t)
)
)
(setq index (1+ index))
)
bool
)
You would call it like this:
Code:
(MKSxAttTagSet objBlk TagOld TagNew)
This routine will update ALL attribute tags with the old name (TagOld). If nothing it updated, nil is returned; otherwise t is returned.
Re: Renaming Attribute Tags?
Similar to msretenovic's code. Although you simply give it the block's name, the old tag name & the new tag name. Renames these for both the block's definition as well as any block references (even when nested inside other blocks).
Code:
;;; Rename attributes
(defun RenAttrib ($blk $old $new / blocks bo eo ao)
;; Get blocks collection in current drawing
(setq blocks (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object))))
;; Step through all blocks
(vlax-for bo blocks
;; Step through all entities inside block
(vlax-for eo bo
(cond
;; If attdef & in target block & old tag
((and (= (vla-get-ObjectName eo) "AcDbAttributeDefinition")
(= (strcase (vla-get-Name bo)) (strcase $blk))
(= (vla-get-TagString eo) $old)
) ;_ end of and
(vla-put-TagString eo $new) ;Change to new name
)
;; If block reference & target block
((and (= (vla-get-ObjectName eo) "AcDbBlockReference")
(= (strcase (vla-get-EffectiveName eo)) (strcase $blk))
) ;_ end of and
;; Step through all attributes
(foreach ao (vlax-safearray->list (vlax-variant-value (vla-GetAttributes eo)))
;; Check if target attrib
(if (= (strcase (vla-get-TagString ao)) (strcase $old))
(vla-put-TagString ao $new) ;Change to new name
) ;_ end of if
) ;_ end of foreach
)
) ;_ end of cond
) ;_ end of vlax-for
) ;_ end of vlax-for
) ;_ end of defun
Example usage, at the command line (or in script). Assume block's name is TESTBLK, old Attrib Tag name is OLDTAG & new tagname is NEWTAG:
Code:
(RenAttrib "TESTBLK" "OLDTAG" "NEWTAG")
Re: Renaming Attribute Tags?
Thanks guys! Worked beautifully!
Re: Renaming Attribute Tags?
Newbie here: How could the be modified to rename several attribute tags and replace each with a corresponding new tag?
Re: Renaming Attribute Tags?
Quote:
Originally Posted by
Clinton.Hill
Newbie here: How could the be modified to rename several attribute tags and replace each with a corresponding new tag?
Renaming the insert's attributes is quite simple. A normal entmod does this without hassles, or as per the previous code. The tricky bit is to figure out which old name needs to be changed to which new name - you either need a prepared list of OldName->NewName or you need to ask the user all the time :roll:. Then a simple AttSync would suffice to modify any properties to the new attribute's definition.
Re: Renaming Attribute Tags?
Do you have an example or tips on constructing a list of old and new attribute tag names that will be used by LISP:
File content formatting
File extension, etc.
Other special considerations...
Are there any links to instruction on or samples on how to use a list using either AUTOLISP or VISUAL LISP that you could forward?:lol:
Re: Renaming Attribute Tags?
Quote:
Originally Posted by
Clinton.Hill
Do you have an example or tips on constructing a list of old and new attribute tag names that will be used by LISP:
File content formatting
File extension, etc.
Other special considerations...
Are there any links to instruction on or samples on how to use a list using either AUTOLISP or VISUAL LISP that you could forward?:lol:
All you need is a list which you'll need to create for yourself. Something like this:
Code:
(setq attrename-list
'(("BlockName1" ("OldAttName1.1" "NewAttName1.1") ("OldAttName1.2" "NewAttName1.2"))
("BlockName2" ("OldAttName2.1" "NewAttName2.1") ("OldAttName2.2" "NewAttName2.2"))
)
)
Then you can step through that list and call something like my routine in post #3 thus:
Code:
(foreach block attrename-list
(foreach attrename-pair (cdr block)
(RenAttrib (car block) (car attrename-pair) (cadr attrename-pair))
)
(command "._ATTSYNC" "_Name" (car block))
)
Note though: this is untested, so you might have to tweak it a bit :mrgreen:. And I think it's working only with case sensitivity - to get around that the RenAttrib defun needs some changes. Also this isn't the most efficient route as it's looping through the list and for each attribute rename pair goes through all the entities inside the DWG (there's probably a much faster method).
Re: Renaming Attribute Tags?
I found this on the web after receiving no help at the autodesk forum. I just tried loading the "RenAttrib" lisp. It loaded okay but when I put the text in the command line I get the error shown below.
ILMST is the block name
04-16-90 is the existing attribute tag name
START_DATE is the new tag name I want to give it
Command: (RenAttrib "ILMST" "04-16-90" "START_DATE")
; error: no function definition: VLAX-GET-ACAD-OBJECT
I'm not a programmer so I don't know how to correct this. Can anyone help?
Thanks
Mike
PS: If this can be done with a script that would be much easier.
Re: Renaming Attribute Tags?
It's using the Visual Lisp extensions - which aren't loaded by default. Just add the following line of code somewhere into that lisp (or any other lisp loaded automatically) - it only need be run once per drawing (so most of us would already have such somewhere, that's why these lisps don't include it):