For primary entities only, use a combination of tblobjname & entnext:
(defun blockcomponents ( blk / ent lst )
(if (setq ent (tblobjname "block" blk))
(while (setq ent (entnext ent))
(setq lst (cons ent lst))
)
)
(reverse lst)
)
Call the above with a block name argument, e.g.:
_$ (blockcomponents "YourBlockName")
(<Entity name: 7ffff706950> <Entity name: 7ffff706960> <Entity name: 7ffff706970>)
To include nested objects, check for the presence of a block reference (INSERT) entity and include a recursive call, e.g.:
(defun blockcomponents ( blk / ent enx lst )
(if (setq ent (tblobjname "block" blk))
(while (setq ent (entnext ent))
(if (= "INSERT" (cdr (assoc 0 (setq enx (entget ent)))))
(setq lst (vl-list* (blockcomponents (cdr (assoc 2 enx))) ent lst))
(setq lst (cons ent lst))
)
)
)
(reverse lst)
)
The above will return a list of entity names with sublists containing the entity names corresponding to the components of nested block references, e.g.:
_$ (blockcomponents "block1")
(<Entity name: 7ffff7069f0> <Entity name: 7ffff706a00>)
_$ (blockcomponents "block2")
(<Entity name: 7ffff706a50> (<Entity name: 7ffff7069f0> <Entity name: 7ffff706a00>) <Entity name: 7ffff706a60> <Entity name: 7ffff706a70>)
_$ (blockcomponents "block3")
(<Entity name: 7ffff706ad0> (<Entity name: 7ffff706a50> (<Entity name: 7ffff7069f0> <Entity name: 7ffff706a00>) <Entity name: 7ffff706a60> <Entity name: 7ffff706a70>) <Entity name: 7ffff706ae0> <Entity name: 7ffff706af0>)
Here, Block1 is nested within Block2 is nested within Block3.
If you don't want the nested list structure, use append in place of vl-list*, e.g.:
(defun blockcomponents ( blk / ent enx lst )
(if (setq ent (tblobjname "block" blk))
(while (setq ent (entnext ent))
(if (= "INSERT" (cdr (assoc 0 (setq enx (entget ent)))))
(setq lst (append (blockcomponents (cdr (assoc 2 enx))) (cons ent lst)))
(setq lst (cons ent lst))
)
)
)
(reverse lst)
)
This now returns a flat list:
_$ (blockcomponents "block1")
(<Entity name: 7ffff7069f0> <Entity name: 7ffff706a00>)
_$ (blockcomponents "block2")
(<Entity name: 7ffff706a50> <Entity name: 7ffff706a00> <Entity name: 7ffff7069f0> <Entity name: 7ffff706a60> <Entity name: 7ffff706a70>)
_$ (blockcomponents "block3")
(<Entity name: 7ffff706ad0> <Entity name: 7ffff706a70> <Entity name: 7ffff706a60> <Entity name: 7ffff7069f0> <Entity name: 7ffff706a00> <Entity name: 7ffff706a50> <Entity name: 7ffff706ae0> <Entity name: 7ffff706af0>)