Script to explode block and transfer to layer

Script to explode block and transfer to layer

Temssi.d
Enthusiast Enthusiast
797 Views
6 Replies
Message 1 of 7

Script to explode block and transfer to layer

Temssi.d
Enthusiast
Enthusiast

I am looking for a script that will explode a block and then transfer all its parts to a specific layer

(01_layer).

Thank you for your help!

0 Likes
Accepted solutions (2)
798 Views
6 Replies
Replies (6)
Message 2 of 7

komondormrex
Mentor
Mentor
Accepted solution

check this

 

(defun c:explode_to_layer (/ list_exploded last_ename sset_exploded)
	(setq target_insert (vlax-ename->vla-object (car (entsel "\nPick target insert: ")))
		  last_ename (entlast)
	)
	(if (setq list_exploded (vlax-invoke target_insert 'explode))
		(mapcar '(lambda (object) (vla-put-layer object "01_layer")) list_exploded)
		(progn
			(setq sset_exploded (ssadd))
			(while (setq last_ename (entnext last_ename))
				(ssadd last_ename sset_exploded) 
			)
			(command "_chprop" sset_exploded "" "_la" "01_layer" "")
		)
	)
	(vla-erase target_insert) 
	(princ)
)

 

Message 3 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

Since the result of an EXPLODE command simply becomes the Previous selection set, there's no need to save the last object, Explode, and then step through and find all newer objects.  It can be simpler:

 

(defun C:X2L (/ ss)
  (prompt "\nTo Explode Block(s) to Layer 01_layer,")
  (if (setq ss (ssget "_:L" '((0 . "INSERT"))))
    (progn ; then
      (initcommandversion)
      (command
        "_.explode" ss ""
        "_.chprop" "_previous" "" "_layer" "01_layer" ""
      ); command
    ); progn
  ); if
  (prin1)
)

 

which also prevents selection of inappropriate objects, and even lets you do it to as many Blocks as you want, all at the same time.

That assumes that the Layer in question exists in the drawing.  If it might not, that can be accommodated.

Kent Cooper, AIA
Message 4 of 7

pendean
Community Legend
Community Legend
@Temssi.d XPLODE command (there is no E at the beginning) has been around for decades and does what you want, and more
https://help.autodesk.com/view/ACD/2023/ENU/?guid=GUID-FC4139AB-D527-4743-8E68-F83DA6E5D192#:~:text=...

It's even in the LT version of AutoCAD.
0 Likes
Message 5 of 7

Temssi.d
Enthusiast
Enthusiast
Thank you so much it works perfectly! ‌‌
0 Likes
Message 6 of 7

Temssi.d
Enthusiast
Enthusiast
Wow!
thanks:)
0 Likes
Message 7 of 7

Temssi.d
Enthusiast
Enthusiast

Awesome, thanks!

0 Likes