Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LISP to Select Blocks and Add Attributes

53 REPLIES 53
SOLVED
Reply
Message 1 of 54
Kyle.para
11012 Views, 53 Replies

LISP to Select Blocks and Add Attributes

Hi All,

 

We have to add a ton of attributes to all of our blocks.

 

I found code that was originally written by Lee Mac, but it was designed to type in the block name that you wanted.

 

I just want to be able to select all the blocks that I want and then have it insert the tags into all of the blocks that are selected.

 

I tried to change the "name enter box" to a select command but the program comes back with no blocks found.

 

Sorry I am new to this and may as well be trying to learn another language.

 

(defun c:addattribs ( / blk def )
  ; Get Entities
    (while (not blk)
            (princ "\nSelect Blocks to Update:")
               (setq blk (ssget '((-4 . "<NOT") (0 . "INSERT,POLYLINE,VIEWPORT") (-4 . "NOT>"))))
    ) ;_  end while
        )
        (if (/= "" blk)
        (progn
            (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
            (vla-addattribute def
                (getvar 'textsize)
                acattributemodelockposition
                "New Attribute 1"
                (vlax-3D-point 0 0)
                "NEW_TAG1"
                "New Value 1"
            )
            (vla-addattribute def
                (getvar 'textsize)
                acattributemodelockposition
                "New Attribute 2"
                (vlax-3D-point 0 (- (* 1.5 (getvar 'textsize))))
                "NEW_TAG2"
                "New Value 2"
            )
            (command "_.attsync" "_N" blk)
        )
    )
    (princ)
)
(vl-load-com) (princ)

 

Thanks.

53 REPLIES 53
Message 21 of 54
Ranjit_Singh2
in reply to: Kyle.para

I had explained that earlier as well. See post 11. 

Message 22 of 54
Kyle.para
in reply to: Ranjit_Singh2

Ranjit, I noticed that also but I have no clue how to mix it with this code.

 

         (vla-addattribute def
             (getvar 'textsize)
             acAttributeModeInvisible
             "New Attribute 1"
             (vlax-3D-point 72 84)
             "NEW_TAG1"
             "New Value 1"
          )

 

Thanks,

Message 23 of 54
john.uhden
in reply to: Kyle.para

As to the text height, reread Post #11 by @Ranjit_Singh2, who deserves all the credit.  The only time I created an ATTDEF as part of a block definition was in 2003 using (entmake).

John F. Uhden

Message 24 of 54
Kyle.para
in reply to: john.uhden

You guys must like watching a fish tread water. Lol.

 

Sorry this stuff is not as simple as it seems to me.

 

Here's the finalized code.  Thanks for your help.

 

(defun c:addattribs ( / ss i blk blks def )
    (and
       (setq ss (ssget '((0 . "INSERT"))))
       (setq i (sslength ss))
       (while (> i 0)
          (setq blk (cdr (assoc 2 (entget (ssname ss (setq i (1- i)))))))
          (if (not (vl-position blk blks))(setq blks (cons blk blks)))
       )
    )
    (foreach blk blks
         (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
         (vla-addattribute def
             8 acAttributeModeInvisible
             "New Attribute 1"
             (vlax-3D-point 72 84)
             "NEW_TAG1"
             "New Value 1"
          )
         (vla-addattribute def
             8 acAttributeModeInvisible
             "New Attribute 2"
             (vlax-3D-point 72 72 (- (* 0 (getvar 'textsize))))
             "NEW_TAG2"
             "New Value 2"
         )
         (command "_.attsync" "_N" blk)
     )
    (princ)
)
(vl-load-com) (princ)

Message 25 of 54
john.uhden
in reply to: Kyle.para

Um, what's with

 

(- (* 0 (getvar 'textsize)))

?

we hafta start getting this done before the tide starts running out.

John F. Uhden

Message 26 of 54
Kyle_Para
in reply to: john.uhden

No kidding @john.uhden

 

I have a new problem now.  How do I enter in the prompt in the code?

 

I tried to do it like:

 

             (vlax-3D-point 72 84)
             "ITEM\U+0020$"
         "ENTER"
         "$"
       )

 

Thanks, Kyle

Message 27 of 54
john.uhden
in reply to: Kyle_Para

For which items do you want to prompt?

John F. Uhden

Message 28 of 54
Kyle.para
in reply to: Kyle.para

I just want it to say "enter item #" on the prompt when you double click the block after the attribute is inserted.

Right now the prompt is blank.
Message 29 of 54
john.uhden
in reply to: Kyle.para

The third item supplied to the addattribute method is the prompt.  I think in your example it was something like "New Attribute 1."  Did you use an empty string "" instead ?  Or is attsynch not doing its job?  Or might you need a regen before the babies are finished being born?  My AutoCAD 2002 precedes attsynch, so I can't test, but I would think it should include a vla-update on the inserts.

John F. Uhden

Message 30 of 54
Kyle.para
in reply to: john.uhden

Yeah you're right John.

 

I figured out the problem I was trying to insert into a dynamic block and it wasn't in the original position.

 

As soon as I reset the block everything was working fine.

 

I spoke to soon and now have one last issue.

Do you know how to change the justification of the attribute?

I would like one of them to be in the center position.

 

Thanks, Kyle

Message 31 of 54
john.uhden
in reply to: Kyle.para

Treating the attribute as a vla-object, you can...

 

(vlax-put object 'Alignment 4) ;; for middle justification

 

For center justification, use 1.

John F. Uhden

Message 32 of 54
Kyle.para
in reply to: john.uhden

I know this is probably something simple, but I am getting an "error object nil" message when I add it to the code.


I'm thinking object should be something related to my code, but I don't know.  I wish I understood this stuff better.

Message 33 of 54
john.uhden
in reply to: Kyle.para

It's only simple once you know everything like @ВeekeeCZ or @Kent1Cooper.

Most AutoLisp functions return a value.

 

F'rinstance (+ 1 2) returns 3

so if you (setq n (+ 1 2)) then you are setting the symbol n to the return value of 3.

 

In your case, (vla-addattribute def ...) will return a vla-object ID (um, if it is successful).

So, if you were to (setq Object (vla-addatribute ...)) then the symbol Object would be bound to the attribute's vla-object ID.

That allows you to change certain properties of the Object.

One way to learn of any object's properties is to use the function (vlax-dump-object) which returns a listing of all the object's properties, some of which may be classified as RO, which stands for Read Only, and cannot be changed via AutoLisp.

 

To give you my previous answer, what I did was to double-click on an attribute and read its justification in the dialog box.

Then I used (setq att (car (nentsel))) to obtain the entity name of the attribute.  (nentsel being for nested entities)

Then I got the attribute's object name via (setq Object (vlax-ename->vla-object Att))

And then read its properties using (vlax-dump-object Object) where I noticed the Alignment property.

So I double-clicked again, changed the justification, and dumped the Object again, noting the changed alignment property value.

 

Most of the time we don't really remember all the properties and possible values, so we use the above technique to learn of them.

You must learn to do the same, or become a pest.  I trust that you will do the former, not the latter, though asking challenging questions is not being a pest because we all get to learn.

 

Read again and again and test for yourself.  I got so sick of typing "vlax-dump-object" that my acaddoc.lsp has a line...

(setq dump vlax-dump-object)

so that all I have to type is (dump Object).

John F. Uhden

Message 34 of 54
Kyle.para
in reply to: john.uhden

John I don't mind trying that, but I honestly don't even know where to begin.

 

How do I setup that in a program to check the properties?

 

I tried editing that command a million times this morning and I am getting no where.

 

By "Object" in my code are we talking about "def"?

Message 35 of 54
john.uhden
in reply to: Kyle.para

Please post your latest code and I will fix it for you.  You want the attribute center justified, right?

Is there more than one attribute you want centered, or just one?  Please tell me their tag names.

John F. Uhden

Message 36 of 54
Kyle.para
in reply to: john.uhden

I'm sorry John, but I truly have tried. I just need it middle center justified because I need to place it in a circle.

It would be: Tag = "ITEM\U+0020$", Prompt = "ENTER ITEM #", Value = "$"

 

Thank you very much!

 

(defun c:add$ ( / ss i blk blks def )
    (and
       (setq ss (ssget '((0 . "INSERT"))))
       (setq i (sslength ss))
       (while (> i 0)
          (setq blk (cdr (assoc 2 (entget (ssname ss (setq i (1- i)))))))
          (if (not (vl-position blk blks))(setq blks (cons blk blks)))
       )
    )
    (foreach blk blks
         (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
         (vla-addattribute def
         8 acattributemodelockposition
         "ENTER ITEM #"
         (vlax-3D-point 72 84)
         (vlax-put def acAlignmentmiddle)
         "ITEM\U+0020$"
         "$"
         )
         (command "_.attsync" "_N" blk)
     )
    (princ)
)
(vl-load-com) (princ)

Message 37 of 54
john.uhden
in reply to: Kyle.para

I wasn't able to test thoroughly because I have only ACAD 2002 without "ATTSYNC" or the lock option, but I think you will find it works...

 

(defun c:add$ ( / ss i blk blks def AttObj)
    (and
       (setq ss (ssget '((0 . "INSERT"))))
       (setq i (sslength ss))
       (while (> i 0)
          (setq blk (cdr (assoc 2 (entget (ssname ss (setq i (1- i)))))))
          (if (not (vl-position blk blks))(setq blks (cons blk blks)))
       )
    )
    (foreach blk blks
         (setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
         (setq AttObj
            (vla-addattribute def
              8
              acattributemodelockposition
              "Enter Item #"
              (vlax-3D-point 72 84)
              "ITEM\U+0020$"
              "$"
            )
         )
         (vlax-put AttObj 'Alignment acAlignmentmiddle) ;; 4
         (command "_.attsync" "_N" blk)
     )
    (princ)
)
(vl-load-com) (princ)

Please let me know if/how it works; accepting the response as a solution is good too.

John F. Uhden

Message 38 of 54
Kyle.para
in reply to: john.uhden

Thanks a lot John!

Message 39 of 54
Dany.jee
in reply to: john.uhden

Hello John, I am very interested by this lisp,

I have the same needs a big library and the same 15 attributes to add on every blocks.

I tried to change the code but it doesn't run.

The dirrefence is that I need to add attributes and the code (vlax-3D-point 72 84) doesn't change nothing of position of the attributes .

Moreover the  acattributemodeinvisible doesnt work too. ( I use autocad 2016, maybe it's the problem?)

Could you help me with plz  

 

(defun c:add$ ( / ss i blk blks def AttObj)
(and
(setq ss (ssget '((0 . "INSERT"))))
(setq i (sslength ss))
(while (> i 0)
(setq blk (cdr (assoc 2 (entget (ssname ss (setq i (1- i)))))))
(if (not (vl-position blk blks))(setq blks (cons blk blks)))
)
)
(foreach blk blks
(setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))
(setq AttObj
(vla-addattribute def
3
acattributemodeposition
"New Attribute 1"
(vlax-3D-point 72 84)
"NEW_TAG1"
"New Value 1"
)
)
(setq AttObj
(vla-addattribute def
3
acattributemodeposition
"New Attribute 2"
(vlax-3D-point 72 -168)
"NEW_TAG2"
"New Value 2"
)
)
(setq AttObj
(vla-addattribute def
3
acattributemodeinvisible
"New Attribute 3"
(vlax-3D-point 72 -252)
"NEW_TAG3"
"New Value 3"
)
)
(vlax-put AttObj 'Alignment acAlignmentmiddle) ;; 4
(command "_.attsync" "_N" blk)
)
(princ)
)
(vl-load-com) (princ)

 

Message 40 of 54
john.uhden
in reply to: Dany.jee

I am truly sorry, but I don't remember any of that code being mine.

Perhaps I added or revised something trying to assist, but I really am not familiar with the entire code.

Please look back to see from whom it came.

John F. Uhden

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost