Change layer color 9-250 to 1 LISP

Change layer color 9-250 to 1 LISP

sven.deleau
Contributor Contributor
2,306 Views
7 Replies
Message 1 of 8

Change layer color 9-250 to 1 LISP

sven.deleau
Contributor
Contributor

Goodday,

I have to change the layer colour of almost 600 drawings, these layers have to be within the range of colour 1-8.
I like to change the colours 9 till 250 into colour code 1 (Red). is this possible?
It has to be in lisp because i'm using Dynamo to do multiple actions at once.

this is the LISP i've got so far, capable of changing 1 colour at once (created by @Lee_Mac )

(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)

 Thanks in advance,

i've posted this in the wrong section i think, with no idea how to change it 

Accepted solutions (2)
2,307 Views
7 Replies
Replies (7)
Message 2 of 8

ronjonp
Advisor
Advisor
Accepted solution

Try this:

 

(defun c:dolayers nil
  (vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
    (if	(<= 9 (vla-get-color layer) 250)
      (vla-put-color layer 1)
    )
  )
  (princ)
)

What about colors 251 - 255 ?

 

 

Message 3 of 8

pbejse
Mentor
Mentor
Accepted solution

@sven.deleau wrote:

Goodday,

I have to change the layer colour of almost 600 drawings, these layers have to be within the range of colour 1-8. I like to change the colours 9 till 250 into colour code 1 (Red). is this possible?...


 

(defun c:dolayers ( /  )
    (vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
        (if
          (< 8 (vla-get-color layer) 251)
                (vla-put-color layer  1)
        )
    )
    (princ)
)

 

Give us a shout if you need assistance in making this work on mulltiple files

 

HTH

 

Message 4 of 8

sven.deleau
Contributor
Contributor

Thanks to both! tried both and they work 🙂

@ronjonp, I meant 255 instead of 250 🙂

@pbejse I have to make it work on 500 dwg drawings (exported from revit)
But i have to do a series of actions which i have written down into a dynamo script. When it works for 1 dwg, then i will try to run it on a folder full of dwgs's 

0 Likes
Message 5 of 8

Sea-Haven
Mentor
Mentor

Accoreconsole will scream through your dwgs you can batch an entire directory with out knowing dwg names as it will just open any dwg, its Autocad but without any input and acts directly on the database so does not open the dwg as such. 

 

https://through-the-interface.typepad.com/through_the_interface/2012/02/the-autocad-2013-core-consol...

 

 

 

 

Message 6 of 8

rolisonfelipe
Collaborator
Collaborator

(defun c:dolayers ( / )


(setq OBJCT (ssget '((0 . "ARC,CIRCLE,ELLIPSE,LINE,LWPOLYLINE,POLYLINE,SPLINE")))); select all type object

(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))); modify all color
(if
(< 8 (vla-get-color OBJCT) 250);old color
(vla-put-color OBJCT 1));new color

(if
(< 8 (vla-get-color OBJCT) 251);old color
(vla-put-color OBJCT 1));new color

(if
(< 8 (vla-get-color OBJCT) 252);old color
(vla-put-color OBJCT 4));new color

(if
(< 8 (vla-get-color OBJCT) 253);old color
(vla-put-color OBJCT 3));new color

(if
(< 8 (vla-get-color OBJCT) 254);old color
(vla-put-color OBJCT 2));new color
)

(if (= (TBLSEARCH "LAYER" "ARQ-LAYOUT")); search new layer
(command "_-LAYER" "_MAKE" "ARQ-LAYOUT" "_C" "7" "" "")); if it doesn't exist, create the new layer
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;now the question is, move all objects to a new layer, with the new colors.... this is possible

(princ)
)

 

0 Likes
Message 7 of 8

pbejse
Mentor
Mentor

@rolisonfelipe wrote:

...
(setq OBJCT (ssget '((0 . "ARC,CIRCLE,ELLIPSE,LINE,LWPOLYLINE,POLYLINE,SPLINE"))))

(vlax-for layer (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
(if
(< 8 (vla-get-color OBJCT) 250);old color
(vla-put-color OBJCT 1));new color

...


You're describing two different things there @rolisonfelipe. What exactly do want to achieve? 

All objects color to be ByLayer (layer) ? or ByBlock (OBJCT)? 

 

Also,


@rolisonfelipe wrote:

... 
(if
(< 8 (vla-get-color OBJCT) 250);old color
(vla-put-color OBJCT 1));new color
....
(if
(< 8 (vla-get-color OBJCT) 254);old color
(vla-put-color OBJCT 2));new color
)

...


The first is targetting colors numbers 9-249
(< 8 (vla-get-color OBJCT) 250)
  
That makes the rest of the codes will only target 1 value
(< 8 (vla-get-color OBJCT) 251) --> 250
(< 8 (vla-get-color OBJCT) 252) --> 251
(< 8 (vla-get-color OBJCT) 253) --> 252
(< 8 (vla-get-color OBJCT) 254) --> 253

could be written like this 
  (if
    (setq newColor
	   (Cond
	     ((< 8 (setq currentColor (vla-get-color OBJCT)) 251) 1)
	     ((= 252 currentColor) 4)
	     ((= 253 currentColor) 3)
	     ((= 254 currentColor) 2)
	   )
    )
     (vla-put-color OBJCT newColor)
  )

It does not take into account numbers 1-8 and 255-256

HTH

0 Likes
Message 8 of 8

rolisonfelipe
Collaborator
Collaborator

I need all objects within a block to change color, based on the existing color, and then move all objects to a single layer, and at the end a purge would be perfect to eliminate any possibility of unwanted data.

0 Likes