Jump to content

Assigning random colors to each line/polyline within a selection set


enthralled
 Share

Recommended Posts

Hello,

I have hundreds of lines and LW polylines and I need to color differentiate them, is there a way to give a random color to each line/polyline within a selection set?

 

Link to comment
Share on other sites

(defun c:ordercolor ( / *error* sel scolor selcount selnum selname sellist check )
 (setvar "cmdecho" 0) 
 (setq sel (ssget '((0 . "*LINE"))))

 (command "_undo" "_be")
   (defun *error*(e)
     (command "_undo" "_e")
     (princ)
   )
 (setq scolor 0)
 (setq selcount (sslength sel)) 
 (setq selnum 0) 

(repeat selcount 
    (setq selname (ssname sel selnum))
    (setq sellist (vlax-ename->vla-object selname))
    (setq check (vlax-property-available-p sellist "Color" T))
	(if check
		(vlax-put-property sellist 'Color scolor)
	)
    (setq selnum (+ selnum 1))
    (setq scolor (+ scolor 1))
    
    (if (> scolor 255) (setq scolor 0))
)

(command "_undo" "_e")
);end_defun



(defun c:randomcolor ( / *error* sel scolor selcount selnum selname sellist check )
 (setvar "cmdecho" 0) 
 (setq sel (ssget '((0 . "*LINE"))))

 (command "_undo" "_be")
   (defun *error*(e)
     (command "_undo" "_e")
     (princ)
   )
 (setq scolor 0)
 (setq selcount (sslength sel)) 
 (setq selnum 0) 

(repeat selcount 
    (setq selname (ssname sel selnum))
    (setq sellist (vlax-ename->vla-object selname))
    (setq check (vlax-property-available-p sellist "Color" T))
	(if check
		(vlax-put-property sellist 'Color scolor)
	)
    (setq selnum (+ selnum 1))
    (setq scolor (LM:randrange 0 255))
)

(command "_undo" "_e")
);end_defun




;; Random in Range  -  Lee Mac
;; Returns a pseudo-random integral number in a given range (inclusive)

(defun LM:randrange ( a b )
    (+ (min a b) (fix (* (LM:rand) (1+ (abs (- a b))))))
)



;; Rand  -  Lee Mac
;; PRNG implementing a linear congruential generator with
;; parameters derived from the book 'Numerical Recipes'

(defun LM:rand ( / a c m )
    (setq m   4294967296.0
          a   1664525.0
          c   1013904223.0
          $xn (rem (+ c (* a (cond ($xn) ((getvar 'date))))) m)
    )
    (/ $xn m)
)

 

RANDOMCOLOR to set random (0 to 255)

ORDERCOLOR to set order (0 to 255)  it changes according to the order of ssget.

Edited by exceed
  • Like 1
Link to comment
Share on other sites

14 minutes ago, exceed said:
(defun c:ordercolor ( / *error* sel scolor selcount selnum selname sellist check )
 (setvar "cmdecho" 0) 
 (setq sel (ssget '((0 . "*LINE"))))

 (command "_undo" "_be")
   (defun *error*(e)
     (command "_undo" "_e")
     (princ)
   )
 (setq scolor 0)
 (setq selcount (sslength sel)) 
 (setq selnum 0) 

(repeat selcount 
    (setq selname (ssname sel selnum))
    (setq sellist (vlax-ename->vla-object selname))
    (setq check (vlax-property-available-p sellist "Color" T))
	(if check
		(vlax-put-property sellist 'Color scolor)
	)
    (setq selnum (+ selnum 1))
    (setq scolor (+ scolor 1))
    
    (if (> scolor 255) (setq scolor 0))
)

(command "_undo" "_e")
);end_defun



(defun c:randomcolor ( / *error* sel scolor selcount selnum selname sellist check )
 (setvar "cmdecho" 0) 
 (setq sel (ssget '((0 . "*LINE"))))

 (command "_undo" "_be")
   (defun *error*(e)
     (command "_undo" "_e")
     (princ)
   )
 (setq scolor 0)
 (setq selcount (sslength sel)) 
 (setq selnum 0) 

(repeat selcount 
    (setq selname (ssname sel selnum))
    (setq sellist (vlax-ename->vla-object selname))
    (setq check (vlax-property-available-p sellist "Color" T))
	(if check
		(vlax-put-property sellist 'Color scolor)
	)
    (setq selnum (+ selnum 1))
    (setq scolor (LM:randrange 0 255))
)

(command "_undo" "_e")
);end_defun




;; Random in Range  -  Lee Mac
;; Returns a pseudo-random integral number in a given range (inclusive)

(defun LM:randrange ( a b )
    (+ (min a b) (fix (* (LM:rand) (1+ (abs (- a b))))))
)



;; Rand  -  Lee Mac
;; PRNG implementing a linear congruential generator with
;; parameters derived from the book 'Numerical Recipes'

(defun LM:rand ( / a c m )
    (setq m   4294967296.0
          a   1664525.0
          c   1013904223.0
          $xn (rem (+ c (* a (cond ($xn) ((getvar 'date))))) m)
    )
    (/ $xn m)
)

 

RANDOMCOLOR to set random (0 to 255)

ORDERCOLOR to set order (0 to 255)  it changes according to the order of ssget.

Thanks. This helps me a lot!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...