AutoCAD Architecture Customization
Welcome to Autodesk’s AutoCAD Architecture Customization Forums. Share your knowledge, ask questions, and explore popular AutoCAD Architecture Customization topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

change the layers Colors by the name of the layers

15 REPLIES 15
Reply
Message 1 of 16
Anonymous
2330 Views, 15 Replies

change the layers Colors by the name of the layers

Hi All

Well I´m trying to get a simpel lisp to change the layers Colors by the name of the layers

like.:

 

Color 8 if name is Work

Color 5 if name is Tech

 

any have some input to make a lisp that work like this?

15 REPLIES 15
Message 2 of 16
David_W_Koch
in reply to: Anonymous

Do you just have one Work layer and one Tech layer?  If so, you could just run the LAYER command via a script or AutoLISP and set the colors.

 

If, instead, you want all layers that start with Work to be one color and all layers that start with Tech to be another color, use the tblnext AutoLISP function to iterate over the LAYER table.  Get the name of the layer you are currently processing from the 2 group.  Use WCMATCH to see if the layer matches any of your target strings; if it does match, change the 62 group value to the desired color and update the layer definition.


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

Message 3 of 16
Anonymous
in reply to: David_W_Koch

Should have told i´m still learning..

What i´m looking for is a lisp to change the color of the layer to a color told in the lisp.. the user may not pick it.
can you help with makeing it?

I have a lisp to make all layers the same color, but i can´t get it to make different layers different colors.

Message 4 of 16
David_W_Koch
in reply to: Anonymous

@Anonymous 

 

Here is something I put together quickly.  Assuming that you do not want to create layers, just set your desired layer color on layers that already exist in the file, this should do what you want.

(defun C:SETLC (			; No arguments.
		/
		icount			; List counter [integer].
		ilngth			; Length of layer/color list [integer].
		ilyrc			; Color number to be processed [integer].
		ldata			; Dotted pair to be processed [list]
		llyrs			; List of dotted pairs of layer names and color numbers [list].
		slyrn			; Layer name to be processed [string].
		)
  ;; Edit below to add your layer names and associated color numbers, maintaining the format given.
  ;; Layer names are quoted strings.
  ;; Color numbers are integers.
  (setq	llyrs
	 (list
	   '("0" . 218)
	   '("1" . 1)
	   '("2" . 2)
	   '("3" . 3)
	   '("4" . 4)
	   '("5" . 5)
	   '("6" . 6)
	   '("7" . 7)
	   '("8" . 8)
	   '("9" . 9)
	 ) ;_ End list.
	icount	0			; Initialize counter.
	ilngth	(length llyrs)		; Number of layers in list.
  ) ;_ End setq.
  (while (< icount ilngth)
    (setq ldata	(nth icount llyrs)	; Get data item for this pass.
	  slyrn (car ldata)		; Layer name to process.
	  ilyrc (cdr ldata)		; Color of layer to be processed.
    ) ;_ End setq.
    (if	(tblsearch "LAYER" slyrn)	; If layer exists...
      (command "_.LAYER" "_C" ilyrc slyrn "") ; ... set layer color.
    ) ;_ End if.
    (setq icount (1+ icount))		; Increment loop counter.
  ) ;_ End while.
  (prompt "\nDone.  Colors of layers found have been set to standard. ")
  (prin1)
) ;_ End C:SETLC.

The routine defines a command called SETLC.  Copy the text above and paste it into a plain text file, and save it with a name of your choice (such as SETLC) with a .lsp extension (SETLC.lsp, for example.)  You can use Notepad, or the Visual LISP Editor that is part of AutoCAD.  (The VLIDE command will open that editor.)

 

You will need to customize this part:

	   '("0" . 218)
	   '("1" . 1)
	   '("2" . 2)
	   '("3" . 3)
	   '("4" . 4)
	   '("5" . 5)
	   '("6" . 6)
	   '("7" . 7)
	   '("8" . 8)
	   '("9" . 9)

Each line here creates a "dotted pair" list, which then gets included as an element in the llyrs list.  The first element in the dotted pair is a quoted string, representing the layer name.  The second element is an integer, representing the color number of the color to be assigned to that layer.  Edit the layer names, substituting the name of one of your layers, and then edit the color number to be the one you want assigned to that layer.  If you have more than 10 layers in your standard (as is likely), simply copy one entire line (including the return at the end) and then paste that in as many times as you have additional layers, then edit the copies.

 

When the command is run, the llyrs list is created, and its length determined.  The while loop is then entered, and it will cycle through once for each dotted pair in the llyrs list.  The layer name and color number are extracted.  If the layer name exists in the drawing, then the -LAYER command is run and is used to assign the specified color number to that layer.  The counter is incremented and the loop repeats for the next item, until all are processed.


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

Message 5 of 16
Anonymous
in reply to: David_W_Koch

Works like a charm.. 

But one question.. you make it look on X-ref layers?  like.: 

 

Xref-name| WORK

Xref-name| TECH

 

 

Message 6 of 16
David_W_Koch
in reply to: Anonymous

For a specific external reference name, include the layer name like any other: 

	   '("xrefname|1" . 1)

 

To get a particular layer name in all external references:

	   '("*|1" . 1)

To get a particular layer name in all external references and in the current drawing:

	   '("*1" . 1)

Note that will get any layer ending in "1"; if you have short layer names like that, and other layers that end in the same string, you will not be able to use that method, and, instead, you will need to use two lines:

	   '("1" . 1)
	   '("*|1" . 1)

 


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

Message 7 of 16
Anonymous
in reply to: David_W_Koch

so i should do it like this?

""

'("INDERVÆGGE" . 1) ; Normal Layer
'("*INDERVÆGGE" . 1) ; Xref Layer
'("GRUPPEOMRÅDE" . 6)
'("ETAGER" . 4)
'("YDERVÆGGE" . 4) ; Normal Layer
'("*YDERVÆGGE" . 4) ; Xref Layer

""

 

add a little star(*) befor the layer name(Indervægge) to find a Xref with that layer?

""
'("*INDERVÆGGE" . 1) ; Xref Layer

""

 

Message 8 of 16
David_W_Koch
in reply to: Anonymous

You can do that, if that works for your layering system.

 

Using "*INDERVÆGGE"  as the layer name will assign the layer color to all layers that end in "INDERVÆGGE", both in the current file and externally referenced layers.  If you want all of those layers to have that color, then that will work.

 

But suppose that walls were drawn on the "WALL" layer, and wall-mounted cabinets were drawn on the "CASE-WALL" layer, and in a particular file, I have walls in the host file as well as in one or more external references.  If I try to get all of the wall layers with "*WALL", I will get the "CASE-WALL" layer along with all of the wall layers.  If I do not want that, I have to use two separate lines, one with "WALL" and one with "*|WALL", so that I get all of the wall layers, but not the wall-mounted cabinet layer.


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

Message 9 of 16
Anonymous
in reply to: David_W_Koch

""
'("*INDERVÆGGE" . 1) ; Xref Layer

""

Dosn´t seams to work? any idea what i do wrong?

Message 10 of 16
David_W_Koch
in reply to: Anonymous

You are right.  I thought I tested that, but it appears that the command-line version of the LAYER command is not accepting wildcards in the layer name.  I apologize for that.  When I get a chance, I will rummage through my LISP routines and see if I can find some code that will allow for changing externally-dependent layers without having to specify the full externally-dependent name.


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

Message 11 of 16
Anonymous
in reply to: David_W_Koch

no need to apologize 🙂

you have already done more than i was hopeing for.

Just glad you wanna help.

Message 12 of 16
David_W_Koch
in reply to: Anonymous

@Anonymous 

 

I finally found some time to look at this.  There were two issues with my original code.  One, I did not explicitly call for the command line version of the LAYER command.  That may not matter, as that is probably used when invoked from AutoLISP, but I added the hyphen in the revised code (_.-LAYER rather than _.LAYER), just to be safe.

 

The main problem is that the tblsearch function in AutoLISP does not treat wild card characters as wild card characters, so when it searched the LAYER table for layer names that had an asterisk character in them, none were found and the routine skipped that item.  I found some code I had written that allows for wild card characters  when searching a table.  It is a separate subroutine, called TBLSWC (TaBLeSearch Wild Card).  It takes three arguments:  A string representing the name of the table to be searched ("LAYER" in this case), a string representing the symbol name for which to search and an indicator as to whether the symbol name string should be treated as a wild card string.  If the third argument evaluates to anything other than nil, the symbol name string will be treated as a wild card string.  If the third argument evaluates to nil, then the symbol name is not treated as a wild card string and the function acts the same way as the tblsearch function would.

 

Here is the code for both the TBLSWC subroutine and the amended C:SETLC.lsp command function:

(defun TBLSWC (stbl sname wcard / tbdxf stname)
  (defun tbdxf (ltbl code) (cdr (assoc code ltbl)))
  (setq stbl (strcase stbl nil))
  (cond					; Cond A.
    ((/= 'STR (type stbl))
     (alert
       "In function TBLSWC:\nArgument STBL was not passed a string.\nTBLSWC will return nil."
     ) ;_ End alert.
    ) ;_ End condition A1.
    ((/= 'STR (type sname))
     (alert
       "In function TBLSWC:\nArgument SNAME was not passed a string.\nTBLSWC will return nil."
     ) ;_ End alert.
    ) ;_ End condition A2.
    ((not (member stbl
		  '("APPID"    "BLOCK"	  "DIMSTYLE" "LAYER"
		    "LTYPE"    "STYLE"	  "UCS"	     "VIEW"
		    "VPORT"
		   )
	  ) ;_ End member.
     ) ;_ End not.
     (alert
       "In function TBLSWC:\nArgument STBL was not passed a valid table name.\nTBLSWC will return nil."
     ) ;_ End alert.
    ) ;_ End condition A3.
    ((tblsearch stbl (strcase sname nil))
					; If sname is a symbol name in stbl, return sname.
     sname
    ) ;_ End condition A4.
    ((not wcard)			; If wild card test is not requested, return nil.
     nil
    ) ;_ End condition A5.
    (T					; Else do wild card test:
     (setq stname (tbdxf (tblnext stbl T) 2))
					; Get 1st table symbol name.
					; While stname is not nil (table end) & does not wild card match sname...
     (while
       (and stname
	    (not (wcmatch (strcase stname nil) (strcase sname nil)))
       ) ;_ End and.
	(setq stname (tbdxf (tblnext stbl) 2)) ; ...get next name.
     ) ;_ End while.
     ;; Return sname if wcmatch successful, else return nil.
     (if stname
       sname
       nil
     ) ;_ End if.
    ) ;_ End condition A6.
  ) ;_ End cond A.
) ;_ End TBLSWC.

(defun C:SETLC (			; No arguments.
		/
		icount			; List counter [integer].
		ilngth			; Length of layer/color list [integer].
		ilyrc			; Color number to be processed [integer].
		ldata			; Dotted pair to be processed [list]
		llyrs			; List of dotted pairs of layer names and color numbers [list].
		slyrn			; Layer name to be processed [string].
		)
  ;; Edit below to add your layer names and associated color numbers, maintaining the format given.
  ;; Layer names are quoted strings.
  ;; Color numbers are integers.
  (setq	llyrs
	 (list
	   '("0" . 7)
	   '("1" . 1)
	   '("2" . 2)
	   '("3" . 3)
	   '("4" . 4)
	   '("5" . 5)
	   '("6" . 6)
	   '("7" . 7)
	   '("8" . 8)
	   '("9" . 9)
	   '("*|1" . 1)
	   '("*|2" . 2)
	   '("*|3" . 3)
	   '("*|4" . 4)
	   '("*|5" . 5)
	   '("*|6" . 6)
	   '("*|7" . 7)
	   '("*|8" . 8)
	   '("*|9" . 9)
	   '("*INDERVÆGGE" . 30)
	   '("*YDERVÆGGE" . 60)
	   '("*NOSPECIAL01" . 80)
	 ) ;_ End list.
	icount	0			; Initialize counter.
	ilngth	(length llyrs)		; Number of layers in list.
  ) ;_ End setq.
  (while (< icount ilngth)
    (setq ldata	(nth icount llyrs)	; Get data item for this pass.
	  slyrn (car ldata)		; Layer name to process.
	  ilyrc (cdr ldata)		; Color of layer to be processed.
    ) ;_ End setq.
    (if	(tblswc "LAYER" slyrn T)	; If layer exists...
      (command "_.-LAYER" "_C" ilyrc slyrn "") ; ... set layer color.
    ) ;_ End if.
    (setq icount (1+ icount))		; Increment loop counter.
  ) ;_ End while.
  (prompt "\nDone.  Colors of layers found have been set to standard. ")
  (prin1)
) ;_ End C:SETLC.

As before, you will need to replace the test layer names and colors in the llyrs list with your own.  This should support both a leading asterisk ("*LAYERNAME") or a leading asterisk and vertical bar ("*|LAYERNAME" ) to be able to set the colors of externally dependent layers.  It worked for me in limited testing.  (You have to load both the subroutine and the command function.)


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

Message 13 of 16
R_Tweed
in reply to: Anonymous

I was following the thread and wanted to add something for other people not quite up on writing lisp but are wanting a simple routine that will get the job done. Your post sounds like the primary goal is to leverage lisp to manipulate layers which would lead to much greater things.

 

However, I thought I would post a script for other people following, who are also looking for a way to accomplish this goal but are lost when reading lisp code like me.   The text below can be saved as a simple txt file and renamed to a *.scr.  I added a couple of lines that change transparency just as an example.

 

;
;begin layer section
-LAYER
C 5 *Tech*
C 8 *Work*
TR 60 *UTIL*
TR 50 *ELEC*

-LAYER S 0

;end

 

Message 14 of 16
Vuxvix
in reply to: David_W_Koch

Hi!
In the case of an RGB color (e.g. 255,0,0). How can I edit the lisp.
Also my layers have Japanese characters. So some layers don't work
Thanks

Message 15 of 16
David_W_Koch
in reply to: Vuxvix

The command line syntax for TrueColors is different.  The program would need to be rewritten to accommodate TrueColors.  The RGB values for a TrueColor could be included in the layer/color list as a string, for example, "255,0,0".

 

The program would need to test data type of the color value.  If it is an integer, then the code as written would work.  If it is a string, then the command line would need to specify that a TrueColor value is being given by adding "T" after the subcommand for color.  The amended code below will handle TrueColor values as a string.  The TrueColor50-23-88 layer added to the example list shows how a TrueColor value is formatted.

 

Lines 100 - 103 are the added test for whether the color value is an integer or not, with the corresponding actions to take.

 

(defun TBLSWC (stbl sname wcard / tbdxf stname)
  (defun tbdxf (ltbl code) (cdr (assoc code ltbl)))
  (setq stbl (strcase stbl nil))
  (cond					; Cond A.
    ((/= 'STR (type stbl))
     (alert
       "In function TBLSWC:\nArgument STBL was not passed a string.\nTBLSWC will return nil."
     ) ;_ End alert.
    ) ;_ End condition A1.
    ((/= 'STR (type sname))
     (alert
       "In function TBLSWC:\nArgument SNAME was not passed a string.\nTBLSWC will return nil."
     ) ;_ End alert.
    ) ;_ End condition A2.
    ((not (member stbl
		  '("APPID"    "BLOCK"	  "DIMSTYLE" "LAYER"
		    "LTYPE"    "STYLE"	  "UCS"	     "VIEW"
		    "VPORT"
		   )
	  ) ;_ End member.
     ) ;_ End not.
     (alert
       "In function TBLSWC:\nArgument STBL was not passed a valid table name.\nTBLSWC will return nil."
     ) ;_ End alert.
    ) ;_ End condition A3.
    ((tblsearch stbl (strcase sname nil))
					; If sname is a symbol name in stbl, return sname.
     sname
    ) ;_ End condition A4.
    ((not wcard)			; If wild card test is not requested, return nil.
     nil
    ) ;_ End condition A5.
    (T					; Else do wild card test:
     (setq stname (tbdxf (tblnext stbl T) 2))
					; Get 1st table symbol name.
					; While stname is not nil (table end) & does not wild card match sname...
     (while
       (and stname
	    (not (wcmatch (strcase stname nil) (strcase sname nil)))
       ) ;_ End and.
	(setq stname (tbdxf (tblnext stbl) 2)) ; ...get next name.
     ) ;_ End while.
     ;; Return sname if wcmatch successful, else return nil.
     (if stname
       sname
       nil
     ) ;_ End if.
    ) ;_ End condition A6.
  ) ;_ End cond A.
) ;_ End TBLSWC.

(defun C:SETLC (			; No arguments.
		/
		icount			; List counter [integer].
		ilngth			; Length of layer/color list [integer].
		ilyrc			; Color number to be processed [integer].
		ldata			; Dotted pair to be processed [list]
		llyrs			; List of dotted pairs of layer names and color numbers [list].
		slyrn			; Layer name to be processed [string].
		)
  ;; Edit below to add your layer names and associated color numbers, maintaining the format given.
  ;; Layer names are quoted strings.
  ;; Color numbers are integers.
  ;; TrueColors are quoted strings of three integers separated by commas.
  (setq	llyrs
	 (list
	   '("0" . 7)
	   '("1" . 1)
	   '("2" . 2)
	   '("3" . 3)
	   '("4" . 4)
	   '("5" . 5)
	   '("6" . 6)
	   '("7" . 7)
	   '("8" . 8)
	   '("9" . 9)
	   '("*|1" . 1)
	   '("*|2" . 2)
	   '("*|3" . 3)
	   '("*|4" . 4)
	   '("*|5" . 5)
	   '("*|6" . 6)
	   '("*|7" . 7)
	   '("*|8" . 8)
	   '("*|9" . 9)
           '("TrueColor50-23-88" . "50,23,88")
	   '("*INDERVÆGGE" . 30)
	   '("*YDERVÆGGE" . 60)
	   '("*NOSPECIAL01" . 80)
	 ) ;_ End list.
	icount	0			; Initialize counter.
	ilngth	(length llyrs)		; Number of layers in list.
  ) ;_ End setq.
  (while (< icount ilngth)
    (setq ldata	(nth icount llyrs)	; Get data item for this pass.
	  slyrn (car ldata)		; Layer name to process.
	  ilyrc (cdr ldata)		; Color of layer to be processed.
    ) ;_ End setq.
    (if	(tblswc "LAYER" slyrn T)	; If layer exists...
      (if (= 'INT (type ilyrc))
        (command "_.-LAYER" "_C" ilyrc slyrn "") ; ... set layer color.
        (command "_.-LAYER" "_C" "T" ilyrc slyrn "") ; ... set layer color.
      ) ;_ End if.
    ) ;_ End if.
    (setq icount (1+ icount))		; Increment loop counter.
  ) ;_ End while.
  (prompt "\nDone.  Colors of layers found have been set to standard. ")
  (prin1)
) ;_ End C:SETLC.

 


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

Message 16 of 16
Vuxvix
in reply to: David_W_Koch

That's useful lisp. Thanks for your detailed instructions. it does exactly what i need.

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

Post to forums  

Autodesk Design & Make Report

”Boost