Learn the basics of AutoCAD and More!

Lesson 5.05 – Using Built-in Functions

🧠 Lesson 5-5 – Using Built-in Functions in AutoLISP

Learn essential AutoLISP functions to store values, define commands, and manipulate data.


πŸ“š What You’ll Learn

By the end of this lesson, you’ll be able to:

  • Use key AutoLISP functions like setq, defun, princ, and prompt
  • Perform basic math operations using built-in functions
  • Combine and manipulate strings
  • Understand how these functions fit into real-world AutoCAD routines

🧠 Why It Matters

AutoLISP offers dozens of built-in functions, but a few form the core of almost every routine. Mastering these will help you write smarter scripts, capture user input, and output clean results to the user.


πŸ› οΈ Tools You’ll Use

FunctionPurpose
setqAssigns values to variables
defunDefines a new function or command
princSilently returns control to AutoCAD
promptDisplays a message in the command line
+, -, *, /Perform arithmetic operations
strcatCombine strings
strlenReturn string length

🧭 Lesson Structure

1️⃣ setq – Assign Values

setq (short for set quoted) is used to store a value in a variable.

(setq a 10)
(setq name "AutoLISP")
ExampleDescription
(setq x 5)Assign 5 to x
(setq pt '(0 0))Assign a list (0, 0) to pt
(setq full (strcat "Hello, " name))Combine and assign text

2️⃣ defun – Define a Function

Use defun to define a new AutoLISP function.

(defun c:demo ( )
(prompt "\nRunning demo command...")
(princ)
)
SyntaxDescription
(defun c:demo () ...)Defines a command that can be typed in AutoCAD
c: prefixEnsures it works from the command line

3️⃣ prompt vs. princ

FunctionPurposeExample
promptWrites a message to command line(prompt "\\nStart drawing...")
princSilently ends a routine(princ)

πŸ”Ή Always end your custom routines with (princ) to avoid cluttering the command line.

4️⃣ Math Functions

AutoLISP supports simple arithmetic with prefix notation.

OperationSyntaxResult
Add(+ 2 3)5
Subtract(- 10 4)6
Multiply(* 3 5)15
Divide(/ 10 2)5.0

You can also assign results to variables:

(setq result (* 4 5))

5️⃣ String Functions

FunctionPurposeExampleResult
strcatConcatenates (joins) strings(strcat "Hello, " "world!")"Hello, world!"
strlenReturns the length of a string(strlen "AutoLISP")8

βœ… Lesson Checklist

TaskCompleted
Used setq to assign numbers and text☐
Defined a command using defun and the c: prefix☐
Displayed a message using prompt☐
Used princ to end a routine silently☐
Performed math and string operations☐

πŸ“Œ Quick Tips

TipWhy It Helps
Always use setq for variable assignmentIt’s clear and reliable
End every command with (princ)Keeps the command line clean
Use prompt only for text messagesFor visual or interactive output
Use strcat only with strings, not numbersAvoids type errors

🧩 Real-World Applications

Use CaseExample Usage
Dynamic block namingCombine prefix and part number using strcat
Area calculatorUse math functions to compute square footage
Layer report toolDisplay string-based summaries in command line

πŸ“ Files and Resources

File / ResourceDescription
builtins_demo.lspSample routine using all functions from this lesson
math_string_worksheet.txtPractice examples for math and string expressions
command_template.lspReusable function template with defun, prompt, etc.

πŸ“– Review Table

FunctionDescriptionExample
setqStore value in variable(setq a 10)
defunDefine a named function(defun c:test () ...)
promptShow message in AutoCAD command line(prompt "\\nStart...")
princCleanly exit a routine(princ)
+, *, -Perform arithmetic operations(+ 2 3), (* a b)
strcatCombine strings(strcat "Layer: " name)