Jump to content

Reference to objects nested in a block


ThatGermanFool

Recommended Posts

Hi Everyone,

 

how can I access the objects inside a given block using Lisp? 

 

As an example take this structure:

 

myParentBlock

- myChildLine

- myChildPolyline
- myChildBlock

---- myGrandchildLine

---- myGrandchildCircle

- myChildArc

 

What Lisp Routine could I use to Input the Entity "myPArentBlock" and get a reference to all the Children Entities (not the grandchildren)? (For a start, I just Need the Entity names.

 

Any hints?

 

Cheers

Seb

Link to comment
Share on other sites

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>)

 

Edited by Lee Mac
  • Like 3
  • Thanks 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...