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.

AutoCAD Color-Matched Layer Generator: Create Layers 1 to 255

Automatically create numerical layers and assign matching ACI colors in AutoCAD using CLA.lsp. Free AutoLISP script supporting single layers and range
Create Multiple Layers and Assign Colors Instantly

In structural detailing, reinforcement drafting, or MEP workflows, drawings often rely on standardized numerical layer systems (where Layer 1 is Red, Layer 2 is Yellow, Layer 3 is Green, and so on). Manually opening the Layer Manager to create dozens of layers, typing numbers one by one, and assigning matching AutoCAD Index Color (ACI) values is a huge time sink during initial project setup.

The CLA.lsp (Create Layers Automatically) routine turns this setup process into a single command prompt entry. You can generate individual layers or a whole sequential range (such as 1-20 or 1~50) in seconds, automatically matching each layer's color index to its numerical name.


Why Use CLA.lsp for Layer Setup?

  • Range Input Support: Supports both hyphens (-) and tildes (~) to define ranges. Typing 1-15 or 1~15 instantly builds layers 1 through 15 in sequence.

  • Auto Color Mapping: Automatically assigns ACI color numbers corresponding directly to the layer name—no manual color picker adjustments required.

  • Single or Bulk Generation: Need just a single color layer? Type a single number like 5 to create layer 5 with color 5 (Blue) instantly.

  • Non-Destructive Execution: Creates new layers without modifying existing layer properties if those layers already exist in your drawing.


Step-by-Step Instructions

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

  2. Type CLA in the command line and hit Enter.

  3. Enter your desired layer number or range when prompted:

    • For a single layer: Type 5

    • For a layer range: Type 1-10 (or 1~10)

  4. Hit Enter. The script builds the layers in the background and reports a success message in your command window.


AutoLISP Code (CLA.lsp)

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

(defun c:CLA ( / old-err old-cmd old-osn inp pos start end i)
  (setq old-err *error*)
  (defun *error* (msg)
    (if (= (type command-s) 'SUBR)
      (command-s "._undo" "_end")
      (command "._undo" "_end")
    )
    (setvar "CMDECHO" old-cmd)
    (if old-osn (setvar "OSMODE" old-osn))
    (setq *error* old-err)
    (princ "\nCAD Lisp [www.daolpro.com]")
    (princ)
  )
  (setq old-cmd (getvar "CMDECHO"))
  (setq old-osn (getvar "OSMODE"))
  (setvar "CMDECHO" 0)
  (command "._undo" "_begin")
  (setq inp (getstring "\nEnter layer number or range (e.g., 3 or 1-10): "))
  (cond
    ((vl-string-search "-" inp)
     (setq pos (vl-string-search "-" inp))
     (setq start (atoi (substr inp 1 pos)))
     (setq end (atoi (substr inp (+ pos 2)))))
    ((vl-string-search "~" inp)
     (setq pos (vl-string-search "~" inp))
     (setq start (atoi (substr inp 1 pos)))
     (setq end (atoi (substr inp (+ pos 2)))))
    (t
     (setq start (atoi inp))
     (setq end start))
  )
  (if (and (> start 0) (<= start 255) (> end 0) (<= end 255) (<= start end))
    (progn
      (setq i start)
      (while (<= i end)
        (command "._-layer" "_n" (itoa i) "_c" i (itoa i) "")
        (setq i (1+ i))
      )
      (princ (strcat "\n[Success] Created layers from " (itoa start) " to " (itoa end) "."))
    )
    (princ "\n[Error] Invalid input. Please enter a valid number or range (1-255).")
  )
  (command "._undo" "_end")
  (setvar "CMDECHO" old-cmd)
  (setq *error* old-err)
  (princ "\nCAD Lisp [www.daolpro.com]")
  (princ)
)

Project Template Tip

Add CLA.lsp to your Startup Suite via APPLOAD so you can spin up color-matched numerical layers instantly whenever starting a fresh drawing from scratch.

Post a Comment