Lisp cad to explode all block to final level

Lisp cad to explode all block to final level

quocanh_elnino
Contributor Contributor
1,271 Views
8 Replies
Message 1 of 9

Lisp cad to explode all block to final level

quocanh_elnino
Contributor
Contributor

I have block level 3 have mutiple level block inside it (like image)

Can anyone help me code to explode all block but keep all block level 1 (smallest level)?

 

BLOCK IN BLOCK.JPG

0 Likes
Accepted solutions (1)
1,272 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant

Your Block Level 3 is only the green parts [rectangle and label].  If I understand your description correctly, aren't the two Blocks Level 2 supposed to be nested in and part of Block Level 3 [just as the Blocks Level 1 are nested in Block Level 2], not just surrounded by it?

 

I think either way could be done, but whether or not the Level 2 Blocks should be nested in the Level 3 Block makes a big difference to the approach needed to do what you're asking.

 

EDIT:  If the Level 2's are supposed to be nested in the Block Level 3 [I REFEDITed Level 3 to make the Level 2 Blocks part of it], then this worked for me:

(defun C:BXNL ; = Block(s) eXplode but Not Lowest level
  (/ ss)
  (command "_.explode" (car (entsel "\nSelect top-level Block: ")))
  (while
    (setq ss (ssget "_P" '((0 . "INSERT")))); any Blocks left from last Explode
    (initcommandversion)
    (command "_.explode" ss "")
  ); while
  (command "_.u"); Undo last Explode
  (prin1)
)

It Exploded Level 2 and Level 3, but left the Level 1 Blocks as Blocks.

Kent Cooper, AIA
0 Likes
Message 3 of 9

quocanh_elnino
Contributor
Contributor

Thanks @Kent1Cooper ! It's exactly as I expected, my drawing will not have any block which contain another block.

But for more convenience , Can you fix it to explode mutiple block instead of click one by one ? 

0 Likes
Message 4 of 9

devitg
Advisor
Advisor

@quocanh_elnino  please give it a test 

(defun c:explode-not-1stlevel (/	     ACAD-OBJ
			       ADOC	     INSERT-ENT-SS
			       INSERT-OBJ-SS
			      )
  (VL-LOAD-COM)
  (SETQ ACAD-OBJ (VLAX-GET-ACAD-OBJECT)) ;_ el programa ACAD 
  (SETQ ADOC (VLA-GET-ACTIVEDOCUMENT ACAD-OBJ)) ;_ el DWG que esta abierto-

  (setq insert-ent-ss (ssget "_X" '((0 . "insert"))))

  (setq insert-obj-ss (VLA-GET-ACTIVESELECTIONSET adoc))

  (vlax-for insert-obj insert-obj-ss

    (VLA-EXPLODE insert-obj)
    (vla-delete insert-obj)
  ) ;_ end of vlax-for



)

 

 

0 Likes
Message 5 of 9

Kent1Cooper
Consultant
Consultant
Accepted solution

@quocanh_elnino wrote:

.... Can you fix it to explode mutiple block instead of click one by one ? 


Minimally tested, but I think this does that:

(defun C:BXNL ; = Block(s) eXplode but Not Lowest level
  (/ bxnlss n ss)
  (if (setq bxnlss (ssget '((0 . "INSERT"))))
    (repeat (setq n (sslength bxnlss)); then
      (command "_.explode" (ssname bxnlss (setq n (1- n))))
      (while
        (setq ss (ssget "_P" '((0 . "INSERT")))); any Blocks left from last Explode
        (initcommandversion)
        (command "_.explode" ss "")
      ); while
      (command "_.u"); Undo last Explode
    ); repeat
  ); if
  (prin1)
)

Consider whether to restrict it to things not on locked Layers, and it should probably also be made to skip XREFs [which it would allow selection of], and to get some of the usual other enhancements.

Kent Cooper, AIA
0 Likes
Message 6 of 9

quocanh_elnino
Contributor
Contributor

Thanks for your help @devitg ,but i have some problem : 

- I need my drawing have only 1st level block, all another block have more than 1st level will be exploded.

- And in another drawing I have many block, so I dont know which block have 1st level, which block have more than 1st level 😅 , when i run the lisp all 1st level block will be exploded too.

0 Likes
Message 7 of 9

quocanh_elnino
Contributor
Contributor

@Kent1Cooper @devitg when I run the lisp on this drawing, some 1st level block will be explode too. I wonder can we filter selection all blocks which had another block inside? and Iwill use "Explode" command in default to 1st level.

0 Likes
Message 8 of 9

Kent1Cooper
Consultant
Consultant

@quocanh_elnino wrote:

... can we filter selection all blocks which had another block inside? ....


No.  There is nothing about the information that (ssget) can filter for that indicates whether a Block contains any nested Block(s).  If "1st level" means different numbers of levels down from the top for different Blocks, then the procedure in my routine will end up Exploding some of what you consider 1st level Blocks -- it was written for your sample drawing situation in which the 1st level is the same for all of them.

 

It might be possible to do what you describe, but I think it would be complicated.  For example, a routine could step through the entire Block table, and for each Block definition, step through its pieces to see whether any of them are Blocks, and in the process make a list of Blocks that contain other Blocks.  That could be used in limiting Exploding operations somehow, but progressing down levels could be a complex operation.

 

Big question:  Would this procedure ever include any Dynamic Blocks?

Kent Cooper, AIA
0 Likes
Message 9 of 9

quocanh_elnino
Contributor
Contributor

@Kent1Cooper It not include Dynamic Blocks. But I think the first Lisp is very helpful , help me save a lot of time  😁

I am truly grateful for your support !

0 Likes