LISP to Create Block for all entities on each layer in a dwg model file

john.a.latimer
Explorer
Explorer

LISP to Create Block for all entities on each layer in a dwg model file

john.a.latimer
Explorer
Explorer

I need to make or find a LISP program that will create a Block for all the entities on each layer of a dwg model file.    I have received and imported a model file from a client that contains the geometry for hundreds of objects each separated on individual layers.    I'd like to create a block for all the entities on each layer and name the block by the layer name. 

Sequence:   Activate a layer;  select all entities on that layer;  create a block from selected entities; save block as layer name;  repeat for all layers. 

 

any ideas

 

0 Likes
Reply
Accepted solutions (1)
1,568 Views
2 Replies
Replies (2)

Kent1Cooper
Consultant
Consultant
Accepted solution

@john.a.latimer wrote:

.... 

Sequence:   Activate a layer;  select all entities on that layer;  create a block from selected entities; save block as layer name;  repeat for all layers. 

....


No need to active the Layers in the process.  Try something like this [minimally tested]:

 

(defun C:BCAL ; = Block Contents of All Layers
  (/ lyr lyrname)
  (tblnext "layer" T); start at 0 [but don't Block it]
  (while (setq lyr (tblnext "layer")); each subsequent Layer
    (command
      "_.block" (setq lyrname (cdr (assoc 2 lyr))) "0,0" (ssget "_X" (list (cons 8 lyrname))) ""
      "_.insert" lyrname "_non" "0,0" "" "" ""
      "_.chprop" "_last" "" "_layer" lyrname ""
    ); command
  ); while
  (princ)
); defun

 

It Inserts because the Block command in a (command) function removes the objects from the drawing.

 

It could be made to bypass the DEFPOINTS Layer if that's appropriate, or any other specific Layers you may want, and/or could Insert them all on Layer 0 or something, and could use the usual other enhancements, but first see whether it does what you want.

Kent Cooper, AIA
0 Likes

john.a.latimer
Explorer
Explorer

Absolutely Perfect.    Simple test proved that it does exactly what I need.   Thank you. 

 

0 Likes