CADLISPMASTER
Stop wasting time on repetitive AutoCAD tasks. CAD LISP Master provides free, efficient AutoLISP codes and clear tutorials designed to automate your workflow and speed up your drafting.

Quick Color Change in AutoCAD: Set Object & Drawing Color by Number

Quickly change object colors or active drawing colors in AutoCAD by color index number using CEX.lsp. Free AutoLISP script with ByLayer toggle.
Change Object Color and Current Layer Color Instantly

Opening the Properties palette or reaching for the ribbon drop-down menu every time you want to switch object colors in AutoCAD gets tiresome fast. When you are editing complex floor plans or mechanical drawings, those extra mouse movements add up and disrupt your focus.

The CEX.lsp (Color Executive) routine simplifies color management down to a single command-line input. It lets you enter a color index number (1 through 255) to immediately update selected objects—or set your active drawing color (CECOLOR) if nothing is selected. Entering 0 instantly resets your selection or active drawing color back to ByLayer.


Key Benefits

  • Context-Sensitive Switching: Automatically applies the color to selected entities. If no selection is active, it updates your current drawing color for new lines instead.

  • Instant ByLayer Reset: Enter 0 to clear explicit color overrides and revert to ByLayer status without extra clicks.

  • Zero Ribbon Dependency: Keeps your crosshairs right on your work area without navigating to the top ribbon or side properties panel.


Step-by-Step Guide

  1. Load CEX.lsp in AutoCAD using the APPLOAD command.

  2. Type CEX in the command prompt and press Enter.

  3. Type an AutoCAD color index number (1-255) or type 0 for ByLayer.

  4. Select the target objects you want to recolor and hit Enter. (If you press Enter without selecting anything, your default drawing color updates for future objects).


AutoLISP Code (CEX.lsp)

Copy the code below and save it as CEX.lsp inside your AutoCAD support folder:

(defun c:CEX ( / *error* old-cmdecho doc col target-col ss i ent elist )
  (vl-load-com)
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  
  (defun *error* (msg)
    (if old-cmdecho (setvar "cmdecho" old-cmdecho))
    (vla-EndUndoMark doc)
    (princ "\nCAD Lisp [www.daolpro.com]")
    (princ)
  )

  (setq old-cmdecho (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  (vla-StartUndoMark doc)

  (setq col (getint "\nEnter color number (0 for ByLayer, 1-255): "))
  
  (if (and col (>= col 0) (<= col 255))
    (progn
      (if (= col 0)
        (setq target-col 256)
        (setq target-col col)
      )
      (setq ss (ssget))
      (if ss
        (progn
          (setq i 0)
          (while (setq ent (ssname ss i))
            (setq elist (entget ent))
            (if (assoc 62 elist)
              (setq elist (subst (cons 62 target-col) (assoc 62 elist) elist))
              (setq elist (append elist (list (cons 62 target-col))))
            )
            (entmod elist)
            (setq i (1+ i))
          )
        )
        (if (= col 0)
          (setvar "cecolor" "BYLAYER")
          (setvar "cecolor" (itoa col))
        )
      )
    )
    (princ "\nInvalid color number. Please enter between 0 and 255.")
  )

  (vla-EndUndoMark doc)
  (setvar "cmdecho" old-cmdecho)
  (princ "\nCAD Lisp [www.daolpro.com]")
  (princ)
)

Practical Workflow Tip

When organizing drawing layers, keep most geometry set to ByLayer (Color 0) and use explicit color indexes sparingly for temporary highlights or redlines during revision checks.

Post a Comment