Jump to content

Is there a simple way to extract Attribute Value from block for use as variable in LISP?


Recommended Posts

Posted

Hello,

I'm here with my hat in my hand hoping people won't be offended by a basic question but I've found many posts about doing this online and many of the examples use VLA and I'm not at all familiar with it and while I've programmed a lot of custom LISP routines in the past (over 15 years ago) I basically have to relearn it because of the time lapse. 

 

All I want to do is pick a single block which has two attribute tags PIPE_DIAM and PIPE_LENGTH and store the values into variables for use elsewhere. 

 

I'm not sure where to go next after getting the entity name.

 

 

Posted

Here is a AutoLISP vanilla example for you to start with and the text strings would be in a list and should be printed to command line if tag strings matched.

(defun c:Test (/ sel ent get lst )
  (and (princ "\nSelect attributed block to replace retrieve values : ")
       (or (setq sel (ssget "_+.:S" '((0 . "INSERT") (66 . 1))))
           (alert "Invalid object or nothing selected! Try again.")
           )
       (setq ent (ssname sel 0))
         (while (not (eq (cdr (assoc 0 (setq get (entget (setq ent (entnext ent)))))) "SEQEND"))
           (and (wcmatch (strcase (cdr (assoc 2 get))) "PIPE_DIAM,PIPE_LENGTH")
                (setq lst (cons (cdr (assoc 1 get)) lst))
                )
           )
       (princ lst)
       )
  (princ) 
)

 

  • Like 2
Posted
1 hour ago, Steven P said:

Thanks, I've come across those in my searches and some of Lee's postings and he's a very helpful guy but for some reason I only had varied success trying to use those. I know it's just me though.

  • Like 1
Posted
50 minutes ago, Tharwat said:

Here is a AutoLISP vanilla example for you to start with and the text strings would be in a list and should be printed to command line if tag strings matched.

(defun c:Test (/ sel ent get lst )
  (and (princ "\nSelect attributed block to replace retrieve values : ")
       (or (setq sel (ssget "_+.:S" '((0 . "INSERT") (66 . 1))))
           (alert "Invalid object or nothing selected! Try again.")
           )
       (setq ent (ssname sel 0))
         (while (not (eq (cdr (assoc 0 (setq get (entget (setq ent (entnext ent)))))) "SEQEND"))
           (and (wcmatch (strcase (cdr (assoc 2 get))) "PIPE_DIAM,PIPE_LENGTH")
                (setq lst (cons (cdr (assoc 1 get)) lst))
                )
           )
       (princ lst)
       )
  (princ) 
)

 

Thank you very much! That gets me where I need to be and should be able to get me started back up in my routine! I'll have some reading to do in order to make sense of some of it but I really do appreciate it!

 

Posted

Excellent, you are most welcome

Feel free to ask if you stuck with any part of the codes.

  • Like 1
Posted

A alternative example is retrieve all the attributes and just get the textstring for an attribute based on its creation order number, note its number -1 as nth starts at 0.

 

(setq obj (vlax-ename->vla-object (car  (entsel "Pick obj"))))
(setq atts (vlax-invoke obj 'Getattributes))
(setq val1 (vla-get-textstring (nth 0 atts)))
(setq val2 (vla-get-textstring (nth 1 atts)))

so I have a block with 20 attributes and just want att 3 & 7 

(setq val1 (vla-get-textstring (nth 2 atts)))
(setq val2 (vla-get-textstring (nth 6 atts)))

 

This is a more global approach as don't need to know Tag names.

 

Also note block appearance of attributes does not reflect the creation order. Just double click the block, will show attribute order.

Posted

Shout out to @ronjonp for showing me getpropertyvaule

 

(setq ent (car (entsel "Pick obj"))))
(setq pdia (getpropertyvalue ent "PIPE_DIAM"))
(setq plen (getpropertyvalue ent "PIPE_LENGTH"))

 

and if you want to update another block with attributes use setpropertyvalue

  • Thanks 1
Posted
10 hours ago, mhupp said:

Shout out to @ronjonp for showing me getpropertyvaule

 

(setq ent (car (entsel "Pick obj"))))
(setq pdia (getpropertyvalue ent "PIPE_DIAM"))
(setq plen (getpropertyvalue ent "PIPE_LENGTH"))

 

and if you want to update another block with attributes use setpropertyvalue

Everyone has been very generous and I'm very thankful for that. You all are a great group of people! But @mhupp, you get the trophy! Thank you!!!

  • Like 1
Posted
1 hour ago, EntDraught said:

Everyone has been very generous and I'm very thankful for that. You all are a great group of people! But @mhupp, you get the trophy! Thank you!!!

You can always (princ) so it shows the value in the command line so you know it stored the correct value. 

Posted

Get & Setproperty are not supported in Bricscad V20 not sure about latest version. So for me use what I posted not everyone has Autocad. Bricscad, ZWcad, Intellicad, Drafsight to mention a few no idea which support Get & Set.

  • Like 1
Posted

You will also find that you can read the bodies of the ATTOUT and ATTIN commands.  When I was first learning to extract attributes, I went through those documents with 3 highlighters and lots of notes.

 

If I remember correctly, the 'assoc' command determines the format of dotted pairs which are extracted.  The '#' switch receives a value of 1-4, which you select.  The output of each of those 4 items determines the format of the value you extract.  If I'm remembering correctly, then you might try playing around with that switch to learn more.  ("The ABC's of AutoLIPS" page 206).

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