Change layers colour with lisp

Change layers colour with lisp

ariskouros
Participant Participant
830 Views
13 Replies
Message 1 of 14

Change layers colour with lisp

ariskouros
Participant
Participant

Hi. I'm searching a lisp to change layers colour without selection.

I have layer 1 to colour red,layer 2 to yellow,layer 3 to green....

 

0 Likes
Accepted solutions (2)
831 Views
13 Replies
Replies (13)
Message 2 of 14

paullimapa
Mentor
Mentor

Please clarify. You have a drawing that has layers named matching the color number like 1, 2, 3 .... all the way to 255 and you would like a lisp routine to cycle through all 255 layers assigning the color number to match?

Or do you have a drawing with no Layer names but you would like a lisp routine to create layers naming them to match the color from 1 to 255?

Or you have a drawing with layer names with numbers that range from 1 to 255 and you only want those layers to be assigned colors to match the name?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 3 of 14

ariskouros
Participant
Participant

I have drawing that I import every time 10 same layer with same name and colour.

And I want lisp to change this each layer colour from 1-255

0 Likes
Message 4 of 14

paullimapa
Mentor
Mentor

I’m still a bit confused with what layers are actually in your drawing.  From your reply I’m reading that you always have ten layers that have names you want to change their colors to match with name?   If so what are these ten layer names that are always the same?  Perhaps post a sample drawing with these ten layer names and that would really help.  


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 14

ariskouros
Participant
Participant

Water_pipe import white (to be red) 

Electric_cable import white (to be yellow)

Phone_cable import white (to be green)

Road_sing import white (to be colour 23)

0 Likes
Message 6 of 14

hak_vz
Advisor
Advisor

@ariskouros wrote:

Hi. I'm searching a lisp to change layers colour without selection.

I have layer 1 to colour red,layer 2 to yellow,layer 3 to green....

 


Maybe something like this. You should edit layercolors list values with proper layer colors.

("layername" .  "layercolor") , this works only for indexed colors.

 

Add function to startup suite and use it after you load your dwgs.

 

(defun c:layerColor ( / adoc layercolors name col)
	(setq layercolors
		'(
			("0" . 15)
			("1" . 25)
			("2" . 35)
			("3" . 45)
			("4" . 55)
		)
	)
	(setq adoc (vla-get-ActiveDocument (vlax-get-acad-object)))
	(vlax-for lay (vla-get-Layers adoc)
		(setq name (vlax-get lay "Name"))
		(setq col (cdr(assoc name layercolors)))
		(vla-put-Color lay col)
		(vlax-release-object lay)
	)
	(vlax-release-object adoc)
	(princ)	
)

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 7 of 14

Kent1Cooper
Consultant
Consultant
Accepted solution

Something like this?

(defun C:LCU () ; = Layer Color Update
  (foreach item '(("Water_pipe" 1) ("Electric_cable" 2) ("Phone_cable" 3) ("Road_sing" 23))
    (if (tblsearch "layer" (car item))
      (command "_.layer" "_color" (cadr item) (car item) "")
    )
  )
  (princ)
)

But should the last one be "Road_sign"?

Kent Cooper, AIA
Message 8 of 14

paullimapa
Mentor
Mentor

Try this (not tested):

(defun c:test ()

(command "_.layer" "_color" “1” “Water_pipe” “_color” “2” “Electric _cable” “_color” “3” “Phone_cable” “_color” “23” “Road_sing” "")

)


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 9 of 14

ariskouros
Participant
Participant

 It's exactly what I wanted. Τhank you very much. You saved me a lot of hours @Kent1Cooper 

0 Likes
Message 10 of 14

ariskouros
Participant
Participant

Thank's for your time!!! @Kent1Cooper @hak_vz 

0 Likes
Message 11 of 14

Kent1Cooper
Consultant
Consultant

@paullimapa wrote:

.... "_.layer" "_color" “1” “Water_pipe” “_color” “2” “Electric _cable” ....


That will also work, if you choose, with integers rather than text strings for the color numbers:

.... "_.layer" "_color" 1 “Water_pipe” “_color” 2 “Electric _cable” ....

Or, you can use text strings as color names for the first 7 colors, if you like:

.... "_.layer" "_color" "red" “Water_pipe” “_color” "yellow" “Electric _cable” ....

Kent Cooper, AIA
Message 12 of 14

ariskouros
Participant
Participant

@Kent1Cooperand if i want to change also the lineweight how dose that go?

0 Likes
Message 13 of 14

Kent1Cooper
Consultant
Consultant
Accepted solution

@ariskouros wrote:

@Kent1Cooperand if i want to change also the lineweight how dose that go?


Add a lineweight entry into each sublist [I put in random numbers -- change to your real ones], and pull those with (caddr) for the third item, in a LWeight option in the Layer command.  You can do as many options within one Layer command as you want -- do it manually with the hyphen-prefixed -LAYER command to see what the correct option names are and the sequence of inputs.  Read about (car) and related AutoLisp functions -- if you get too many option entries in the sublists for those functions, you may need to switch to using (nth) to pull them.  But for this many, something like this [untested]:

(defun C:LCLU () ; = Layer Color & Lineweight Update
  (foreach item '(("Water_pipe" 1 0.1) ("Electric_cable" 2 0.2) ("Phone_cable" 3 0.3) ("Road_sing" 23 0.4))
    (if (tblsearch "layer" (car item))
      (command "_.layer" "_color" (cadr item) (car item) "_lweight" (caddr item) (car item) "")
    )
  )
  (princ)
)

 

Kent Cooper, AIA
Message 14 of 14

ariskouros
Participant
Participant

You are the best @Kent1Cooper 

0 Likes