Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Select Layer by Color Lisp

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
Anonymous
3693 Views, 9 Replies

Select Layer by Color Lisp

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.

9 REPLIES 9
Message 2 of 10
Lee_Mac
in reply to: Anonymous

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)

 

Message 3 of 10
Anonymous
in reply to: Lee_Mac

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.

Message 4 of 10
Lee_Mac
in reply to: Anonymous


@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 Smiley Wink

Message 5 of 10
Anonymous
in reply to: Lee_Mac

Works great.  Thanks for your help on this.

Message 6 of 10
Lee_Mac
in reply to: Anonymous

You're welcome azrdgldr Smiley Happy

Message 7 of 10
Anonymous
in reply to: Lee_Mac

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. 

Message 8 of 10
Lee_Mac
in reply to: Anonymous


@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 Smiley Happy

 

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 Smiley Wink), 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

Message 9 of 10
j_p_salvador
in reply to: Lee_Mac

Hi @Lee_Mac ,

 

Can you write me a code to select all color 254 and delete.

Thanks in advace.

Message 10 of 10
Sea-Haven
in reply to: j_p_salvador

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.

 

 

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

Post to forums  

Autodesk Design & Make Report

”Boost