Autolisp layer group

Autolisp layer group

larsTB09
Contributor Contributor
1,833 Views
10 Replies
Message 1 of 11

Autolisp layer group

larsTB09
Contributor
Contributor

Hello all, 

 

I need some help to create a autolisp command that changes the color of the layers in a certain layer group?

 

Thanks by advance?

0 Likes
Accepted solutions (1)
1,834 Views
10 Replies
Replies (10)
Message 2 of 11

ВeekeeCZ
Consultant
Consultant

Do you mean a Layer Filter Group, or just list of layers...

 

The most simple way is:

 

(defun c:LayersToRed ()
  (command "_.layer" "_Color" 1 "Layer1,Layer2" "")  
  (princ)
)

List of layers delimited by a comma. Or a wildcard can be used too: Layer*

0 Likes
Message 3 of 11

Anonymous
Not applicable
(defun crl (layname laycol)
  (entmake (list
	     (cons 0 "layer")
	     (cons 100 "AcDbSymbolTableRecord")
	     (cons 100 "AcDbLayerTableRecord")
	     (cons 2 layname)
	     (cons 70 0)
	     (cons 62 laycol)
	     (cons 6 "Continuous")
	   )
	   )
)

(defun c:my_layers ()
      (setq lay_cba (list
		       (list "Lay1" 1)
		       (list "Lay2" 2)
		       (list "Lay3" 3)
		       (list "Lay5" 4)
		       (list "Lay5" 5)
		       (list "Lay6" 6)
		       (list "Lay7" 7)	
		       )
      )
      (foreach n lay_cba
	(crl (car n) (cadr n))
      )
  (princ)
)

I use this.

0 Likes
Message 4 of 11

Kent1Cooper
Consultant
Consultant

@larsTB09 wrote:

....I need some help to create a autolisp command that changes the color of the layers in a certain layer group? ....


If a "layer group" is a collection of Layers with related names, you can handle them collectively, with wildcards.  For example, if you want to make all Layers whose names start with ABC [say, ABC1, ABC2, ABC3, ABCwhatever, ABCetc], you can do:

(command "_.layer" "_color" yourColor "ABC*" "")

Kent Cooper, AIA
0 Likes
Message 5 of 11

larsTB09
Contributor
Contributor

Is it possible to change all layers to color 253 extept for the one i asked in the getSting?

 

(defun c:TEST ()
(setq EXTEPT(getstring  "\nyour prompt:"))
  (command "_.layer" "_Color" 253 EXTEPT)  
  (princ)
)
0 Likes
Message 6 of 11

_gile
Consultant
Consultant
Accepted solution

Here's a way using tblnext to get all layers and entmod change the layer color.

The routine also uses wcmatch to check which layer(s) must not be changed so that you can enter more than one layer names separated by commas and/or use wild-card patterns.

 

(defun c:TEST (/ except layer name)
  (setq except (strcase (getstring "\nyour prompt: ")))
;; iterate through all layers (while (setq layer (tblnext "layer" (not layer))) (setq name (cdr (assoc 2 layer)))
;; check if the layer name matches with the exception (if (not (wcmatch (strcase (cdr (assoc 2 layer))) extept))
;; change the layer color (progn (setq layer (entget (tblobjname "layer" name))) (entmod (subst '(62 . 253) (assoc 62 layer) layer)) ) ) ) (princ) )

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 11

ВeekeeCZ
Consultant
Consultant

Well, if it is the only ONE exception, then its easy, use a tilde (~) prefix.

 

(defun c:TEST ( / except)
  (setq except (getstring T "\nChange all layers to gray, but <all>: "))
  (command "_.layer" "_Color" 253 (strcat "~" except) "")
  (princ)
)

 

You may add T to the (getstring T "Prompt") command to allow to type the layer name with spaces.

 

Edit: You can also use _giles routine if you need the exception for more layers -- just fix the misspell: 

(if (not (wcmatch (strcase (cdr (assoc 2 layer))) except))

 

0 Likes
Message 8 of 11

larsTB09
Contributor
Contributor

Hi great! thanks but i have only one more problem. the layer's with the hatches doesn't change?

 

0 Likes
Message 9 of 11

_gile
Consultant
Consultant

@larsTB09 wrote:

Hi great! thanks but i have only one more problem. the layer's with the hatches doesn't change?

 


Is it the layer color which doesn't change or the Hatch color which is different from ByLayer?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 11

larsTB09
Contributor
Contributor

No it’s by later but it happens with all the hatches

 

Nevermind it works 🙂

0 Likes
Message 11 of 11

_gile
Consultant
Consultant

Nice.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes