Author Topic: Renaming attribute tag via script  (Read 10304 times)

0 Members and 1 Guest are viewing this topic.

ELOQUINTET

  • Guest
Renaming attribute tag via script
« on: June 11, 2007, 03:05:48 PM »
I have been updating some of our revision blocks to work across multiple layouts but have been dealing with alot of conflicts as a result. Basically what I need to do now is from a script open up a drawing and look for a tag name and if it exists rename it on all layout tabs. The key is that I need to preserve all of the exisitng attribute values. How would I go about doing something like this?


David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: Renaming attribute tag via script
« Reply #1 on: June 11, 2007, 03:20:00 PM »
I dont think you can.  How would you do it without a script file?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #2 on: June 11, 2007, 04:42:36 PM »
You can try this:
Code: [Select]
(defun c:TagReplace (/ blkname blkobj)

  (defun ReplaceBlockTag (OldName NewName obj / atts idx obj)
    (vl-load-com)
    ;;  step through the block definition
    (vlax-for blkobj (vla-item
                       (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
                       (vla-get-name obj)
                     )
      (if (and (vlax-property-available-p blkobj 'tagstring)
               (= (strcase (vla-get-tagstring blkobj)) (strcase OldName))
          )
        (vla-put-tagstring blkobj NewName)
      )
    )
  )

  (setq blkname "Testblk")
  (if (setq ss (ssget "X" (list '(0 . "INSERT") (cons 2 blkname) '(66 . 1))))
    (progn
      (setq blkobj (vlax-ename->vla-object (ssname ss 0)))
      (ReplaceBlockTag "OldTag" "NewTag" blkobj)
    )
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Renaming attribute tag via script
« Reply #3 on: June 11, 2007, 04:45:31 PM »
Or this:

Code: [Select]
(defun rjp-replacetagname (lst / lays ss)
  (setq lays (vla-get-layers
       (vla-get-activedocument (vlax-get-acad-object))
     )
ss   (ssget "x" '((0 . "INSERT") (66 . 1)))
  )
  (if ss
    (progn
      (setq ss (mapcar 'vlax-ename->vla-object
       (vl-remove-if
'listp
(mapcar 'cadr (ssnamex ss))
       )
       )
      )
      (mapcar
'(lambda (b)
   (if (eq (vla-get-lock (vla-add lays (vla-get-layer b)))
   :vlax-false
       )
     (progn
       (mapcar
'(lambda (a)
    (mapcar '(lambda (c)
       (if (eq (strcase (car c))
       (strcase (vla-get-tagstring a))
   )
(vla-put-tagstring a (cdr c))
       )
     )
    lst
    )
  )
(vlax-invoke b 'getattributes)
       )
     )
   )
)
ss
      )
    )
  )
  (princ)
)
;;(rjp-replacetagname '(("funny" . "guy")("smelly" . "dung")("hairy" . "back")))



*updated to ignore objects on locked layers
*updated to check multiple tagnames and replace
« Last Edit: June 11, 2007, 05:48:19 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ELOQUINTET

  • Guest
Re: Renaming attribute tag via script
« Reply #4 on: June 11, 2007, 04:53:24 PM »
hmmm not sure but the thing is i need to do this on many many drawings. if i could run it on a folder which would include all subfolders that would work i suppose. any ideas?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #5 on: June 11, 2007, 05:12:05 PM »
Put the lisp in a script? 8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ELOQUINTET

  • Guest
Re: Renaming attribute tag via script
« Reply #6 on: June 11, 2007, 05:17:52 PM »
indeed cab i will try these out and let you guys know. thanks

dan

ELOQUINTET

  • Guest
Re: Renaming attribute tag via script
« Reply #7 on: June 11, 2007, 05:28:35 PM »
just something to think about, i would like to be able to search for several revision tags but if those revision tags don't exist i.e. there was no 3rd revision i don't want it to error out. i'll try them out tomorrow and we can proceed with error checking. thanks a million you guys are the best.

dan

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Renaming attribute tag via script
« Reply #8 on: June 11, 2007, 05:48:47 PM »
just something to think about, i would like to be able to search for several revision tags but if those revision tags don't exist i.e. there was no 3rd revision i don't want it to error out. i'll try them out tomorrow and we can proceed with error checking. thanks a million you guys are the best.

dan

Changed code above...

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #9 on: June 11, 2007, 06:20:45 PM »
My variant.
Code: [Select]
(defun c:TagReplace (/ blkname blkobj)

  (defun ReplaceBlockTag (OldName NewName obj / atts idx obj)
    (vl-load-com)
    (setq OldName (mapcar 'strcase OldName))
    ;;  step through the block definition
    (vlax-for blkobj (vla-item
                       (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
                       (vla-get-name obj)
                     )
      (if (and (vlax-property-available-p blkobj 'tagstring)
               (vl-position (strcase (vla-get-tagstring blkobj)) OldName)
          )
        (vla-put-tagstring blkobj NewName)
      )
    )
  )

  (setq blkname "Testblk")
  (if (setq ss (ssget "X" (list '(0 . "INSERT") (cons 2 blkname) '(66 . 1))))
    (progn
      (setq blkobj (vlax-ename->vla-object (ssname ss 0)))
      (ReplaceBlockTag '("OldTag1" "OldTag2") "NewTag" blkobj)
    )
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ELOQUINTET

  • Guest
Re: Renaming attribute tag via script
« Reply #10 on: June 12, 2007, 08:55:10 AM »
Hey guys,

I was fooling around with these and haven't been able to get anything to work. Maybe I'm doing something wrong here? I modified the red parts putting my block info in. I tried yours too Ron and it keeps giving me unknown command even though I manually load it and am sure i'm typing the right thing? I'll continue to fool around with these.

Code: [Select]
(defun c:TagReplace (/ blkname blkobj)

  (defun ReplaceBlockTag (OldName NewName obj / atts idx obj)
    (vl-load-com)
    ;;  step through the block definition
    (vlax-for blkobj (vla-item
                       (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
                       (vla-get-name obj)
                     )
      (if (and (vlax-property-available-p blkobj 'tagstring)
               (= (strcase (vla-get-tagstring blkobj)) (strcase OldName))
          )
        (vla-put-tagstring blkobj NewName)
      )
    )
  )

  (setq blkname "[color=red]REVTITLE[/color]")
  (if (setq ss (ssget "X" (list '(0 . "INSERT") (cons 2 blkname) '(66 . 1))))
    (progn
      (setq blkobj (vlax-ename->vla-object (ssname ss 0)))
      (ReplaceBlockTag "[color=red]REV_DATE" "REV__DATE[/color]" blkobj)
    )
  )
  (princ)
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #11 on: June 12, 2007, 09:16:37 AM »
Just add some reporting code to see what is happining.
Code: [Select]
(defun c:TagReplace (/ blkname blkobj)

  (defun ReplaceBlockTag (OldName NewName obj / atts idx obj cnt)
    (vl-load-com)
    (setq cnt 0)
    ;;  step through the block definition
    (vlax-for blkobj (vla-item
                       (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
                       (vla-get-name obj)
                     )
      (if (and (vlax-property-available-p blkobj 'tagstring)
               (= (strcase (vla-get-tagstring blkobj)) (strcase OldName))
          )
        (progn
          [color=red](Princ "\nUpdating Block")[/color]
        (vla-put-tagstring blkobj NewName)
          )
      )
    )
  )

  (setq blkname "REVTITLE")
  (if (setq ss (ssget "X" (list '(0 . "INSERT") (cons 2 blkname) '(66 . 1))))
    (progn
      (setq blkobj (vlax-ename->vla-object (ssname ss 0)))
      (ReplaceBlockTag "REV_DATE" "NEW_TAG" blkobj)
    )
    [color=red](Princ "\nNo Inserts found.")[/color]
  )
  (princ)
)

PS I made the new tag name more obvious.
« Last Edit: June 12, 2007, 09:17:49 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ELOQUINTET

  • Guest
Re: Renaming attribute tag via script
« Reply #12 on: June 12, 2007, 09:29:27 AM »
cab i see the updating block prompt but the tag was not updated?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #13 on: June 12, 2007, 09:33:19 AM »
Can you email me or post a test DWG?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #14 on: June 12, 2007, 09:51:09 AM »
I found the problem. Hold one.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.