« Creating a Selection Set Using ObjectARX | Main | Beyond the UI Reaches 100th Post Milestone »


Jessye Ford

I am looking for either a VBA or AutoLISP routine that will look at a piece of text, and based on the value of that text, change the visibility state of a dynamic block. I don't want the CAD operator to have to actually "select" anything, but I also would like to be able to apply the process to several other blocks within the drawing. So that as the referenced text changes (to display the correct visibility state) the corresponding block would change as well. I have been working on this for about 6 months, but I have little to NO VBA or LISP experience and trying to teach myself this has been....exhausting....to say the least! I would appreciate ANY help, advice, or voodoo magic you could throw my way. Thank you.

Lee Ambrosius

Without a specific use case to design the sample code around, quickly created a very generic example that could be refined a number of ways. The sample code looks for text the text value of "123" and if found, it will change the Visibility State of blocks named "Trees - Imperial". I built the example around Single-line and Multiline text, but it could be done for block attribute values as well. Good luck and let me know if you have any specific questions.

;; Looks for text objects with 123 in them, and if it finds the dynmaic block
;; "Trees - Imperial" it changes the visibility state to Shrub (Plan).
(defun c:blockFlip ( / ssAll nCnt nTextPos lBlocks lText oEnt lEd sTextCompare)
(setq ssAll (ssget "X")
nCnt 0
nTextPos nil
lBlockRefs nil
lTextObjs nil
sTextCompare "123"

;; Step through all objects in a drawing and look for the text objects and block references
(while (> (sslength ssAll) nCnt)
(setq oEnt (ssname ssAll nCnt)
lEd (entget oEnt)

;; Create two lists; one with text objects (single-line and multiline) and one with block references
;; Note: Could have also done multiple (ssget) statements with a filter
((or (= (cdr (assoc 0 lEd)) "TEXT")(= (cdr (assoc 0 lEd)) "MTEXT"))
(if (= lTextObjs nil)
(setq lTextObjs (append (list oEnt)))
(setq lTextObjs (append lTextObjs (list oEnt)))
((= (cdr (assoc 0 lEd)) "INSERT")
(if (= lBlockRefs nil)
(setq lBlockRefs (append (list oEnt)))
(setq lBlockRefs (append lBlockRefs (list oEnt)))

(setq nCnt (1+ nCnt))

;; Step through all text objects and look for text strings that match the value of sTextCompare
(foreach oEnt lTextObjs
(setq lEd (entget oEnt))

(if (= (cdr (assoc 0 lEd)) "TEXT")
(setq nTextPos (vl-string-search sTextCompare (strcase (getpropertyvalue oEnt "TextString"))))
(setq nTextPos (vl-string-search sTextCompare (strcase (getpropertyvalue oEnt "Text"))))

;; If a match to text string was found, step through the blocks
(if (/= nTextPos nil)
;; If the block is named "Trees - Imperial" one of the sample dynamic block son the tool palette
;; and the block is indeed a dynamic block, change its viibility style
(if (and (= (getpropertyvalue ent "BlockTableRecord/Name") "Trees - Imperial")
(= (getpropertyvalue ent "IsDynamicBlock") 1)
(setq lDynProps (vlax-safearray->list (vlax-variant-value oDynProps)))

(foreach oDynProp lDynProps
(vla-put-Value oDynProp "Shrub (Plan)")


The comments to this entry are closed.

My Photo

My Other Accounts

LinkedIn Twitter

Twitter Updates

    follow me on Twitter