Author Topic: Filter list of entities to keep only blocks  (Read 233 times)

0 Members and 1 Guest are viewing this topic.

Red Nova

  • Newt
  • Posts: 72
Filter list of entities to keep only blocks
« on: May 04, 2024, 08:23:36 PM »
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: [Select]
(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.")
)
)
« Last Edit: May 04, 2024, 08:28:32 PM by Red Nova »

BIGAL

  • Swamp Rat
  • Posts: 1422
  • 40 + years of using Autocad
Re: Filter list of entities to keep only blocks
« Reply #1 on: May 04, 2024, 09:19:20 PM »
Not sure not really tested but try this.
Code: [Select]
; 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)
A man who never made a mistake never made anything

Red Nova

  • Newt
  • Posts: 72
Re: Filter list of entities to keep only blocks
« Reply #2 on: May 04, 2024, 10:41:54 PM »
Thank you. Unfortunately this returned an error.

ribarm

  • Gator
  • Posts: 3296
  • Marko Ribar, architect
Re: Filter list of entities to keep only blocks
« Reply #3 on: May 05, 2024, 07:40:53 AM »
Really don't have time now, but try to fix the code you provided... I edited it, but it's totally untested...

Code: [Select]
(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.")
  )
)

Regards...
« Last Edit: May 05, 2024, 07:48:16 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Red Nova

  • Newt
  • Posts: 72
Re: Filter list of entities to keep only blocks
« Reply #4 on: May 05, 2024, 11:23:06 AM »
thank you ribarm, works well :)