Bird
Raised Fist0
AutoCADbi_tool~20 mins

AutoLISP basics for automation in AutoCAD - Real Business Scenario

Choose your learning style10 modes available
Scenario Mode
👤 Your Role: You are a CAD technician working in a construction company.
📋 Request: Your manager wants you to automate the process of labeling room areas in floor plans to save time and reduce errors.
📊 Data: You have floor plan drawings with rooms outlined as closed polylines. Each room needs a label showing its area in square meters placed at the centroid of the room.
🎯 Deliverable: Create an AutoLISP script that calculates the area of each room polyline and places a text label with the area at the centroid of that polyline.
Progress0 / 4 steps
Sample Data
Room IDPolyline Coordinates
101(0,0), (5,0), (5,4), (0,4)
102(6,0), (10,0), (10,3), (6,3)
103(0,5), (4,5), (4,9), (0,9)
1
Step 1: Open AutoCAD and load the floor plan drawing containing the room polylines.
No formula needed.
Expected Result
Floor plan drawing with closed polylines representing rooms is open.
2
Step 2: Write an AutoLISP function to calculate the area of a closed polyline.
(defun c:RoomAreaLabel () (vl-load-com) (setq ss (ssget "X" '((0 . "LWPOLYLINE")))) (if ss (progn (setq i 0) (repeat (sslength ss) (setq ent (ssname ss i)) (setq area (vlax-curve-getarea ent)) (setq pt (vlax-curve-getpointatparam ent (/ (vlax-curve-getendparam ent) 2.0))) (command "TEXT" pt 2.0 0.0 (strcat (rtos area 2 2) " m2")) (setq i (1+ i))))) (princ))
Expected Result
AutoLISP function defined to select all polylines, calculate their area, and place text labels.
3
Step 3: Run the AutoLISP function to label all room areas automatically.
Type 'RoomAreaLabel' in the AutoCAD command line and press Enter.
Expected Result
Each room polyline is labeled with its area in square meters at the centroid.
4
Step 4: Verify the labels are correctly placed and the area values are accurate.
Manually check a few room areas by measuring or using AutoCAD properties.
Expected Result
Labels match the actual room areas and are positioned at the center of each room.
Final Result
Floor Plan with Room Area Labels

+---------------------+
| Room 101: 20.00 m2  |
|                     |
| Room 102: 12.00 m2  |
|                     |
| Room 103: 16.00 m2  |
+---------------------+
✓Automation saves time by labeling all rooms at once.
✓Area labels help in quick verification of room sizes.
✓Placing labels at centroids improves readability.
Bonus Challenge

Modify the AutoLISP script to also label the perimeter of each room polyline.

Show Hint
Use the 'vlax-curve-getdistatparam' function with 'vlax-curve-getendparam' to calculate perimeter.

Practice

(1/5)
1. What is the main purpose of using AutoLISP in AutoCAD?
(defun) is often used to define what?
easy
A. To export drawings to other formats
B. To manually draw shapes faster
C. To automate repetitive tasks by defining functions
D. To change the color of objects automatically

Solution

  1. Step 1: Understand AutoLISP purpose

    AutoLISP is designed to automate tasks in AutoCAD to save time and reduce errors.
  2. Step 2: Recognize (defun) usage

    (defun) defines a function that can be run as a command to automate tasks.
  3. Final Answer:

    To automate repetitive tasks by defining functions -> Option C
  4. Quick Check:

    Automation = Functions with (defun) [OK]
Hint: AutoLISP automates tasks using functions with defun [OK]
Common Mistakes:
  • Thinking AutoLISP changes colors automatically
  • Confusing manual drawing speed with automation
  • Assuming AutoLISP exports files
2. Which of the following is the correct syntax to define a function named drawSquare in AutoLISP?
easy
A. defun drawSquare () (command "RECTANGLE")
B. (defun drawSquare [] (command "RECTANGLE"))
C. (defun drawSquare () command "RECTANGLE")
D. (defun drawSquare () (command "RECTANGLE"))

Solution

  1. Step 1: Recall correct function syntax

    AutoLISP functions start with (defun functionName () ...) using parentheses for parameters.
  2. Step 2: Check command usage inside function

    The (command "RECTANGLE") is correctly inside the function body.
  3. Final Answer:

    (defun drawSquare () (command "RECTANGLE")) -> Option D
  4. Quick Check:

    Correct parentheses and command call = (defun drawSquare () (command "RECTANGLE")) [OK]
Hint: Use parentheses () for parameters in defun [OK]
Common Mistakes:
  • Using square brackets [] instead of parentheses
  • Missing parentheses around defun
  • Omitting parentheses around command
3. What will be the output when running this AutoLISP code?
(defun c:test ()
  (command "LINE" '(0 0) '(10 0) "")
  (princ "Line drawn")
)
medium
A. Draws a line from (0,0) to (10,0) and prints 'Line drawn'
B. Nothing happens because function is not called
C. Draws a circle instead of a line
D. Syntax error due to incorrect quotes

Solution

  1. Step 1: Analyze the function definition

    The code uses (defun c:test () ...) to define a command named TEST, but does not call or execute it.
  2. Step 2: Understand command and output

    Running this code only defines the function. No line is drawn, and no message is printed because the body is not executed. To run it, type TEST or (c:test).
  3. Final Answer:

    Nothing happens because function is not called -> Option B
  4. Quick Check:

    defun defines, does not run [OK]
Hint: defun defines functions/commands; they must be called to execute [OK]
Common Mistakes:
  • Thinking function runs automatically without calling
  • Confusing quotes causing syntax errors
  • Assuming wrong command draws circle
4. Identify the error in this AutoLISP function:
(defun c:drawLine ()
  (command "LINE" (0 0) (10 10) "")
  (princ "Done")
)
medium
A. Points should be quoted lists, not bare numbers
B. Missing closing parenthesis for defun
C. Command name should be lowercase
D. princ cannot print strings

Solution

  1. Step 1: Check point format in command

    AutoLISP requires points as quoted lists like '(0 0), not bare numbers (0 0).
  2. Step 2: Verify parentheses and commands

    Parentheses are balanced, command name case is not an error, and princ prints strings correctly.
  3. Final Answer:

    Points should be quoted lists, not bare numbers -> Option A
  4. Quick Check:

    Points need quotes for lists [OK]
Hint: Use single quotes for coordinate lists '(x y) [OK]
Common Mistakes:
  • Using unquoted coordinates causing errors
  • Assuming case sensitivity for commands
  • Thinking princ can't print strings
5. You want to create an AutoLISP function that draws a rectangle with width 20 and height 10 starting at point (5,5). Which code correctly automates this task?
hard
A. (defun c:drawRect () (command "RECTANGLE" '(5 5) '(25 15)) (princ "Rectangle drawn") )
B. (defun c:drawRect () (command "RECTANGLE" (5 5) (20 10)) (princ "Rectangle drawn") )
C. (defun c:drawRect () (command "RECTANGLE" '(5 5) '(20 10)) (princ "Rectangle drawn") )
D. (defun c:drawRect () (command "RECTANGLE" '(0 0) '(20 10)) (princ "Rectangle drawn") )

Solution

  1. Step 1: Calculate rectangle corner points

    Starting at (5,5), width 20 means x=25, height 10 means y=15, so opposite corner is (25 15).
  2. Step 2: Check correct point format and command

    Points must be quoted lists '(x y). (defun c:drawRect () (command "RECTANGLE" '(5 5) '(25 15)) (princ "Rectangle drawn") ) uses correct points and syntax.
  3. Final Answer:

    (defun c:drawRect () (command "RECTANGLE" '(5 5) '(25 15)) (princ "Rectangle drawn") ) -> Option A
  4. Quick Check:

    Correct points and quotes = (defun c:drawRect () (command "RECTANGLE" '(5 5) '(25 15)) (princ "Rectangle drawn") ) [OK]
Hint: Calculate corner points, use quoted lists '(x y) [OK]
Common Mistakes:
  • Using unquoted points causing errors
  • Using width/height as points instead of coordinates
  • Starting rectangle at wrong point
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard