Jump to content

Change Block Attribute


forum32

Recommended Posts

Hi guys,

 

I am pretty new to autocad, but am familiar with programming in general. I am trying to write a script to change some information in a block.

 

Unfortunately the change is complex and needs to be done on about a hundred drawings.

 

The current block has:

Designed by: and Approved by:

 

The person who originally made the block should have separated the two tags to make the process much easier (DName & AName would have been nice).

 

Is there a way to only change the XXX tag in the "Approved by" box? I have tried messing with -attedit and the commands, but it seems to apply to both instances of the tag. Can I reference the tag using a numerical value? Say XXX(1) similar to an array? I have seen that VBA can be used to reference using an array but unfortunately that does not get installed with 2010 and would required a call to IT.

 

Thanks!

Link to comment
Share on other sites

Hi forum32,

 

Welcome to CADTutor :)

 

Take a look at the selection of subfunctions here - the SetAttributeValue function will only set the value of the first attribute it finds with a specific tag - you can approach the problem using either Visual LISP or Vanilla LISP.

 

Lee

Link to comment
Share on other sites

Here's a snipet that steps thru a selection set of attributed inserts and dumps their attribute tagnames and values in an associative list:

 

[b][color=BLACK]([/color][/b]defun c:dump-att [b][color=FUCHSIA]([/color][/b]/ i ss en ed an ad al[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq i -1
           ss [b][color=MAROON]([/color][/b]ssget '[b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]0 . [color=#2f4f4f]"INSERT"[/color][b][color=BLUE])[/color][/b][b][color=BLUE]([/color][/b]66 . 1[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss [b][color=BLUE]([/color][/b]setq i [b][color=RED]([/color][/b]1+ i[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq ed [b][color=GREEN]([/color][/b]entget en[b][color=GREEN])[/color][/b]
                   an [b][color=GREEN]([/color][/b]entnext en[b][color=GREEN])[/color][/b]
                   ad [b][color=GREEN]([/color][/b]entget an[b][color=GREEN])[/color][/b]
                   al nil[b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]while [b][color=GREEN]([/color][/b]= [color=#2f4f4f]"ATTRIB"[/color] [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 0 ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]setq al [b][color=BLUE]([/color][/b]cons [b][color=RED]([/color][/b]cons [b][color=PURPLE]([/color][/b]cdr [b][color=TEAL]([/color][/b]assoc 2 ad[b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b]
                                         [b][color=PURPLE]([/color][/b]cdr [b][color=TEAL]([/color][/b]assoc 1 ad[b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b] al[b][color=BLUE])[/color][/b]
                          an [b][color=BLUE]([/color][/b]entnext an[b][color=BLUE])[/color][/b]
                          ad [b][color=BLUE]([/color][/b]entget an[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq al [b][color=GREEN]([/color][/b]reverse al[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]textpage[b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]princ [b][color=GREEN]([/color][/b]strcat [color=#2f4f4f]"\n"[/color] [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 2 ed[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b] [color=#2f4f4f]" Attributes [b][color=BLUE]([/color][/b]Tag . Value[b][color=BLUE])[/color][/b]\n"[/color][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]prin1 al[b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]getstring [color=#2f4f4f]"\nPress Enter To Continue..."[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
   [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

Do I understand that you (2) attributes with the same tag name? -David

Link to comment
Share on other sites

Thanks guys, this is good info already. Could SetAttrib be easily modified to pick the second instance of the given tag? I've never used lisp before but am starting to grasp it.

 

David: Yep. That is right, the block was originally set up to have both the designed by and approved by fields filled in with identical information (I'm not sure why.)

 

Here is a screenshot from the Block Editor

example.jpg

 

The idea is to fill only the "Approved:" field with a name. (Will be the same across 100 drawings)

 

Both fields are currently blank.

 

Here is the output from that script:

STANDARD Attributes (Tag . Value)
(("DRAWING_NAME" . "removed") ("DRAWING-LINE_TWO" . "removed") ("XXX" . "<blank>") ("XXX" . "<blank>") ("MM_YY" . "FEB/05") 
("SCALE" . "NTS") ("X-1" . "removed") ("FILE_NAME" . "removed"))

 

I was planning on copying the drawings that need the change to a folder, running a batch file to process them all. Originally I had hoped to simply make a script to do something like -attedit, n, n, standard, XXX, *, but it changed both instances of XXX.

 

It would seem the command entnext cycles through the tags?

Edited by forum32
additional info
Link to comment
Share on other sites

I ran Lee Mac's Lisp and it changed the "Designed by:" field. So I think the one I need to change is the second occurence.

 

Hi Forum,

 

To reach the second one first, use this:

 

;;----------------=={ Set Attribute Value }==-----------------;;
;;                                                            ;;
;;  Populates the first attribute matching the tag specified  ;;
;;  found within the block supplied with the value specified, ;;
;;  if present.                                               ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2010 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  block - VLA Block Reference Object                        ;;
;;  tag   - Attribute TagString                               ;;
;;  value - Value to which the Attribute will be set          ;;
;;------------------------------------------------------------;;
;;  Returns:  Value the attribute was set to, else nil        ;;
;;------------------------------------------------------------;;

;; Modified to process the attributes in reverse order.

(defun LM:SetAttributeValue ( block tag value )
 ;; © Lee Mac 2010
 (vl-some
   (function
     (lambda ( attrib )
       (if (eq tag (vla-get-TagString attrib))
         (progn (vla-put-TextString attrib value) value)
       )
     )
   )
   [color=red](reverse[/color] (vlax-invoke block 'GetAttributes)[color=red])[/color]
 )
)

Link to comment
Share on other sites

Change the string value for nv as needed

 

This should change the second attribute with the tag name "XXX"

 

[b][color=BLACK]([/color][/b]defun c:xxx-att [b][color=FUCHSIA]([/color][/b]/ nv i ss en ed an ad tg xt[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq nv [color=#2f4f4f]"New Name Value"[/color][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq i -1
           ss [b][color=MAROON]([/color][/b]ssget '[b][color=GREEN]([/color][/b][b][color=BLUE]([/color][/b]0 . [color=#2f4f4f]"INSERT"[/color][b][color=BLUE])[/color][/b][b][color=BLUE]([/color][/b]66 . 1[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss [b][color=BLUE]([/color][/b]setq i [b][color=RED]([/color][/b]1+ i[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq ed [b][color=GREEN]([/color][/b]entget en[b][color=GREEN])[/color][/b]
                   an [b][color=GREEN]([/color][/b]entnext en[b][color=GREEN])[/color][/b]
                   ad [b][color=GREEN]([/color][/b]entget an[b][color=GREEN])[/color][/b]
                   xt nil[b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]while [b][color=GREEN]([/color][/b]= [color=#2f4f4f]"ATTRIB"[/color] [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 0 ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]setq tg [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 2 ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]cond [b][color=BLUE]([/color][/b][b][color=RED]([/color][/b]and [b][color=PURPLE]([/color][/b]= tg [color=#2f4f4f]"XXX"[/color][b][color=PURPLE])[/color][/b]
                                [b][color=PURPLE]([/color][/b]not xt[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b]
                           [b][color=RED]([/color][/b]setq xt T[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                          [b][color=BLUE]([/color][/b][b][color=RED]([/color][/b]and [b][color=PURPLE]([/color][/b]= tg [color=#2f4f4f]"XXX"[/color][b][color=PURPLE])[/color][/b]
                                xt[b][color=RED])[/color][/b]
                           [b][color=RED]([/color][/b]entmod [b][color=PURPLE]([/color][/b]subst [b][color=TEAL]([/color][/b]cons 1 nv[b][color=TEAL])[/color][/b] [b][color=TEAL]([/color][/b]assoc 1 ad[b][color=TEAL])[/color][/b] ad[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]setq an [b][color=BLUE]([/color][/b]entnext an[b][color=BLUE])[/color][/b]
                          ad [b][color=BLUE]([/color][/b]entget an[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
   [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

1 gotcha is that it will also change the 3rd 4th 5th ....XXXs as well. Just not the 1st.

 

Have fun! -David

Link to comment
Share on other sites

Have you considered the use of Sheet Set Manager (SSM)?

 

We use attributes title blocks, which are converted to fields (the attribute values), and populated by SSM Custom Properties. One change, and every plan sheet which has the revised attribute will reflect the changes when printed. For a drawing already open, simply regen.

 

Hope this helps!

Link to comment
Share on other sites

You guys are awesome....it is working perfectly so far. I ran a batch file in the folder with a couple test pdf's.

 

1) I put lisp in autoloader for autocad (forget exact name, but loads lsp's for you when autocad starts).

2) Batch file calls acad.exe /b "replaceit" for all dwgs.

3) replaceit.scr: basically runs lsp, provides block, _.saves, then quits.

 

Probably not the most elegant solution, but saves someone hours of manually editing the drawings.

 

SSM eh? Too new to AutoCAD to be familiar with that but I will play with it to see what I can figure out.

Link to comment
Share on other sites

SSM eh? Too new to AutoCAD to be familiar with that but I will play with it to see what I can figure out.

 

I wouldn't suggest it for a one-time use scenario, because there is some in-depth thought put into this type of system. The benefits come when the 'system' is in place, and changes are needed prior to submittal time. Especially when some of our plan sets are 400+ sheets each.

 

SSM is a great tool for our workflow - I only wish they'd expose SSM objects, properties, and methods to Visual LISP. While I'm excited to learn .NET, I kind of think that it's ridiculous LISP has no capabilities with SSM. I digress.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...