Jump to content

Recommended Posts

Posted

I wanted this a long time but I wasn't ready to make it.

But BAM here it is, it is a pretty simple code after I found the random number generator.

What it does:

Asks for a selection takes the solids and give them a random color!

Pretty handy to get a decent overview. I posted the whole code, there is some copyright nonsens on the random code but if I don't place it here every-one knows that site: http://cat2.mit.edu/4.207/tutorials/alisp/random_01/random_01.html will go down some day and all will be lost.

Type RCS to use it

 

;; Function made by Marijn van den Heuvel
;; you can change the 10 160 to get different color range.
;; type rcs to use it.
(defun C:rcs(/)
(setq ss (ssget '((0 . "3dsolid"))))
(setq count 0)
(repeat (sslength ss)
(setq ent (ssname ss count))
 	 (setq r (rand 10 160))
 	 (setq g (rand 10 160))
 	 (setq b (rand 10 160))
 	 (command "chprop" ent "" "color" "truecolor"
    	 	(strcat (itoa r) "," (itoa g) "," (itoa b) )     
   	 "")
 (setq count (1+ count))
  );end repeat
);end function


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;                                                        ;;;;
;;;; Copyright (c) Jun. 1996 by Takehiko Nagakura.          ;;;;
;;;; All rights reserved.                                   ;;;;
;;;;                                                        ;;;;
;;;; Do not copy, use, modify or distribute this software   ;;;;
;;;; without written permission by Nagakura. Nagakura will  ;;;;
;;;; not be responsible for any consequence of its use.     ;;;;
;;;;                                                        ;;;;
;;;; Takehiko Nagakura  (e-mail: takehiko@mit.edu)          ;;;;
;;;;    Massachusetts Institute of Technology               ;;;;
;;;;    77 Massachusetts Ave. 10-472M, Cambridge, MA 02139  ;;;;
;;;;                                                        ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;; Last updated Feb 28, 2005 by TN for true color sample
;;;; Last updated Dec 29, 1996 by TN 

;; The following pseudo-random number generator was
;; adopted from Kernighan and Ritchie's "C Programming Language"
;; second edition, p46.
;; If you want to use a random number in your program, you can just
;; copy this whole program into your program and use the function,
;; (rand min max)
;; as described below. It works, but do not ask me why this code 
;; can generate fairly good randomness. If you are curious
;; to know it, some discussion on peudo-random number generator 
;; is found in Paul Kohut's web page,
;; http://xarch.tu-graz.ac.at/~rurban/news/comp.cad.autocad/rand.lsp.html.

;; functions and their descriptions                                     
;;
;; (rand16)       : generates a random number between 1 and 32767, 
;;                  that is, a positive 16 bit integer
;; (rand min max) : generates a random number between min and max.
;;                  Min and max must be an integer between 0 and 32767. 
;; (c:demo)       : demo function to generate random points




(setq *SeedRand* nil) ; initialize the global


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun rand16 ( / s )
 ; when this is used for the first time, initialize the seed
 ; from the system clock.
 (if (null *SeedRand*) 
(progn
  (setq s (getvar "date"))
     (setq *SeedRand* (fix (* 86400 (- s (fix s))))) 
   ) ; progn
 ) ; if

 ; To generate a psudo-sandom number sequence
 ; I use the routine described in Kernighan and Ritchie's
 ; "C Programming Language" second edition, p46
 (setq *SeedRand* (+ (* *SeedRand* 1103515245) 12345))

; trim off the bits left of the 16th bits      
 (logand (/ *SeedRand* 65536) 32767)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; generates a random number between min and max.
; min and max must be a non-negative integer smaller than 32678. 
(defun rand (min max / r16 range quotient remainder result)
 (setq r16         (rand16))          ; random number smaller than 32678
 (setq range       (+ 1 (- max min))) ; number of integers to be produced
 (setq quotient    (/ r16 range))     ; result in non-neg. integer
 (setq remainder   (- r16 (* quotient range))) 
 (setq result      (+ min remainder))
 result
)

; test it
; (rand 5 7)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:demo   ( / x y n)
 (command "plan" "")
 (command "cmdecho" 0)
 (command "zoom" "window"
           (list -10000 -10000) (list 40000 40000))
 (command "pdmode" 65)
 (command "pdsize" -2)
 (repeat 500 
  (setq x (rand16) ) (setq y (rand16))
  (command "point" (list x y ))

  ;AutoCAD takes color numbers between 1 and 255
  (setq n (rand 1 255))
  (command "chprop" (entlast) "" "color" n "") 
  ;(print (list x y n))
))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:demo2   ( / x y n)
 (command "plan" "")
 (command "cmdecho" 0)
 (command "zoom" "window"
           (list -10000 -10000) (list 40000 40000))
 (command "pdmode" 65)
 (command "pdsize" -2)
 (repeat 500 
  (setq x (rand16) ) (setq y (rand16))
  (command "point" (list x y ))

 ;AutoCAD true color uses numbers between 1 and 255 for rgb channels
 ;I use 100-160 range to make pastel colors. 
	
  (setq r (rand 160 255))
  (setq g (rand 160 255))
  (setq b (rand 160 255))
  (command "chprop" (entlast) "" "color" "truecolor" 
    (strcat (itoa r) "," (itoa g) "," (itoa b) )     
   "")
  ;(print (list x y n))
))

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...