π§ͺ Lesson 5.3 β Your First AutoLISP Routine
Write, test, and run a simple AutoLISP routine that interacts with the user and draws geometry.
π What Youβll Learn
By the end of this lesson, youβll be able to:
- Create a basic AutoLISP function with user input
- Use AutoCAD commands inside AutoLISP using
(command ...)
- Define a custom command you can run from the command line
- Save and load your LISP file into AutoCAD
π§ Why It Matters
The true power of AutoLISP lies in creating custom tools tailored to your drafting needs. This lesson walks you through building a simple but functional commandβyour first step toward full CAD automation.
π οΈ Tools Youβll Use
Tool / Feature | Purpose |
---|---|
DEFUN | Define your custom AutoLISP function |
GETPOINT | Get user-specified coordinates |
COMMAND | Run native AutoCAD commands from AutoLISP |
PRINC | Quietly return control to AutoCAD |
.LSP File | Save your routine for reuse |
APPLOAD | Load the routine into AutoCAD |
π§ Lesson Structure
1οΈβ£ Plan the Routine
The goal: Create a custom command called drawline
that:
- Prompts the user for two points
- Draws a line between them
- Returns control silently
2οΈβ£ Write the Code
Open Notepad or VLIDE and enter the following:
(defun c:drawline ( / pt1 pt2 )
(setq pt1 (getpoint "\\nSpecify first point: "))
(setq pt2 (getpoint "\\nSpecify second point: "))
(command "LINE" pt1 pt2 "")
(princ)
)
Explanation of the code:
Line | What it does |
---|---|
(defun c:drawline ...) | Creates a custom command drawline |
setq lines | Stores user-selected points |
(command "LINE" ...) | Calls the AutoCAD LINE command using those points |
(princ) | Ends the function cleanly without extra output |
3οΈβ£ Save the File
- Save the file as
drawline.lsp
- Be sure to use quotation marks when saving in Notepad:
"drawline.lsp"
β this forces the.lsp
extension
4οΈβ£ Load the Routine
In AutoCAD:
- Type
APPLOAD
- Browse to
drawline.lsp
- Click Load
- You should see a message in the command line confirming it loaded
5οΈβ£ Run the Command
Now type:
drawline
- AutoCAD will prompt:
Specify first point:
- Then:
Specify second point:
- A line will be drawn between those points
- π Congratulations! Youβve written and executed your first AutoLISP tool.
β Lesson Checklist
Task | Completed |
---|---|
Wrote a custom AutoLISP function | β |
Saved file with a .lsp extension | β |
Loaded the file into AutoCAD using APPLOAD | β |
Ran the command successfully from the AutoCAD prompt | β |
Understood how to collect user input and draw geometry | β |
π Quick Tips
Tip | Why It Helps |
---|---|
Always include (princ) at the end | Prevents your function from printing return values |
Use c: before the function name | Makes your function callable from the command line |
Use comments in your code with ; | Improves readability and future editing |
Keep test files short and focused | Easier to debug and understand |
π§© Real-World Applications
Scenario | Use Case Example |
---|---|
Surveyor placing property lines | Automate drawing of multiple boundary lines |
Architect marking walls | Use consistent layer/color via scripted LINE |
Designer prototyping automation | Test input prompts and workflows with users |
π Files and Resources
File / Resource | Description |
---|---|
drawline.lsp | Your first AutoLISP routine to draw a line |
routine_walkthrough.pdf | Step-by-step guide to writing and testing your routine |
command_reference.txt | List of common AutoCAD commands usable inside AutoLISP |
π Review Table
Function / Keyword | Description | Example |
---|---|---|
DEFUN | Defines a function in AutoLISP | (defun c:test () ...) |
GETPOINT | Prompts the user to pick a point | (setq pt1 (getpoint "Pick a point: ")) |
COMMAND | Runs AutoCAD command from script | (command "LINE" pt1 pt2 "") |
PRINC | Quietly ends the function | (princ) |