AutoLISP Basics
Resources
Basics
- Use
VLIDEto write and test AutoLISP code. - You Can Open Existing Lisp file by
File->Open File - Windows
- Main Code Window : This is where you write and edit your AutoLISP code.
- Console/Command Line : This is where you can see output and error messages.
- Build Output Window : This shows the results of building or compiling your code.
- Trace Window : This is useful for debugging, showing the flow of execution.
- You can Use
Apploadbutton orCtrl + F9to load your lisp code into AutoCAD. - You can add your Lisp file to Startup Suite so that it loads automatically when AutoCAD starts.
- You can also run Lisp Code Directly in AutoCAD Command Line
Sample Code
- Here is a simple AutoLISP function that prints "Hello, World!" to the command line when you type
helloin AutoCAD. defundefines a new function,c:indicates it's a command, andprincis used to print text.
Create Custom Command
- Below is an example of a custom command that prompts the user to select two points and then
c:testis the name of the command you will type in AutoCAD to run this function.
Function for Calculations
- You can also define functions that are not directly callable as commands.
Basic Syntax
Comments
- Use
;to add comments in your code. Anything after;on the same line is ignored by AutoLISP.
- Used to display messages in the command line.
Message Box
- Used to display a message box to the user.
Data Types
-
Integers
-
Real Numbers
-
Strings
-
Lists
(setq myList '(1 2 3 4 5)) (princ myList) ; This will print the list (princ (car myList)) ; This will print the first element of the list (princ (cdr myList)) ; This will print the rest of the list after the first element (princ (cadr myList)) ; This will print the second element of the list (princ (caddr myList)) ; This will print the third element of the list
If Statement
- Used for conditional execution of code.
IF Else Statement
- Used for conditional execution with an alternative path.
Loops
-
Repeat Loop
-
While Loop
Special Functions
Setq
- Used to assign values to variables.
GetPoint
- Used to get a point from the user in the drawing area.
GetReal
- Used to get a real number from the user.
GetDist
- Used to get a distance from the user.
Polar
- Used to calculate a new point based on a starting point, distance, and angle.
Command
- Used to execute AutoCAD commands from within AutoLISP.
""is used to indicate the end of a command sequence, similar to pressing Enter.
Table Search
- Used to search for a value in a list or table.