[Search tip detail and code files using keywords, tip number, author name, etc ]
    For tip to function correctly, you must ensure that the downloaded file name matches the file name
    displayed in the Rename File To field. Please rename downloaded files when necessary.
Change Layer Colors
Tip# 3934 By Leland Leahy On 16-Jul-2012
Rated By 0 users Downloaded : 530
Categories : Object Properties
Software type : AutoCAD 2013
Rename File To : changecolor.lsp
Quickly change all the layers of a certain color to another color.

Leland Leahy shares a routine he created for AutoCAD and its verticals that helps him change the color of layers that objects are on.

"The LISP routine below is a good way to quickly change all the layers of a certain color to another color without having to go through the dialog box."

(defun c:colorchange (/ c1 c2 )
    (setq c1 (getint " What is the original color: ") c2 (getint " What is the new color: "))
    (vlax-for layer (vla-get-Layers (vla-get-ActiveDocument (vlax-get-Acad-Object)))(if (= c1 (vla-get-Color layer))(vla-put-Color layer c2)))

Notes from Cadalyst Tip Reviewer Brian Benton: This routine is very easy to use. Load it, then type colorchange on the Command line. Type in the value of the color you want to change, then type in the color you want it to be. The routine doesn’t change the color of the object; it changes the color of the layer it is on. If an object’s color is set to 1 (red), it won’t be changed.


Average Rating:

User comments
Comment by Man,Render
Posted on 2012-07-16 14:54:54
c1 = nil, while not accounted for in the posted code will not throw any *error* as no Layer Object has a Color Property = nil. However, beware of c2 = nil, as this will cause the code to throw an *error*: [code] ; error: ActiveX Server returned an error: Parameter not optional [/code] Perhaps a better alternative is to use an IF statement to verify the valid input of the color to be replaced and the replacement color. The following code also adds the color dialog for graphical selection, and an undo mark which the code above lacks: [code] (vl-load-com) (defun c:LAYCOL () (c:LayerColor)) (defun c:LayerColor (/ *error*) (vl-load-com) (princ " LAYERCOLOR ") (defun *error* (msg) (if acDoc (vla-endundomark acDoc) ) (cond ((not msg)) ; Normal exit ((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit) ((princ (strcat " ** Error: " msg " ** "))) ) ; Fatal error, display it (princ) ) ((lambda (acDoc / oldColor newColor) (vla-startundomark acDoc) (if (= "BYLAYER" (setq color (getvar 'cecolor))) (setq color 1) (setq color (atoi color)) ) (if (and (princ " Select color to be replaced: ") (setq oldColor (acad_colordlg color nil)) (princ " Select replacement color: ") (setq newColor (acad_colordlg oldColor nil)) ) (progn (vlax-for oLayer (vla-get-layers acDoc) (if (= oldColor (vla-get-color oLayer)) (vla-put-color oLayer newColor) ) ) (*error* nil) ) (*error* "Color not specified") ) ) (vla-get-activedocument (vlax-get-acad-object)) ) ) (princ) [/code]