In a recent thread at CADTutor
here, a question was posed regarding how to find where a block is nested.
I posted some code to show the general block hierarchy, but I was then was curious about how I might code a function to search for the location of a specific nested block (no matter how deep) - i.e., given a block name, return a list showing where it is nested.
So I put this together:
(defun FindNestedBlock ( name / _name _SearchBlockDefinition blocks result )
;; © Lee Mac 2011
(defun _name ( obj )
(vlax-get-property obj
(if (vlax-property-available-p obj 'effectivename) 'effectivename 'name)
)
)
(defun _SearchBlockDefinition ( block / result nest )
(vlax-for obj block
(if (eq "AcDbBlockReference" (vla-get-objectname obj))
(if (eq name (_name obj))
(setq result (list name))
(if (setq nest (_SearchBlockDefinition (vla-item blocks (_name obj))))
(setq result (cons (_name obj) nest))
)
)
)
)
result
)
(setq blocks (vla-get-blocks (vla-get-ActiveDocument (vlax-get-acad-object))))
(vlax-for obj blocks
(if (eq name (_name obj))
(setq result (cons (list name) result))
(if (setq nest (_SearchBlockDefinition obj))
(setq result (cons nest result))
)
)
)
(car (vl-sort result '(lambda ( a b ) (> (length a) (length b)))))
)
But I'm really not happy with it, and so wondered if any of you guys could code anything more elegant.
Thanks,
Lee