Results 1 to 4 of 4

Thread: Lisp routine for changing an object's layer's colour (not object's colour)...

  1. #1
    Member
    Join Date
    2015-08
    Location
    Brisbane, Queensland, Australia
    Posts
    13
    Login to Give a bone
    0

    Question Lisp routine for changing an object's layer's colour (not object's colour)...

    Hi,

    I have unsuccessfully searched the AutoLISP forum for a routine to help deal with a few received dwg files generated in Archicad. At least I think it's Archicad. All the layers are white, with every object even having a colour override of 'white'. Pure pain.

    My first point of call with these non-Autodesk dwg files was to use setbylayer (changing only the colour properties) to rid myself of all object's colour overrides.

    As the layer names are insanely vague, I would like to be able to change all of the layer's colours to specific colours that will suit my .ctb file. I would like to simply select objects and have the routine invoke the Colour Index Table so I can choose a new colour for the selected object's layer.

    I do not want to create colour overrides of any object (it's a pet hate). I wish to only change a layer's colour by selecting an object.

    For example, if I pick on one of the gridlines, the lisp routine will have the Colour Index dialog box come up so I can manually change the gridline layer's colour to, say, 250. Then, by repeating the command and selecting a line that is an outline of a concrete column, I can change the coloumn's layer colour to, say, 100. That way I create global colour changes.

    If a lisp routine like this exists, could you please direct me to where it has been posted...

    Thank you.

    Luke

  2. #2
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Lisp routine for changing an object's layer's colour (not object's colour)...

    The first rule of LISP, is that LISP may not always the best tool for the job.

    Rather than using LISP to change a layer's color, consider using the LAYMRG Command to merge an undesired layer (and the entities on it) into one of your standard layers (and effectively remove the undesired layer from the drawing). Of course, you could always isolate layers and change their layer to the correct standard layer as well (SETBYLAYER Command may still be required).

    If you're getting drawings like this frequently, then you might also consider the LAYTRANS Command to do the grunt work for you en-mass.



    Now, if you're still interested in a LISP routine, and assuming you've already run SETBYLAYER to mitigate entity overrides, this old LISP routine will help you to change one or more Layer colors using standard color index:

    Code:
    (vl-load-com)
    
    (defun c:CLC () (c:ChangeLayerColor))
    (defun c:ChangeLayerColor
           (/ *error* ss color acDoc oLayers oLayer layers regen)
      (princ "\rCHANGELAYERCOLOR ")
    
      (defun *error* (msg)
        (if acDoc
          (progn
            (vla-endundomark acDoc)
            (if regen
              (vla-regen acDoc acAllViewports)
            )
          )
        )
        (cond ((not msg))                                                   ; Normal exit
              ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
              ((princ (strcat " ** Error: " msg " ** ")))
        )                                                                   ; Fatal error, display it
        (princ)
      )
    
      (if (not (setq ss (ssget "_i")))
        (progn
          (prompt "\nSelect entity on layer to change color: ")
          (setq ss (ssget))
        )
      )
    
      (if (= 1 (sslength ss))
        (setq color
               (cdr
                 (assoc 62
                        (tblsearch "layer"
                                   (cdr (assoc 8 (entget (ssname ss 0))))
                        )
                 )
               )
        )
      )
    
      (if
        (and ss
             (princ "\nSelect replacement layer color: ")
             (setq color (acad_colordlg
                           (if color
                             color
                             1
                           )
                           nil
                         )
             )
        )
        (progn
          (vla-startundomark
            (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
          )
          (setq oLayers (vla-get-layers acDoc))
          (vlax-for x (setq ss (vla-get-activeselectionset acDoc))
            (if
              (not
                (vl-position
                  (setq oLayer (vla-item oLayers (vla-get-layer x)))
                  layers
                )
              )
               (setq layers (cons oLayer layers))
            )
          )
          (vla-delete ss)
          (foreach oLayer layers
            (if (or regen
                    (= :vlax-true (vla-get-lock oLayer))
                )
              (setq regen T)
            )
            (vla-put-color oLayer color)
            (vla-put-lineweight oLayer aclnwtbylwdefault)
          )
        )
      )
      (*error* nil)
    )

    Or, if you'd instead like to simply move the selected entities to one of your standard layers (which already has the correct color assignment), and set the entities to ByLayer color in one step, you could use this old routine, which will also get-or-create the layer name you enter, remembers the last layer you entered (per drawing), and supports undo functionality (for when you fat-finger a layer name typo Haha):

    Code:
    (vl-load-com)
    
    (defun c:MOLAY () (c:MoveToLayer))
    (defun c:MoveToLayer (/ *error* layerName acDoc oLayer ss)
      (princ "\rMOVETOLAYER ")
    
      (defun *error* (msg)
        (if ss (vla-delete ss))
        (if acDoc (vla-endundomark acDoc))
        (cond ((not msg))                                                   ; Normal exit
              ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
              ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
        )
        (princ)
      )
    
      (if
        (and
          (ssget "_:L")
          (or (/= ""
                  (setq layerName
                         (strcase
                           (getstring
                             T
                             (strcat "\nMove selection to layer name "
                                     (if *MoveToLayerName*
                                       (strcat " <" *MoveToLayerName* ">: ")
                                       ": "
                                     )
                             )
                           )
                         )
                  )
              )
              (setq layerName *MoveToLayerName*)
          )
          (setq *MoveToLayerName* layerName)
        )
         (progn
           (vla-startundomark
             (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
           )   
           (setq oLayer (vla-add (vla-get-layers acDoc) layerName))
           (vlax-for x (setq ss (vla-get-activeselectionset acDoc))
             (vl-catch-all-apply 'vla-put-color (list x acbylayer))
             (vla-put-layer x layerName)
           )
         )
      )
    
      (*error* nil)
    )

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  3. #3
    I could stop if I wanted to
    Join Date
    2015-10
    Location
    Central New Jersey
    Posts
    439
    Login to Give a bone
    0

    Default Re: Lisp routine for changing an object's layer's colour (not object's colour)...

    Check out the Layer Translator. Laytrans. It'll translate their layers to your layers and in the process force everything to 'bylayer'. (or whatever you choose in the settings)

  4. #4
    Member
    Join Date
    2015-08
    Location
    Brisbane, Queensland, Australia
    Posts
    13
    Login to Give a bone
    0

    Default Re: Lisp routine for changing an object's layer's colour (not object's colour)...

    Thank you BlackBox and MMccall for your great input.

    There's only so much care-factor with these received drawings. They will be used as backgrounds that enable us to provide an installation proposal drawing. I don't want to fettle too much with them, just freeze the layers that are clutter when compared to the installation story I need to tell and change the remaining layers colours to suit my .ctb file. I'm not interested in taking any sort of ownership of them, just make them comply with how we show what we plan to install, and where, in a way that has our gear pop, and their background simply be there to give reference to our installation locations. Similar to what electrical draftspeople do, except with a fair bit more finesse.

    BlackBox, thank you, that first lisp routine looks the goods. I'll give her tank a splash of fuel and see how she runs!

    L

Similar Threads

  1. Replies: 1
    Last Post: 2017-10-04, 07:48 PM
  2. Replies: 1
    Last Post: 2016-01-31, 03:08 AM
  3. Replies: 4
    Last Post: 2012-08-15, 04:59 PM
  4. Replies: 5
    Last Post: 2006-12-11, 09:45 AM
  5. Changing layer colour
    By JASONM30395 in forum AutoCAD Tips & Tricks
    Replies: 6
    Last Post: 2005-08-30, 11:38 AM

Tags for this Thread

Posting Permissions

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