How to Automate AutoCAD Using Lisp: Simple Guide
You can automate AutoCAD by writing
Lisp scripts that run commands and manipulate drawings automatically. Use defun to define functions and command to execute AutoCAD commands inside Lisp. Load your Lisp file in AutoCAD and run your custom functions to automate tasks.Syntax
In AutoCAD Lisp, automation scripts are written as functions using defun. The command function runs AutoCAD commands. Variables store data, and princ outputs messages.
(defun functionName () ... ): Defines a new function.(command "COMMAND" args): Runs an AutoCAD command.princ: Prints text to the command line.
lisp
(defun c:hello ()
(princ "Hello, AutoCAD!")
(princ)
)Output
Hello, AutoCAD!
Example
This example creates a simple Lisp function that draws a line between two points automatically when run in AutoCAD.
lisp
(defun c:drawline () (command "LINE" '(0 0) '(100 100) "") (princ "\nLine drawn from (0,0) to (100,100).") (princ) )
Output
Line drawn from (0,0) to (100,100).
Common Pitfalls
Common mistakes include forgetting to end commands with an empty string "", which causes AutoCAD to wait for more input. Also, not using princ at the end of functions can clutter the command line. Avoid naming conflicts by prefixing custom functions with c:.
lisp
(defun c:badline () (command "LINE" '(0 0) '(50 50)) ; Missing "" ends command (princ "This will cause AutoCAD to wait for input.") (princ) ) (defun c:goodline () (command "LINE" '(0 0) '(50 50) "") ; Properly ends command (princ "Line drawn correctly.") (princ) )
Quick Reference
Here is a quick cheat sheet for common Lisp automation commands in AutoCAD:
| Command | Description |
|---|---|
| defun | Defines a new Lisp function |
| command | Executes AutoCAD commands |
| princ | Prints text to command line |
| c: | Prefix for user commands callable in AutoCAD |
| (list x y) | Defines a point coordinate |
Key Takeaways
Use
defun to create reusable automation functions in AutoCAD Lisp.Always end
command sequences with an empty string "" to finish commands properly.Prefix custom functions with
c: to make them callable from AutoCAD command line.Use
princ to print messages and keep the command line clean.Test Lisp scripts in a safe drawing to avoid unintended changes.