Jump to content
JoeC

Find nested blocks

Recommended Posts

JoeC

Anyone know a lisp or a way to find nested blocks in a drawing? I have drawings with both nested and simple blocks, and the nested ones are causing problems with an excel program. I need to identify and eliminate the nested blocks...hopefully globally.

 

Thanks

Share this post


Link to post
Share on other sites
CJJ

I've been looking for a way to do this, also.

 

Obviously, the easy answer would be to explode everything. I usually yell out "BOOM!" when I run the explode command. This way I get to annoy everyone twice, once while exploding and then again if anyone else ever needs to use the drawing.

 

But seriously, I always feel like i'm wasting a lot of time trying to do detective work and identify blocks within blocks (within blocks withing blocks within...) one at a time, when there must be an easier way.

Share this post


Link to post
Share on other sites
David Bethel

Incorporating all nested blocks into the parent block in a single swoop would a very challenging project to say the least.

 

It could also make some extremely large drawings.

 

Is it the naming conventions that causing problems? or attribute values?

 

-David

Share this post


Link to post
Share on other sites
JoeC

Hmmm, not sure I understand your reply. I don't want to incorporate the nested blocks. I only want to identify which ones are nested (without inspecting each block individually), so that I can "un-nest" them.

Share this post


Link to post
Share on other sites
Lee Mac

How about something like this:

 

[b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] c:NestSel [b][color=RED]([/color][/b] [b][color=BLUE]/[/color][/b] ss def n l [b][color=RED])[/color][/b]
 [i][color=#990099];; © Lee Mac  ~  03.06.10[/color][/i]
 [b][color=RED]([/color][/b][b][color=BLUE]vl-load-com[/color][/b][b][color=RED])[/color][/b]

 [b][color=RED]([/color][/b][b][color=BLUE]while[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] def [b][color=RED]([/color][/b][b][color=BLUE]tblnext[/color][/b] [b][color=#a52a2a]"BLOCK"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]not[/color][/b] def[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b]ContainsNested [b][color=RED]([/color][/b][b][color=BLUE]tblobjname[/color][/b] [b][color=#a52a2a]"BLOCK"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] n [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]2[/color][/b] def[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] l [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] n l[b][color=RED])[/color][/b][b][color=RED])[/color][/b]
   [b][color=RED])[/color][/b]
 [b][color=RED])[/color][/b]

 [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] l
   [b][color=RED]([/color][/b][b][color=BLUE]sssetfirst[/color][/b] [b][color=BLUE]nil[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]ssget[/color][/b] [b][color=#a52a2a]"_X"[/color][/b]
       [b][color=RED]([/color][/b][b][color=BLUE]list[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=RED]([/color][/b][b][color=#009900]0[/color][/b] . [b][color=#a52a2a]"INSERT"[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]2[/color][/b] [b][color=RED]([/color][/b]lst->str l [b][color=#a52a2a]","[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
     [b][color=RED])[/color][/b]
   [b][color=RED])[/color][/b]
 [b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b][b][color=RED])[/color][/b]
[b][color=RED])[/color][/b]

[b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] ContainsNested [b][color=RED]([/color][/b] ent [b][color=BLUE]/[/color][/b] foo [b][color=RED])[/color][/b]
 [i][color=#990099];; © Lee Mac  ~  03.06.10[/color][/i]

 [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] foo [b][color=RED]([/color][/b] e [b][color=RED])[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] e [b][color=RED]([/color][/b][b][color=BLUE]entnext[/color][/b] e[b][color=RED])[/color][/b][b][color=RED])[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] e [b][color=RED]([/color][/b]foo e[b][color=RED])[/color][/b][b][color=RED])[/color][/b]
   [b][color=RED])[/color][/b]
 [b][color=RED])[/color][/b]

 [b][color=RED]([/color][/b][b][color=BLUE]vl-some[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]function[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]lambda[/color][/b] [b][color=RED]([/color][/b] x [b][color=RED])[/color][/b]
       [b][color=RED]([/color][/b][b][color=BLUE]eq[/color][/b] [b][color=#a52a2a]"INSERT"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]0[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] x[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
     [b][color=RED])[/color][/b]
   [b][color=RED])[/color][/b]
   [b][color=RED]([/color][/b]foo ent[b][color=RED])[/color][/b]
 [b][color=RED])[/color][/b]
[b][color=RED])[/color][/b]

[b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] lst->str [b][color=RED]([/color][/b] lst del [b][color=RED])[/color][/b]
 [i][color=#990099];; © Lee Mac  ~  03.06.10[/color][/i]
 [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] lst[b][color=RED])[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]strcat[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]car[/color][/b] lst[b][color=RED])[/color][/b] del [b][color=RED]([/color][/b]lst->str [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] lst[b][color=RED])[/color][/b] del[b][color=RED])[/color][/b][b][color=RED])[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]car[/color][/b] lst[b][color=RED])[/color][/b]
 [b][color=RED])[/color][/b]
[b][color=RED])[/color][/b]

 

 

R12 Friendly Version for David :)

 

[b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] c:NestSel [b][color=RED]([/color][/b] [b][color=BLUE]/[/color][/b] ss def n l [b][color=RED])[/color][/b]
 [i][color=#990099];; © Lee Mac  ~  03.06.10[/color][/i]

 [b][color=RED]([/color][/b][b][color=BLUE]while[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] def [b][color=RED]([/color][/b][b][color=BLUE]tblnext[/color][/b] [b][color=#a52a2a]"BLOCK"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]not[/color][/b] def[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b]ContainsNested [b][color=RED]([/color][/b][b][color=BLUE]tblobjname[/color][/b] [b][color=#a52a2a]"BLOCK"[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] n [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]2[/color][/b] def[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] l [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] n l[b][color=RED])[/color][/b][b][color=RED])[/color][/b]
   [b][color=RED])[/color][/b]
 [b][color=RED])[/color][/b]

 [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] l
   [b][color=RED]([/color][/b][b][color=BLUE]sssetfirst[/color][/b] [b][color=BLUE]nil[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]ssget[/color][/b] [b][color=#a52a2a]"_X"[/color][/b]
       [b][color=RED]([/color][/b][b][color=BLUE]list[/color][/b] [b][color=DARKRED]'[/color][/b][b][color=RED]([/color][/b][b][color=#009900]0[/color][/b] . [b][color=#a52a2a]"INSERT"[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] [b][color=#009900]2[/color][/b] [b][color=RED]([/color][/b]lst->str l [b][color=#a52a2a]","[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
     [b][color=RED])[/color][/b]
   [b][color=RED])[/color][/b]
 [b][color=RED])[/color][/b]
 [b][color=RED]([/color][/b][b][color=BLUE]princ[/color][/b][b][color=RED])[/color][/b]
[b][color=RED])[/color][/b]

[b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] ContainsNested [b][color=RED]([/color][/b] ent [b][color=BLUE]/[/color][/b] foo [b][color=RED])[/color][/b]
 [i][color=#990099];; © Lee Mac  ~  03.06.10[/color][/i]

 [b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] foo [b][color=RED]([/color][/b] e [b][color=RED])[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]setq[/color][/b] e [b][color=RED]([/color][/b][b][color=BLUE]entnext[/color][/b] e[b][color=RED])[/color][/b][b][color=RED])[/color][/b]
     [b][color=RED]([/color][/b][b][color=BLUE]cons[/color][/b] e [b][color=RED]([/color][/b]foo e[b][color=RED])[/color][/b][b][color=RED])[/color][/b]
   [b][color=RED])[/color][/b]
 [b][color=RED])[/color][/b]

 [b][color=RED]([/color][/b][b][color=BLUE]member[/color][/b] [b][color=#a52a2a]"INSERT"[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]mapcar[/color][/b] 
     [b][color=RED]([/color][/b][b][color=BLUE]function[/color][/b]
       [b][color=RED]([/color][/b][b][color=BLUE]lambda[/color][/b] [b][color=RED]([/color][/b] x [b][color=RED])[/color][/b]
         [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]assoc[/color][/b] [b][color=#009900]0[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]entget[/color][/b] x[b][color=RED])[/color][/b][b][color=RED])[/color][/b][b][color=RED])[/color][/b]
       [b][color=RED])[/color][/b]
     [b][color=RED])[/color][/b]
     [b][color=RED]([/color][/b]foo ent[b][color=RED])[/color][/b]
   [b][color=RED])[/color][/b]
 [b][color=RED])[/color][/b]
[b][color=RED])[/color][/b]

[b][color=RED]([/color][/b][b][color=BLUE]defun[/color][/b] lst->str [b][color=RED]([/color][/b] lst del [b][color=RED])[/color][/b]
 [i][color=#990099];; © Lee Mac  ~  03.06.10[/color][/i]
 [b][color=RED]([/color][/b][b][color=BLUE]if[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] lst[b][color=RED])[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]strcat[/color][/b] [b][color=RED]([/color][/b][b][color=BLUE]car[/color][/b] lst[b][color=RED])[/color][/b] del [b][color=RED]([/color][/b]lst->str [b][color=RED]([/color][/b][b][color=BLUE]cdr[/color][/b] lst[b][color=RED])[/color][/b] del[b][color=RED])[/color][/b][b][color=RED])[/color][/b]
   [b][color=RED]([/color][/b][b][color=BLUE]car[/color][/b] lst[b][color=RED])[/color][/b]
 [b][color=RED])[/color][/b]
[b][color=RED])[/color][/b]

Share this post


Link to post
Share on other sites
alanjt
How about something like this:

[/code]

 

LoL, I guess your vacation is over. :lol:

Share this post


Link to post
Share on other sites
Lee Mac
LoL, I guess your vacation is over. :lol:

 

It looked like an interesting thing to code is all :)

Share this post


Link to post
Share on other sites
JoeC

Thanks Lee. Once again, exactly what I was looking for!

 

Best regards.

Share this post


Link to post
Share on other sites
Lee Mac

You're welcome Joe - it was fun to write :)

Share this post


Link to post
Share on other sites
alanjt
It looked like an interesting thing to code is all :)

 

Whatever floats your boat. :wink:

Share this post


Link to post
Share on other sites
David Bethel

lee,

 

I must admit, I don't quite understand. Are you simply deleting all nested blocks?

 

My guess is that the OP wants to keep the geometry of the nested blocks in the parent block. In order to replicate that, you would need to take into take into account the nested block scale factors ( mirrored as well), rotations, UCS, insert points, minsert values. And then if the blocks are nested 3 or 4 deep, wow!

 

Maybe I'm missing something. -David

Share this post


Link to post
Share on other sites
Lee Mac

Hi David,

 

I was under the impression that the OP just wanted to select blocks that had other blocks nested within.

 

My code merely takes each block definition in turn, and if the definition contains a block nested at least one level deep, it will be contained in the selection set.

 

Lee

Share this post


Link to post
Share on other sites
rkmcswain

@LeeMac - consider the problem where you know certain blocks are nested (no insertions of them, and not purge-able) - and you want to find out where they are nested.

 

I was envisioning an output like this:

 

>Block1
 >> Block-A
      >> Block-B

>Block2
 >> Block-C
      >> Block-D

>Block3
  >> Block-E
      >> Block-A
           >> Block-B
>Block4
  >> Block-B

 

So looking at that, you could easily see that both Block-A and Block4 contain Block-B.

 

 

Of course another way to tackle this, would be to ask the user for the name of the block, and then just iterate everything looking for that one block and report back the parent block(s) that contain it.

 

Cheers all.

Share this post


Link to post
Share on other sites
Lee Mac

Here's a basic form of such a program -

([color=BLUE]defun[/color] _nestedblocks ( blk [color=BLUE]/[/color] enx rtn )
   ([color=BLUE]while[/color] ([color=BLUE]setq[/color] blk ([color=BLUE]entnext[/color] blk))
       ([color=BLUE]if[/color]
           ([color=BLUE]and[/color]
               ([color=BLUE]setq[/color] enx ([color=BLUE]entget[/color] blk))
               ([color=BLUE]=[/color] [color=MAROON]"INSERT"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 enx)))
               ([color=BLUE]/=[/color] 1 ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 60 enx)))
               ([color=BLUE]not[/color] ([color=BLUE]member[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 enx)) rtn))
           )
           ([color=BLUE]setq[/color] rtn ([color=BLUE]cons[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 enx)) rtn))
       )
   )
   rtn
)
([color=BLUE]defun[/color] _blockhierarchy ( [color=BLUE]/[/color] blk def rtn )
   ([color=BLUE]while[/color] ([color=BLUE]setq[/color] def ([color=BLUE]tblnext[/color] [color=MAROON]"block"[/color] ([color=BLUE]null[/color] def)))
       ([color=BLUE]setq[/color] rtn
           ([color=BLUE]cons[/color]
               ([color=BLUE]cons[/color]
                   ([color=BLUE]setq[/color] blk ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 def)))
                   (_nestedblocks ([color=BLUE]tblobjname[/color] [color=MAROON]"block"[/color] blk))
               )
               rtn
           )
       )
   )
   rtn
)
([color=BLUE]defun[/color] _printblockhierarchy ( blk lst ind )
   ([color=BLUE]terpri[/color])
   ([color=BLUE]repeat[/color] ind ([color=BLUE]princ[/color] [color=MAROON]"    "[/color]))
   ([color=BLUE]princ[/color] [color=MAROON]"+---"[/color])
   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] nst ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] blk lst)))
       ([color=BLUE]progn[/color]
           ([color=BLUE]princ[/color] [color=MAROON]"+ "[/color])
           ([color=BLUE]princ[/color] blk)
           ([color=BLUE]foreach[/color] sub nst
               (_printblockhierarchy sub lst ([color=BLUE]1+[/color] ind))
           )
       )
       ([color=BLUE]progn[/color]
           ([color=BLUE]princ[/color] [color=MAROON]"> "[/color])
           ([color=BLUE]princ[/color] blk)
       )
   )
)
([color=BLUE]defun[/color] c:blockhierarchy ( )
   (   ([color=BLUE]lambda[/color] ( lst )
           ([color=BLUE]foreach[/color] blk lst
               (_printblockhierarchy ([color=BLUE]car[/color] blk) lst 0)
           )
           ([color=BLUE]textpage[/color])
       )
       (_blockhierarchy)
   )
   ([color=BLUE]princ[/color])
)

Share this post


Link to post
Share on other sites
rkmcswain

Very nice @LeeMac !

Didn't intend for you to jump on this and code it. Bonus points!

Share this post


Link to post
Share on other sites
Lee Mac

Cheers RK :thumbsup:

Share this post


Link to post
Share on other sites
ronjonp
Here's a basic form of such a program -

([color=BLUE]defun[/color] _nestedblocks ( blk [color=BLUE]/[/color] enx rtn )
   ([color=BLUE]while[/color] ([color=BLUE]setq[/color] blk ([color=BLUE]entnext[/color] blk))
       ([color=BLUE]if[/color]
           ([color=BLUE]and[/color]
               ([color=BLUE]setq[/color] enx ([color=BLUE]entget[/color] blk))
               ([color=BLUE]=[/color] [color=MAROON]"INSERT"[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 0 enx)))
               ([color=BLUE]/=[/color] 1 ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 60 enx)))
               ([color=BLUE]not[/color] ([color=BLUE]member[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 enx)) rtn))
           )
           ([color=BLUE]setq[/color] rtn ([color=BLUE]cons[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 enx)) rtn))
       )
   )
   rtn
)
([color=BLUE]defun[/color] _blockhierarchy ( [color=BLUE]/[/color] blk def rtn )
   ([color=BLUE]while[/color] ([color=BLUE]setq[/color] def ([color=BLUE]tblnext[/color] [color=MAROON]"block"[/color] ([color=BLUE]null[/color] def)))
       ([color=BLUE]setq[/color] rtn
           ([color=BLUE]cons[/color]
               ([color=BLUE]cons[/color]
                   ([color=BLUE]setq[/color] blk ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 def)))
                   (_nestedblocks ([color=BLUE]tblobjname[/color] [color=MAROON]"block"[/color] blk))
               )
               rtn
           )
       )
   )
   rtn
)
([color=BLUE]defun[/color] _printblockhierarchy ( blk lst ind )
   ([color=BLUE]terpri[/color])
   ([color=BLUE]repeat[/color] ind ([color=BLUE]princ[/color] [color=MAROON]"    "[/color]))
   ([color=BLUE]princ[/color] [color=MAROON]"+---"[/color])
   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] nst ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] blk lst)))
       ([color=BLUE]progn[/color]
           ([color=BLUE]princ[/color] [color=MAROON]"+ "[/color])
           ([color=BLUE]princ[/color] blk)
           ([color=BLUE]foreach[/color] sub nst
               (_printblockhierarchy sub lst ([color=BLUE]1+[/color] ind))
           )
       )
       ([color=BLUE]progn[/color]
           ([color=BLUE]princ[/color] [color=MAROON]"> "[/color])
           ([color=BLUE]princ[/color] blk)
       )
   )
)
([color=BLUE]defun[/color] c:blockhierarchy ( )
   (   ([color=BLUE]lambda[/color] ( lst )
           ([color=BLUE]foreach[/color] blk lst
               (_printblockhierarchy ([color=BLUE]car[/color] blk) lst 0)
           )
           ([color=BLUE]textpage[/color])
       )
       (_blockhierarchy)
   )
   ([color=BLUE]princ[/color])
)

 

That's pretty slick 8)

Share this post


Link to post
Share on other sites
Lee Mac
That's pretty slick 8)

 

 

Thanks Ron :)

Share this post


Link to post
Share on other sites
rlx

Wow Lee , you allways make it look so easy :notworthy:

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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