Jump to content

how to increment through layer names & create block name


sln8458

Recommended Posts

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 by sln8458
Link to comment
Share on other sites

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?

 

 

  • Agree 1
Link to comment
Share on other sites

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 by BIGAL
  • Like 1
Link to comment
Share on other sites

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 by SLW210
Added Code Tags!
Link to comment
Share on other sites

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 by Steven P
Link to comment
Share on other sites

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 by sln8458
no preview post option!
Link to comment
Share on other sites

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 by sln8458
I can't spell !!!!
  • Like 1
Link to comment
Share on other sites

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!!

  • Like 1
Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...