sln8458 Posted February 21 Posted February 21 (edited) Hi All, I've had a batch of drawing arrive from a client that I need to work with. Part of what I need to do is create a block of the contents of each layer. As a start point, this code will allow me to create a selection set of everything on the ayer 'steel' (setq sel1 (ssget "x" '((8 . "STEEL")))) and this should create the block steel (command "_block" [STEEL] "0,0,0" sel1 "") However I am lost as to how I create the a list of the layer names in the active drawing, loop through them and place them in the code to create the blocks NOTE: this is to be used in Intellicad and not all VL code works Edited February 21 by sln8458 Quote
Steven P Posted February 21 Posted February 21 I use this for some routines which returns a list of layers (defun getmylayerlist ( / lyr mylayers) (vlax-for lyr (vla-get-layers (vla-get-activedocument (vlax-get-acad-object) ) ) (setq mylayers (cons (vla-get-name lyr) mylayers)) ) mylayers ) and this is another (Lee Mac I think) (defun LayLiast ( / lay lst) (while (setq lay (tblnext "layer" (null lay))) (setq lst (cons (cdr (assoc 2 lay)) lst)) ) lst ) I think I have another couple of versions Both return the layers as a list, so can do maybe a while or, foreach loops Obviously (...) for your selection set you need to change the ' for list so that the layer name will be evaluated in the filter (setq --LayerName-- "0") ; set layer name as 0 as an example (setq sel1 (ssget "x" (list (cons 8 --LAYERNAME-- )))) Does that help? 1 Quote
BIGAL Posted February 22 Posted February 22 (edited) A variation of Steven P code (defun myblkbylayer( / lyr lname) (setvar 'ctab "Model") (vlax-for lyr (vla-get-layers (vla-get-activedocument (vlax-get-acad-object) ) ) (setq lname (vla-get-name lyr)) (setq ss (ssget "X" (list (cons 8 lname)))) (command "-block" lname "0,0,0" ss "") ) (princ) ) (myblkbylayer) I would add lee-mac get bounding box multiple objects that way can make insert point lower left of objects. Edited February 22 by BIGAL 1 Quote
jessicavergas11 Posted February 22 Posted February 22 (edited) To create a list of layer names in the active drawing and loop through them to create blocks for each layer, you can use the following approach in Intellicad:```lisp (defun create-blocks-for-layers () (setq doc (vla-get-ActiveDocument (vlax-get-acad-object))) (setq layers (vla-get-layers doc)) (setq blk_name_prefix "BLOCK_") ; Change this prefix as needed (setq blk_counter 1) (vlax-for layer layers (setq layer_name (vla-get-name layer)) (if (/= layer_name "0") ; Exclude layer 0 (progn (setq sel (ssget "x" (list '(8 . layer_name)))) (if sel (progn (setq blk_name (strcat blk_name_prefix (itoa blk_counter))) (command "_block" blk_name "0,0,0" sel "") (setq blk_counter (1+ blk_counter)) ) ) ) ) ) (princ "Blocks created for all layers except layer 0.") ) (create-blocks-for-layers) ``` This function `create-blocks-for-layers` retrieves all the layer names in the active drawing and then loops through each layer. For each layer (excluding layer 0), it creates a selection set of entities on that layer and uses the `command` function to create a block with a unique name for each layer. Please make sure to adjust the `blk_name_prefix` variable according to your naming convention for the blocks. This code should work in Intellicad, but please test it thoroughly in your environment to ensure it meets your requirements. Edited February 22 by SLW210 Added Code Tags! Quote
SLW210 Posted February 22 Posted February 22 Please use Code Tags in the future. (<> in the editor toolbar) Quote
Steven P Posted February 22 Posted February 22 (edited) 4 hours ago, jessicavergas11 said: (setq sel (ssget "x" (list '(8 . layer_name)))) This should be (setq sel (ssget "x" (list (cons 8 layer_name)))) and in (setq blk_name (strcat blk_name_prefix (itoa blk_counter))) the OP wants the layer name: (setq blk_name (strcat blk_name_prefix layer_name (itoa blk_counter))) or (setq blk_name layer_name) Edited February 22 by Steven P Quote
sln8458 Posted February 23 Author Posted February 23 (edited) Thanks for the code jessicavergas11 I have modified it a little to suit my needs. Also thanks to StevenP for his pointers. This is what I have atm: (defun create-blocks-for-layers (/ doc layers blk_name_prefix layer_name sel blk_name ) (setq doc (vla-get-ActiveDocument (vlax-get-acad-object))) (setq layers (vla-get-layers doc)) (setq blk_name_prefix "Pipe_") ; Change this prefix as needed ; (setq blk_counter 1) (vlax-for layer layers (setq layer_name (vla-get-name layer)) (if (/= layer_name "0") ; Exclude layer 0 (progn (setq sel (ssget "x" (list (cons 8 layer_name)))) (if sel (progn (setq blk_name (strcat blk_name_prefix layer_name )) ;(itoa blk_counter) (command "_block" blk_name "0,0,0" sel "") ; (setq blk_counter (1+ blk_counter)) ); end of PROGN ); end of IF ) ; end of PROGN ); end of IF ) (princ "Blocks created for all layers except layer 0.") ) I kept the 'prefix' as this is handy, but removed the counter (though retained the code for future) note, a couple of the lines are indented incorectly above, not why this is but it's not how it is shown in NP++ SteveN Edited February 23 by sln8458 no preview post option! Quote
sln8458 Posted February 26 Author Posted February 26 (edited) Just to give a final update. The code above caused Intellicad to crash frequently while testing. In the end I had to give up using it, I believe the problem stems from the VL code though I can't be certain. I then did a bit of research on 'foreach' and have settled on this: (defun create-blocks-for-layers (/ lay lst blk_name_prefix filepath layer_name sel blk_name ) ; (while (setq lay (tblnext "layer" (null lay))) (setq lst (cons (cdr (assoc 2 lay)) lst)) ) (setq blk_name_prefix "Pipe_") ; Change this prefix as needed (setq filepath (getvar "dwgprefix")) ; (foreach layer_name lst (if (/= layer_name "0") ; Exclude layer 0 (progn (setq sel (ssget "x" (list (cons 8 layer_name)))) (if sel (progn (setq blk_name (strcat blk_name_prefix layer_name )) (command "._CLAYER" layer_name) (command "_-Wblock" (strcat filepath blk_name) blk_name "" "0,0,0" sel "") (command "_Xref" "A" (strcat filepath blk_name ".dwg") "0,0,0" "1" "1" "0") ); end of PROGN ); end of IF ) ; end of PROGN ); end of IF ); end foreach ; (princ "\n ") (princ "Blocks created for all layers except layer 0.") ) As you can see I have also changed from creating blocks to Wblocks & xrefs. Just made a coffee while the lsp was run on the first real drawing, 80 layers converted into dwg's & xref in before I'd made the coffee Edited February 26 by sln8458 I can't spell !!!! 1 Quote
Steven P Posted February 26 Posted February 26 1 hour ago, sln8458 said: Just made a coffee while the lsp was run on the first real drawing, 80 layers converted into dwg's & xref in before I'd made the coffee which is exactly why we do this!! 1 Quote
Recommended Posts
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.