Block attribute tag rename

Block attribute tag rename

Angelswing91
Contributor Contributor
1,593 Views
7 Replies
Message 1 of 8

Block attribute tag rename

Angelswing91
Contributor
Contributor

Hi all, 

I need to rename tag name in block. Block name, old tag name and new tag name is known. I've found this code. It's working, but i want to specify actual block name in code, not by selection.

 


(defun C:C1 ( / oldtagname newtagname tagname ss x n blk att atts )
(vl-load-com)
(setq oldtagname "AUKSTIS")
(setq newtagname "ag")
(setq ss nill)
(prompt "\nPick All Blocks to Change: ")
(setq ss (ssget))
(if (and (/= oldtagname "")(/= newtagname "")(/= ss nil))
(progn
(setq x 0 n 0)
(repeat (sslength ss)
(setq blk (vlax-ename->vla-object (ssname ss x)))
(if (safearray-value (setq atts (vlax-variant-value (vla-getattributes blk))))
(progn
(setq atts (vlax-safearray->list (vlax-variant-value (vla-getattributes blk))))
(foreach att atts
(setq tagname (strcase (vla-get-tagstring att))); tagname
(if (and (/= newtagname "")(= tagname oldtagname))
(progn
(vla-put-tagstring att newtagname)
(setq n (+ n 1))
); progn
); if
); foreach
); progn
); if
(setq x (+ x 1))
); repeat length ss
(alert (strcat "Changed " (itoa n) " Tagnames to: " newtagname))
); progn
); if
(princ)
); function

0 Likes
Accepted solutions (1)
1,594 Views
7 Replies
Replies (7)
Message 2 of 8

ronjonp
Advisor
Advisor

@Angelswing91 

Beware that if you use this code and then ATTSYNC is run, you'll lose your value of the attribute and the block definition will revert to to the original tagname.

0 Likes
Message 3 of 8

ronjonp
Advisor
Advisor
Accepted solution

Give this a try. You'll need to put your blockname in the code at the top.

 

(defun c:foo (/ at b o ot nt r s x)
  ;; RJP » 2022-04-30
  (setq b "NAMEOFYOURBLOCK")
  ;; Changed for the nitpickers ;-)
  (setq ot (strcase "AUKSTIS"))
  (setq nt (strcase "AG"))
  (cond
    ((and (tblobjname "BLOCK" b) (setq s (ssget "_X" (list '(0 . "INSERT") (cons 2 b)))))
     ;; Gather ALL existing values
     (foreach e	(mapcar 'cadr (ssnamex s))
       (setq at (vlax-invoke (setq o (vlax-ename->vla-object e)) 'getattributes))
       (setq r (cons (list o (mapcar '(lambda (x) (vla-get-textstring x)) at)) r))
     )
     ;; Update block definition
     (vlax-for a (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) b)
       (if (and (= "AcDbAttributeDefinition" (vla-get-objectname a)) (= ot (vla-get-tagstring a)))
	 (vla-put-tagstring a nt)
       )
     )
     ;; Sync all blocks
     (command "_.attsync" "_N" b)
     ;; Restore values
     (foreach a	r
       (mapcar '(lambda (y z) (vla-put-textstring y z))
	       (vlax-invoke (car a) 'getattributes)
	       (cadr a)
       )
     )
    )
  )
  (princ)
)

 

 

0 Likes
Message 4 of 8

_Tharwat
Advisor
Advisor

Maybe you wanted to strcase the tag string that comes from the block and not the variable 'ot' since its already in capital letter. 

0 Likes
Message 5 of 8

ronjonp
Advisor
Advisor

@_Tharwat AFAIK tagstrings are always uppercase but it wouldn't hurt to force it.

0 Likes
Message 6 of 8

_Tharwat
Advisor
Advisor

@ronjonp I am referring to the variable 'ot' that its already in capital letter at the top of codes then you convert it to capital also in the routine which is redundant.   

0 Likes
Message 7 of 8

ronjonp
Advisor
Advisor

@_Tharwat Yes I know, but what if the op makes a change and does not use all uppercase? 😉

Also you mentioned making the tagstring uppercase that is why I answered you as I did.

0 Likes
Message 8 of 8

_Tharwat
Advisor
Advisor

No worries, I know that you will find a reasonable clue to my comment as always. lol

0 Likes