I've been referencing post "Replace certain attribute value script?" I want to scan a drawing for three custom blocks , if found, then transfer their "INST" value (ex. 123456) to their respective custom attribute on the titleblock(dwg1, dwg2, dwg3). So far...
(defun c:bl()
(setq gt (ssget "_X" '((2 . "HDV1_GENERIC_BLOCK_LEFT"))))
(setq ik 0)
(if (< ik (sslength gt))
(progn
(setq ent (ssname gt ik))
(setq rtrn (c:wd_getattrval ent "INST"))
(princ rtrn)
)
)
(princ)
)
I'm using Group Code 2, as you can see, but my block HDV1_GENERIC_BLOCK_LEFT is dynamic so when it changes from its inserted version the Group Code also changes. I tried adding the new Code description (*U10) to my list but still get ; error: bad argument type: lselsetp nil. Are dynamic blocks out of the question when working with entities or is their a way around this problem?
To find real block name use (vlax-get-property object 'EffectiveName).
Also, change "if" to "while" loop with ik incrementation.
Following code should work:
(defun c:bl()
(setq gt (ssget "_X" '((0 . "INSERT"))))
(setq ik 0)
(while (< ik (sslength gt))
(setq ent (ssname gt ik))
(setq object (vlax-ename->vla-object ent))
(setq block_name (vlax-get-property object 'EffectiveName))
(if (= block_name "HDV1_GENERIC_BLOCK_LEFT")
(progn
(setq rtrn (c:wd_getattrval ent "INST"))
(princ rtrn)
)
)
(setq ik (1+ ik))
)
(princ)
)
Vladimir
Added one more line (vl-load-com) just in case.
(defun c:bl()
(vl-load-com)
(setq gt (ssget "_X" '((0 . "INSERT"))))
(setq ik 0)
(while (< ik (sslength gt))
(setq ent (ssname gt ik))
(setq object (vlax-ename->vla-object ent))
(setq block_name (vlax-get-property object 'EffectiveName))
(if (= block_name "HDV1_GENERIC_BLOCK_LEFT")
(progn
(setq rtrn (c:wd_getattrval ent "INST"))
(princ rtrn)
)
)
(setq ik (1+ ik))
)
(princ)
)
Thanks for the help. Could you give a brief explanation of what those lines do? As is, the code will only work for the last block inserted. Inserted HDV1_GENERIC_BLOCK_LEFT and it put the INST value in the correct title block attribute(DWG1). However, if I insert HDV1_GENERIC_BLOCK_RIGHT it will do the same but then HDV1_GENERIC_BLOCK_LEFT won't work. I want it to update all three at the same time not based on the last block inserted.(As you can see HDV1_GENERIC_BLOCK_MIDDLE is my third block.) Since their will only be one instance of each of the three blocks do I need the While loop? Also if I run the code it updates attribute DWG1 to 111222 for example. If I change the block INST value to 333666 and run it again I get 111222333666. If it has a number already there I would like it to erase that number and update based on the current value of INST. To solve this problem I was thinking using similar code to get the value of the three attributes(dwg1, dwg2, dwg3) and compare to the block INST values and correct accordingly. What do ya think?
(defun c:bl()
(vl-load-com)
(setq gt (ssget "_X" '((0 . "INSERT"))))
(setq ik 0)
(while (< ik (sslength gt))
(setq ent (ssname gt ik))
(setq object (vlax-ename->vla-object ent))
(setq block_name (vlax-get-property object 'EffectiveName))
(cond ((= block_name "HDV1_GENERIC_BLOCK_LEFT")
(setq dg1 (c:wd_getattrval ent "INST"))
(command "-attedit" "n" "n" "ele_d2b" "dwg1" "" "" dg1)
)
((= block_name "HDV1_GENERIC_BLOCK_MIDDLE")
(setq dg2 (c:wd_getattrval ent "INST"))
(command "-attedit" "n" "n" "ele_d2b" "dwg2" "" "" dg2)
)
((= block_name "HDV1_GENERIC_BLOCK_RIGHT")
(setq dg3 (c:wd_getattrval ent "INST"))
(command "-attedit" "n" "n" "ele_d2b" "dwg3" "" "" dg3)
)
)
(setq ik (1+ik))
)
(princ)
)
With this line:
(setq gt (ssget "_X" '((0 . "INSERT"))))
all blocks in the drawing are selected, so while loop is used to check them all and find HDV1_GENERIC_BLOCK_... blocks.
I have no idea why other things described happen.
Did you try to find error using Visual LISP debugging menu?