+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Full Member
    Using
    AutoCAD 2010
    Join Date
    Jul 2011
    Posts
    28

    Smile Putting each Solid Object in a separate layer with RANDOM color for each layer

    Registered forum members do not see this ad.

    Hello to all

    I have been using a lisp rutine to put each solid object onto a separate layer for exporting to another application. The lisp works great but it assigns only one color to all layers (Color red). Is there a way to apply a random color to each layer? Below is the routine i use to do this.

    Thank you for your help

    Code:
     
    (defun C:l2l (/ a lname b index b1 name n)
    (setvar "regenmode" 0)
    (setq a (ssget))
    (setq lname "3d" )
    (setq b 1)
    (setq n (sslength a))
    (setq index 0)
    (repeat n
       (setq b1 (entget (ssname a index)))
       (setq index (1+  index))
       (setq b2 (rtos b 2 0))
       (setq na (strcat lname b2))
       (command "layer" "n" na "" "")
       (command "layer" "s" na "c" 1 "" "s" 0 "")
       (setq c (assoc  8 b1))
       (setq d (cons (car c)na))
        (setq e (subst d c b1))
        (entmod e)
    ;   (command "change" !b1 "")
    ;   (command "change" "p" "" "p" "la" na "" "")
       (setq b (1+  b))
       )
       (setvar "regenmode" 1)
    )
    (defun C:l2lA (/ a lname b index b1 name n)
    (setvar "regenmode" 0)
    (setq a (ssget))
    (setq lname (getstring "\Enter the starting string (A MAXIMUM OF 5 CHARACTERS): "))
    (setq b 1)
    (setq n (sslength a))
    (setq index 0)
    (repeat n
       (setq b1 (entget (ssname a index)))
       (setq index (1+  index))
       (setq b2 (rtos b 2 0))
       (setq na (strcat lname b2))
       (command "layer" "n" na "" "")
       (command "layer" "s" na "c" 1 "" "s" 0 "")
       (setq c (assoc  8 b1))
       (setq d (cons (car c)na))
        (setq e (subst d c b1))
        (entmod e)
    ;   (command "change" !b1 "")
    ;   (command "change" "p" "" "p" "la" na "" "")
       (setq b (1+  b))
       )
    (setvar "regenmode" 1)
    )

  2. #2
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,710

    Default

    Very quickly written:

    Code:
    (defun c:Solids2Layers ( / _padzeros a b e i l n p s )
        (setq p "3d")
    
        (defun _padzeros ( s l )
            (if (< (strlen s) l) (_padzeros (strcat "0" s) l) s)
        )
        (if (setq s (ssget "_:L" '((0 . "*SOLID"))))
            (progn
                (setq
                    i (sslength s)
                    l (1+ (fix (/ (log i) (log 10))))
                    n 0
                )
                (repeat i
                    (setq e (entget (ssname s (setq i (1- i)))))
                    (entmod
                        (subst
                            (cons  8 (strcat p (_padzeros (itoa (setq n (1+ n))) l)))
                            (assoc 8 e)
                            e
                        )
                    )
                )
                (setq n 0)
                (while (setq a (tblnext "LAYER" (null a)))
                    (if (wcmatch (setq b (cdr (assoc 2 a))) (strcat p "*"))
                        (entmod
                            (setq b (entget (tblobjname "LAYER" b))
                                  b (subst (cons 62 (setq n (1+ (rem n 254)))) (assoc 62 b) b)
                            )
                        )
                    )
                )
            )
        )
        (princ)
    )
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  3. #3
    Full Member
    Using
    AutoCAD 2010
    Join Date
    Jul 2011
    Posts
    28

    Default

    Hi Lee

    Thank you for your help. One question. Do I need to use your lisp AFTER i use the L2L routine or your code will also select the solids and put them all in separate layers with different layer names and colors? Also, is the a way you could add your code to the original lisp routine?

    Thank you again for helping.

    ADSK

  4. #4
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,710

    Default

    Registered forum members do not see this ad.

    My code is independent of the code you have posted, it will prompt the user for a selection, move the objects to separate layers, and change the colours of these layers.

    Lee
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

Similar Threads

  1. save each layer in a separate file
    By x.bonnet in forum AutoLISP, Visual LISP & DCL
    Replies: 33
    Last Post: 11th Dec 2011, 11:40 pm
  2. Replies: 2
    Last Post: 24th Nov 2010, 11:43 am
  3. Replies: 5
    Last Post: 19th Oct 2010, 01:36 pm
  4. Cannot Change Layer Color for One Object
    By David_Feynman in forum AutoCAD Beginners' Area
    Replies: 3
    Last Post: 21st Feb 2010, 05:04 am
  5. Layer color changes with Layer State
    By Crazy J in forum AutoCAD Drawing Management & Output
    Replies: 0
    Last Post: 15th Feb 2010, 11:18 pm

Bookmarks

Posting Permissions

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