Code Red > AutoLISP (Vanilla / Visual)

Filter list of entities to keep only blocks

(1/1)

Red Nova:
I have a block (let's call it MAIN BLOCK) that contains other blocks and also may contain some elements that are not blocks (dimensions, lines, etc)
I need to get the list of entity names of blocks contained in the MAIN BLOCK.
I found this code that returns the list of all entity names contained in the MAIN BLOCK.
However, I this list contains also entity names that are not block.
How can i filter the list of entities to keep only blocks and remove everything else?
Or can this code be modified to only return entities that are blocks?

--- Code: ---(defun getblkitems ( / sel nfo items)
(if
(and
(setq sel (entsel "Select a block: "))
(= (cdr (assoc 0 (setq nfo (entget (car sel))))) "INSERT")
)
(vlax-for
item
(vla-item
(vla-get-blocks
(vla-get-activedocument
(vlax-get-acad-object)
)
)
(cdr (assoc 2 nfo))
)
(setq items (cons (vlax-vla-object->ename item) items))
)
(prompt "\nNo block selected.")
)
)
--- End code ---

BIGAL:
Not sure not really tested but try this.

--- Code: ---; For GTA
; nested blocks code by Moshe-A
; Modified by alan H to pick block April 2023


(defun c:blist ( / bnames blocks ans)
(defun in-loop (bname / )
  (vlax-for AcDbEntity (vla-item blocks bname)
   (if (eq (vla-get-objectName AcDbEntity) "AcDbBlockReference")
    (progn
     (in-loop (vla-get-effectiveName AcDbEntity)) ; recursion call
     (setq bnames (cons (vla-get-effectiveName AcDbEntity) bnames))
    ); progn
   ); if
   (vlax-release-object AcDbEntity)     
  ); vlax-for
)


(setq AcDbBlockReference (vlax-ename->vla-object (car (entsel "\nSelect block "))))
(in-loop (vla-get-effectiveName AcDbBlockReference))

(princ)
)
(c:blist)
--- End code ---

Red Nova:
Thank you. Unfortunately this returned an error.

ribarm:
Really don't have time now, but try to fix the code you provided... I edited it, but it's totally untested...


--- Code: ---(defun getnestblkitems ( / sel nfo ent items )

  (or (not (vl-catch-all-error-p (vl-catch-all-apply (function vlax-get-acad-object) nil))) (vl-load-com))

  (or cad (setq cad (vlax-get-acad-object)))
  (or doc (setq doc (vla-get-activedocument cad)))
  (or alo (setq alo (vla-get-activelayout doc)))
  (or spc (setq spc (vla-get-block alo)))
  (or blk (setq blk (vla-get-blocks doc)))
  (if
    (and
      (setq sel (entsel "Select a block: "))
      (= (cdr (assoc 0 (setq nfo (entget (car sel))))) "INSERT")
    )
    (vlax-for item (vla-item blk (cdr (assoc 2 nfo)))
      (if (= (cdr (assoc 0 (entget (setq ent (vlax-vla-object->ename item))))) "INSERT")
        (setq items (cons ent items))
      )
    )
    (prompt "\nNo block selected.")
  )
)

--- End code ---

Regards...

Red Nova:
thank you ribarm, works well :)

Navigation

[0] Message Index

Go to full version