TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: PM on June 13, 2021, 12:15:52 PM

Title: Change crosshait color with lisp
Post by: PM on June 13, 2021, 12:15:52 PM
Hi . I am searching how to change the color of modelspace to 40 with lisp and how  to bring iti back in normal (white) with lisp. I have seen some post with this but i can not understand the colors.

Thanks
Title: Re: Change crosshait color with lisp
Post by: Stefan on June 13, 2021, 05:23:37 PM
First, I think you should not mess with these properties in lisp, unless you know exactly what you're doing.
When you change them with Options command, autocad takes care of the contrast you need to clearly distinguish them on screen.
And not only the modelspace and crosshair, but also the little Viewport Control text on the top-left of the modelspace window.

Second, which one is correct? Crosshair from the title, or modelspace, from your post?

The colors, for some obscure reason, are always expressed in BlueGreenRed values and not RGB!!!

This will change the crosshair color only.
Use vla-put-graphicswinmodelbackgrndcolor for modelspace and  (setenv "2D Model viewport control color" ... ) for viewport control
Code - Auto/Visual Lisp: [Select]
  1. (defun c:chcol (/ dsp chc)
  2.               (vla-get-preferences
  3.                 (vlax-get-acad-object)
  4.               )
  5.             )
  6.   )
  7.   (setq chc (vlax-variant-value
  8.               (vlax-variant-change-type
  9.                 (vla-get-modelcrosshaircolor dsp)
  10.                 vlax-vblong
  11.               )
  12.             )
  13.   )
  14.   ;color     40 -> 16760576 -> RGB = 255 191   0
  15.   ;crosshair 40 ->    49151 -> BGR =   0 191 255 ???
  16.   (if
  17.     (= chc 49151)
  18.       (vlax-make-variant 16777215 vlax-vblong)
  19.     )
  20.       (vlax-make-variant    49151 vlax-vblong)
  21.     )
  22.   )
  23.  
  24.   (princ)
  25. )
Title: Re: Change crosshait color with lisp
Post by: Lee Mac on June 13, 2021, 05:36:33 PM
You could use something like this -
Code - Auto/Visual Lisp: [Select]
  1. (defun-q c:msc ( / d )
  2.     (eval
  3.         (list 'defun-q 'c:msc '( / c )
  4.             (list 'setq 'c
  5.                 (list 'quote
  6.                     (list 49151
  7.                         (vla-get-modelcrosshaircolor
  8.                             (setq d
  9.                                 (vla-get-display
  10.                                     (vla-get-preferences
  11.                                         (vlax-get-acad-object)
  12.                                     )
  13.                                 )
  14.                             )
  15.                         )
  16.                     )
  17.                 )
  18.             )
  19.             (list 'vla-put-modelcrosshaircolor d '(car c))
  20.            '(setq c:msc (vl-list* (car c:msc) (list 'setq 'c (list 'quote (reverse c))) (cddr c:msc)))
  21.            '(princ)
  22.         )
  23.     )
  24.     (c:msc)
  25. )

The above function redefines itself with every evaluation, reversing the order of the stored colour numbers.

The colours are expressed as OLE colours, for which you can calculate the appropriate values using the OLE conversion functions as part of my Colour Conversion functions (http://lee-mac.com/colourconversion.html).
Title: Re: Change crosshait color with lisp
Post by: Stefan on June 13, 2021, 06:36:01 PM
Nice one Lee, I like the way you switch the colors.

The only thing is, if the initial color is 40 when you load the lisp, nothing happens.
I had the same problem trying to store  the initial value with setenv. But if 40 is hard coded, white could be also hard coded.


Title: Re: Change crosshait color with lisp
Post by: PM on June 14, 2021, 03:05:45 AM
Thanks for the help  :smitten:
Title: Re: Change crosshait color with lisp
Post by: Lee Mac on June 14, 2021, 06:26:59 PM
Nice one Lee, I like the way you switch the colors.

The only thing is, if the initial color is 40 when you load the lisp, nothing happens.
I had the same problem trying to store  the initial value with setenv. But if 40 is hard coded, white could be also hard coded.

Thanks Stefan - I take your point about the initial colour issue, though as you've gathered, if the initial colour is 40, the program has no way to determine what the 'normal' crosshair colour should be without hardcoding a default, and so in my opinion, this is moot.
Title: Re: Change crosshait color with lisp
Post by: Lonnie on June 14, 2021, 07:00:29 PM
Not sure it helps but I did this years ago.


Code: [Select]
(setq acadobject (vlax-get-acad-object))
(setq acadpref (vlax-get-property acadobject 'preferences))
(setq acaddisp (vlax-get-property acadpref 'display))
(vlax-put-property acaddisp 'GraphicsWinmodelBackgrndColor 65986) ;;Model space background
(vlax-put-property acaddisp 'GraphicsWinLayoutBackgrndColor  63434) ;;command area
(vlax-put-property acaddisp 'ModelCrosshairColor 865785);; crosshairs
(vlax-put-property acaddisp 'TextWinBackgrndColor 3687) ;;command area

(princ "Done doing")

You can see my old thread here if you like.

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/changing-page-layout-paper-background-via-lisp/m-p/6390939#M342190
Title: Re: Change crosshait color with lisp
Post by: JohnK on June 15, 2021, 11:29:33 AM
There isn't a need to hardcode the original value; just check and change.
Code - Auto/Visual Lisp: [Select]
  1. (defun toggleCursor ( / *disply-preferences* currentvalue)
  2.   ;; toggle the current cursor color from 40 to white (and vise versa)
  3.   (setq *disply-preferences*
  4.         (vla-get-display
  5.           (vla-get-preferences
  6.             (vlax-get-acad-object)))
  7.         currentvalue
  8.         (vlax-variant-value
  9.           (vlax-variant-change-type
  10.             (vla-get-modelcrosshaircolor *disply-preferences* )
  11.             vlax-vblong
  12.             )
  13.           )
  14.         )
  15.     *disply-preferences*
  16.     (vlax-make-variant
  17.       (boole (- 7 (cond ((<= currentvalue 16777215) 1) (0)))
  18.              currentvalue
  19.              16728064)
  20.       vlax-vblong
  21.       )
  22.     )
  23.   )



EDIT: fixed spelling error in first sentence.
Title: Re: Change crosshait color with lisp
Post by: PM on June 15, 2021, 06:53:28 PM
Where i can find the number for the colors

for example 49151 --> 40 and 16777215->white  ???? Is any list of colors and numbers ??

Thanks
Title: Re: Change crosshait color with lisp
Post by: Lee Mac on June 15, 2021, 07:02:47 PM
There isn't a need to hardcode the original value; just check and change.
Code - Auto/Visual Lisp: [Select]
  1. ;; toggle the current cursor color from 40 to white (and vise versa)

But here white is arbitrarily hardcoded as a default.
Title: Re: Change crosshait color with lisp
Post by: Lee Mac on June 15, 2021, 07:03:20 PM
Where i can find the number for the colors

for example 49151 --> 40 and 16777215->white  ???? Is any list of colors and numbers ??

As noted in my post:
The colours are expressed as OLE colours, for which you can calculate the appropriate values using the OLE conversion functions as part of my Colour Conversion functions (http://lee-mac.com/colourconversion.html).
Title: Re: Change crosshait color with lisp
Post by: JohnK on June 15, 2021, 09:40:14 PM
There isn't a need to hardcode the original value; just check and change.
Code - Auto/Visual Lisp: [Select]
  1. ;; toggle the current cursor color from 40 to white (and vise versa)

But here white is arbitrarily hardcoded as a default.

No it is not (you must not have tried it). I'm just using either a LOGIOR (7) or XOR (6) to set or clear a bit(s).

I just saw this as an excellent opportunity to show how BOOLE can be used for different boolean operators all in one call--as I too was shown a long time ago.
Title: Re: Change crosshait color with lisp
Post by: Lee Mac on June 19, 2021, 07:14:57 PM
There isn't a need to hardcode the original value; just check and change.
Code - Auto/Visual Lisp: [Select]
  1. ;; toggle the current cursor color from 40 to white (and vise versa)

But here white is arbitrarily hardcoded as a default.

No it is not (you must not have tried it). I'm just using either a LOGIOR (7) or XOR (6) to set or clear a bit(s).

It is; and no, I don't need to try it as I can see & understand the operations being performed.

Firstly, the cond statement to change the boole operator seems redundant, as 16777215 (224-1) represents OLE 255,255,255 which is the largest possible value, and so (<= currentvalue 16777215) will always be true, which means the expression may become a simple XOR of the current value and 16728064 = 16777215 XOR 49151 (colour 40). Hence, for colour 40 you have essentially arbitrarily hardcoded white as the alternative colour (as I noted earlier); for all other colours, the default becomes the XOR of the current colour with 16728064.