Author Topic: Lisp for Coloring entities by Elevation in RGB Colors (Gradient)  (Read 1476 times)

0 Members and 1 Guest are viewing this topic.

yasser.mahran

  • Mosquito
  • Posts: 5
Hey all, I was searching for a lisp that would do the following: Color entities (polylines/lines) according to their elevation in RGB Gradient manner, what so far I came with is a lisp that would color point clouds but I couldn't use, and some non-free tools like EzySurf (Extra) and Carlson Civil but both use ACI Index, therefore options are limited, and so elevations, What I need is a one that could color entitis in RGB colors, let's say I have a set of polylines, minimum has zero elevation, maximum is 45, so I need the color of the lowest one to be (0,0,0)-Absolute Black, and the color of the highest to be (255,255,255) i.e absolute white, and the ones between to be gradient of both? and thanks in advance!

Master_Shake

  • Swamp Rat
  • Posts: 1290
Re: Lisp for Coloring entities by Elevation in RGB Colors (Gradient)
« Reply #1 on: March 09, 2016, 09:49:26 am »
Need some more information, will you really have more than 255 different elevations? You list a min. elevation, is there a maximum?

 

yasser.mahran

  • Mosquito
  • Posts: 5
Re: Lisp for Coloring entities by Elevation in RGB Colors (Gradient)
« Reply #2 on: March 09, 2016, 09:57:24 am »
Actually I have more than  255, contour line every 0.5 m for 180 meters, that would be 180*2=360 elevations, but since maximum is 255, it will be just fine, maximum elevation is 45 cm (180 m)

Lee Mac

  • Seagull
  • Posts: 11852
  • AutoCAD 2015 Windows 7 London, England
Re: Lisp for Coloring entities by Elevation in RGB Colors (Gradient)
« Reply #3 on: March 09, 2016, 10:02:14 am »
This old code will get you started for polylines: Elevation Colour Map

yasser.mahran

  • Mosquito
  • Posts: 5
Re: Lisp for Coloring entities by Elevation in RGB Colors (Gradient)
« Reply #4 on: March 09, 2016, 10:19:53 am »
that code is fine, however I was looking for something that would allow more elevations, I actually have more than 255, PLUS I need the colors to be GRADIENT between two colors, this code is similar to "color contours by elevation" function in Carlson Civil tools, What I was looking for is something that would give the following result: , see? coloring in a Gradient manner

ronjonp

  • Needs a day job
  • Posts: 6430
Re: Lisp for Coloring entities by Elevation in RGB Colors (Gradient)
« Reply #5 on: March 09, 2016, 10:26:28 am »
You could try this .. welcome to TheSwamp :)

Code - Auto/Visual Lisp: [Select]
  1. (defun c:clrelev (/ _dxf co i mx n out ss)
  2.  (defun _dxf (code ename)
  3.    (if (and ename (= (type ename) 'ename))
  4.      (cdr (assoc code (entget ename '("*"))))
  5.    )
  6.  )
  7.  (if (setq ss (ssget ":L" (list '(0 . "lwpolyline,line"))))
  8.    (progn
  9. (strcat "AutoCAD.AcCmColor." (substr (getvar "acadver") 1 2))
  10.       )
  11.      )
  12.      (foreach x (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
  13. (setq out (cons (list x
  14.      (if (= "LINE" (_dxf 0 x))
  15. (apply 'max (list (last (_dxf 10 x)) (last (_dxf 11 x))))
  16. (_dxf 38 x)
  17.      )
  18. )
  19. out
  20.  )
  21. )
  22.      )
  23.      (if (zerop (setq mx (apply 'max (mapcar 'cadr out))))
  24. (print "Largest Elevation is Zero...")
  25.  (setq i (/ 255. mx))
  26.  (foreach x out
  27.    (if (minusp (cadr x))
  28.      (print "Negative Elevation")
  29.      (progn
  30. (setq n (fix (* i (cadr x))))
  31. (if (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-setrgb (list co n n n))))
  32.  (vl-catch-all-apply 'vla-put-truecolor (list (vlax-ename->vla-object (car x)) co))
  33. )
  34.      )
  35.    )
  36.  )
  37. )
  38.      )
  39.    )
  40.  )
  41.  (princ)
  42. )
« Last Edit: March 09, 2016, 12:27:23 pm by ronjonp »

Windows 10 x64 - AutoCAD /C3D 2018

Custom Build PC

yasser.mahran

  • Mosquito
  • Posts: 5
Re: Lisp for Coloring entities by Elevation in RGB Colors (Gradient)
« Reply #6 on: March 09, 2016, 12:41:20 pm »
EXCELLENT! Can you just be more generous and give hints about how someone could play with color values so that it the gradient can be used between any two colors? I mean (n n n) is the color, ain't it? anyways, million thanks!

Lee Mac

  • Seagull
  • Posts: 11852
  • AutoCAD 2015 Windows 7 London, England
Re: Lisp for Coloring entities by Elevation in RGB Colors (Gradient)
« Reply #7 on: March 09, 2016, 01:23:32 pm »
Here's another, adjust the colour ranges to suit (RGB values between 0-255):
Code - Auto/Visual Lisp: [Select]
  1. (defun c:zmap ( / cma cmi del elv enx idx lst pct rng sel zmn zmx )
  2.    (setq cmi '(255   0   0) ;; Minimum colours
  3.          cma '(255 255   0) ;; Maximum colours
  4.          zmx 0.0
  5.          zmn 0.0
  6.    )
  7.    (if (setq sel (ssget "_:L" '((0 . "LINE,LWPOLYLINE"))))
  8.        (progn
  9.            (repeat (setq idx (sslength sel))
  10.                (setq enx (entget (ssname sel (setq idx (1- idx))))
  11.                      elv (if (= "LINE" (cdr (assoc 0 enx)))
  12.                              (max (cadddr (assoc 10 enx)) (cadddr (assoc 11 enx)))
  13.                              (cdr (assoc 38 enx))
  14.                          )
  15.                      lst (cons (list enx elv) lst)
  16.                      zmx (max zmx elv)
  17.                      zmn (min zmn elv)
  18.                )
  19.            )
  20.            (setq rng (mapcar '- cma cmi)
  21.                  del (- zmx zmn)
  22.            )
  23.            (if (equal 0.0 del 1e-8)
  24.                (princ "\nNo change in elevation.")
  25.                (foreach itm lst
  26.                    (setq pct (/ (- (cadr itm) zmn) del))
  27.                    (entmod
  28.                        (append (car itm)
  29.                            (list
  30.                                (cons 420
  31.                                    (apply 'LM:rgb->true
  32.                                        (mapcar '(lambda ( a b ) (+ a (* b pct))) cmi rng)
  33.                                    )
  34.                                )
  35.                            )
  36.                        )
  37.                    )
  38.                )
  39.            )
  40.        )
  41.    )
  42.    (princ)
  43. )
  44.  
  45. ;; RGB -> True  -  Lee Mac
  46. ;; Args: r,g,b - [int] Red, Green, Blue values
  47.  
  48. (defun LM:RGB->True ( r g b )
  49.    (logior (lsh (fix r) 16) (lsh (fix g) 8) (fix b))
  50. )
  51.  

Colour Conversion function from here.
« Last Edit: March 09, 2016, 01:27:39 pm by Lee Mac »

ronjonp

  • Needs a day job
  • Posts: 6430
Re: Lisp for Coloring entities by Elevation in RGB Colors (Gradient)
« Reply #8 on: March 09, 2016, 01:33:02 pm »
EXCELLENT! Can you just be more generous and give hints about how someone could play with color values so that it the gradient can be used between any two colors? I mean (n n n) is the color, ain't it? anyways, million thanks!
The (n n n) represents (Red Green Blue) each with a value of 0-255. Look at the graphic below to see how the number change colors.

Windows 10 x64 - AutoCAD /C3D 2018

Custom Build PC

dgorsman

  • Water Moccasin
  • Posts: 2253
Re: Lisp for Coloring entities by Elevation in RGB Colors (Gradient)
« Reply #9 on: March 09, 2016, 05:38:35 pm »
In order to establish a gradient between two RGB colors you can do a linear interpolation between each of the three values (R, G, B) based on the number of steps needed.  For example, if the (R) value changes from 10 to 200, and you need 50 steps, then each step changes the (R) value by (200 - 10)/50 or 3.8.  RGB colors are integers so there will have to be some kind of rounding or truncation applied to the final value.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

yasser.mahran

  • Mosquito
  • Posts: 5
Re: Lisp for Coloring entities by Elevation in RGB Colors (Gradient)
« Reply #10 on: March 10, 2016, 03:28:25 am »
Thanks Lee! it works perfectly, amd thanks Ron for clarifying, yup I know about RGB, i is probably the increment, wonderful script, a lot of people were looking for it, now we can create elevation maps with autocad, and Yes dgorsman, I know values won't be exact, but rounded will be just fine!