Lisp for color changing...

Lisp for color changing...

rob.aHH2T8
Enthusiast Enthusiast
1,982 Views
25 Replies
Message 1 of 26

Lisp for color changing...

rob.aHH2T8
Enthusiast
Enthusiast

I need a lisp that turns every layer in a drawing from one color to another.  In my case it's blue to magenta, but I'd like to be able to customize it.  So, if the code is hard to decipher, please point it out.

Thank you!

0 Likes
Accepted solutions (1)
1,983 Views
25 Replies
Replies (25)
Message 2 of 26

ronjonp
Mentor
Mentor
Accepted solution

Try this:

(defun c:foo (/ old new)
  (setq	old 5
	new 6
  )
  (vlax-for l (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
    (if	(= old (vla-get-color l))
      (vla-put-color l new)
    )
  )
  (princ)
)
0 Likes
Message 3 of 26

Kent1Cooper
Consultant
Consultant

That is clearly "doable," but it's not clear to me exactly what you want to do.  Is it to find all Layers that have blue color assigned to them, and change their color to magenta?  And only those Layers?  That is, does "every layer" in your description refer not to every layer in the drawing, but to every layer with a particular color assigned?

 

If that's the intent, it can be done pretty simply without any code.  Open the Layer Manager and pick on the Color heading at the top of the color indicator column, to sort the Layers by color.  That will put all those that are blue together, making it easy to select them together and change their color collectively.  But it could certainly be automated, if you really need that.

Kent Cooper, AIA
0 Likes
Message 4 of 26

rob.aHH2T8
Enthusiast
Enthusiast

Unknown command...

0 Likes
Message 5 of 26

rob.aHH2T8
Enthusiast
Enthusiast

Yes, only layers that are blue- change to magenta.

0 Likes
Message 6 of 26

ronjonp
Mentor
Mentor

@rob.aHH2T8 wrote:

Unknown command...


ronjonp_0-1666631858534.png

 

0 Likes
Message 7 of 26

rob.aHH2T8
Enthusiast
Enthusiast

Added

(vl-load-com)

to the end of the file... to no avail.

0 Likes
Message 8 of 26

john.uhden
Mentor
Mentor

@rob.aHH2T8 

Did you save the file and then load it?

Or did you copy the code into your command line?

Or did you enter the command as "C:foo?"  If so, the command is just "FOO."

John F. Uhden

0 Likes
Message 9 of 26

ronjonp
Mentor
Mentor

@rob.aHH2T8 wrote:

Added

(vl-load-com)

to the end of the file... to no avail.


How are you loading the code ? Perhaps read THIS if you're having problems. 

0 Likes
Message 10 of 26

rob.aHH2T8
Enthusiast
Enthusiast

I've loaded lisps before... (see attachment)

0 Likes
Message 11 of 26

paullimapa
Mentor
Mentor

further developing @ronjonp foo code try this:

; foo.lsp function changes every layer in a drawing from a selected color to another selected color
(defun c:foo (/ ccol get_color lyrtbl new old)
  (vl-load-com)
  ; get_color presents color chart & returns color selected from AutoCAD's 255 color index
  (defun get_color (col)
   (acad_colordlg col nil) ; select from AutoCAD 255 color index chart color to change
  )  
  (setq	lyrtbl (tblsearch"layer"(getvar"clayer")) ; get current layer
	      ccol (abs(cdr(assoc 62 lyrtbl))) ; get current layer color
  )
  (if (setq old (get_color ccol)) ; get old layer color
    (progn
     (if (setq new (get_color old)) ; get new layer color
       (progn
        (vlax-for l (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) ; loop through all layers
         (if	(= old (vla-get-color l))
          (vla-put-color l new) ; change all match colors
         )
        )
        (alert(strcat"All Layers with Color [" (itoa old) "] Successfully changed to Color: [" (itoa new) "]"))
       ) ; progn
       (princ"\nNo Color Selected.")
	   ) ; if new color selected
    ) ; progn
    (princ"\nNo Color Selected.")
  ) ; if old color selected
  (princ)
) ; defun foo

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 12 of 26

ronjonp
Mentor
Mentor

@rob.aHH2T8 wrote:

I've loaded lisps before... (see attachment)


I'm not sure what's going on. It works as intended on my machine. 🤔

0 Likes
Message 13 of 26

rob.aHH2T8
Enthusiast
Enthusiast

Does the "princ" part need to be at the end?

0 Likes
Message 14 of 26

rob.aHH2T8
Enthusiast
Enthusiast

This one didn't work either.

0 Likes
Message 15 of 26

ronjonp
Mentor
Mentor

@rob.aHH2T8 wrote:

Does the "princ" part need to be at the end?


The (princ) is a quiet end and has no impact on how the code runs. What errors are you seeing ? I see that @paullimapa posted some code too but "it does not work' is fairly vague.

0 Likes
Message 16 of 26

rob.aHH2T8
Enthusiast
Enthusiast

Unknown command.

0 Likes
Message 17 of 26

Sea-Haven
Mentor
Mentor

Which is correct, I have

Full Autocad or CIV3D or Architecture

LT 

Autocad MAC

Bricscad

Another cad program

0 Likes
Message 18 of 26

paullimapa
Mentor
Mentor

the problem is you're not saving the contents of the code into a file that AutoCAD will load automatically.

From your captured image, it shows file name as: acad

But for AutoCAD to load the file automatically, it should be: acad.lsp

paulli_apa_0-1666654009066.png

 

And this file needs to be in one of the AutoCAD support search path which is listed under OPTIONS>Files>Support File Search Path

paulli_apa_1-1666654072495.png

So do this: open attached FOO.zip and then save FOO.lsp into one of the above folders.

Then at the AutoCAD command prompt enter:

(load"foo")

FOO

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 19 of 26

ronjonp
Mentor
Mentor

@rob.aHH2T8 wrote:

Unknown command.


Try to copy and paste the code into the command line then call 'FOO'. You're missing a step somewhere me thinks.

0 Likes
Message 20 of 26

rob.aHH2T8
Enthusiast
Enthusiast

The lisp above it works...

0 Likes