See the top rated post in this thread. Click here

Results 1 to 9 of 9

Thread: Renaming Tags

  1. #1
    Woo! Hoo! my 1st post
    Join Date
    2008-02
    Posts
    1
    Login to Give a bone
    0

    Default Renaming Tags

    Is it possible to rename Tags in Blocks using AutoLisp?

  2. #2
    I could stop if I wanted to
    Join Date
    2007-08
    Posts
    202
    Login to Give a bone
    0

    Default Re: Renaming Tags

    Hi,

    You can get ideas from this little routine

    Code:
    (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:

    Code:
    (command "_attsync" "_n" blockname)
    by

    Code:
    (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))
    	  )
    	)
          )
    Last edited by 'gile'; 2008-02-08 at 04:43 PM.

  3. #3
    I could stop if I wanted to
    Join Date
    2007-08
    Posts
    202
    Login to Give a bone
    0

    Default Re: Renaming Tags

    Another way using Visual LISP functions

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

  4. #4
    AUGI Addict
    Join Date
    2005-08
    Posts
    1,043
    Login to Give a bone
    0

    Default Re: Renaming Tags

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

    Code:
    (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
    )
    Code:
    (defun c:Test ()
        
        (ReturnInsertedEnames (cdr (assoc 2 (entget (car (entsel "\n Select block: "))))))
    )

  5. #5
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    1

    Default Re: Renaming Tags

    Tim, that's perfect
    Thanks

    Here is my 2¢ (just an extension of your function)
    to select blocks on particular sheet
    Code:
    (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 :
    Code:
    (ReturnInsertedEnamesOnLayout (cdr (assoc 2 (entget (car (entsel "\n Select block: "))))) "Layout1")
    ~'J'~

  6. #6
    I could stop if I wanted to
    Join Date
    2007-08
    Posts
    202
    Login to Give a bone
    0

    Default Re: Renaming Tags

    Very nice Tim !!!
    I didn't know this, and I thank you very much to share this trick.

  7. #7
    AUGI Addict
    Join Date
    2005-08
    Posts
    1,043
    Login to Give a bone
    0

    Default Re: Renaming Tags

    You're welcome, both. I just share what little I know, and hope it is useful to others.

  8. #8
    I could stop if I wanted to
    Join Date
    2007-08
    Posts
    202
    Login to Give a bone
    0

    Default Re: Renaming Tags

    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

    Code:
    (defun ReturnInsertedEnames (BlkName)
      (mapcar 'cdr
    	  (vl-remove-if-not
    	    '(lambda (x) (= (car x) 331))
    	    (entget (cdr (assoc 330 (entget (tblobjname "BLOCK" BlkName))))
    	    )
    	  )
      )
    )

  9. #9
    AUGI Addict
    Join Date
    2005-08
    Posts
    1,043
    Login to Give a bone
    0

    Default Re: Renaming Tags

    Quote Originally Posted by 'gile' View Post
    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.

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

Similar Threads

  1. Renaming Attribute Tags?
    By rkingery in forum AutoLISP
    Replies: 25
    Last Post: 2013-01-09, 01:19 PM
  2. renaming blocks
    By d_m_hopper in forum AutoLISP
    Replies: 16
    Last Post: 2010-06-21, 04:24 AM
  3. Layer Renaming
    By CadDog in forum AutoCAD General
    Replies: 1
    Last Post: 2009-03-09, 03:47 PM
  4. Renaming worksets
    By sblackburn in forum Revit - Worksharing/Worksets/Revit Server
    Replies: 2
    Last Post: 2008-07-29, 01:17 PM
  5. Renaming Fields in Room Name Tags
    By caroline1772 in forum Revit Architecture - General
    Replies: 3
    Last Post: 2003-09-04, 01:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •