Lisp for Coloring entities by Elevation in RGB Colors (Gradient)

Lisp for Coloring entities by Elevation in RGB Colors (Gradient)

YasserMahran
Participant Participant
1,536 Views
1 Reply
Message 1 of 2

Lisp for Coloring entities by Elevation in RGB Colors (Gradient)

YasserMahran
Participant
Participant

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!

0 Likes
1,537 Views
1 Reply
Reply (1)
Message 2 of 2

Kent1Cooper
Consultant
Consultant

Lightly tested, and without some of the usual features yet, but this seems to work:

(defun C:B2WG ; = Black-to-White Gradient
  (/ ss n ent edata elist sslist elmin elmax col)
  (if (setq ss (ssget "_:L" '((0 . "LINE,LWPOLYLINE"))))
    (progn ; then
      (repeat (setq n (sslength ss))
        (setq
          ent (ssname ss (setq n (1- n)))
          edata (entget ent)
          elist
            (list ; entity name with its elevation
              ent
              (if (= (cdr (assoc 0 edata)) "LINE") (cadddr (assoc 10 edata)) (cdr (assoc 38 edata)))
            ); list
          sslist (cons elist sslist)
        ); setq
      ); repeat
      (setq
        elmin (apply 'min (mapcar 'cadr sslist))
        elmax (apply 'max (mapcar 'cadr sslist))
      ); setq
      (if (> elmax elmin); not all at same elevation [prevents divide-by-0 error]
        (foreach e sslist ; then -- assign colors
          (setq col (itoa (fix (* 255 (/ (- (cadr e) elmin) (- elmax elmin))))))
          (command "_.chprop" (car e) "" "_color" "_truecolor" (strcat col "," col "," col) "")
        ); foreach
        (command "_.chprop" ss "" "_color" "_truecolor" "0,0,0"); else -- all "true black"
          ; [change to "255,255,255" for "true white", or other desired all-objects color]
      ); if
    ); progn
  ); if
  (princ)
); defun

It assumes that Lines lie in or parallel to the WCS XY plane [not different end elevations -- if they are, it will use the elevation of the start], but it could be enhanced to check and account for other possibilities.  It should work with LWPolylines that are not in or parallel to the WCS, provided they were drawn in the same UCS as each other, because it gets the elevations from entity data, which has it in relation to the Polyline's UCS, not the WCS -- again, it could be made to check that such is the case.  It could also be made to accept "heavy" Polylines, as well as a host of other entity types.

Kent Cooper, AIA