How to Use AutoLISP in AutoCAD: Simple Guide for Beginners
To use
AutoLISP in AutoCAD, write your Lisp code in a text editor and load it into AutoCAD using the APPLOAD command. Then run your functions by typing their names in the command line to automate tasks or customize drawings.Syntax
AutoLISP code is written using parentheses to group commands and functions. The basic syntax includes defining functions with (defun functionName (parameters) body). You can use built-in functions to interact with AutoCAD objects and commands.
- defun: Defines a new function.
- functionName: The name you give your function.
- parameters: Inputs your function can take, inside parentheses.
- body: The code that runs when the function is called.
lisp
(defun functionName (parameters)
; function body
)Example
This example defines a simple AutoLISP function that draws a circle at the origin with radius 5. It shows how to define a function, call AutoCAD commands, and load the code.
lisp
(defun c:DrawCircle () (command "CIRCLE" '(0 0) 5) (princ "\nCircle drawn at origin with radius 5.") (princ) )
Output
Circle drawn at origin with radius 5.
Common Pitfalls
Common mistakes when using AutoLISP include:
- Not loading the Lisp file before running the function.
- Forgetting to prefix custom commands with
c:so AutoCAD recognizes them. - Missing
(princ)at the end of functions, which can cause unwanted output. - Incorrect parentheses causing syntax errors.
lisp
(defun DrawCircle () ; Wrong: missing c: prefix (command "CIRCLE" '(0 0) 5) (princ)) (defun c:DrawCircle () ; Correct (command "CIRCLE" '(0 0) 5) (princ))
Quick Reference
| Command | Description |
|---|---|
| APPLOAD | Load AutoLISP files into AutoCAD |
| defun | Define a new AutoLISP function |
| command | Call AutoCAD commands from Lisp |
| princ | Print text to command line and suppress output |
| c:FunctionName | Prefix to make function callable as command |
Key Takeaways
Write AutoLISP code in a text editor and load it with APPLOAD in AutoCAD.
Prefix functions with c: to run them as commands in AutoCAD.
Use (command ...) to call AutoCAD commands from AutoLISP.
Always end functions with (princ) to avoid unwanted output.
Check parentheses carefully to avoid syntax errors.