How to change layer colour IF layer name CONTAINS

How to change layer colour IF layer name CONTAINS

agedzevicius
Contributor Contributor
1,043 Views
8 Replies
Message 1 of 9

How to change layer colour IF layer name CONTAINS

agedzevicius
Contributor
Contributor

Hi folks, first time trying to write a script myself, and almost certain it's possible, because i used something similar before.

What i'm looking ot achieve is:

I have a drawing with many different layers.
I want to run a script that will:

Apply colour 220 to all layers that CONTAIN the phrase/letters  "-CA(HQ)-"
(Then repeat same command, to apply a different colour to all layers containing "-CA(INS)-".)

Not sure where to start, but some help writing this would be very appreciated.
I'll pay it forward.

0 Likes
Accepted solutions (1)
1,044 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant
Accepted solution

@agedzevicius wrote:

....

Apply colour 220 to all layers that CONTAIN the phrase/letters  "-CA(HQ)-"
(Then repeat same command, to apply a different colour to all layers containing "-CA(INS)-".)
....


Wildcards can be used to include multiple Layer names under a given option such as a color assignment -- see >this<.  Each * below stands for any other content [including nothing].  If you're talking about a Script as AutoCAD means the word:

 

-LAYER
Color

220

*-CA(HQ)-*

Color

{yourDifferentColor}

*-CA(INS)-*

{empty line for final Enter to end Layer command}

 

The same can be done in AutoLisp or command macro format [I never know what people mean by the word "script," but we see a lot of people using the word when they really mean an AutoLisp routine or a macro].

Kent Cooper, AIA
0 Likes
Message 3 of 9

agedzevicius
Contributor
Contributor

 

The same can be done in AutoLisp or command macro format [I never know what people mean by the word "script," but we see a lot of people using the word when they really mean an AutoLisp routine or a macro].


Apologies, just used to using scripts in terms of groups of commands to be carried out.

I'll bookmark the page for future reference, very curious to try it out. Will get back to you with results.
Thanks Kent 👍


0 Likes
Message 4 of 9

komondormrex
Mentor
Mentor

what is right with you, Kent?

script partly or entirely just mimics your manual inputting commands and their modifiers/options in command line. 

0 Likes
Message 5 of 9

Kent1Cooper
Consultant
Consultant

@komondormrex wrote:

what is right with you, Kent?

script partly or entirely just mimics your manual inputting commands and their modifiers/options in command line. 


Any of the possible approaches can be "right" -- they just need to pick one.  I wrote something that should [untested] work in a SCRIPT as AutoCAD uses the word [specific file type, specific command to invoke it], but the OP's wording "groups of commands to be carried out" can also be done by an AutoLisp routine [presumably defined into a custom command name] or a command macro [with certain limitations, though those should not interfere in this instance], with some similarities but also meaningful differences in format & syntax.

 

As much as anything else, the "right" way may depend on how they want to invoke whatever it is.  An AutoLisp command definition would mean they could type in the command name.  A Tool Palette button could contain a command macro without a defined command, or could contain just the name of a defined command.  A Script would require use of a .scr file and the SCRIPT command to run it, but that could also be contained in a Tool Palette button command macro or even in an AutoLisp command definition.  A Script file can just run an AutoLisp-defined command name, rather than contain the commands itself.  I haven't used the action recorder, but that's another way, and there's assignment to a keystroke combination, and....

Kent Cooper, AIA
0 Likes
Message 6 of 9

agedzevicius
Contributor
Contributor

When i start the command group with -layer, it only changes the colour of the current layer.
Is there a way to apply this command to ALL layers?

After some messing about, my colleague figured out that by using below will work for all layers.
(Learning process for me to understand that " is treated as list.)

Below works for what i need, how would i go about turning this into a loadable script? @Kent1Cooper 

-LAYER
C
220
"*CA(*"
[enter]
[enter]

0 Likes
Message 7 of 9

komondormrex
Mentor
Mentor

hey,

 

 

 

(defun c:change_layer_color_220 ()
	(vlax-map-collection (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
		'(lambda (layer)
			(if (wcmatch (vla-get-name layer) "*-CA(HQ)-*")
				(vla-put-color layer 220)
			)
		)
	)
	(princ)
)
0 Likes
Message 8 of 9

Kent1Cooper
Consultant
Consultant

@agedzevicius wrote:

.... by using below will work for all layers.
(Learning process for me to understand that " is treated as list.)

Below works for what i need, how would i go about turning this into a loadable script? .... 

-LAYER
C
220
"*CA(*"
[enter]
[enter]


Something is off here.  In a Script, at least, you should not need those double-quotes, which define the bounds of a text string, not of a list.  If you were doing this in an AutoLisp function where list is an appropriate term [but would not be used here], yes, those quotes would be needed, but they should not be in a Script or a command macro.

 

You can turn it into a "loadable script" in different ways, depending again on what you mean by the word.  If you really mean SCRIPT in AutoCAD's usage, put that text [it should work without the quotes], with a blank line in place of the first [enter] (the second is extraneous), into a plain-text editor such as Notepad, and save it to a file with a .scr filetype ending.  Use the SCRIPT command to run it.

 

But I used the word "run" there.  Your word "loadable" makes me wonder.  "Load" is something one does with AutoLisp routines.  That could be like this:

 

(defun C:YourCommandName ()

  (command "_.layer" "_color" 220 "*CA(*" "")

  (prin1)

)

 

Save that [with a command name that means something to you substituted], similarly in a plain-text editor, to a file with a .lsp filetype ending, and use APPLOAD to Load it, then type in the YourCommandName to use it.

 

You could also do it in a command macro built into a Tool Palette button, such as [untested]:

^C^C_.-LAYER _color 220 *CA(* ;

Kent Cooper, AIA
0 Likes
Message 9 of 9

agedzevicius
Contributor
Contributor

Folks, below worked for what i needed.


-LAYER
C
220
"*CA(*"


Pasted into notepad and saved as .scr file, and ran it with no issues.


0 Likes