sln8458 Posted February 21, 2024 Posted February 21, 2024 (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, 2024 by sln8458 Quote
Steven P Posted February 21, 2024 Posted February 21, 2024 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, 2024 Posted February 22, 2024 (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, 2024 by BIGAL 1 Quote
sln8458 Posted February 22, 2024 Author Posted February 22, 2024 Thanks guys, will play with these today 1 Quote
SLW210 Posted February 22, 2024 Posted February 22, 2024 Please use Code Tags in the future. (<> in the editor toolbar) Quote
Steven P Posted February 22, 2024 Posted February 22, 2024 (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, 2024 by Steven P Quote
sln8458 Posted February 23, 2024 Author Posted February 23, 2024 (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, 2024 by sln8458 no preview post option! Quote
sln8458 Posted February 26, 2024 Author Posted February 26, 2024 (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, 2024 by sln8458 I can't spell !!!! 1 Quote
Steven P Posted February 26, 2024 Posted February 26, 2024 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.