Author Topic: Renaming attribute tag via script  (Read 10215 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.

ELOQUINTET

  • Guest
Re: Renaming attribute tag via script
« Reply #15 on: June 12, 2007, 10:00:19 AM »
CAB I was thinking that too. I have inserted our revision blocks into a drawing and also included the lisp rotine we are now using to update the rev 1 block. we used to have to update every page one by one and this one works across multiple layouts. Originally both of the tag names were REV_DATE and the uptoday or today is date formatting code from our acad.lsp. I was having conflicts if I opened up an old file and accidently used the new tool. Now we have an old and new update tool and I'd like to eliminate it by converting all of the old tag names to the new but retain the job info. Am I making sense, I hope so. I just wanted to tell the whole story in case someone is just jumping in at this point.

Try this

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #16 on: June 12, 2007, 10:04:39 AM »
My problem was that I updated the block but not the inserts, so new inserts are correct but old ones still have the wrong tag.
Just tried Ron's & it worked fine and his honers locked layers.

Try his again.
Code: [Select]
(rjp-replacetagname '(("REV_DATE" . "TestTag")("NEW_TAG" . "TestTag")("TestTag" . "Xtag")))You get an error?
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 #17 on: June 12, 2007, 10:17:11 AM »
OK we will try to get his working. I tried this and get unknown command when i type replacetagname, why?

Code: [Select]
(defun 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)
)
(replacetagname '(("REV__DATE" . "TestTag")("NEW_TAG" . "TestTag")("TestTag" . "Xtag")))

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #18 on: June 12, 2007, 10:32:38 AM »
You have to paste this at the command line:
Code: [Select]
(replacetagname '(("REV__DATE" . "TestTag")("NEW_TAG" . "TestTag")("TestTag" . "Xtag")))
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 #19 on: June 12, 2007, 10:50:27 AM »
OK, here is my corrected code. Honers locked layers. Note that this is the layer the attribute is on & not layer the insert is on.
Code: [Select]
(defun c:TagReplace (/ blkname blkobjs)
  ;;  replace attr Tag in Inserts & Block
  (defun ReplaceBlockTag (OldNames  ; list of old tag names
                          NewName   ; string - New tag name
                          objlst    ; lit of Inserts in DWG
                          / atts idx obj)
    (vl-load-com)
    (setq OldNames (mapcar 'strcase OldNames))
    ;;  Update Inserts
    (foreach obj objlst
      (foreach att (vlax-invoke obj 'getattributes)
        (if (vl-position (strcase (vla-get-tagstring att)) OldNames)
          (setq err (vl-catch-all-apply 'vla-put-tagstring (list att NewName)))))
    )
    (if (vl-catch-all-error-p err)
      (princ "\nFailed - Attr on Locked layer.")
      ;;  step through the block definition
      (vlax-for blkobj (vla-item
                         (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
                         (vla-get-name (car objlst))
                       )
        (if (and (vlax-property-available-p blkobj 'tagstring)
                 (vl-position (strcase (vla-get-tagstring blkobj)) OldNames)
            )
          (vla-put-tagstring blkobj NewName)
        )
      )
    )
  )

  (setq blkname "REVTITLE")
  (if (setq ss (ssget "X" (list '(0 . "INSERT") (cons 2 blkname) '(66 . 1))))
    (progn
      (setq blkobjs (mapcar 'vlax-ename->vla-object (mapcar 'cadr (ssnamex ss))))
      (ReplaceBlockTag '("REV_DATE" "NEW_TAG" "Tag1") "Tag2" blkobjs)
    )
    (princ "\nNo Inserts Found.")
  )
  (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 #20 on: June 12, 2007, 10:52:47 AM »
Ah ok My result was Xtag so it does work. Now that it works from the command line i can just put this is my script right. So how do I specify multiple blocks names or do I not need to?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #21 on: June 12, 2007, 11:01:30 AM »
Mine will not handle multiple block names.
Ron's code only looks at the tag names so block names don't come into play.
Unless there is a block with matching name you don't want changed.
Also note that Ron's honers the locked layer of the Insert, unlike mine.
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 #22 on: June 12, 2007, 11:06:42 AM »
Hold the presses.
I just caught that Ron's doesn't update the Block Definition, so if you insert another of that block
it will have the old tag name. Let's see if Ron wants to add that.
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 #23 on: June 12, 2007, 11:19:46 AM »
CAB i have uploaded an old drawing to illustrate another potential problem. The tags in the old blocks both have the same name but the new block has different names. I don't know how I would specify which is which. I'll ask maybe a stupid question but do the attributes really need to have different names to use the new routine. I'm trying to figure out which would be the easier to change? Have a look

Old Method

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #24 on: June 12, 2007, 12:08:11 PM »
Having attributes with the same tag is a problem with both of our routines.
Additional programing is needed to specify which attribute needs to be changed else all matching attributes will be changed.

What problem are you experiencing with using 2 underscore characters?

To specify specify which is which.
You would need to specify first or last, or 1 or 2, etc.
« Last Edit: June 12, 2007, 12:09:57 PM 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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #25 on: June 12, 2007, 12:14:06 PM »
I see that c:Rec1 Updates the date attributes on all tabs.
To be more precise it updates the last block added to that tab & ignores the blocks added prior to the last 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.

ELOQUINTET

  • Guest
Re: Renaming attribute tag via script
« Reply #26 on: June 12, 2007, 01:44:11 PM »
not sure i follow you cab but basically i want all of our drawings old or new to update the same way. now we are having conflicts between the old and new blocks as we have combonations of old and new revisions. Yikes I think I've created a monster here. I just got so annoyed with having to update each layout but dealing with the legacy files is the real PITA. I keep getting users telling me that the update is not working and it's because they are trying to use the wrong tool for the wrong block and it's a mess.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #27 on: June 12, 2007, 02:01:15 PM »
OK, after some snooping the deference is that one string has 5 characters & the other has 8 characters.
Change REV_DATE with 8 characters  to  REV4__DATE

Change REV_DATE with 5 characters  to  REV4__DATE_ABBRV

Is that what you want to do?

Is REV_DATE the only old tag or are there others like REV__DATE ?
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 #28 on: June 12, 2007, 03:33:56 PM »
CAB Here is a breakdown of the block names and associated tags

Block name and Tags for old blocks:

Revtitle   REV_DATE
Revtitl2   REV2_DATE
Revtitl3   REV3_DATE
Revtitl4   REV4_DATE

Block name and Tags for new blocks:

Revtitle   REV__DATE (Bottom)
Revtitle   REV__DATE__ABBRV (Top)
Revtitl2   REV2__DATE (Bottom)
Revtitl2   REV2__DATE__ABBRV (Top)
Revtitl3   REV3__DATE (Bottom)
Revtitl3   REV3__DATE__ABBRV (Top)
Revtitl4   REV4__DATE (Bottom)
Revtitl4   REV4__DATE__ABBRV (Top)



ELOQUINTET

  • Guest
Re: Renaming attribute tag via script
« Reply #29 on: June 13, 2007, 08:53:41 AM »
so cab any thoughts on how this could be done. i was reading through the original post as saw mention of doing it by order. There are 2 attributes of the same name in the old blocks but they occur in a certain order everytime so in effect the first one could be changed to this and the second to that, no?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #30 on: June 13, 2007, 09:07:53 AM »
It will take a specialised routine and it can be done using the order or the length of the text string "06/21" vs "06/21/06"
I don't have time to play with it today but if no one steps up I may have time tomorrow.
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 #31 on: June 13, 2007, 09:40:10 AM »
that's cool cab it's no rush, i appreciate anything you can do. I just need to have a way to eventually clean this mess up. Thanks man

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Renaming attribute tag via script
« Reply #32 on: June 13, 2007, 08:44:54 PM »
Hold the presses.
I just caught that Ron's doesn't update the Block Definition, so if you insert another of that block
it will have the old tag name. Let's see if Ron wants to add that.


CAB,

How would I update the block definition? I'm not sure where to start.

Ron

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #33 on: June 13, 2007, 09:12:09 PM »
Maybe 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 (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)
       )
               ;;  update block def  -------------------
      (vlax-for blkobj (vla-item
                         (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
                         (vla-get-name b)
                       )
    (mapcar '(lambda (c)
       (if (and (vlax-property-available-p blkobj 'tagstring)
                                        (eq (strcase (car c))
                                            (strcase (vla-get-tagstring blkobj))
   ))
(vla-put-tagstring blkobj (cdr c))
       )
     )
    lst
    )
       
       ) ; ----------------
     )
   )
)
ss
      )
    )
  )
  (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.

Fatty

  • Guest
Re: Renaming attribute tag via script
« Reply #34 on: June 14, 2007, 05:48:30 AM »
Hi Alan
I use
Code: [Select]
(vlax-property-available-p blkobj 'tagstring T)when i want to change tagstring property
Is it correct?

~'J'~

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Renaming attribute tag via script
« Reply #35 on: June 14, 2007, 07:50:40 AM »
Hey Fatty,
You have led me to a flaw in the code.
Other than a Locked layer, why would the property be unavailable?

I did some more testing & if the layer the attribute is on is locked it will fail.
So another test is needed to prevent that failure.

But what other condition would prevent the tag replacement?

Bad tag name is the only thing that comes to mind. Anyone know of another?
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 #36 on: June 14, 2007, 08:53:33 AM »
wow you guys have been busy this morning. I have encountered some instances where these attributes are on a locked layer. To safeguard against this I was just thinking of unlocking all the layers first via script. We don't use layers that much here so it should be on a specific layer but it is not guarenteed. I know it is good practice as a coder to cover all the bases but like I said this is primarily being used for housekeeping purposes. Most jobs done before this year are already installed and will not be revised so there will be no conflict but I'd like to retag as many as I can anyway. I'll check back in a little bit thanks guys

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Renaming attribute tag via script
« Reply #37 on: June 14, 2007, 03:08:58 PM »
Thanks CAB...I see the light now. :)

I updated to catch if the attribute is on a locked layer.

Code: [Select]
(defun rjp-replacetagname (lst / blkobj 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 (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
   (and (eq (strcase (car c))
    (strcase (vla-get-tagstring a))
)
(eq (vla-get-lock
      (vla-add lays (vla-get-layer a))
    )
    :vlax-false
)
   )
    (vla-put-tagstring a (cdr c))
)
       )
      lst
    )
  )
(vlax-invoke b 'getattributes)
       )
       (vlax-for blkobj (vla-item
  (vla-get-blocks
    (vla-get-activedocument
      (vlax-get-acad-object)
    )
  )
  (vla-get-name b)
)
(mapcar
   '(lambda (c)
      (if
(and
  (vlax-property-available-p blkobj 'tagstring)
  (eq (strcase (car c))
      (strcase (vla-get-tagstring blkobj))
  )
)
(vla-put-tagstring blkobj (cdr c))
      )
    )
   lst
)
       )
     )
   )
)
ss
      )
    )
  )
  (princ)
)
(rjp-replacetagname '(("tagnametoreplace" . "newtagname")))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ELOQUINTET

  • Guest
Re: Renaming attribute tag via script
« Reply #38 on: June 18, 2007, 08:59:30 AM »
I thought the main thing we were trying to figure out was how to distinguish which one of the old tags to change to what. I think the only way would be to have it read the number of characters as CAB suggested. One tag will read as Month/Day and the other Month/Day/Year so I think it would be best to do something like look at the value and if it contains more than 5 characters it should be changed to REV__DATE and if it is 5 or less it should be changed to REV__DATE__ABBRV. or like CAB said it could be based on 5 or 8 characters as well. Anybody have time to look into this?