See the top rated post in this thread. Click here

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: LISP to change layer colors to random colors

  1. #1
    Member
    Join Date
    2007-04
    Location
    Boise, ID
    Posts
    47
    Login to Give a bone
    0

    Smile LISP to change layer colors to random colors

    I am in need of a Lisp routine that will create a selection set of a large group of layers with a specific layer name (PP-*). Then take that set of layers and change the color of each of those layers to a different random color in Autocad. I will have thousands of layers, so I wanted to avoid singling out each layer individually; that would take forever, and make a huge Lisp or Script.

  2. #2
    I could stop if I wanted to tyshofner's Avatar
    Join Date
    2004-03
    Location
    Dallas, Tx
    Posts
    229
    Login to Give a bone
    0

    Default Re: LISP to change layer colors to random colors

    Do you want each (PP-*) layer to be a different color, or all (PP-*) layers changed to a single color?

    The following will do the second option:

    Code:
    (defun C:TEST ( / clr lay_col lay_nm lay)
    (vl-load-com)
    (initget 1)
    (setq clr (getint "\nSpecify layer color number: "))
    (setq lay_col (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))) ;get layer collection
    (vlax-for lay lay_col ;Iterate through layers
    	(setq lay_nm (vla-get-name lay)) ;find the name
    	(if (= (substr lay_nm 1 3) "PP-") ;check if first three characters = "PP-"
    	 (vla-put-color lay clr) ;Recolor if "PP-"
    	) ;end_if
    	(vlax-release-object lay) ;Release layer object
    ) ;end_vlax-for
    (vlax-release-object lay_col) ;release layer collection object
    (princ) ;clear command line
    ) ;end_defun
    Ty
    Last edited by tyshofner; 2007-04-23 at 04:22 PM.

  3. #3
    Member
    Join Date
    2007-04
    Location
    Boise, ID
    Posts
    47
    Login to Give a bone
    0

    Default Re: LISP to change layer colors to random colors

    In reponse to: "Do you want each (PP-*) layer to be a different color, or all (PP-*) layers changed to a single color?"

    I need each (PP-*) layer to be a different color. I could easily write a script routine for the latter. I am looking for a way for AutoCAD to random cycle through the selection set and assign each layer a different (independent) color. I am looking for a way to avoid writing an insanely huge script.

  4. #4
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: LISP to change layer colors to random colors

    Quote Originally Posted by mtubbs
    In reponse to: "Do you want each (PP-*) layer to be a different color, or all (PP-*) layers changed to a single color?"

    I need each (PP-*) layer to be a different color. I could easily write a script routine for the latter. I am looking for a way for AutoCAD to random cycle through the selection set and assign each layer a different (independent) color. I am looking for a way to avoid writing an insanely huge script.
    Here is a quick lisp to do it
    Give this a shot

    ~'J'~

    Code:
    ;;local functions:
    
    ; by Paul Kohut
    (setq SeedRand 1)
    (defun rand (/)
      (setq SeedRand (+ (* (fix SeedRand) 214013) 2531011))
      (boole 1 (/ SeedRand 65536) 32767)
    )
    
    ; edited by Fatty
    (defun randomgen (num init / rlst SeedRand init ncount val)
    
      (setq SeedRand (abs (fix (* 128256 (- init (fix init))))))
      (setq nCount 0)
      (while (< nCount num)
            (setq val (rem (rand) num))
        (if (not (member val rlst))
          (progn
     (setq rlst (cons val rlst))
     (setq nCount (+ nCount 1))
          )
        )
      )
      rlst
      )
    (defun check-layer (name color)
        (or
          (tblsearch "layer" name)
          (entmake
            (list
              '(0 . "LAYER")
              '(100 . "AcDbSymbolTableRecord")
              '(100 . "AcDbLayerTableRecord")
              '(70 . 0)
              (cons 2 name)
              (cons 62 color))))
    )
    ;main 
    (defun c:MBL (/ colors data i lnames makelist)
    (princ "\n\t >> Layers to create >>")
    (setq lnames (list "PP-100A"
    		   "PP-100B"
    		   "PP-100C"
    		   "PP-100D"
    		   "PP-100E"
    		   "PP-100F"
    		   "PP-100G"
    		   "PP-100H"
    		   "PP-100ETC"))
    (prin1 lnames)
    (setq data (randomgen 200 255))
    (setq i 0)
    (while (< i (length lnames))
      (setq colors (cons (car data) colors))
      (setq data (cdr data))
      (setq i (1+ i)))
    (princ "\n\t >> Randomly generated colors >>")  
    (prin1 colors)
    (setq makelist (mapcar 'cons lnames colors))
      (foreach item makelist
        (check-layer (car item) (cdr item))
        )
      (princ)
      )
    (princ "\n\t >> Type MBL to create layers >>")
    (princ)
    Last edited by fixo; 2007-04-23 at 08:16 PM.

  5. #5
    AUGI Addict kennet.sjoberg's Avatar
    Join Date
    2002-05
    Posts
    1,707
    Login to Give a bone
    1

    Default Re: LISP to change layer colors to random colors

    Quote Originally Posted by mtubbs
    . . .I am looking for a way for AutoCAD to random cycle through the selection set and assign each layer a different (independent) color. . . .
    . . .one more way
    Code:
    (defun C:RLC ;| RandomLayerColor |; ( / LayDxf random modulus multiplier increment ) ;; *seed* is a global random variable
      (if (not *seed* ) (setq *seed* (getvar "DATE" )) )
      (setq LayDxf (tblnext "LAYER" T ) ) ;; take the very first layer
      (while LayDxf ;; repeat as many times as needed
        (if (= (substr (cdr (assoc 2 LayDxf )) 1 3 ) "PP-" ) ;; check if "PP-" is the layer prefix
          (progn
            (setq LayDxf (entget (tblobjname "LAYER" (cdr (assoc 2 LayDxf )))) ) ;; reread the layer dxf from the layer Entity
            (setq LayDxf ;; and substitute the color to a random color
              (subst
                (cons
                  62
                  (fix (+ 0.5 (setq random (+ (* (setq modulus 65536 multiplier 25173 increment 13849 *seed* (rem (+ (* multiplier *seed* ) increment ) modulus ) random (/ *seed* modulus ) ) 254) 1 ))) )
                )
                (assoc 62 LayDxf )
                LayDxf
              )
            )
            (entmod LayDxf ) ;; update the layer
            (princ (strcat "\nLayer " (cdr (assoc 2 LayDxf )) " changed to color " (itoa (cdr (assoc 62 LayDxf )))) ) ;; you can remove this line
          )
          ( ) ;; the layer prefix was not "PP-"
        )
        (setq LayDxf (tblnext "LAYER" nil ) ) ;; prepare for the next layer
      )
      (princ) ;; silence end
    )
    : ) Happy Computing !

    kennet
    Last edited by kennet.sjoberg; 2007-04-25 at 02:58 PM. Reason: fix 0.5 to correct integer

  6. #6
    Login to Give a bone
    0

    Default Re: LISP to change layer colors to random colors

    I created the GenLayers function for testing purposes per this thread.
    You may find the GetRnd function valuable for other applications as well.
    Code:
    ;-------------------------------------------------------------------------------
    ; GenLayers - Generate 260 PP-* layers of color white to test functions
    ;-------------------------------------------------------------------------------
    (defun c:GenLayers (/ Cnt# LayerName$)
      (setq Cnt# 1)
      (repeat 260
        (setq LayerName$ (strcat "PP-" (substr (itoa (+ 1000 Cnt#)) 2)))
        (command "LAYER" "M" LayerName$ "C" 7 "" "")
        (setq Cnt# (1+ Cnt#))
      );repeat
      (princ)
    );defun c:GenLayers
    ;-------------------------------------------------------------------------------
    ; LayerRnd - Changes colors of layers PP-* to random colors using all 255 colors
    ;-------------------------------------------------------------------------------
    (defun c:LayerRnd (/ Color# ColorList@ LayerList@ LayerName$ Loop)
      (setq LayerList@ (tblnext "LAYER" t))
      (while LayerList@
        (setq LayerList@ (entget (tblobjname "LAYER" (cdr (assoc 2 LayerList@)))))
        (setq LayerName$ (cdr (assoc 2 LayerList@)))
        (if (= (substr LayerName$ 1 3) "PP-")
          (progn
            (setq Loop t)
            (while Loop
              (setq Color# (1+ (GetRnd 254)));Colors 1-255
              (if (not (member Color# ColorList@))
                (setq Loop nil ColorList@ (append ColorList@ (list Color#)))
              );if
            );while
            (if (= (length ColorList@) 255)
              (progn
                (setq ColorList@ nil)
                (alert "255 random colors have been created\nfor the PP-* layers. Starting over again.")
              );progn
            );if
            (entmod (subst (cons 62 Color#) (assoc 62 LayerList@) LayerList@))
          );progn
        );if
        (setq LayerList@ (tblnext "LAYER"))
      );while
      (princ)
    );defun c:LayerRnd
    ;-------------------------------------------------------------------------------
    ; GetRnd - Generates a random number
    ; Arguments: 1
    ;   Num# = Maximum random number range
    ; Returns: Random integer number between 0 and Num#.
    ;-------------------------------------------------------------------------------
    (defun GetRnd (Num# / MaxNum# PiDate$ RndNum# Minus Loop)
      (if (or (/= (type Num#) 'INT)(= Num# 0))
        (progn
          (princ "\nSyntax: (GetRnd Num#) Num# = Maximum random integer number range\ngreater than or less than 0.")
          (exit)
        );progn
      );if
      (if (< Num# 0)
        (setq MaxNum# (abs (1- Num#)) Minus t)
        (setq MaxNum# (1+ Num#))
      );if
      (if (not *RndNum*) (setq *RndNum* 10000))
      (setq Loop t)
      (while Loop
        (if (or (null *int*)(> *int* 100))
          (setq *int* 1)
          (setq *int* (1+ *int*))
        );if
        (setq PiDate$ (rtos (* (getvar "cdate") (* pi *int*)) 2 8))
        (cond
          ((>= MaxNum# 10000)
            (setq RndNum# (fix (* (atof (substr PiDate$ 13 5)) (* MaxNum# 0.00001))))
          )
          ((>= MaxNum# 1000)
            (setq RndNum# (fix (* (atof (substr PiDate$ 14 4)) (* MaxNum# 0.0001))))
          )
          ((>= MaxNum# 100)
            (setq RndNum# (fix (* (atof (substr PiDate$ 15 3)) (* MaxNum# 0.001))))
          )
          ((>= MaxNum# 10)
            (setq RndNum# (fix (* (atof (substr PiDate$ 16 2)) (* MaxNum# 0.01))))
          )
          ((>= MaxNum# 1)
            (setq RndNum# (fix (* (atof (substr PiDate$ 17 1)) (* MaxNum# 0.1))))
          )
          (t (setq RndNum# 0))
        );cond
        (if (/= RndNum# *RndNum*)
          (setq Loop nil)
        );if
      );while
      (setq *RndNum* RndNum#)
      (if Minus
        (setq RndNum# (* RndNum# -1))
      );if
      RndNum#
    );defun GetRnd
    ;-------------------------------------------------------------------------------
    Last edited by Terry Cadd; 2007-04-25 at 05:21 AM. Reason: Added comment.

  7. #7
    100 Club
    Join Date
    2007-02
    Posts
    148
    Login to Give a bone
    0

    Default Re: LISP to change layer colors to random colors

    try this:

    Code:
    (setq s1 (ssget "X" '((8 . "layer name"))))
    (command "change" s1 "" "p" "c" (color code) "")
    repeat this for all the layers you want to color change, the color code can be obtain in the color option in autoCAD.
    Last edited by Opie; 2007-04-26 at 02:23 PM. Reason: [CODE] tags added

  8. #8
    AUGI Addict kennet.sjoberg's Avatar
    Join Date
    2002-05
    Posts
    1,707
    Login to Give a bone
    0

    Default Re: LISP to change layer colors to random colors

    Quote Originally Posted by tany0070
    try this: . . . (setq s1 (ssget "X" '((8 . "layer name")))) . . .
    We are talking layers not objects, you can not ssget a layer.

    : ) Happy Computing !

    kennet

  9. #9
    Member
    Join Date
    2007-04
    Location
    Boise, ID
    Posts
    47
    Login to Give a bone
    0

    Thumbs up Re: LISP to change layer colors to random colors

    Thanks "kennet.sjoberg" and "Terry Cad"! Both of your Lisps work flawlessly with the same result. Now I have to figure out which to use!

  10. #10
    100 Club
    Join Date
    2007-02
    Posts
    148
    Login to Give a bone
    0

    Default Re: LISP to change layer colors to random colors

    Quote Originally Posted by kennet.sjoberg
    We are talking layers not objects, you can not ssget a layer.

    : ) Happy Computing !

    kennet
    oh my bad sorry.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 2010-06-20, 11:58 AM
  2. Lisp to change layers colors in viewport
    By moonlight_9630 in forum AutoCAD General
    Replies: 1
    Last Post: 2008-06-09, 05:00 AM
  3. Change Layer colors to Plot gray on a few Layouts
    By andrewp in forum AutoCAD General
    Replies: 5
    Last Post: 2006-12-12, 11:05 PM
  4. Layer Colors change in Paperspace
    By MikeM4OSU in forum AutoCAD General
    Replies: 2
    Last Post: 2006-02-07, 07:59 PM
  5. Compare layer colors to pantone colors
    By gandre in forum AutoCAD LT - General
    Replies: 5
    Last Post: 2005-11-18, 01:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •