After about a couple months of messing with Lisp codes that users have been sending me. I'm pretty happy with this one. Taking this code, and placing it in my Lisp toolbox allows me to call it from many other Lisp codes. Tremendously opening the door to more uses of Dynamic Blocks. Before we would use a lot of (ENTMAKE's to create custom blocks. The code is relatively simply to manipulate Dynamic blocks by simply getting a selection set. Then modifying the bottom portion of the Dynamic block code for the parameter and what you want it to be set to.
Here is the code. Don't get scared if you're new to LISP because only the bottom line is what needs to be modified.
(defun myModifyBk ( lstProp / ss index cnt oBkRef oProps i j oSBReferenceProperty)
(vl-load-com)
(SETQ ss (SSGET "L"))
(setq index 0
cnt (sslength ss)
)
(while (< index cnt)
(setq e (ssname ss index))
(setq oBkRef (vlax-ename->vla-object e))
(setq oProps (vlax-variant-value (vla-GetDynamicBlockProperties oBkRef)))
(setq i (vlax-safearray-get-l-bound oProps 1))
(while (<= i (vlax-safearray-get-u-bound oProps 1))
(setq oSBReferenceProperty (vlax-safearray-get-element oProps i))
(print (strcat (vla-get-PropertyName oSBReferenceProperty) "="))
(princ (vlax-variant-value (vla-get-value oSBReferenceProperty)))
(setq i (1+ i))
)
(setq j 0)
(while (< j (length lstProp))
(setq sProp (strcase (nth j lstProp)))
(setPropValue oProps sProp (nth (+ 1 j) lstProp))
(setq j (+ 2 j))
) ;while < j (length lstProp)
(setq index (1+ index))
) ;while < index cnt
(princ)
)
(defun setPropValue (oProps sProp Val / i oSBReferenceProperty sPName iFound)
(setq i (vlax-safearray-get-l-bound oProps 1))
(setq iFound 0)
(while (and (<= i (vlax-safearray-get-u-bound oProps 1)) (= iFound 0))
(setq oSBReferenceProperty (vlax-safearray-get-element oProps i))
(setq sPName (vla-get-PropertyName oSBReferenceProperty))
(if (= (strcase sPName) sProp)
(progn
(print (strcat "Old value of " sPName "="))
(princ (vlax-variant-value (vla-get-value oSBReferenceProperty)))
(vla-put-value oSBReferenceProperty
(vlax-make-variant Val
(vlax-variant-type (vla-get-value oSBReferenceProperty))
)
)
(print "New value=")
(princ (vlax-variant-value (vla-get-value oSBReferenceProperty)))
(setq iFound 1)
)
)
(setq i (1+ i))
)
(princ)
)
;;;;DTR converts degrees to radians.
(defun dtr (a)
(* pi (/ a 180.0))
)
;;RTD converts radians to degrees.
(defun rtd (a)
(/ (* a 180.0) pi)
)
;;The section below is the portion to modify to manipulate your dynamic block afte ryou establish a selection set.
(myModifyBk (list "DynamicBlockParmatername" DynamicBlockValue))
Hope this helps everyone with getting their Dynamic blocks into some custom LISP codes.
Click here to download the LISP file:Download Dblockcode.lsp
I would like to thank very much for the lisp "Dblockcode.lsp". Really, I have been looking for codes to work with dynamic blocks. At last, thanks for you, because you have given me what I was in need. I work in a large company in the Middle East known as Dar Al-Handasah and I have been working as a Lisp programmer since AutoCAD release five. Thanks again and keep on!
Victor Jammal
Posted by: victor.jammal | August 29, 2005 at 11:04 PM
Glad to help!! This also has really helped the office I work at transition to 2006 and Dynamic blocks. There still needs some work, but im sure a lot of the minor problems will be fixed in a future release.
Thanks for the comments...
Mark
Posted by: Mark Douglas | August 30, 2005 at 07:05 AM
Holy Cow man, this is exactly the type of thing I've been looking for since we switched to 2006.
Thanks,
Lance
Posted by: Lance | November 22, 2005 at 08:04 AM