Learn the basics of AutoCAD and More!

Lesson 5.03 – Your First AutoLISP Routine

πŸ§ͺ 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 / FeaturePurpose
DEFUNDefine your custom AutoLISP function
GETPOINTGet user-specified coordinates
COMMANDRun native AutoCAD commands from AutoLISP
PRINCQuietly return control to AutoCAD
.LSP FileSave your routine for reuse
APPLOADLoad 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:

LineWhat it does
(defun c:drawline ...)Creates a custom command drawline
setq linesStores 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

TaskCompleted
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

TipWhy It Helps
Always include (princ) at the endPrevents your function from printing return values
Use c: before the function nameMakes your function callable from the command line
Use comments in your code with ;Improves readability and future editing
Keep test files short and focusedEasier to debug and understand

🧩 Real-World Applications

ScenarioUse Case Example
Surveyor placing property linesAutomate drawing of multiple boundary lines
Architect marking wallsUse consistent layer/color via scripted LINE
Designer prototyping automationTest input prompts and workflows with users

πŸ“ Files and Resources

File / ResourceDescription
drawline.lspYour first AutoLISP routine to draw a line
routine_walkthrough.pdfStep-by-step guide to writing and testing your routine
command_reference.txtList of common AutoCAD commands usable inside AutoLISP

πŸ“– Review Table

Function / KeywordDescriptionExample
DEFUNDefines a function in AutoLISP(defun c:test () ...)
GETPOINTPrompts the user to pick a point(setq pt1 (getpoint "Pick a point: "))
COMMANDRuns AutoCAD command from script(command "LINE" pt1 pt2 "")
PRINCQuietly ends the function(princ)