Customising AutoCAD: Creating Macros and LISP Routines

Every AutoCAD user eventually finds themselves repeating the same sequences of commands: drawing a standard detail, applying a consistent layer setup, running a multi-step annotation process, or performing the same edit on dozens of objects. At that point, the question becomes: can the software do this for me?

The answer is yes — and AutoCAD provides two primary avenues for automation. The first is macros: recorded or written sequences of commands that execute in order when triggered. The second is AutoLISP: a full programming language embedded in AutoCAD that allows you to write true custom commands with logic, loops, user input, and access to AutoCAD’s full object database.

This guide covers both approaches, starting with the more accessible macro system before moving into the fundamentals of AutoLISP programming. No prior programming experience is assumed for the LISP introduction — the examples are designed to be practical and immediately usable, building up in complexity.

These features are available in AutoCAD 2023–2026 (£39.99 from GetRenewedTech). AutoCAD LT does not support AutoLISP, so full AutoCAD is required for the programming section.

AutoCAD Macros: Scripted Command Sequences

An AutoCAD macro is a text string of commands and parameters that AutoCAD executes when the macro is triggered — from a button, a toolbar, a keyboard shortcut, or a CUI (Customise User Interface) entry.

Macro Syntax

Macros use a simple text syntax:

  • ^C^C — Cancels the current command (equivalent to pressing Escape twice). Always start macros with this.
  • ; — Equivalent to pressing Enter (confirms a command or input)
  • (space) — Also equivalent to Enter in most contexts
  • \ — Pauses for user input (allows the user to select a point, type a value, etc.)
  • * — At the start of a macro, repeats the command until cancelled

Example: a macro to create a standard annotation layer and make it current:

^C^C-LAYER;M;ANNOTATION;C;7;;S;ANNOTATION;;

This macro: cancels current command → enters the command-line LAYER command → Make option → layer name ANNOTATION → Colour option → colour 7 (white/black) → confirm (empty Enter) → Set current → layer name ANNOTATION → confirm twice.

Assigning Macros to Buttons via CUI

  1. Type CUI to open the Customise User Interface editor.
  2. In the left panel, expand Toolbars or find Quick Access Toolbar.
  3. To create a new button: right-click a toolbar and click New Button.
  4. In the right panel, enter the button Name, Description, and Macro.
  5. Assign an image to the button from the library or create a custom one.
  6. Drag the button to your preferred toolbar position.
  7. Click Apply and OK.

For keyboard shortcuts, expand Keyboard Shortcuts → Shortcut Keys in the CUI editor. Click New Shortcut Key, enter the key combination (e.g., Alt+L), and assign the macro.

Practical Macro Examples

Insert and explode a block (useful when you always want a particular block exploded):

^C^C_INSERT;MyBlockName;\;1;1;0;_EXPLODE;L;;

Draw a rectangle then hatch it immediately:

^C^CRECTANG;\;\;HATCH;SOLID;\;;

Move selected objects to the ANNOTATION layer:

^C^CCHPROP;\;;LA;ANNOTATION;;

Zoom to fit and plot to PDF:

^C^CZOOM;E;PLOT;\;\;\;\;YES;

Script Files: Batch Automation

Script files (.scr) are plain text files containing sequences of AutoCAD commands, executed in order when the script is loaded. Unlike macros (which are assigned to buttons), scripts are loaded via SCRIPT command and useful for batch operations.

Example script to batch-set up layers in a new drawing (save as setup_layers.scr):

-LAYER
M
STRUCTURAL
C
1

M
ARCHITECTURAL
C
3

M
MECHANICAL
C
4

M
ANNOTATION
C
7

S
STRUCTURAL

ZOOM
E

Load and run this script in any drawing by typing SCRIPT, browsing to the .scr file, and clicking Open. AutoCAD executes each line in sequence.

For batch processing multiple drawings, use AutoCAD’s Action Recorder or the BATCHPLT / PUBLISH command. Alternatively, AutoLISP with the COMMAND function can process multiple drawings programmatically.

AutoLISP: Introduction to Programming AutoCAD

AutoLISP is a dialect of LISP (List Processing) that is embedded in AutoCAD and gives you access to nearly everything AutoCAD can do. You can create custom commands, automate multi-step workflows, read and write drawing entity properties, interact with the user, and perform calculations.

Where to Write and Test LISP Code

The simplest place to start is the AutoCAD Visual LISP Editor:

  1. Type VLIDE to open the Visual LISP Integrated Development Environment.
  2. The editor provides syntax highlighting, code completion, and a console for testing expressions.
  3. Alternatively, type (your-lisp-expression) directly in the AutoCAD command line for quick testing.

LISP files have the extension .lsp. Load a LISP file into AutoCAD with:

(load "C:\\LispRoutines\\myroutine.lsp")

To load automatically when AutoCAD starts, add the load command to acaddoc.lsp (runs in each drawing session) or acad.lsp (runs once at startup).

LISP Syntax Basics

LISP expressions are always wrapped in parentheses. The first element inside is the function name; the rest are arguments:

; Basic arithmetic
(+ 5 3)         ; Returns 8
(* 10 2.5)      ; Returns 25.0
(/ 100 4)       ; Returns 25

; String functions
(strcat "Hello" " " "World")  ; Returns "Hello World"
(strlen "AutoCAD")             ; Returns 7

; Variables
(setq myVar 42)
(setq myString "Layer1")
(setq myList '(1 2 3 4 5))

; Conditionals
(if (> myVar 10)
  (alert "Variable is greater than 10")
  (alert "Variable is 10 or less")
)

Running AutoCAD Commands from LISP

The command function executes AutoCAD commands from LISP:

; Draw a circle at point (100,100) with radius 50
(command "CIRCLE" '(100 100) 50)

; Create a layer named "ANNOTATION" with colour 7
(command "-LAYER" "M" "ANNOTATION" "C" "7" "" "S" "ANNOTATION" "")

; Move the last created object 50mm in X direction
(command "MOVE" "L" "" '(0 0) '(50 0))

Getting User Input

; Ask user to select a point
(setq pt1 (getpoint "\nClick start point: "))

; Ask user to enter a distance
(setq dist (getdist pt1 "\nEnter length: "))

; Ask user to type a string
(setq layerName (getstring "\nEnter layer name: "))

; Ask user to select an object
(setq selObj (entsel "\nSelect an object: "))

Working with Drawing Entities

AutoLISP can read and modify any property of any entity in the drawing:

; Get the last entity
(setq lastEnt (entlast))

; Get all properties of the entity as an association list
(setq entProps (entget lastEnt))

; Read a specific property: the layer name (group code 8 = layer)
(setq layerName (cdr (assoc 8 entProps)))

; Change the entity's layer
(entmod (subst (cons 8 "NewLayerName") (assoc 8 entProps) entProps))

Writing Your First Real LISP Routine

Here is a practical routine that draws a circle and automatically labels it with its diameter. This combines user input, drawing commands, and annotation:

; Circle with diameter label
(defun C:LABELCIRCLE (/ cen rad diam labelPt labelText)
  ; Get circle centre from user
  (setq cen (getpoint "\nClick circle centre: "))
  ; Get radius from user
  (setq rad (getdist cen "\nEnter radius: "))
  ; Calculate diameter
  (setq diam (* rad 2))
  ; Draw the circle
  (command "CIRCLE" cen rad)
  ; Format the label text
  (setq labelText (strcat "Ø" (rtos diam 2 0)))
  ; Calculate label position (offset from centre)
  (setq labelPt (list (+ (car cen) rad 10) (cadr cen)))
  ; Place the text
  (command "TEXT" labelPt 3.5 0 labelText)
  ; Return nil cleanly
  (princ)
)

After loading this routine, type LABELCIRCLE in AutoCAD to run it as a command. The C: prefix is what makes it a command that users can type at the command prompt.

Iterating Over Multiple Objects: Selection Sets

; Change all objects on a specified layer to a new layer
(defun C:MOVELAYER (/ oldLayer newLayer ss i ent entProps)
  (setq oldLayer (getstring "\nSource layer name: "))
  (setq newLayer (getstring "\nDestination layer name: "))
  ; Create a selection set of all objects on oldLayer
  (setq ss (ssget "X" (list (cons 8 oldLayer))))
  (if ss
    (progn
      (setq i 0)
      (repeat (sslength ss)
        (setq ent (ssname ss i))
        (setq entProps (entget ent))
        (entmod (subst (cons 8 newLayer) (assoc 8 entProps) entProps))
        (setq i (1+ i))
      )
      (princ (strcat "\nMoved " (itoa (sslength ss)) " objects."))
    )
    (princ "\nNo objects found on that layer.")
  )
  (princ)
)

AutoLISP Best Practices

  • Always use local variables: Declare variables as local in the function definition to avoid polluting the global namespace: (defun C:MYCOMMAND (/ var1 var2 var3) ...). Variables listed after the slash are local.
  • End functions with (princ): This suppresses the nil return value from echoing to the command line — users should see clean output.
  • Handle errors gracefully: Use *error* function to clean up and print a friendly error message if something goes wrong.
  • Comment your code: LISP comments start with a semicolon. Future you (and colleagues) will thank present you for clear comments.
  • Test in a throwaway drawing: Buggy LISP code can corrupt a drawing. Test new routines in a file you do not mind discarding.
  • Back up your LISP files: Store them in source control or at minimum in cloud backup. They represent significant intellectual investment.

Going Further: ObjectARX and .NET APIs

AutoLISP is the entry point for AutoCAD programming, but for more demanding automation — particularly anything requiring a custom dialogue box, deep integration with Windows APIs, or very high performance — Autodesk also provides:

  • Visual LISP with ActiveX: The (vlax-create-object) and related functions give LISP access to AutoCAD’s COM automation interface, opening up capabilities beyond the basic command function approach.
  • AutoCAD .NET API: Develop AutoCAD plugins in C# or VB.NET using Visual Studio. This is the most powerful approach and is used by professional add-on developers.
  • ObjectARX: C++ API for maximum performance. Used for creating custom object types, not just commands.

For most practitioners, AutoLISP covers the vast majority of useful automation. The .NET API becomes relevant when you need custom dialogue boxes, integration with external databases, or very complex object-oriented logic.

Conclusion

AutoCAD customisation pays dividends every day. Even the simplest macro that eliminates a five-step process saves time and reduces errors. AutoLISP takes this further — routine tasks that used to take an hour can run in seconds when properly automated, and the consistency of programmatically generated geometry eliminates the small variations that accumulate in manual drafting.

Start with macros for your most repetitive button-click sequences. Progress to script files for batch operations. Then, when you find yourself thinking “if only AutoCAD could just do X automatically”, that is the cue to write your first LISP routine. AutoCAD 2023–2026 is available from GetRenewedTech for £39.99.

Leave a Reply

Your email address will not be published. Required fields are marked *