Author Topic: Collect entities from a block definition  (Read 1491 times)

0 Members and 1 Guest are viewing this topic.

QuestionEverything

  • Guest
Collect entities from a block definition
« on: December 20, 2016, 02:13:05 PM »
Hello everyone,
I'd like to see how many options can be figured out, about collecting the objects/entities inside a block definition.  :whistling:

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Collect entities from a block definition
« Reply #1 on: December 20, 2016, 02:17:46 PM »
Same as any other collection.

(vlax-for object collection (setq result (cons object result))) (reverse result)

vlax-map-collection is generally faster but at the expense of clarity.

[ Untested, abbreviated response from my phone. ]
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

QuestionEverything

  • Guest
Re: Collect entities from a block definition
« Reply #2 on: December 20, 2016, 03:59:24 PM »
What about Vanilla?

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Collect entities from a block definition
« Reply #3 on: December 20, 2016, 04:09:03 PM »
If provided a vanilla block entity (e.g. block_ename) start iterating thru the collection via (entnext (cdr (assoc -2 (entget block_ename)))) bailing once an "ENDBLK" entity is encountered.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

QuestionEverything

  • Guest
Re: Collect entities from a block definition
« Reply #4 on: December 20, 2016, 04:40:39 PM »
This seems to be right:
Code - Auto/Visual Lisp: [Select]
  1. (defun CollectObjsInBlockDef1 ( BlockName / Lst )
  2.     (setq Lst (cons o Lst))
  3.   )
  4.   (reverse Lst)
  5. )
  6.  
  7. (defun CollectObjsInBlockDef2 ( BlockName / Lst )
  8.   (reverse Lst)
  9. )
  10.  
  11. (defun CollectEnamesInBlockDef ( BlockName / e Lst )
  12.   (setq e (cdr (assoc -2 (tblsearch "BLOCK" BlockName))))
  13.   (while (and e (/= "ENDBLK" (cdr (assoc 0 (entget e)))))
  14.     (setq Lst (cons e Lst))
  15.     (setq e (entnext e))
  16.   )
  17.   (reverse Lst)
  18. )
  19.  
Thank you M.Puckett!

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Collect entities from a block definition
« Reply #5 on: December 21, 2016, 01:01:38 AM »
Both are correct, just my second (which I often use) is little shorter and cleaner...

Code - Auto/Visual Lisp: [Select]
  1. (defun CollectEnamesInBlockDef ( BlockName / e Lst )
  2.   (setq e (cdr (assoc -2 (tblsearch "BLOCK" BlockName))))
  3.   (while (and e (/= "ENDBLK" (cdr (assoc 0 (entget e)))))
  4.     (setq Lst (cons e Lst))
  5.     (setq e (entnext e))
  6.   )
  7.   (reverse Lst)
  8. )
  9.  
  10. (defun CollectEnamesInBlockDef ( BlockName / e Lst )
  11.   (setq e (tblobjname "BLOCK" BlockName))
  12.   (while (and (setq e (entnext e)) (/= "ENDBLK" (cdr (assoc 0 (entget e)))))
  13.     (setq Lst (cons e Lst))
  14.   )
  15.   (reverse Lst)
  16. )
  17.  
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Collect entities from a block definition
« Reply #6 on: December 21, 2016, 04:54:45 AM »
Two more (less effective):

Code: [Select]
(defun CollectObjsInBlockDef ( BlockName / BlockDef i Lst )
  (setq BlockDef (vla-item (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object))) BlockName))
  (repeat (setq i (vla-get-Count BlockDef))
    (setq Lst (cons (vla-item BlockDef (setq i (1- i))) Lst))
  )
  (reverse Lst)
)

(defun CollectObjsInBlockDef ( BlockName / BlockDef n o Lst )
  (setq BlockDef (vla-item (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object))) BlockName))
  (setq n 0)
  (while (not (vl-catch-all-error-p (setq o (vl-catch-all-apply 'vla-item (list BlockDef n)))))
    (setq Lst (cons o Lst))
    (setq n (1+ n))
  )
  (reverse Lst)
)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

QuestionEverything

  • Guest
Re: Collect entities from a block definition
« Reply #7 on: December 22, 2016, 08:45:08 AM »
So the conclusion I did is that the entities inside a block definition are located between the "BLOCK" and "ENDBLK" entities.
And for visual lisp they are simply collection objects.
Thank you all!