Jump to content

Lisp for attribute extract from block


sadhu

Recommended Posts

I got most of the code below from here

What I am trying to do is read the attribute value and tag from a block (the block has multiple attributes). When I (princ) it, it returns nil. What am I doing wrong ?

 

thanks

 

(defun c:sk (/)
(if (setq ent(entsel "\n Select a Block: "))  ;- Let the user select a block
(progn                                                   
 (setq en(car ent))                                                        ;- Get the entity name of the block
 (setq enlist(entget en))                                          ;- Get the DXF group codes
 (setq blkType(cdr(assoc 0 enlist)))                 ;- Save the type of entity
 (if (= blkType "INSERT")                                          ;- If the entity type is an Insert entity
  (progn
   (if(= (cdr(assoc 66 enlist)) 1)    ;- See if the attribute flag equals one (if so, attributes follow)
   (progn
     (setq en2(entnext en))                                    ;- Get the next sub-entity
     (setq enlist2(entget en2))                           ;- Get the DXF group codes
     (while (/= (cdr(assoc 0 enlist2)) "SEQEND")  ;- Start the while loop and keep   
       (princ "\n ")                                                 ;-Print a new line
       (princ enlist2)                                               ;- Print the attribute DXF group codes
       (setq en2(entnext en2))                             ;- Get the next sub-entity
       (setq enlist2(entget en2))                      ;- Get the DXF group codes
     )
    )
   )  ;- Close the if group code 66 = 1 statement
  )
 )   ;- Close the if block type = "ATTRIB" statement
)
)   ;- Close the if an Entity is selected statement
 [color=Red](setq att_value (cdr (assoc 1 enlist2)))
 (princ "\nAtt_value  :  ")
 (princ att_value)
 (setq att_tag (cdr (assoc 2 enlist2)))
 (princ "\nAtt_tag  :  ")
 (princ att_tag)
[/color] 
)

Link to comment
Share on other sites

  • Replies 34
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    7

  • CheSyn

    7

  • sadhu

    6

  • dpolsky

    6

Perhaps:

 

(defun c:sk (/ dxf ent)

 (defun dxf (code ent) (cdr (assoc code (entget ent))))

 (if (and (setq ent (car (entsel "\nSelect an Attributed Block: ")))
          (eq "INSERT" (dxf 0 ent))
          (= 1 (dxf 66 ent)))

   (while (not (eq "SEQEND" (dxf 0 (setq ent (entnext ent)))))
     (princ (strcat "\n\nAtt_Tag:" (dxf 2 ent) "\nAtt_Value: " (dxf 1 ent)))))

 (princ))    

Link to comment
Share on other sites

What a strange thing to list. This information can be seen by double-clicking on the attributed block and looking at that editor.

Link to comment
Share on other sites

What a strange thing to list. This information can be seen by double-clicking on the attributed block and looking at that editor.

 

I think it's more of a learning exercise :thumbsup:

Link to comment
Share on other sites

I just read the posts. Thanks Lee for your code. It is what I wanted to do. Now I have to study all of your code because what I have to do to is even more complex. All the posts I have made serve for the same project.

 

Anyway I have started.

 

Now I need to transfer these att_value to another block (the block isn't on the drawing yet). So I have to call this block "RH" change its att_value (replace them with the ones just read and place it on the drawing.

 

So you see, it is learning and exercise though it may seem odd. Maybe what I ask is so simple to you guys that it seems odd.

 

Both of you are of great help. :)

Link to comment
Share on other sites

Can you add another feature to your code that can read and extract sub-entity ( I mean blocks inside blocks - one level) tags, values and prints them.

 

sub-entity-1 name : xxx

sub-entity-1 tag: xxx

sub-entity-1 value : xxx

 

....

(defun c:sk (/ dxf ent)

 (defun dxf (code ent) (cdr (assoc code (entget ent))))

 (if (and (setq ent (car (entsel "\nSelect an Attributed Block: ")))
          (eq "INSERT" (dxf 0 ent))
          (= 1 (dxf 66 ent)))

   (while (not (eq "SEQEND" (dxf 0 (setq ent (entnext ent)))))
     (princ (strcat "\n\nAtt_Tag:" (dxf 2 ent) "\nAtt_Value: " (dxf 1 ent)))))

 (princ))

Thanks

Link to comment
Share on other sites

You can definately change the values of one block but pick another block to get the values. You just need to know the name of each block your working with.

 

We do something like your asking pick a block on the layout (model space) it then updates another block with information from the model the other block can be anywhere in the total Autocad dwg usually in a layout tab.

 

It reads the displayed attributes as a common ID link

Link to comment
Share on other sites

Thank you for the link.

I managed to work it out by inserting this :

 

 

(if  (and
    (= (cdr (assoc 0 data)) "ATTRIB")
    (= (cdr (assoc 2 data)) "NO") ; just a tag
    )

 (setq att_value (cons (dxf 1) att_value))

:D

Link to comment
Share on other sites

It was more of a reference for you to see the structure of different blocks, so that it is easier for you to see how to modify them, but each to their own I suppose.

Link to comment
Share on other sites

  • 1 year later...

Hey Sadhu,

 

I am facing the exact same sort of problem and I was curious how yours went? I would love to check out your code if possible. Feel free to PM me, for some reason it wouldn't let me send you a message.

 

Thanks,

 

Ben

Link to comment
Share on other sites

  • 7 months later...

Greetings to all, I need a little help I RELATED TO A LISP EXTRACTION attributes in dynamic blocks meet a certain geometry TABLE (EXAMPLE 190X500) and does not change when I try to update the table.

Link to comment
Share on other sites

  • 1 year later...
Perhaps:

 

(defun c:sk (/ dxf ent)

 (defun dxf (code ent) (cdr (assoc code (entget ent))))

 (if (and (setq ent (car (entsel "\nSelect an Attributed Block: ")))
          (eq "INSERT" (dxf 0 ent))
          (= 1 (dxf 66 ent)))

   (while (not (eq "SEQEND" (dxf 0 (setq ent (entnext ent)))))
     (princ (strcat "\n\nAtt_Tag:" (dxf 2 ent) "\nAtt_Value: " (dxf 1 ent)))))

 (princ))    

 

 

I know this is a 4 year old thread, but I was doing some Googling and this is almost what I need.

 

We keep text data (program numbers, sheet call outs, etc) as blocks. And we use the attributes to populate them. We are trying to change how we are doing this, either by making a different/cleaner blocks or by changing to fields.

 

I was hoping to write a LISP routine that would read the block and extract the fields, but instead of displaying them like this routine does, saving them for me to do something else with. I'm not great at LISP, I'm having trouble figuring out if you are storing the values anywhere, or if it reads it and it's gone?

 

Thanks,

Daniel

Link to comment
Share on other sites

Tags:

DDate

Part

Sheetsize

Prgm

Name

 

Not sure what I'm going to do with them yet, I hadn't gotten that far, was just trying to figure out how to read them at this point. If I could grab the values of the Tags and then !Name and return the value of the Tag "Name" I would be in good shape to figure things out.

 

Thanks,

Daniel

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