Jump to content

Random colornumbers


halam

Recommended Posts

Hi all

 

 

I am trying to get a routine better that will help me get in some contrast to see what are my block (/layer) definitions in a 3D model.

My starting point is this code beneith that will assign colors to layers following +1 in the range 1-255.

This c values should be randomly in this range and rounded. This way follwing logical parts get much more contrast (for engineering purposes..)

 

 

Does anyone have a nice method for this?

 

 

(EDIT : THE COMPLETE CODE USING LM:.. & ROY_043 method)

 


; random layer colors
;; 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)
)
;; 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))))))
)


(defun c:Lcolor ()
 (vlax-for x (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
   (if
     (and
       (= :vlax-false (vla-get-lock x))
       (= :vlax-false (vla-get-freeze x))
       (not (wcmatch (vla-get-name x) "*|*"))
     )
     (vla-put-color x (LM:randrange 10 249))
   )
 )
 (vla-regen (vla-get-activedocument (vlax-get-acad-object)) acactiveviewport)
 (princ)
)

(vl-load-com) (princ)

not random.jpg

Edited by halam
keep it updated
Link to comment
Share on other sites

Thanks mr. Grrr. I was thinking the same

User should consider using this code in combination with a saved layerstate on forhand.

Throw in a question: 'Do you want to save the current state?'

 

I'm just a bit better in having some ideas than really brillant coding...anyway

Link to comment
Share on other sites

Thinking and trying hard to make this work as well

So it will not effect layers that are locked, frozen or xreffed (?)

 

 



;; CAB
;; [url]https://www.theswamp.org/index.php?topic=888.msg11892#msg11892[/url]
;;


;;;  Returns T if Locked
;;;        nil if Unlocked or not found
;;;        nil if lname is not a string
(defun islayerlocked (lname / entlst)
 (and (= 'str (type lname))
      (setq entlst (tblsearch "LAYER" lname))
      (= 4 (logand 4 (cdr (assoc 70 entlst))))
 )
)

;;;  Returns T if Frozen
(defun islayerfrozen (lname / entlst)
 (and (= (type lname) 'str)
      (setq entlst (tblsearch "layer" lname))
      (= 1 (logand 1 (cdr (assoc 70 entlst))))
 )
) ; end defun


Link to comment
Share on other sites

Thinking and trying hard to make this work as well

So it will not effect layers that are locked, frozen or xreffed (?)

 

For locked layers, try this:

(defun c:test nil
(vlax-for x (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
	(if (= (vla-get-Lock x) :vlax-true)
		(progn
			(vla-put-lock x :vlax-false)
			(vla-put-color x (LM:randrange 10 249))
			(vla-put-lock x :vlax-true)
		)
		(vla-put-color x (LM:randrange 10 249))
	)
)
(vla-Regen (vla-get-ActiveDocument (vlax-get-acad-object)) acActiveViewport)
(princ)
)
(vl-load-com) (princ)

;; 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)
)

Link to comment
Share on other sites

It doesn't work, neither does this i woth the ()() i was thinking would do..

 

 

(defun c:lcolor nil
(vlax-for x (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
 (if (= (vla-get-Lock x) :vlax-true) () (
  (progn (
   (vla-put-lock x :vlax-false)
   (vla-put-color x (LM:randrange 10 249))
   (vla-put-lock x :vlax-true)
  ))
  (vla-put-color x (LM:randrange 10 249))
 )
)
(vla-Regen (vla-get-ActiveDocument (vlax-get-acad-object)) acActiveViewport)
(princ)
)
(vl-load-com) (princ)
)

 

 

returns : "; error: no function definition: nil"

Link to comment
Share on other sites

...So it will not effect layers that are locked, frozen or xreffed

(defun c:lcolor ()
 (vlax-for x (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
   (if
     (and
       (= :vlax-false (vla-get-lock x))
       (= :vlax-false (vla-get-freeze x))
       (not (wcmatch (vla-get-name x) "*|*"))
     )
     (vla-put-color x (LM:randrange 10 249))
   )
 )
 (vla-regen (vla-get-activedocument (vlax-get-acad-object)) acactiveviewport)
 (princ)
)

Link to comment
Share on other sites

.. true Chuck Norris style breaking it into bits and pieces !

So cool and useful !!! Thanks Roy!

 

 

For promotion ..

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