halam Posted August 1, 2016 Share Posted August 1, 2016 (edited) 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) Edited August 4, 2016 by halam keep it updated Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted August 1, 2016 Share Posted August 1, 2016 You can use my Random in Range function, e.g.: (defun c:lcolor nil (vlax-for x (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) (vla-put-color x (LM:randrange 10 249)) ) (princ) ) (vl-load-com) (princ) Quote Link to comment Share on other sites More sharing options...
halam Posted August 2, 2016 Author Share Posted August 2, 2016 Fantastic stuff ! (if you dont mind some color swapping ..) Quote Link to comment Share on other sites More sharing options...
Grrr Posted August 2, 2016 Share Posted August 2, 2016 Awesome idea, and nice routine from Lee! Quote Link to comment Share on other sites More sharing options...
halam Posted August 2, 2016 Author Share Posted August 2, 2016 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 Quote Link to comment Share on other sites More sharing options...
tzframpton Posted August 3, 2016 Share Posted August 3, 2016 Fantastic stuff ! (if you dont mind some color swapping ..) Very cool stuff Hans. Quote Link to comment Share on other sites More sharing options...
halam Posted August 4, 2016 Author Share Posted August 4, 2016 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 Quote Link to comment Share on other sites More sharing options...
Grrr Posted August 4, 2016 Share Posted August 4, 2016 Thinking and trying hard to make this work as wellSo 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) ) Quote Link to comment Share on other sites More sharing options...
halam Posted August 4, 2016 Author Share Posted August 4, 2016 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" Quote Link to comment Share on other sites More sharing options...
Roy_043 Posted August 4, 2016 Share Posted August 4, 2016 ...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) ) Quote Link to comment Share on other sites More sharing options...
Grrr Posted August 4, 2016 Share Posted August 4, 2016 Woops! I've misread halam's request, I thought that the code wasn't working for locked layers.. so I posted the solution in my previous post. Quote Link to comment Share on other sites More sharing options...
halam Posted August 4, 2016 Author Share Posted August 4, 2016 .. true Chuck Norris style breaking it into bits and pieces ! So cool and useful !!! Thanks Roy! For promotion .. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.