bababarghi Posted May 12, 2014 Posted May 12, 2014 (edited) Folks, I know I am doing something critically wrong here but don't know how to fix it. What I need to do is to read an attribute from a specific title block and write its value to DWGPROPS. Following code will read the attribute for me: (defun Display_Tag_Value (blkname TAG_NAME) (vl-load-com) (ssget "x" (list '(0 . "INSERT") (cons 2 blkname) '(66 . 1)) ) (vlax-for item (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object)) ) (foreach ATTRIBUTE_NAME (vlax-safearray->list (vlax-variant-value (vla-getattributes item)) ) (if (= TAG_NAME (vla-get-tagstring ATTRIBUTE_NAME)) (setq str (vla-get-TextString ATTRIBUTE_NAME)) ) ) ) (PRINC str) (PRINC) ) So till now I can read "ATTRIBUTE NAME" value of "BLOCKNAME" with this line: (Display_Tag_Value "[color="red"]BLOCKNAME[/color]" "[color="red"]ATTRIBUTE NAME[/color]") Also this code, can push values to drawing properties: (defun dProps (/ dProp) (vl-load-com) (setq acadObject (vlax-get-acad-object)) (setq acadDocument (vla-get-ActiveDocument acadObject)) (setq dProp (vlax-get-Property acadDocument 'SummaryInfo)) (vlax-put-Property dProp 'Title [color="green"]"test title"[/color]) (princ) ) Hence, I was hoping to reach my goal by mixing these two together: (defun dProps (/ dProp) (vl-load-com) (setq acadObject (vlax-get-acad-object)) (setq acadDocument (vla-get-ActiveDocument acadObject)) (setq dProp (vlax-get-Property acadDocument 'SummaryInfo)) (vlax-put-Property dProp 'Title [color="red"](Display_Tag_Value "BLOCKNAME" "ATTRIBUTE NAME")[/color] ) (princ) ) But ... here is the error I get: error: lisp value has no coercion to VARIANT with this type: In brief: how can I pass a returned value of my function to 'vlax-put-Property'? P.S: Read Lee's code here, but had no idea how to use it. Edited May 12, 2014 by bababarghi Typo Quote
ymg3 Posted May 12, 2014 Posted May 12, 2014 bababarghi, I believe the problem is in Display_Tag_Value, because of the princ at the end you are returning nil, even though you printed it before exiting. If you remove it it will work. (defun dProps (/ dProp) (vl-load-com) (setq acadObject (vlax-get-acad-object)) (setq acadDocument (vla-get-ActiveDocument acadObject)) (setq dProp (vlax-get-Property acadDocument 'SummaryInfo)) (vlax-put-Property dProp 'Title (Display_Tag_Value "TEST" "TITLE")) ) (defun Display_Tag_Value (blkname TAG_NAME) (vl-load-com) (ssget "x" (list '(0 . "INSERT") (cons 2 blkname) '(66 . 1))) (vlax-for item (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object))) (foreach attribute_name (vlax-safearray->list (vlax-variant-value (vla-getattributes item))) (if (= TAG_NAME (vla-get-tagstring ATTRIBUTE_NAME)) (setq str (vla-get-TextString ATTRIBUTE_NAME)) ) ) ) ) ymg Quote
bababarghi Posted May 12, 2014 Author Posted May 12, 2014 Thanks ymg3. I was expecting some more serious logic errors in my code ! Your guess was correct and the code is working now although you deleted both PRINCs. However, (PRINC str) is still required. I suspect that was a typo in your replied code. (defun dProps (/ dProp) (vl-load-com) (setq acadObject (vlax-get-acad-object)) (setq acadDocument (vla-get-ActiveDocument acadObject)) (setq dProp (vlax-get-Property acadDocument 'SummaryInfo)) (vlax-put-Property dProp 'Title (Display_Tag_Value "TEST" "TITLE")) ) (defun Display_Tag_Value (blkname TAG_NAME) (vl-load-com) (ssget "x" (list '(0 . "INSERT") (cons 2 blkname) '(66 . 1))) (vlax-for item (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object))) (foreach attribute_name (vlax-safearray->list (vlax-variant-value (vla-getattributes item))) (if (= TAG_NAME (vla-get-tagstring ATTRIBUTE_NAME)) (setq str (vla-get-TextString ATTRIBUTE_NAME)) ) ) [color="red"] (PRINC str)[/color] ) ) Quote
ymg3 Posted May 12, 2014 Posted May 12, 2014 bababarghi, You are right, to retain the original functionality you need that "(princ str)". However when dealing with subfunction I would rather accumulate in a list and returns the list at the end. (defun Display_Tag_Value (blkname TAG_NAME l) (vl-load-com) (ssget "x" (list '(0 . "INSERT") (cons 2 blkname) '(66 . 1))) (vlax-for item (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object))) (foreach attribute_name (vlax-safearray->list (vlax-variant-value (vla-getattributes item))) (if (= TAG_NAME (vla-get-tagstring ATTRIBUTE_NAME)) (setq l (cons (vla-get-TextString ATTRIBUTE_NAME)) l) ) ) ) l ) ymg Quote
bababarghi Posted May 12, 2014 Author Posted May 12, 2014 Thanks. I just fixed some syntax errors from your code for future uses: (defun Display_Tag_Value (blkname TAG_NAME [color="red"][b]/[/b][/color] l) (vl-load-com) (ssget "x" (list '(0 . "INSERT") (cons 2 blkname) '(66 . 1))) (vlax-for item (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object))) (foreach attribute_name (vlax-safearray->list (vlax-variant-value (vla-getattributes item))) (if (= TAG_NAME (vla-get-tagstring ATTRIBUTE_NAME)) (setq l (cons (vla-get-TextString ATTRIBUTE_NAME)[b] l[/b][color="red"])[/color]) ) ) ) l ) Quote
ymg3 Posted May 12, 2014 Posted May 12, 2014 bababarghi, Sorry about that. This is the peril of writing the code in the reply window. As an aside the idea is that when you are dealing with subfunction, we should have a predictable return value. ymg Quote
Recommended Posts
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.