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

Get the value of an attribute.

6 REPLIES 6
Reply
Message 1 of 7
AaronLucy
894 Views, 6 Replies

Get the value of an attribute.

Is there a quick way of getting the value of an attribute?

 

I have an attributed block called "tp_attributes" and it has and attribute called "OrderNum".

 

I just want to be able to quickly grab that value.

 

Thanks

6 REPLIES 6
Message 2 of 7
gjrcmb
in reply to: AaronLucy

Could do something like the following, if you just want a generic solution.  Or you could modify to have an if statement, so that when it finds the attribute "OrderNum" it prints or stores its value in a variable.

 

(defun c:getattval(/ enam atttag attval)
    (setq enam (car (entsel "Select Attributed Block: ")))
    (while (setq enam (entnext enam))
        (if (= (cdr (assoc 0 (entget enam))) "ATTRIB")
            (progn
                (setq atttag (cdr (assoc 2 (entget enam))))
                (setq attval (cdr (assoc 1 (entget enam))))
                (princ (strcat "\nValue of Attribute \"" atttag "\": "))
                (princ attval)
            )
        )
    )
    (princ)
)

Message 3 of 7
pbejse
in reply to: AaronLucy


@AaronLucy wrote:

Is there a quick way of getting the value of an attribute?

 

I have an attributed block called "tp_attributes" and it has and attribute called "OrderNum".

 

I just want to be able to quickly grab that value.

 

Thanks


Another

 

(defun ShowVal (blkn Tag / aDoc ss tvl)
  (vl-load-com)
  (setq aDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (cond
    ((and
       (ssget "_X" (list '(0 . "INSERT") '(66 . 1) (cons 2 (strcat blkn ",`*U*"))))
       (vlax-for bls (setq ss (vla-get-ActiveSelectionSet aDoc))
         (if (and ( eq (strcase (vla-get-effectivename bls))
                  	(strcase blkn))
	           	(setq tvl (assoc (strcase Tag) (mapcar
	                    '(lambda (j)
	                       (list
	                         (vla-get-tagstring j)
	                         (vla-get-textstring j)))
	                    (vlax-invoke bls 'GetAttributes))))
               )
           (print (cadr tvl))
           )
         )
       (vla-delete ss)
       )
     )
    )
  (princ)
  )

 

 (SHOWVAL  "tp_attributes" "OrderNum")

 

Notice its ShowVAl and not GrabVal... not sure what you mean by "grab" thoughSmiley LOL

 

HTH

 

 

Message 4 of 7
gjrcmb
in reply to: gjrcmb

Actually discovered that what I posted previously will list the attributes of all blocks including the one selected and others added to the drawing after the one selected.  So the following lists the attribute of the selected block only.  Just thought I would correct my mistake.

 

(defun c:getattval(/ enam atttag attval elst)
    (setq enam (car (entsel "Select Attributed Block: ")))
    (setq elst (entget enam))
    (while (and (setq enam (entnext enam)) (/= (cdr (assoc 0 elst)) "SEQEND"))
        (setq elst (entget enam))
        (if (= (cdr (assoc 0 elst)) "ATTRIB")
            (progn
                (setq atttag (cdr (assoc 2 elst)))
                (setq attval (cdr (assoc 1 elst)))
                (princ (strcat "\nValue of Attribute \"" atttag "\": "))
                (princ attval)
            )
        )
    )
    (princ)
)

Message 5 of 7
joqarmaza
in reply to: pbejse

Hi pbejse,

 

I know this thread is old but I wanted to say thank you, I used this code and was able to get an attribute value from the "SCALE" one question I do have.  In your code you pass both the name of the block (first parameter) and the name of the tag / attribute name (in my case SCALE).  So I called it like so:

 

(defun ShowVal (blkn Tag / aDoc ss tvl)
(vl-load-com)
(setq aDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
(cond
((and
(ssget "_X" (list '(0 . "INSERT") '(66 . 1) (cons 2 (strcat blkn ",`*U*"))))
(vlax-for bls (setq ss (vla-get-ActiveSelectionSet aDoc))
(if (and ( eq (strcase (vla-get-effectivename bls))
(strcase blkn))
(setq tvl (assoc (strcase Tag) (mapcar
'(lambda (j)
(list
(vla-get-tagstring j)
(vla-get-textstring j)))
(vlax-invoke bls 'GetAttributes))))
)
(print (cadr tvl))
)
)
(vla-delete ss)
)
)
)
(princ)
)

 

(SHOWVAL "Block1_A0" "Scale")

 

My question is can you or anyone for that matter modify this code to work on ANY block, meaning modify the function call to ignore the block and just look for the tag name scale, like so:

 

(SHOWVAL "Scale")

 

So it should maybe ignore the block name (I dont know enough autocad so maybe ignore means loop through all blocks).  I simply need to get the attribute value irregardless of the block name.

 

Please and thanks!

 

Message 6 of 7
pbejse
in reply to: joqarmaza


@joqarmaza wrote:

 

My question is can you or anyone for that matter modify this code to work on ANY block, meaning modify the function call to ignore the block and just look for the tag name scale, like so:

 


Nothing to it joqarmaza

 

Here's a quick mod oif the original code;

 

(defun ShowVal (blkn Tag  / aDoc ss tvl)
  (vl-load-com)
  (setq aDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (cond
    ((and
       (ssget "_X"
	      (list '(0 . "INSERT")
		    '(66 . 1)
		    (cons 2 (strcat blkn ",`*U*"))
	      )
       )
       (vlax-for bls (setq ss (vla-get-ActiveSelectionSet aDoc))
	 	 (if (and
		  (wcmatch (strcase (vla-get-effectivename bls)) (strcase blkn))
		  (setq	tvl (assoc (strcase Tag)
				   (mapcar
				     '(lambda (j)
					(list
					  (vla-get-tagstring j)
					  (vla-get-textstring j)
					)
				      )
				     (vlax-invoke bls 'GetAttributes)
				   )
			    )
		  )
	     )
	   (print (cadr tvl))
	 )
       )
       (vla-delete ss)
     )
    )
  )
  (princ)
)

 

(SHOWVAL "Block1_A0" "Scale");< Specific Blcok name

 

(SHOWVAL "*" "Scale");<---- All blocks use "*" 

 

HTH

 

EDIT: silly me... 🙂

Message 7 of 7
pbejse
in reply to: pbejse

Ooops , silly of me, did not realize the OP created a new thread for this describing the intention. Anyhoo. just holler when you hit a snag 

 

pBe

 

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

Post to forums  

Autodesk Design & Make Report

”Boost