Lisp for changing object's color from BYLAYER to equivalent TrueColor.

Lisp for changing object's color from BYLAYER to equivalent TrueColor.

kwalkerHNUPR
Explorer Explorer
822 Views
11 Replies
Message 1 of 12

Lisp for changing object's color from BYLAYER to equivalent TrueColor.

kwalkerHNUPR
Explorer
Explorer

I'd like to select several hundred objects at once and change their individual color properties from BYLAYER to their equivalent TrueColor so they plot as their visual color without changing the CTB file. (Attached image shows original on the left and goal on the right so layer color can be plotted to see how it looks to a drafter on a black background.) 

kwalkerHNUPR_0-1739295989384.png

Its very time consuming to do them individually for hundreds of items. For example, object is BYLAYER, layer color is Magenta. The equivalent TrueColor (RGB) is 255,0, 255. Using CHPROP>C>T>255,0, 255 works individually. 

This lisp just is not working as envisioned and AI has been unsuccessful at helping me fix it. it seems to correctly get the true color but is not able to apply it to the object's properties. Any help would be greatly appreciated.

0 Likes
Accepted solutions (1)
823 Views
11 Replies
Replies (11)
Message 2 of 12

Kent1Cooper
Consultant
Consultant

Another example of AI not understanding AutoLisp and/or AutoCAD well enough yet, and therefore making incorrect assumptions about how they work.  Try changing this:

(command "CHPROP" ent "" "C" (strcat "RGB:" (itoa r) "," (itoa g) "," (itoa b)) "")

to this, using the correct CHPROP option and color designation [untested, and it may not be the only problem, but it stood out for me]:

(command "_.CHPROP" ent "" "_Color" "_Truecolor" (strcat (itoa r) "," (itoa g) "," (itoa b)) "")

[I put in the underscore prefixes to make it work in AutoCAD for any language, and the decimal prefix on the command name to make it use the native command in case you have any custom definition, and spelled out the option words in full, in case future editions might add options that start with the same letters.  If you feel none of those precautions are necessary, you can omit those.]

Kent Cooper, AIA
0 Likes
Message 3 of 12

kwalkerHNUPR
Explorer
Explorer

I appreciate the help, this makes sense, (kicking myself for not seeing that sooner) but still not working. I was very surprised that there wasn't a lisp already written for this floating around out there. 

0 Likes
Message 4 of 12

ronjonp
Advisor
Advisor
Accepted solution

@kwalkerHNUPR Try this:

 

(defun c:foo (/ a co nm o r s)
  ;; RJP » 2025-02-11
  ;; Select bylayer objects and apply truecolor pulled from layer color
  (cond	((setq s (ssget "_:L" '((62 . 256))))
	 (vlax-for l (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
	   (setq co (vla-get-truecolor l))
	   (setq nm (vla-get-name l))
	   (setq r (cons (list nm (vla-get-red co) (vla-get-green co) (vla-get-blue co)) r))
	 )
	 (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
	   (if (setq a (cdr (assoc (vla-get-layer (setq o (vlax-ename->vla-object e))) r)))
	     (progn (eval (append (list vla-setrgb co) a)) (vla-put-truecolor o co))
	   )
	 )
	)
  )
  (princ)
)

 

0 Likes
Message 5 of 12

kwalkerHNUPR
Explorer
Explorer

Thank you!!!!

0 Likes
Message 6 of 12

ronjonp
Advisor
Advisor

You're welcome! 🍻

 

Alternatively since your objects are bylayer already, you could just change the layer properties. Here are two functions to go between RGB and INDEX colors for all layers:

 

(defun c:lrgb (/ co)
  ;; RJP » 2025-02-11
  ;; Changes all layers to RGB color
  (vlax-for l (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
    (setq co (vla-get-truecolor l))
    (vla-setrgb co (vla-get-red co) (vla-get-green co) (vla-get-blue co))
    (vla-put-truecolor l co)
  )
  (princ)
)
(defun c:lindex	(/ co)
  ;; RJP » 2025-02-11
  ;; Changes all layers to INDEX color
  (vlax-for l (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
    (vla-put-colorindex (setq co (vla-get-truecolor l)) (vla-get-colorindex co))
    (vla-put-colormethod co 195)
    (vla-put-truecolor l co)
  )
  (princ)
)

 

0 Likes
Message 7 of 12

komondormrex
Mentor
Mentor

@ronjonp 

will falsely convert certain indexed bylayer colors

komondormrex_0-1739346211032.png

 

0 Likes
Message 8 of 12

cadffm
Consultant
Consultant

and you revealed not to use black(7) as the background color 🙂

Sebastian

0 Likes
Message 9 of 12

komondormrex
Mentor
Mentor

wierd

0 Likes
Message 10 of 12

cadffm
Consultant
Consultant

and on top, think also o difference beween monitor vs plot (aci vs rgb)

cadffm_0-1739356564497.png

 

Sebastian

0 Likes
Message 11 of 12

komondormrex
Mentor
Mentor

pdf result in numbers

komondormrex_1-1739359605934.png

 

0 Likes
Message 12 of 12

ronjonp
Advisor
Advisor

@komondormrex Yes, it is an approximation.

0 Likes