Community
AutoCAD Forum
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Script to change layers proprieties

35 REPLIES 35
SOLVED
Reply
Message 1 of 36
nmareis
9828 Views, 35 Replies

Script to change layers proprieties

hello people...

 

i need a script that changes all layers of the drawing color and lineweight....and i need that the script  that changes color and lineweight of a specific layers too...

example:

 

---layers of the base of the drawing---

 

layer 0 - color 1 - lineweight 0.1

layer 1 - color 1 - lineweight 0.1

layer 2 - color 1 - lineweight 0.1

layer 3 - color 1 - lineweight 0.1

layer 4 - color 1 - lineweight 0.1

layer 5 - color 1 - lineweight 0.1

layer 6 - color 1 - lineweight 0.1

layer 7 - color 1 - lineweight 0.1

 

----layers that i am using-----

 

layer 8 - color 3 - lineweight 2

layer 9 - color 6 - lineweight 2

layer 10 - color 4 - lineweight 2

 

i need a script that change all layers of drawing ( are what they are )...next the script changes the layers that i am working....

 

thanks

35 REPLIES 35
Message 2 of 36
Alfred.NESWADBA
in reply to: nmareis

Hi,

 

start command _-LAYER (with the underline and the dash) and see the options to use, type in the option, layername, color, linetype ... whatever you are asked and what you want to do. All you typed can be written down 1:1 in an ascii file that is then your script.

 

You should also know that there exit wild-char like "*" that makes it possible to select more layernames with one step, as a sample for changing all layers to color red you can use:

_-LAYER<ENTER>

_COLOR<ENTER>

10<ENTER>

*<ENTER>

<ENTER>

 

Good luck, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 36
Kent1Cooper
in reply to: nmareis

More on what Alfred said....

 

I would suggest also using the period prefix to the Layer command name, to force it to use the native command instead of any redefinition that may have been made [such as by an overlay program]:  _.-LAYER

[The three preceding _.- characters can be used in certain other sequences.  I think some of the possible sequences don't work -- I'm not sure which -- but I know that sequence works.]

 

You can also apply the same option to more than one Layer at a time [but not all if you don't want all], by using Layer names separated by commas.  And you can use multiple options, and multiple applications of the same option, within one Layer command.

 

_.-LAYER

_color 47 LayerA,LayerB,LayerD,LayerH

_color 246 LayerC,LayerF,LayerQ

_lweight 1.5 LayerA,LayerC,LayerM

... etc. ...

 

The blank line [Alfred's <Enter>] at the end of the .scr file (or prior to invoking the next command, if any) tells the Layer command you're finished applying options.

 

If your Layer names may include spaces, I think you need to wrap them [or comma-delimited lists of them] in double-quotes -- in your example:

 

_.-LAYER

_color 1 "layer 0,layer 1,layer 2,layer 3,layer 4,layer 5,layer 6,layer 7"

....

 

Otherwise, because of the space, I believe it will try to apply color 1 to a Layer called just layer, and presumably won't find one [or if there is one, you may not want color 1 assigned to it].

Kent Cooper, AIA
Message 4 of 36
Bratz2
in reply to: Kent1Cooper

do these commands all have to be on a single line?

 

 

Example:

 

.-layer

_color 47 S-E-ROAD-HTH-DEMO,S-E-ROAD-HTH,S-E-BANK-DEMO

 

or will

 

.-layer

_color 47 S-E-ROAD-HTH-DEMO,

S-E-ROAD-HTH,S-E-BANK-DEMO,S-E-CURB-DEMO

 

work as well? And, what about linetypes, what happens if you're calling one out and it's not in the DWG?

 

Thanks

 

Message 5 of 36
Kent1Cooper
in reply to: Bratz2


@Bratz2 wrote:

do these commands all have to be on a single line?

... will

 

.-layer

_color 47 S-E-ROAD-HTH-DEMO,

S-E-ROAD-HTH,S-E-BANK-DEMO,S-E-CURB-DEMO

 

work...? And, what about linetypes, what happens if you're calling one out and it's not in the DWG?

.... 


That comma-delimited line of Layer names would have to be on one line in a Script.  If not, the hard return between portions is an Enter, and the comma just before it won't help you [a string of Layer names like that is allowed to end with a comma].  It could be done this way, though, if it shortens the line enough for you:

 

.-layer _color 47

S-E-ROAD-HTH-DEMO,S-E-ROAD-HTH,S-E-BANK-DEMO,S-E-CURB-DEMO

 

Or, you could assign that color to two sets of Layers separately:

 

.-layer

_color 47 S-E-ROAD-HTH-DEMO,S-E-ROAD-HTH

_color 47 S-E-BANK-DEMO,S-E-CURB-DEMO

 

[though I admit to wondering what makes the line length so critical].

 

BUT there's good news on the other question.  One terrific thing about using a command-line Layer command like this, rather than the dialog-box-based Layer Manager, is that even if a linetype is not yet loaded in the drawing, if it's in the acad.lin file, it will find it.  [This is also true for assigning an override linetype to objects, in a CHPROP command or the Properties option in a CHANGE command.]  So for those linetypes, at least, no, you don't have to worry about loading them first.  I'm not positive about linetypes in other files, even if they're in a Support File Search Path location, but it may be able to find those, too.  [I just put them all into acad.lin, after making a copy of the original under an extended name such as acadORIGINAL.lin, just in case.]

Kent Cooper, AIA
Message 6 of 36
Bratz2
in reply to: Kent1Cooper

I suppose the single line length isn't an issue I guess as long as I set word wrap in my textpad 🙂 I was hoping to just copy and paste a series of layer names and not have to worry about hard returns, etc. There's about 458 layers (don't ask me why, because I couldn't tell you) that would need to be changed over to various colors/linetypes and I thought scripting might be the way to go. It's easy "one click" stuff to run a script.

Here they want you to start a new drawing using the 

 

SURVEY-ENG.DWT

 

Then go and insert your survey drawing so it take on the color and linetype of the DWT, explode it save it purge it...... It just seems a bit excessive to do it this way. 

Message 7 of 36
mc5fan
in reply to: Alfred.NESWADBA

This code works fine except my layers with # in the layer name. Is the # being interpreted as a something else? Quotes do not help.

Lee M.
Message 8 of 36
Kent1Cooper
in reply to: mc5fan


@mc5fan wrote:

This code works fine except my layers with # in the layer name. Is the # being interpreted as a something else? Quotes do not help.


Yes -- it's the wildcard for any numerical character.  For example, if you have Layers called abc1, abc2 & abc3, you can do this to assign Cyan color to all of them:

_.-Layer _color 4 abc#

 

Read about the (wcmatch) AutoLisp function for other wild-card characters that it's best to avoid in things like Layer names.

 

If you can wangle some code that can insert a reverse apostrophe character [ ` ] before the # to feed to the Layer command, that's the wildcard escape character that means "read the next character literally, not as a wildcard."

Kent Cooper, AIA
Message 9 of 36
mc5fan
in reply to: Kent1Cooper

Here is what is being generated as layers. The # in the name is an established layer standard:


-layer M #8_AWG_GREEN C 3 #8_AWG_GREEN L CONTINUOUS #8_AWG_GREEN LW DEFAULT #8_AWG_GREEN D Mylayer #8_AWG_GREEN plot plot #8_AWG_GREEN
-layer M #10_AWG_GREEN C 3 #10_AWG_GREEN L CONTINUOUS #10_AWG_GREEN LW DEFAULT #10_AWG_GREEN D Mylayer #10_AWG_GREEN plot plot #10_AWG_GREEN
-layer M #16_AWG_GREEN C 3 #16_AWG_GREEN L CONTINUOUS #16_AWG_GREEN LW DEFAULT #16_AWG_GREEN D Mylayer #16_AWG_GREEN plot plot #16_AWG_GREEN


These 3 layers come out WHITE instead of the color 3 (green) as indicated. The other layers that do not contain the # come out as intended:

-layer M PSYMS C WHITE PSYMS L CONTINUOUS PSYMS LW DEFAULT PSYMS D Mylayer PSYMS plot plot PSYMS
-layer M SYMS C WHITE SYMS L CONTINUOUS SYMS LW DEFAULT SYMS D Mylayer SYMS plot plot SYMS
-layer M MISC C WHITE MISC L CONTINUOUS MISC LW DEFAULT MISC D Mylayer MISC plot plot MISC


This was the reason to try to change the layer colors after being generated originally.


Lee M.

Lee M.
Message 10 of 36
mc5fan
in reply to: mc5fan

I added the character in front of every # but no difference. Those layers all come out WHITE instead of GREEN.

 

Lee M.

Lee M.
Message 11 of 36
Kent1Cooper
in reply to: mc5fan


@mc5fan wrote:
Here is what is being generated as layers. The # in the name is an established layer standard:

-layer M #8_AWG_GREEN C 3 #8_AWG_GREEN L CONTINUOUS #8_AWG_GREEN LW DEFAULT #8_AWG_GREEN D Mylayer #8_AWG_GREEN plot plot #8_AWG_GREEN
....

The simplest thing would be to change the established Layer standard....

 

But since those are using the Make option in the Layer command, the Layer that is made becomes current, and therefore becomes the default  for assigning color and linetype and so on.  Enter accepts the default <current> Layer name for such assignments, so the name doesn't need to be spelled out again.  You can put double spaces after the color number and so on, the first to "register" the value and the second to accept the current Layer as the one to assign it to.  The one for which you do  need to spell out the Layer name is the Description, since the default for that is, for some reason, all  Layers <*>.

 

But you can also shorten these things.  Continuous is the default linetype for new Layers, and "Default" is [as one might expect] the default lineweight, and Plotting is the default option, so you don't need to assign those.  I think you could do it this way for each Layer, for all except the Description part:

 

-layer M #8_AWG_GREEN C 3  

 

There are three  spaces after that 3, the last one concluding the Layer command.  In a command macro, you could use semicolons to make it clearer -- if yours is a SCRIPT, hard returns to new lines will do.

 

But if the escape ` preceding the # character doesn't work [it did for me manually (not in a Script) trying Layer names in which # was not the first  character -- maybe that's the difference], I don't have any better ideas that will let you put a Description on one Layer like that.  [If I think of something, I'll write back.]

Kent Cooper, AIA
Message 12 of 36
mc5fan
in reply to: Kent1Cooper

Thanks for your input. The layers still come out to be created WHITE and "by layer."

 

Lee

Lee M.
Message 13 of 36
mc5fan
in reply to: Kent1Cooper

I am not in a position to change standards or I would. A script to change the layer colors does not work either. Apparently # in the layer name is mucking it up. Any further suggestions??

 

 

Lee

Lee M.
Message 14 of 36
mc5fan
in reply to: mc5fan

I was also wondering why the different styles of script lines. What advantage is there to adding the underscore and period?

 

_.ZOOM _ALL _.QSAVE

 

ZOOM ALL QSAVE

Lee M.
Message 15 of 36
Alfred.NESWADBA
in reply to: mc5fan

Hi,

 

>> What advantage is there to adding the underscore and period?

A underscore makes sure that the English command-name works in all languages of AutoCAD, _LINE works also in a German version of AutoCAD, without the underscore the German AutoCAD would result in a unknown command message

A point in front of a command names makes sure the "original built in command" is called. In AutoCAD application developers can overwrite commands, so when you start _LINE an application can hook into that command and does something different/more/other/... To make sure the script runs the orignal command and not from any plugins the point is used for that.

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 16 of 36
Kent1Cooper
in reply to: mc5fan


@mc5fan wrote:

Thanks for your input. The layers still come out to be created WHITE and "by layer."


It worked for me to when I put it in a command macro, called by a Tool Palette button.  If yours is a Script  file, I think you would have to use hard returns instead of the spaces at the end, because Layer names can have spaces in them, even at the beginning, so the 2nd & 3rd spaces after that 3 could be taken as the beginning of the entry of some Layer name(s), left uncompleted at the end, so the color isn't applied and the command is not concluded.

 

Explain more about what you mean by layers coming out "by layer" -- that's a property of objects, but not of Layers.

Kent Cooper, AIA
Message 17 of 36
Kent1Cooper
in reply to: mc5fan


@mc5fan wrote:

.... Apparently # in the layer name is mucking it up. Any further suggestions??


(entmake)

 

You can find examples of using (entmake) to make Layers with some Searching in these Forums, more likely over in the Customization Forum than here.

Kent Cooper, AIA
Message 18 of 36
mc5fan
in reply to: Alfred.NESWADBA

Thank you for your explanation Alfred! I suppose that I should make it a practice to use this method from now on.

 

Lee M.

Lee M.
Message 19 of 36
dbroad
in reply to: mc5fan

You probably used the wrong escape character.  Use `, not '.  Scripts should use returns for at least some of the entries also, such as the description which allows for spaces.

 

@Kent1Cooper: I thought the default would be the current layer for everything but there are several issues:

1.  The description will default to all layers for some strange reason.

2.  The current name will be used as a search string for the command name and unless the layer name is entered with the escape character, it won't find any layers that match.  I think that is a dumb situation but there you are.

 

I am attaching a script file for the first layer name that works here. Rename the extension of the script file to get it to work. I did not include _ or . characters.

Architect, Registered NC, VA, SC, & GA.
Message 20 of 36
mc5fan
in reply to: dbroad

debroad,

 

YES, I did use the wrong character. That IS the ticket! Thank you, I had no idea.

 

 

Lee M.

Lee M.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost