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
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)
)
@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" though
HTH
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)
)
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!
@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... 🙂
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