Jump to content

Recommended Posts

Posted

Is there any way to colorize different layers randomly?

I wish to assign different colors to different layers.

Please help.

 

Sunil

Posted

One way .....

 

(defun c:Test (/ i l obj)
 ;;; Tharwat 08. MAy. 2012 ;;;
 (setq i 1)
 (while
   (setq l (tblnext "LAYER" (null l)))
        (entmod
        (subst
          (cons 62 i)
          (assoc 62 (setq obj (entget (tblobjname "LAYER" (cdr (assoc 2 l))))))
          obj
        )
      )
    (if (eq i 255)
      (setq i 1)
      (setq i (1+ i))
    )
 )
 (princ)
)

Posted

Since you are looking to assign the colors randomly, I suggest you to check the DOS_RANDOM function from McNeel's DOSLib pack:

(rem (dos_random) 255)

Posted

Eventually it might help, indicating the number of the first color and then a loop with a number of increment.

Posted

Another variant

;;; ************************************************************************
;; * Library DWGruLispLib http://forum.dwg.ru/showthread.php?t=15691       *
;; *                                                                       *
;; * _dwgru-Random                                                         *
;; *                                                                       *
;; Based vk_RandNum                                                        *
;; Http://www.caduser.ru/forum/index.php?PAGE_NAME=read&FID=23&TID=32763   *
;; *                                                                       *
;; *                                                                       *
;; * 07.12.2007 version 0001. Revision Vladimir Azarko (VVA)               *
;; * 21.07.2008 version 0002. Revision Vov.Ka                              *
;;               http://forum.dwg.ru/showpost.php?p=798277&postcount=9     *
;;; ************************************************************************

(defun _dwgru-random (/ modulus multiplier increment)
;;; Generate a random real number between 0 and 1
;;; Use a global variable *DWGRU_SEED*
 (if (not *DWGRU_SEED*)
   (setq *DWGRU_SEED* (getvar "DATE"))
 )
 (setq  modulus     65536
 multiplier 25173
 increment  13849
 *DWGRU_SEED*     (rem (+ (* multiplier *DWGRU_SEED*) increment) modulus))
 (/ *DWGRU_SEED* modulus)
)

;;; ************************************************************************
;;; * 
;;; *
;;; * dwgru-random-range http://forum.dwg.ru/showthread.php?t=15691
;;; *
;;; * 07/12/2007 Версия 0001.  Редакция Владимир Азарко (VVA)
;;; ************************************************************************
(defun dwgru-random-range (Minnum Maxnum )
;;; Generate a random number from a specified range of integers
;;; Uses the library _dwgru-Random
;;; Parameters:
;;; Minnum - the smallest integer
;;; Maxnum - the maximum integer
;;; Return: An integer in the range specified
;;; Use:
 ;|
 (dwgru-random-range 1 256)
 (dwgru-random-range 1 15)
 |;
;(- Maxnum (atoi (rtos (* (- Maxnum Minnum) (_dwgru-num-random)) 2 0))) ;_Comment VVA 07.12.2007
;;;Comment VVA http://forum.dwg.ru/showpost.php?p=798277&postcount=9
;;;(- Maxnum (fix (* (- Maxnum Minnum) (_dwgru-random)))) ;_Add VVA 07.12.2007 Так наверное быстрее
;;;
(- Maxnum (fix (* (- Maxnum Minnum -1) (_dwgru-random)))) ;;;Vov.ka http://forum.dwg.ru/showpost.php?p=798277&postcount=9
)


(defun c:Test (/ l obj)
 (while
   (setq l (tblnext "LAYER" (null l)))
        (entmod
        (subst
          (cons 62 (dwgru-random-range 1 255))
          (assoc 62 (setq obj (entget (tblobjname "LAYER" (cdr (assoc 2 l))))))
          obj
        )
      )
 )
 (princ)
)

Posted
Since you are looking to assign the colors randomly, I suggest you to check the DOS_RANDOM function from McNeel's DOSLib pack:

(rem (dos_random) 255)

 

 

Watch out for a zero return from random or (rem) calls.

 

-David

Posted

Using dos_random

(vlax-map-collection
 (vla-get-layers
   (vla-get-ActiveDocument
     (vlax-get-acad-object)
     )
   )
 (function
   (lambda (x)
     (vla-put-Color x (1+ (rem (dos_random) 254)))
     )
   )
 )

Posted

Or using my own random & changing the layer's RGB colours:

(vl-load-com)
(defun Random  (seed /)
 (if (not seed)
   (setq seed (if *random:seed*
                *random:seed*
                (setq *random:seed* (getvar "DATE"))))
   (setq *random:seed* seed))
 (setq seed          (rem (+ (* 25173 seed) 13849) 65536)
       *random:seed* seed)
 (/ seed 65536))

(defun c:RandomLayerColour  (/ col)
 (vlax-for lay  (vla-get-Layers (vla-get-ActiveDocument (vlax-get-acad-object)))
   (setq col (vla-get-TrueColor lay))
   (apply 'vla-SetRGB (cons col (mapcar '(lambda (n) (fix (* n 255))) (list (random nil) (random nil) (random nil)))))
   (vla-put-TrueColor lay col))
 (princ))

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

×
×
  • Create New...