Visual LISP, AutoLISP and General Customization

Reply
*Expert Elite*
azrdgldr
Posts: 1,250
Registered: ‎01-09-2007
Message 1 of 8 (1,224 Views)
Accepted Solution

Select Layer by Color Lisp

1224 Views, 7 Replies
08-28-2011 09:42 AM

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.

*Expert Elite*
Lee_Mac
Posts: 1,101
Registered: ‎12-29-2009
Message 2 of 8 (1,222 Views)

Re: Select Layer by Color Lisp

08-28-2011 09:53 AM in reply to: azrdgldr

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 Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
*Expert Elite*
azrdgldr
Posts: 1,250
Registered: ‎01-09-2007
Message 3 of 8 (1,218 Views)

Re: Select Layer by Color Lisp

08-28-2011 10:25 AM 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.

*Expert Elite*
Lee_Mac
Posts: 1,101
Registered: ‎12-29-2009
Message 4 of 8 (1,215 Views)

Re: Select Layer by Color Lisp

08-28-2011 10:40 AM in reply to: azrdgldr

azrdgldr 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 :smileywink:

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
*Expert Elite*
azrdgldr
Posts: 1,250
Registered: ‎01-09-2007
Message 5 of 8 (1,211 Views)

Re: Select Layer by Color Lisp

08-28-2011 10:44 AM in reply to: Lee_Mac

Works great.  Thanks for your help on this.

*Expert Elite*
Lee_Mac
Posts: 1,101
Registered: ‎12-29-2009
Message 6 of 8 (1,204 Views)

Re: Select Layer by Color Lisp

08-28-2011 11:03 AM in reply to: azrdgldr

You're welcome azrdgldr :smileyhappy:

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
*Expert Elite*
azrdgldr
Posts: 1,250
Registered: ‎01-09-2007
Message 7 of 8 (1,201 Views)

Re: Select Layer by Color Lisp

08-28-2011 11:29 AM 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. 

*Expert Elite*
Lee_Mac
Posts: 1,101
Registered: ‎12-29-2009
Message 8 of 8 (1,196 Views)

Re: Select Layer by Color Lisp

08-28-2011 02:55 PM in reply to: azrdgldr

azrdgldr 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 :smileyhappy:

 

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 :smileywink:), 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

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community