Assign Attribute Value to a Variable

Assign Attribute Value to a Variable

Anonymous
Not applicable
1,669 Views
7 Replies
Message 1 of 8

Assign Attribute Value to a Variable

Anonymous
Not applicable

Hi,

 

I have a block name "TB2", in which I am trying to extract an attribute of tag "REV". I want to assign REV to a variable using setq which I can use throughout the rest of my program.

 

How can I go about this? I've seen Lee Mac's approach, however being new to LISP I am unsure of implementation / execution.

 

http://www.lee-mac.com/attributefunctions.html#algetattributevalueit

0 Likes
Accepted solutions (1)
1,670 Views
7 Replies
Replies (7)
Message 2 of 8

SeeMSixty7
Advisor
Advisor

If you can select the attribute directly this will work.

 

(setq myvar (cdr (assoc 1 (entget (car (nentsel "\nSelect Attribute: "))))))

 

see if that works well enough for you.

 

0 Likes
Message 3 of 8

Anonymous
Not applicable

Hi,

 

Thank you for your input. I'm implementing this into a more complex script where I will need to be able to use the variable throughout.

 

I'm looking for a way to automatically select the block by name, and pull the data from the tag by name

0 Likes
Message 4 of 8

SeeMSixty7
Advisor
Advisor
Accepted solution

This should work for you, but understand that if you have more than one of the block you are looking for it will only find the first occurrence of it and use its value.

 

;Here is the function to find the block insert and get the value
(defun getblockatt (blockname atttag)
  (setq gba-ss (ssget "X" (list (cons 0 "INSERT") (cons 2 blockname))))
  (if gba-ss
    (progn
       (setq gba-blockent (ssname gba-ss 0)
             gba-blockdata (entget gba-blockent)
             gba-sent gba-blockent
       )
       (if (dxf 66 gba-blockdata)
  		(progn
  		 (while (and (not (equal (dxf 0 gba-blockdata) "SEQEND"))
  		             (not (equal (dxf 2 gba-blockdata) atttag))
  		        )
  		   (setq gba-sent (entnext gba-sent)
  		         gba-blockdata(entget gba-sent)
  		   )
  		 )
  		 (if (= (dxf 0 gba-blockdata) "SEQEND")
  		   (setq gba-sent nil)
  		 )
  	    )
  	   )
  	 )
  	)
  	(if gba-sent
  	   (setq gba-retval (cdr (assoc 1 gba-blockdata)))
  	   (setq gba-retval nil)
  	)
  	gba-retval
)
;How to call the function and assign to a variable
(setq myvalue (getblockatt "TB2" "REV"))

Good luck,

 

0 Likes
Message 5 of 8

dlanorh
Advisor
Advisor

To select the Block by name use this filter in an ssget.

 

'((0 . "INSERT") (2 . "TB2"))

 

To emulate an entsel selection

Then pass the block entity to one of the LM: functions

 

(defun LM:getattributevalue ( blk tag / enx )
    (if (and (setq blk (entnext blk)) (= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk))))))
        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
            (cdr (assoc 1 (reverse enx)))
            (LM:getattributevalue blk tag)
        )
    )
)
(setq ss (ssget "_+.:E:S" '((0 . "INSERT") (2 . "TB2")))) (if ss (setq val (LM:getattributevalue (ssname ss 0) "REV")))

The (ssname ss 0) returns the first entity selected, and since this emulates an entsel there is only one object selected. The if statement to extract the value will only happen if something is selected

 

 

 

I am not one of the robots you're looking for

0 Likes
Message 6 of 8

Anonymous
Not applicable

SeemSixty,

 

This looks very promising. When I execute it, I get the following in command line:

 

Command: ; error: no function definition: DXF

Note that this is a DWG, however changing this to DWG doesn't appear (and realistically wouldn't) to solve the error

0 Likes
Message 7 of 8

SeeMSixty7
Advisor
Advisor

DOH! I forgot to include it

 

Here is that defun

(defun dxf(code elist)
    (cdr (assoc code elist))
)
0 Likes
Message 8 of 8

dbhunia
Advisor
Advisor

check this way.......

 

(defun LM:getattributevalue ( blk tag / enx )
    (if (and (setq blk (entnext blk)) (= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk))))))
        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
            (cdr (assoc 1 (reverse enx)))
            (LM:getattributevalue blk tag)
        )
    )
)
(setq ss (ssget "_A" '((0 . "INSERT") (2 . "TB2"))))
(if ss 
	(repeat (setq n (sslength ss)) ;;;go thtough each block
		(setq Att_val (LM:getattributevalue (ssname ss (setq n (1- n))) "REV"));;;extract each att value
		(if Att_val			;;; If Attribute has a value
			(print Att_val) ;;; for example just printing
			;;;;;;Do your stuff ;;;;;;;
		)
	)
)

Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes