Adding block attributes with AutoLISP...but new attribute isn't going into the block definiton?

Adding block attributes with AutoLISP...but new attribute isn't going into the block definiton?

acadadmin3KNUF
Advocate Advocate
442 Views
4 Replies
Message 1 of 5

Adding block attributes with AutoLISP...but new attribute isn't going into the block definiton?

acadadmin3KNUF
Advocate
Advocate

I think I may have just shot myself in the foot, HARD.  I was using a LISP routine to add the same attribute to over 200 individual blocks...but what I TRULY need is for the attribute to go directly into the block defintion itself, NOT the inserted block reference on the the screen.  

 

I've attached the LISP code I used, as well as one of the blocks I modified with this code.  The attribute SYS_NEW_RAILS is clearly showing at the bottom of the attributes list in the Properties palette, as this is a Block Reference (which is a concept I didn't fully understand until right now)

 

Is what I truly want even possible, to use AutoLISP to add the attribute to the base block definition itself?

0 Likes
443 Views
4 Replies
Replies (4)
Message 2 of 5

Moshe-A
Mentor
Mentor

@acadadmin3KNUF  hi,

 

from what i see,  you are trying to add the attribute to the block definition 💪

 

 

(setq def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk))

 

 

Your problem may be that you are not aware that AutoCAD changes the block reference (insert) name once you activate a dynamic property. the block reference name is turning to "*Unn" same as anonymous blocks.

 

So to solve this you have to retrieve the real block reference name like this:

 

(setq blkRef (vlax-ename->vla-object (car (entsel))))

(setq RealBlockName (vla-get-effectiveName blkRef))

 

Moshe

 

 

 

0 Likes
Message 3 of 5

acadadmin3KNUF
Advocate
Advocate

THanks very much for your contribution, but it was not successful.  I added the lines of code and added the new variables in the first line of the program.

 

Now, when I start the routine and pick a block, I get two successive prompts telling me to select an object, then the error message "; error: bad argument type: VLA-OBJECT nil"

 

Updated LISP file is attached

0 Likes
Message 4 of 5

komondormrex
Mentor
Mentor

try this one

(defun c:add_new_rails_att ( / ss i blk blks def AttObj)
    (and
       (setq ss (ssget '((0 . "INSERT"))))
       (setq i (sslength ss))
       (while (> i 0)
          (setq blk (vla-get-effectivename (vlax-ename->vla-object (ssname ss (setq i (1- i)))))) ;	that one corrected
          (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
              "NEW RAILS?"
              (vlax-3D-point 72 84)
              "SYS_NEW_RAILS"
              "YES"
            )
         )
         (vlax-put AttObj 'Alignment acAlignmentmiddle) ;; 4
         (command "_.attsync" "_N" blk)
     )
    (princ)
)
(vl-load-com) (princ)
0 Likes
Message 5 of 5

ec-cad
Collaborator
Collaborator

I see in your sample drawing, you have added an Attribute definition to the Block(s), rather than specific blocks.

I hope you have a backup of those original drawings.

Updating the 'inserts' in a drawing does not update the Block Definition or Block table.

To do that, you will need a Drawing (made from Wblock), with the Blockname, (not 'Uxx), but the effective name.

Place this drawing of the 'modified' block in a support location, or some block library.

Then a simple Lisp can 'insert & redefine' the block, so it has that added attribute.

Something like this when it's in your block library:

(command ".-INSERT" "BLOCKNAME=C:\\PATH\\TO\\NEW\\BLOCKNAME.DWG" nil)

Or, more simply, when it's in a support path:

    (setvar 'Attreq 0)

    (command ".-INSERT" "BLOCKNAME=" nil)

Also, you may want to search for 'Attsync' command

Lee Mac has posted a program called 'redefall.lsp' 09-13-2013

Is under topic "Update blocks & attributes LISP"

 

0 Likes