Author Topic: Attribute Change text (value) based on Attribute Tag - Lisp  (Read 1584 times)

0 Members and 1 Guest are viewing this topic.

AVCAD

  • Newt
  • Posts: 29
Hey all,

I am trying to make a lisp to run to allow me to change a Attribute value Based on the TAG. I want anything with the attribute tag to be changed to a set value like XXX. This is kind of a reset for all blocks in a drawing. We use a certain TAG to list out what rooms the device is in. And when coping from drawing to drawing we have to remember to change these...I would like to reset anything in the drawing to a default value (XXX) and then i can use find replace to set it to the correct value.

Any one have something like this or know how to make it work? I have tried -ATTEDIT but it only does 1 object at a time and i want to do the entire drawing.

Thanks in Advance!



Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Attribute Change text (value) based on Attribute Tag - Lisp
« Reply #1 on: June 28, 2023, 02:50:55 PM »
Consider the following simple code -
Code - Auto/Visual Lisp: [Select]
  1. (defun c:battedit ( / blk tag val )
  2.     (setq blk (strcase (getstring t "\nSpecify optional block filter <*>: "))
  3.           tag (strcase (getstring "\nSpecify optional tag filter <*>: "))
  4.           val (getstring t "\nSpecify new attribute value <blank>: ")
  5.     )
  6.     (if (= "" blk) (setq blk "*"))
  7.     (if (= "" tag) (setq tag "*"))
  8.         (if (= :vlax-false (vla-get-isxref def))
  9.             (vlax-for obj def
  10.                 (if (and (= "AcDbBlockReference" (vla-get-objectname obj))
  11.                          (wcmatch (blockname obj) blk)
  12.                          (= :vlax-true (vla-get-hasattributes obj))
  13.                          (vlax-write-enabled-p obj)
  14.                     )
  15.                     (foreach att (vlax-invoke obj 'getattributes)
  16.                         (if (and (wcmatch (strcase (vla-get-tagstring att)) tag)
  17.                                  (vlax-write-enabled-p att)
  18.                             )
  19.                             (vla-put-textstring att val)
  20.                         )
  21.                     )
  22.                 )
  23.             )
  24.         )
  25.     )
  26.     (princ)
  27. )
  28.  
  29. (defun blockname ( obj )
  30.     (if (vlax-property-available-p obj 'effectivename)
  31.         (defun blockname ( obj ) (strcase (vla-get-effectivename obj)))
  32.         (defun blockname ( obj ) (strcase (vla-get-name obj)))
  33.     )
  34.     (blockname obj)
  35. )
  36.  

Note that this will operate on all blocks (primary & nested) within all drawing layouts.
« Last Edit: June 28, 2023, 02:54:03 PM by Lee Mac »

AVCAD

  • Newt
  • Posts: 29
Re: Attribute Change text (value) based on Attribute Tag - Lisp
« Reply #2 on: June 29, 2023, 03:15:59 PM »
Lee mac! works perfectly for what I need. Thank you very much!

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Attribute Change text (value) based on Attribute Tag - Lisp
« Reply #3 on: June 29, 2023, 04:51:32 PM »
You're most welcome, happy to help!  :-)

AVCAD

  • Newt
  • Posts: 29
Re: Attribute Change text (value) based on Attribute Tag - Lisp
« Reply #4 on: July 11, 2023, 09:54:31 AM »
Is there a way to use this to find blocks that have a attribute but is blank? I would like to reset the blank ones to some thing like xxx so i can find them.

Problem is when we have blocks that have blank attributes it messes up some routines we have, that rely on these attributes.

What is above works great for mass changing everything, again any way to make this work with attributes that are blank to reset them to a defined state?

Thanks again!

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Attribute Change text (value) based on Attribute Tag - Lisp
« Reply #5 on: July 11, 2023, 12:16:32 PM »
Is there a way to use this to find blocks that have a attribute but is blank? I would like to reset the blank ones to some thing like xxx so i can find them.

Try the following -
Code - Auto/Visual Lisp: [Select]
  1. (defun c:battedit ( / blk tag val vft )
  2.     (setq blk (strcase (getstring t "\nSpecify optional block filter <*>: "))
  3.           tag (strcase (getstring "\nSpecify optional tag filter <*>: "))
  4.           vft (strcase (getstring t "\nSpecify optional value filter (type SPACE for blank values) <*>: "))
  5.           val (getstring t "\nSpecify new attribute value <blank>: ")
  6.     )
  7.     (if (= "" blk) (setq blk "*"))
  8.     (if (= "" tag) (setq tag "*"))
  9.     (if (= "" vft) (setq vft "*") (setq vft (vl-string-trim " " vft)))
  10.         (if (= :vlax-false (vla-get-isxref def))
  11.             (vlax-for obj def
  12.                 (if (and (= "AcDbBlockReference" (vla-get-objectname obj))
  13.                          (wcmatch (blockname obj) blk)
  14.                          (= :vlax-true (vla-get-hasattributes obj))
  15.                          (vlax-write-enabled-p obj)
  16.                     )
  17.                     (foreach att (vlax-invoke obj 'getattributes)
  18.                         (if (and (wcmatch (strcase (vla-get-tagstring  att)) tag)
  19.                                  (wcmatch (strcase (vla-get-textstring att)) vft)
  20.                                  (vlax-write-enabled-p att)
  21.                             )
  22.                             (vla-put-textstring att val)
  23.                         )
  24.                     )
  25.                 )
  26.             )
  27.         )
  28.     )
  29.     (princ)
  30. )
  31.  
  32. (defun blockname ( obj )
  33.     (if (vlax-property-available-p obj 'effectivename)
  34.         (defun blockname ( obj ) (strcase (vla-get-effectivename obj)))
  35.         (defun blockname ( obj ) (strcase (vla-get-name obj)))
  36.     )
  37.     (blockname obj)
  38. )
  39.  

Type a space at the attribute value filter to target blank attribute values.

AVCAD

  • Newt
  • Posts: 29
Re: Attribute Change text (value) based on Attribute Tag - Lisp
« Reply #6 on: July 11, 2023, 12:44:20 PM »
Thanks LeeMAC, Works as needed. Thank you much...one day I will be able to do this alone...lol