Jump to content

Recommended Posts

Posted

Hi All,

I want to make a lisp program to add prefix/suffix to all attributes of a selected block.

Say, what I want to add is "(W1)".

If the block has two attributes and if the attributes have their values 12134 and 23564, it should become 12134 (W1) and 23564 (W1)

This process has to be looped so as to enable editing of multiple blocks one after the other.

So far I have this;

 


(defun c:w1()
 (setq n 1)
 (while
   (> n 0)
   (setq ent (car (entsel "Select Block:")))


;This is where I am stuck. This is where I need to edit the attributes

 );While
);Function

 

Please help...

Posted

Consider stepping through a filtered selection set of the desired, attributed blocks. Then step through the attributes (as a list) and check for the desired prefix/suffix in the textstring. If not present, add accordingly.

Posted (edited)

Maybe:

 

[b][color=BLACK]([/color][/b]defun c:addstr [b][color=FUCHSIA]([/color][/b]/ atype ns ss en ed an ad av nv[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]initget 1 [color=#2f4f4f]"Suffix Prefix"[/color][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq atype [b][color=NAVY]([/color][/b]getkword [color=#2f4f4f]"\nAdd Suffix or Prefix [b][color=MAROON]([/color][/b]S/P[b][color=MAROON])[/color][/b]:   "[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]initget 1[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq ns [b][color=NAVY]([/color][/b]getstring t [color=#2f4f4f]"\nString To Add:  "[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq 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 0[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][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]while [b][color=GREEN]([/color][/b]/= [color=#2f4f4f]"SEQEND"[/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 av [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 1 ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                          nv [b][color=BLUE]([/color][/b]if [b][color=RED]([/color][/b]= atype [color=#2f4f4f]"Prefix"[/color][b][color=RED])[/color][/b]
                                 [b][color=RED]([/color][/b]strcat ns av[b][color=RED])[/color][/b]
                                 [b][color=RED]([/color][/b]strcat av ns[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]entmod [b][color=BLUE]([/color][/b]subst [b][color=RED]([/color][/b]cons 1 nv[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]assoc 1 ad[b][color=RED])[/color][/b] ad[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=MAROON]([/color][/b]entupd en[b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]ssdel en ss[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]

 

 

-David

Edited by David Bethel
Fixed attribute loop value
Posted

Thanks David.. That Worked...!!!

:)

Posted

David,

By the way, how do I check if two strings are alike?

In VBA we use the 'Like' operator...

Say,

If AttributeText Like "*(W1)" then

what is it in Lisp?

Posted

David, I think that will change both attributes to the same value :wink:

Posted

May be... In my case I just have only one attribute. So it worked....

Posted

But I am a starter in lisp. So I dont know how to iterate through the attributes. Well... David's code helped. Thanks for your suggestion.... RenderMan....

Posted

Give this a try:

 

(defun c:test ( / as el en i ss str typ )

 (initget "Prefix Suffix")
 (setq typ (cond ((getkword "\nAdd Prefix or Suffix? [Prefix/Suffix] <Prefix>: ")) ("Prefix")))
 (setq str (getstring t (strcat typ " to Add: ")))

 (if (setq ss (ssget '((0 . "INSERT") (66 . 1))))
     (repeat (setq i (sslength ss))
         (setq en (ssname ss (setq i (1- i))))
         (while (eq "ATTRIB" (cdr (assoc 0 (setq el (entget (setq en (entnext en)))))))
             (setq as (cdr (assoc 1 el)))
             (if (eq "Prefix" typ)
                 (if (not (wcmatch as (strcat str "*")))
                     (entmod (subst (cons 1 (strcat str as)) (assoc 1 el) el))
                 )
                 (if (not (wcmatch as (strcat "*" str)))
                     (entmod (subst (cons 1 (strcat as str)) (assoc 1 el) el))
                 )
             )
         )
     )
 )
 (princ)
)

  • Like 1
Posted
David, I think that will change both attributes to the same value :wink:

 

Lee is correct. I edited post #3 to reflect the correct code.

 

Thanks! -David

  • 2 years later...
Posted

As a relative CAD newbie, with only "Google Training", I really have to thank guys like you for contributing and helping me out! You just saved me countless time with this solution. One would assume that adding .1 to numbers of wire labels in a set of drawings wouldn't need to be so difficult. You guys made it easy! Thanks again!

  • 2 years later...
Posted

Hi David

I just tried your LSP and it worked great. Thanks for posting it.

If you have the time would you mind explaining the processes in the your LSp. I am a bit of a novice but would like to know how each line worked. Obviously defun C;addstr is the command to run the file but the other commands including atype ns ss en ed an ad av nv. I understand down to teh "\nString to add: "))

I would certainly appreciate you comments and explaination.

Thanks

Regards Rick.

Posted (edited)

Wow, It's been a while since I've commented a routine :shock:

 

I have changed a bit on the format but it is basically the same, just a bit more efficient

 

     [color=#8b4513];  define a function named addstr[/color]
     [color=#8b4513];  the c: creates a command line call[/color]
     [color=#8b4513];  [b][color=BLACK]([/color][/b]/ local_variable_list[b][color=BLACK])[/color][/b][/color]
[b][color=BLACK]([/color][/b]defun c:addstr [b][color=FUCHSIA]([/color][/b]/ atype ns ss en ed an ad av nv i[b][color=FUCHSIA])[/color][/b]

 [color=#8b4513];  initialize a [b][color=FUCHSIA]([/color][/b]getxxxx[b][color=FUCHSIA])[/color][/b] call[/color]
 [color=#8b4513];  if bit1 is set in [b][color=FUCHSIA]([/color][/b]getkword[b][color=FUCHSIA])[/color][/b], it forces an input of 1 of of the keywords[/color]
 [color=#8b4513];  [color=#2f4f4f]"Keyword List"[/color][/color]
 [b][color=FUCHSIA]([/color][/b]initget 1 [color=#2f4f4f]"Suffix Prefix"[/color][b][color=FUCHSIA])[/color][/b]

 [color=#8b4513];  Select the type of action needed[/color]
 [b][color=FUCHSIA]([/color][/b]setq atype [b][color=NAVY]([/color][/b]getkword [color=#2f4f4f]"\nAdd Suffix or Prefix [b][color=MAROON]([/color][/b]S/P[b][color=MAROON])[/color][/b]:   "[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]


 [color=#8b4513];  if bit 1 is set , this forces a non nil input[/color]
 [b][color=FUCHSIA]([/color][/b]initget 1[b][color=FUCHSIA])[/color][/b]

 [color=#8b4513];  get user input of NewString[/color]
 [color=#8b4513];  allow spaces , case sensitive[/color]
 [b][color=FUCHSIA]([/color][/b]setq ns [b][color=NAVY]([/color][/b]getstring t [color=#2f4f4f]"\nString To Add:  "[/color][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

      [color=#8b4513];  if user creates a SelectionSet of INSERTs with ATTRIButes[/color]
 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq 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]

      [color=#8b4513];  Initialize an integer counter[/color]
      [b][color=NAVY]([/color][/b]setq i 0[b][color=NAVY])[/color][/b]

      [color=#8b4513]; step thru the selection SS 1 EName at a time[/color]
      [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
                   [color=#8b4513];  retrieve the EntityDefinition[/color]
             [b][color=MAROON]([/color][/b]setq ed [b][color=GREEN]([/color][/b]entget en[b][color=GREEN])[/color][/b]

                   [color=#8b4513];  retrieve the 1st Attribute eName[/color]
                   an [b][color=GREEN]([/color][/b]entnext en[b][color=GREEN])[/color][/b]
                   [color=#8b4513];  retrieve the 1st Attribute Definition[/color]
                   ad [b][color=GREEN]([/color][/b]entget an[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]


             [color=#8b4513];  step thru the INSERT definition and[/color]
             [color=#8b4513];  retrieve each attribute until a SEQEND entity is returned[/color]
             [b][color=MAROON]([/color][/b]while [b][color=GREEN]([/color][/b]/= [color=#2f4f4f]"SEQEND"[/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]

                    [color=#8b4513];  retrieve the current Attribute Value[/color]
                    [b][color=GREEN]([/color][/b]setq av [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 1 ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]

                    [color=#8b4513]; concatenate the New Value[/color]
                          nv [b][color=BLUE]([/color][/b]if [b][color=RED]([/color][/b]= atype [color=#2f4f4f]"Prefix"[/color][b][color=RED])[/color][/b]
                                 [b][color=RED]([/color][/b]strcat ns av[b][color=RED])[/color][/b]
                                 [b][color=RED]([/color][/b]strcat av ns[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]

                    [color=#8b4513];  modify the attribute entity definition with the new value[/color]
                    [b][color=GREEN]([/color][/b]entmod [b][color=BLUE]([/color][/b]subst [b][color=RED]([/color][/b]cons 1 nv[b][color=RED])[/color][/b] [b][color=RED]([/color][/b]assoc 1 ad[b][color=RED])[/color][/b] ad[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]

                    [color=#8b4513]; go to the next Attribute Name and Definition[/color]
                    [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]

             [color=#8b4513];  update the main insert Entity Name[/color]
             [b][color=MAROON]([/color][/b]entupd en[b][color=MAROON])[/color][/b]

             [color=#8b4513];  increase the integer counter[/color]
             [b][color=MAROON]([/color][/b]setq i [b][color=GREEN]([/color][/b]1+ i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

[color=#8b4513];  exit the routine cleanly[/color]
[b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

Have fun -David

Edited by David Bethel
  • 1 year later...
Posted

Hi,

 

I believe that threat is old but I found it very helpful.

That lisp routine is exactly what I need except one missing piece. The block I need to edit has multiple attributes while I need to append a suffix only to values of single attribute out of all these attributes in the same block. I'm not an expert enough in list routines. So, if it's possible to explain to me how to modify that routine to modify only one attribute value in the block it would be great.

 

Thanks in advance.

Sameh Saad

Posted

Hi David,

 

what am I supposed to change in your routine in order to add a prefix to a specific attribute of a specific block ?

 

I also have to do this on multiple drawings (same block, same attribute).

 

Any help would be greatly appreciated.

 

Thank you,

Ste

Posted

Wow that is an older 1.

 

For INSERTs with multiple ATTRIButes, filtering a TAGNAME is the easiest. You could also filter by the placement ( ie 1st 2nd 3rd 4th place ) in the order of ATTRIButes

 

Then there also by text color, style, size, angle so on and so on

 

What is your criteria ?

 

 

But be fore warned, they are ways to that pretty much all of these can fail

-David

Posted

I was thinking of:

 

1. Writing an AutoLisp routine that edits a specific attribute value of a specific block;

2. Writing a script that executes the routine;

3. Using Script-Pro in order to run the script on multiple drawings.

 

Does this make any sense? Unfortunately I am a total newbie in AutoLisp :(

Posted
I was thinking of:

 

1. Writing an AutoLisp routine that edits a specific attribute value of a specific block;

2. Writing a script that executes the routine;

3. Using Script-Pro in order to run the script on multiple drawings.

 

Does this make any sense? Unfortunately I am a total newbie in AutoLisp :(

 

Is that regardless of the tag name and/or attribute sequence ?

Case sensitive ?

 

-David

Posted

Actually every drawing has the same block in it (same block name, same tags).

In this block there is an attribute whose tag name is "18".

The routine should be able to add the prefix "0" to the value of this attribute.

 

Thank you for your help David.

 

Ste

  • 3 years later...
Posted
On 7/28/2011 at 9:45 AM, Lee Mac said:

David, I think that will change both attributes to the same value 😉

 

Hello, I am facing a similar task and would very much appreciate if you could help me with this code so it changes a selected attribute and not all of them.

 

As long as we on subject I leave the information below that may be of interest.

 

  • My Incremental Numbering Suite application will provide a far more extensive set of options to allow you to customise the numbering format, permitting a Prefix & Suffix, multiple incrementing sections, and alphanumerical incrementing.

    You can number existing attributed block references using this application by typing 'R' or 'r' at the AutoCAD command-line during object placement to enter Replacement Mode.

  • My Incremental Array application will allow you to automatically increment the attribute values whilst arraying an attributed block reference (or other objects).

 

 

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...