Hello guys,
I am trying to find a lisp that would get an attribute value (tag name = NBR_5) . I'm trying to run a simple routine that would let the user place the value from the titleblock attribute as text on a drawing. I see a ton of articles regarding getting attributes but I am not well versed in programming.
Any help is greatly appreciated.
Thanks in advance.
Solved! Go to Solution.
Solved by alanjt_. Go to Solution.
Solved by BlackBox_. Go to Solution.
(defun _findMatchingAttributeValue (block tagstring) (vl-some (function (lambda (att) (if (eq (vla-get-tagstring att) (strcase tagstring)) (vla-get-textstring att) ) ) ) (vlax-invoke block 'GetAttributes) ) ) (_findMatchingAttributeValue <vla-object> <tagstring>)
Thanks Alan,
I am not familiar with Vlisp. I received this error "; error: bad argument type: VLA-OBJECT nil". What am I doing wrong? I know your answer is going to be obvious, isn't it?
thanks again.
The last line of Alan's code is an example, where you would replace <vla-object>, and <tagstring> with actual values.
Consider this pseduo-code:
(vl-load-com) (defun c:FOO (/ _findMatchingAttributeValue ss value) (defun _findMatchingAttributeValue (block tagstring) (vl-some (function (lambda (att) (if (eq (vla-get-tagstring att) (strcase tagstring)) (vla-get-textstring att) ) ) ) (vlax-invoke block 'GetAttributes) ) ) (if (and (setq ss (ssget ":S:E" '((0 . "INSERT") (66 . 1)))) (setq value (_findMatchingAttributeValue (vlax-ename->vla-object (ssname ss 0)) ;<-- <vla-object> "NBR_5" ;<-- <tagstring> ) ) ) (prompt (strcat "\n[Attribute Value] : " value)) (prompt "\n** Invalid selection ** ") ) (princ) )
"How we think determines what we do, and what we do determines what we get."
It hardly worth a program. Use ddatte. Pick the titleblock. Highlight the text. CTRL+C. Then, CTRL+V into the drawing.
BlackBox answered your question about converting to a vla-object. However, just for completeness, here's a non VLISP version:
(defun _findMatchingAttributeValue (block tagstring / data string) (while (and (not (eq (cdr (assoc 0 (setq data (entget (setq block (entnext block)))))) "SEQEND")) (not string) ) (if (and (eq (cdr (assoc 0 data)) "ATTRIB") (eq (cdr (assoc 2 data)) (strcase tagstring))) (setq string (cdr (assoc 1 data))) ) ) string )
FWIW -
I think you'll find both of these adaptations to be *slightly* faster:
(defun _setq (block tagstring) (setq tagstring (strcase tagstring)) (vl-some (function (lambda (att) (if (eq (vla-get-tagstring att) tagstring) (vla-get-textstring att) ) ) ) (vlax-invoke block 'GetAttributes) ) ) (defun _cond (block tagstring) (vl-some (function (lambda (att) (if (eq (vla-get-tagstring att) (cond (tagstring) ((setq tagstring (strcase tagstring))) ) ) (vla-get-textstring att) ) ) ) (vlax-invoke block 'GetAttributes) ) )
... Speed test of 1000 iterations (same block, same tag😞
_FINDMATCHINGATTRIBUTEVALUE Elapsed: 312 Average: 0.3120 _SETQ Elapsed: 281 Average: 0.2810 _COND Elapsed: 280 Average: 0.2800
Minor advantage; not worth recoding multiple functions, etc., IMO. Just thought I'd mention it... Again, only FWIW.
Cheers
"How we think determines what we do, and what we do determines what we get."
Thanks BlackBox,
How would I revise the code to skip the block selection as it is always the same block "TitleBlockMfg" and have that attribute value placed as text ?
This is actually part of a macro that I use when I cycle through many drawings tagging specific objects with the "NBR" tag value loacate in the titleblock.
It would make life so much easier here.
Thanks for all of the help everyone !
BTW, your _cond won't work, since it only checks if 'tagstring is an assigned variable, which it is. You need to setq a different variable for _cond.
eg.
(defun _cond (block tagstring / upcase) (vl-some (function (lambda (att) (if (eq (vla-get-tagstring att) (cond (upcase) ((setq upcase (strcase tagstring))) ) ) (vla-get-textstring att) ) ) ) (vlax-invoke block 'GetAttributes) ) )
However, I'd just stick with upcasing the string before running anything, IMO.
Yes, of course... What a silly mistake (not changing the name)... Keen eye, as always.
Cheers
"How we think determines what we do, and what we do determines what we get."
@Anonymous wrote:Thanks BlackBox,
How would I revise the code to skip the block selection as it is always the same block "TitleBlockMfg" and have that attribute value placed as text ?
This is actually part of a macro that I use when I cycle through many drawings tagging specific objects with the "NBR" tag value loacate in the titleblock.
It would make life so much easier here.
Thanks for all of the help everyone !
Alan's answered your question already, about how to revise the SSGET call... Did you get your macro working they way you wanted?
Cheers
"How we think determines what we do, and what we do determines what we get."
@BlackBox_ wrote:Yes, of course... What a silly mistake (not changing the name)... Keen eye, as always.
Cheers
You caught my coding boo boo first. 🙂
Thank Everyone. After playing with the code supplied by Blackbox, I was able to get my macro running.
I still have a lot to learn. I'm not in full time CAD like I used to be.
Xi
@Anonymous wrote:Thank Everyone. After playing with the code supplied by Blackbox, I was able to get my macro running.
That is kind of you to say; I'm happy to help. :beer:
@Anonymous wrote:I still have a lot to learn. I'm not in full time CAD like I used to be.
Don't we all... And you're not alone; I was just busted down to half-time myself. *Looking*
"How we think determines what we do, and what we do determines what we get."
Can't find what you're looking for? Ask the community or share your knowledge.