Routine to change RGB colors to ACI/Index 255 colors (and the opposite)

Routine to change RGB colors to ACI/Index 255 colors (and the opposite)

braudpat
Mentor Mentor
9,948 Views
11 Replies
Message 1 of 12

Routine to change RGB colors to ACI/Index 255 colors (and the opposite)

braudpat
Mentor
Mentor

 

Hello Friends

 

I am looking for a routine to change

- on ALL objects of the current drawing (entire DWG)

OR

- on ONLY on a classic AutoCAD selection

 

*** Routine: TO255COLORS

Brownse all entities and all blocks and sub-blocks and sub-entities (of the selection OR of ALL current drawing)

and force the RGB color [or Pantone or RAL]  (if used) to the "nearest" ACI/Index 255 color

 

*** Routine: TORGBCOLORS

Exactly the opposite : entities (and sub-entities) with classic ACI/Index 255 colors will be updated with the nearest RGB color !

 

The good functions are here :

---- Colour Conversion Functions ---- from Lee_Mac
http://www.lee-mac.com/colourconversion.html#rgbaci

 

I have tried to find the right routines with Google but no success !

 

Thanks in advance for your help, Regards, Patrice

 

 

 

 

 

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Accepted solutions (3)
9,949 Views
11 Replies
Replies (11)
Message 2 of 12

Lee_Mac
Advisor
Advisor
Accepted solution

For the first program, try the following:

 

(defun c:toACI ( / i l s )
    (if (setq s (ssget "_:L"))
        (repeat (setq i (sslength s))
            (toACI (entget (ssname s (setq i (1- i)))))
        )
    )
    (command "_.regen")
    (princ)
)
(defun toACI ( x / e n )
    (entmod (vl-remove-if '(lambda ( x ) (member (car x) '(420 430))) x))
    (if (and (= "INSERT" (cdr (assoc 0 x)))
             (not (member (setq n (cdr (assoc 2 x))) l))
             (setq e (tblobjname "block" n))
             (setq l (cons n l))
        )
        (while (setq e (entnext e)) (toACI (entget e)))
    )
)
(princ)
Message 3 of 12

braudpat
Mentor
Mentor

 

Hello Lee

 

1) THANKS

 

2) The routine acts on Entities with forced colors = OK

 

3) But if the default Color Layer is : RGB, Pantone, Ral, etc  and if the entities are "ByLayer/ByBlock", I have an "other" problem !

 

So is it possible to get an other routine ("LayerToACI") that will change (if necessary) ONLY the default Color (RGB, Pantone, Ral, etc) Layer TO ACI/Index 255 color scheme ?

 

French Humour : Mr LEE, you are a Lisp/VLisp legend !!!

 

Regards, Patrice

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 4 of 12

3wood
Advisor
Advisor
Accepted solution

To change TrueColor layer colors to Index layer colors, please try the lisp routine below:

 

(defun C:LAYERCOLORTOINDEX (/ acad doc color2 n1 color1 method1 colorindex)
  ;;; By 3wood 24/03/2016, Change truecolor layer colors to index colors 
  (setq acad (vlax-get-acad-object)
	doc (vla-get-activedocument acad)
	color2 (vla-GetInterfaceObject acad "AutoCAD.AcCmColor.19")
	)
  (vla-startundomark doc)
  (vlax-for n1 (vla-get-layers doc)
    (setq color1 (vla-get-truecolor n1)
	  method1 (vla-get-colormethod color1)
	  colorindex (vla-get-colorindex color1)
	  )
    (if (/= method1 195)
      (progn
	(vla-put-colorindex color2 colorindex)
	(vla-put-truecolor n1 color2)
      )
      )
    )
  (vla-endundomark doc)
  (princ)
  )
Message 5 of 12

braudpat
Mentor
Mentor

 

Hello Mr 3wood

 

1) Sorry for the delay

 

2) Thanks your routine is OK but I change ONE line :

color2 (vla-GetInterfaceObject acad "AutoCAD.AcCmColor.19")  is for 2014/2013

to

color2 (vla-GetInterfaceObject acad "AutoCAD.AcCmColor.20")  is for 2015/2016

 

3) Please is it possible to get a NEW version that will detect automatically the version

and will run ??

 

Thanks again for your help, Regards, Patrice

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 6 of 12

3wood
Advisor
Advisor
Accepted solution

@braudpat wrote:

3) Please is it possible to get a NEW version that will detect automatically the version

and will run ??


Good suggestion!

Here it is:

(defun C:LAYERCOLORTOINDEX (/ acad doc color2 n1 color1 method1 colorindex)
  ;;; By 3wood 31/03/2016, Change truecolor layer colors to index colors 
  (setq acad (vlax-get-acad-object)
	doc (vla-get-activedocument acad)
	color2 (vla-GetInterfaceObject acad (strcat "AutoCAD.AcCmColor." (substr (vlax-product-key) 28 2)))
	)
  (vla-startundomark doc)
  (vlax-for n1 (vla-get-layers doc)
    (setq color1 (vla-get-truecolor n1)
	  method1 (vla-get-colormethod color1)
	  colorindex (vla-get-colorindex color1)
	  )
    (if (/= method1 195)
      (progn
	(vla-put-colorindex color2 colorindex)
	(vla-put-truecolor n1 color2)
      )
      )
    )
  (vla-endundomark doc)
  (princ)
  )
Message 7 of 12

paulo.belesa
Contributor
Contributor

Newbie question 
How do I run the lisp? 

 

 

Thanks.

0 Likes
Message 8 of 12

komondormrex
Mentor
Mentor

most quicker way is to copy afore mentioned or other lisp code from '(defun (...' to closing bracket to the clipboard and paste it from clipboard to autocad command line. press <enter> if needed. it shows there command loaded c:.... voile. use that command as if it were native autocad command. that's it. 

0 Likes
Message 9 of 12

Sea-Haven
Mentor
Mentor

I use copy and paste all the time but one problem is if the code has blank lines these will be interpreted as like a command so lisp will stop working, easier is save file, then use Explorer and just drag lisp onto the dwg. If you want it to run straight away just add the C: defun as last line.

 

eg (defun c:drawline  add this as last line (c:drawline)

0 Likes
Message 10 of 12

komondormrex
Mentor
Mentor

agree. in such situation i copy/paste a code into vlisp editor and do load it from there. for repititious loading a code it needs to be saved as *. lsp program. 

0 Likes
Message 11 of 12

Sea-Haven
Mentor
Mentor

Notepad ++ has a load lisp function which is great for testing as you write. An ActiveX add on. Runs current text.

 

Lets go back to the post.

 

Message 12 of 12

komondormrex
Mentor
Mentor

i meant not to test, but just load what is already tested by whoever. 

0 Likes