View Full Version : Renaming Tags
kenneth_white
2008-02-08, 12:31 PM
Is it possible to rename Tags in Blocks using AutoLisp?
_gile
2008-02-08, 03:43 PM
Hi,
You can get ideas from this little routine
(defun rename-tag (blockname oldtag newtag / def ent loop elst)
(if (setq def (tblsearch "BLOCK" blockname))
(progn
(setq ent (cdr (assoc -2 def))
loop T
)
(while (and ent loop)
(setq elst (entget ent))
(if (and (= (cdr (assoc 0 elst)) "ATTDEF")
(= (cdr (assoc 2 elst)) (strcase oldtag))
)
(progn
(entmod (subst (cons 2 newtag) (assoc 2 elst) elst))
(setq loop nil)
)
)
(setq ent (entnext ent))
)
(command "_attsync" "_n" blockname)
)
)
(princ)
)
If you don't want to use command, replace:
(command "_attsync" "_n" blockname)
by
(if (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 blockname))))
(repeat (setq n (sslength ss))
(setq ent (ssname ss (setq n (1- n)))
att (entnext ent)
)
(while att
(setq elst (entget att))
(= (cdr (assoc 0 elst)) "ATTRIB")
(if (and (= (cdr (assoc 0 elst)) "ATTRIB")
(= (cdr (assoc 2 elst)) (strcase oldtag))
)
(entmod
(subst (cons 2 (strcase newtag)) (assoc 2 elst) elst)
)
)
(setq att (entnext att))
)
)
)
_gile
2008-02-08, 04:41 PM
Another way using Visual LISP functions
(defun rename-tag (blockname oldtag newtag)
(vl-load-com)
(or *acad*
(setq *acad* (vla-get-ActiveDocument (vlax-get-acad-object)))
)
(if (tblsearch "BLOCK" blockname)
(progn
(vlax-for obj (vla-item
(vla-get-Blocks
*acad*
)
blockname
)
(if (and (= (vla-get-ObjectName obj) "AcDbAttributeDefinition")
(= (strcase oldtag) (vla-get-Tagstring obj))
)
(vla-put-TagString obj (strcase newtag))
)
)
(if (ssget "_X" (list '(0 . "INSERT") (cons 2 blockname)))
(vlax-for ref (vla-get-ActiveSelectionSet
*acad*
)
(foreach att (vlax-invoke ref 'getAttributes)
(if (= (strcase oldtag) (vla-get-Tagstring att))
(vla-put-TagString att (strcase newtag))
)
)
)
)
)
)
(princ)
)
T.Willey
2008-02-08, 05:36 PM
gile,
Instead of using ssget to find all the inserted blocks, you can grab that information from the dxf information of the block definition. Just an FYI..
(defun ReturnInsertedEnames (BlkName / EnameList)
(foreach lst (entget (cdr (assoc 330 (entget (tblobjname "block" BlkName)))))
(if (equal (car lst) 331)
(setq EnameList (cons (cdr lst) EnameList))
)
)
EnameList
)
(defun c:Test ()
(ReturnInsertedEnames (cdr (assoc 2 (entget (car (entsel "\n Select block: "))))))
)
Tim, that's perfect
Thanks
Here is my 2ยข (just an extension of your function)
to select blocks on particular sheet
(defun ReturnInsertedEnamesOnLayout (BlkName LayoutName / EnameList)
(foreach lst (entget (cdr (assoc 330 (entget (tblobjname "block" BlkName)))))
(if (equal (car lst) 331)
(if (eq LayoutName (cdr (assoc 410 (entget (cdr lst)))))
(setq EnameList (cons (cdr lst) EnameList))
)
)
)
EnameList
)
Usage :
(ReturnInsertedEnamesOnLayout (cdr (assoc 2 (entget (car (entsel "\n Select block: "))))) "Layout1")
~'J'~
_gile
2008-02-08, 09:36 PM
Very nice Tim !!!
I didn't know this, and I thank you very much to share this trick.
T.Willey
2008-02-08, 09:53 PM
You're welcome, both. I just share what little I know, and hope it is useful to others.
_gile
2008-02-08, 10:38 PM
Tim,
I notice that if some bloc have been erased from the drawing, their enames still appear in the list.
Just for fun, another way to write the routine
(defun ReturnInsertedEnames (BlkName)
(mapcar 'cdr
(vl-remove-if-not
'(lambda (x) (= (car x) 331))
(entget (cdr (assoc 330 (entget (tblobjname "BLOCK" BlkName))))
)
)
)
)
T.Willey
2008-02-08, 10:52 PM
I notice that if some bloc have been erased from the drawing, their enames still appear in the list.
Good catch. I would have to think that has something to do with being able to undo everything. Maybe there should be a check to make sure you only get non-erased entity names.
(defun ReturnInsertedEnames (BlkName)
(vl-remove-if
'vlax-erased-p
(mapcar 'cdr
(vl-remove-if-not
'(lambda (x) (= (car x) 331))
(entget (cdr (assoc 330 (entget (tblobjname "BLOCK" BlkName)))))
)
)
)
)
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.