I am interested in a program that can select layers by its color, and then change those layers to a different color and add a prefix of Z- in front of those layers.
Example:
Layer Names: Layer Color: New Layer Names: New Layer Color:
Wall 2 Z-Wall 100
Berm 2 Z-Berm 100
Fence 2 Z-Fence 100
I found a program (below) that works pretty close to what I am looking for but not quite.
;;; Change the color of a layer to another color by Jeff Mishler July 9, 2003
;;; Usage - (lay_col_chg oldcolor newcolor
;;; example: Command: (lay_col_chg 142 100)
(defun lay_col_chg (oldcolor newcolor / layers)
(vl-load-com)
(setq layers (vla-get-layers
(vla-get-activedocument
(vlax-get-acad-object))))
(vlax-for x layers
(if (= oldcolor (vla-get-color x))
(vla-put-color x newcolor)
)
)
(princ)
)
Thanks ahead of time for your help.
Solved! Go to Solution.
Solved by Lee_Mac. Go to Solution.
The most intuitive way would be to iterate through the Layer Collection and use a conditional statement to test for the desired properties of the layers to be changed:
e.g.:
(defun c:dolayers ( / name ) (vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) (if (and (= 2 (vla-get-color layer)) (not (wcmatch (setq name (vla-get-name layer)) "Z-*")) (not (tblsearch "LAYER" (setq name (strcat "Z-" name)))) ) (progn (vla-put-color layer 100) (vla-put-name layer name) ) ) ) (princ) ) (vl-load-com) (princ)
Lee, thanks for your reply and code. I should have elaborated my requests a little further. I was hoping for something more like prompting me for the color of the existing layers and also for the color of the new layer. On another note I took a look at your layerextract tool to see if I could use it for this instance, which didn't look like I could, but nice program though.
@Anonymous wrote:Lee, thanks for your reply and code. I should have elaborated my requests a little further. I was hoping for something more like prompting me for the color of the existing layers and also for the color of the new layer.
No worries, try something like this:
(defun c:dolayers ( / c1 c2 nm pr ) (if (and (princ "\nSelect Existing Layer Color...") (princ) (setq c1 (acad_colordlg 7 nil)) (princ "\nSelect New Layer Color...") (princ) (setq c2 (acad_colordlg 7 nil)) (setq pr (getstring t "\nSpecify Layer Prefix: ")) ) (vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) (if (and (= c1 (vla-get-color layer)) (not (wcmatch (setq nm (vla-get-name layer)) (strcat pr "*"))) (not (tblsearch "LAYER" (setq nm (strcat pr nm)))) ) (progn (vla-put-color layer c2) (vla-put-name layer nm) ) ) ) ) (princ) ) (vl-load-com) (princ)
azrdgldr wrote:
On another note I took a look at your layerextract tool to see if I could use it for this instance, which didn't look like I could, but nice program though.
Thanks azrdgldr, I'm glad you like the program - unfortunately it can only to be used for extracting data however
You're welcome azrdgldr
I have only stuck my toes in to check the temperature when it comes to Vl- commands and their usage. You and Kent make this stuff look so easy and I do mean easy. I still consider myself a hacker at this even though I have written quite a few programs. I know now that I just have to jump in no matter what and see if I sink or swim in VL-. Thanks again.
@Anonymous wrote:I have only stuck my toes in to check the temperature when it comes to Vl- commands and their usage. You and Kent make this stuff look so easy and I do mean easy. I still consider myself a hacker at this even though I have written quite a few programs. I know now that I just have to jump in no matter what and see if I sink or swim in VL-. Thanks again.
Thanks azrdgldr
Once you have a basic understanding of the underlying structure of the AutoCAD Object Model*, and how to work with properties and methods derived from objects in the model, the best way to learn is to experiment and post a question when you get stuck; then learn from the mistakes that you make.
Studying the code posted by others on the forums is also a great benefit, (just make sure that you study code from a good source ), but when you attempt a program yourself you realise where your knowledge is lacking and the parts that you need to study some more.
*For what its worth, I wrote a brief intro to the Object Model in a post at CADTutor here, I'm not sure of your level at VL programming, but maybe it will be of some benefit to your learning.
Hope this helps,
Lee
To quote post here.
Studying the code posted by others on the forums is also a great benefit, just make sure that you study code from a good source.