Blog

AutoLISP Functions Reference guide

Introduction to AutoLISP

AutoLISP is a programming language primarily used for automating tasks and creating custom functions within Autodesk’s AutoCAD software. It is a dialect of the LISP (LISt Processing) programming language and is specifically tailored for use in the AutoCAD environment. AutoLISP allows you to write scripts, macros, and custom commands that can greatly enhance your productivity when working with AutoCAD.

Here’s a brief introduction to some fundamental AutoLISP concepts:

Print Statement: The print statement in AutoLISP is used to display text or values to the command line within AutoCAD. For example, to print the message “Hello, AutoLISP!” to the command line, you would use the following code:

(print “Hello, AutoLISP!”)

When you execute this code, you’ll see the message “Hello, AutoLISP!” displayed in the command line area of AutoCAD.

Prompting for User Input: AutoLISP programs often interact with the user by prompting for input. The getstring function is commonly used for this purpose. It allows you to request text input from the user and store their response in a variable. Here’s an example:

(setq user-input (getstring “Enter your name: “))
(print (strcat “Hello, ” user-input))

In this example, the getstring function prompts the user to enter their name, and the entered name is then concatenated with “Hello, ” and printed to the command line.

Exiting an AutoLISP Program: To exit an AutoLISP program, you can use the quit function. This function immediately terminates the execution of the program. Here’s an example of how to use it:

(if (= (getvar “cmdactive”) 1)
(progn
(prompt “Exiting AutoLISP program.”)
(quit)
)
)

In this code, we first check if a command is currently active (using (getvar "cmdactive")). If a command is active, we display a message using the prompt function and then exit the program using quit.

AutoLISP can be a powerful tool for automating repetitive tasks and creating custom AutoCAD functionality. With these basic concepts, you can start to explore and develop your own AutoLISP programs to streamline your AutoCAD workflow.


Basic Arithmetic Operations

Basic arithmetic operations are fundamental mathematical operations that you can perform in various programming languages, including AutoLISP. Here’s how you can perform these operations in AutoLISP:

Addition (add): Addition is used to find the sum of two or more numbers. In AutoLISP, you can use the + operator for addition. For example:

(setq result (+ 5 3))

In this code, result will be assigned the value 8, which is the sum of 5 and 3.

Subtraction (subtract): Subtraction is used to find the difference between two numbers. In AutoLISP, you can use the - operator for subtraction. For example:

(setq result (- 10 4))

In this code, result will be assigned the value 6, which is the result of subtracting 4 from 10.

Multiplication (multiply): Multiplication is used to find the product of two or more numbers. In AutoLISP, you can use the * operator for multiplication. For example:

(setq result (* 6 7))

In this code, result will be assigned the value 42, which is the product of 6 and 7.

Division (/): Division is used to find the quotient when one number is divided by another. In AutoLISP, you can use the / operator for division. For example:

(setq result (/ 20 4))

In this code, result will be assigned the value 5, which is the result of dividing 20 by 4.

These basic arithmetic operations are fundamental for performing mathematical calculations in AutoLISP programs. You can use them to manipulate numbers and solve various mathematical problems as needed in your AutoCAD automation tasks.


Comparisons

In AutoLISP, you can perform comparisons between values using various operators to determine relationships between them. Here are some common comparison operators:

Less Than (<): The < operator checks if one value is less than another. It returns T (true) if the condition is met, and nil (false) otherwise. For example:

(if (< 5 10)
(print “5 is less than 10”)
)

This code will print “5 is less than 10” because the condition < 5 10 is true.

Less Than or Equal To (<=): The <= operator checks if one value is less than or equal to another. It returns T if the condition is true, and nil if it’s false. For example:

(if (<= 5 5)
(print “5 is less than or equal to 5”)
)

This code will print “5 is less than or equal to 5” because the condition <= 5 5 is true.

Greater Than (>): The > operator checks if one value is greater than another. It returns T if the condition is true and nil if it’s false. For example:

(if (> 10 5)
(print “10 is greater than 5”)
)

This code will print “10 is greater than 5” because the condition > 10 5 is true.

Not Equal To (/=): The /= operator checks if two values are not equal to each other. It returns T if they are not equal and nil if they are equal. For example:

(if (/= 10 5)
(print “10 is not equal to 5”)
)

This code will print “10 is not equal to 5” because the condition /= 10 5 is true.

These comparison operators are essential for implementing conditional logic in your AutoLISP programs. They allow you to make decisions based on the relationships between values, which is crucial for creating dynamic and responsive code.


Boolean Operations

In AutoLISP, you can perform boolean operations to manipulate and evaluate logical conditions. Here are some common boolean operations:

AND (and): The and function evaluates multiple conditions and returns T (true) only if all the conditions are true. If any of the conditions is false, it returns nil (false). For example:

(if (and (< 5 10) (= 7 7))
(print “Both conditions are true”)
)

This code will print “Both conditions are true” because both conditions within the and function are true.

OR (or): The or function evaluates multiple conditions and returns T if at least one of the conditions is true. It returns nil only if all conditions are false. For example:

(if (or (> 5 10) (= 7 7))
(print “At least one condition is true”)
)

This code will print “At least one condition is true” because the second condition within the or function is true.

NOT (not): The not function negates a single condition. It returns T if the condition is false and nil if the condition is true. For example:

(if (not (= 5 10))

(print "5 is not equal to 10")


)

This code will print “5 is not equal to 10” because the not function negates the true condition (= 5 10).

Boole: The Boole function performs bitwise logical operations. It is typically used for more advanced bitwise manipulation and is less commonly used for simple boolean operations.

EQ (eq): The eq function checks if two values are identical. It returns T if the values are the same and nil if they are different. It is used for comparing symbols and memory references.

EQUAL (equal): The equal function checks if two values are equal. It is more flexible than eq and can be used to compare a wider range of data types. It returns T if the values are equal and nil if they are different.

These boolean operations are crucial for controlling the flow of your AutoLISP programs and making decisions based on conditions. You can use them in conjunction with conditional statements like if and cond to create complex logic and implement various behaviors in your code.

Number Manipulation: Basic

In AutoLISP, you can perform various number manipulation operations to modify and work with numeric values. Here are some common basic number manipulation functions:

Increment (1+) and Decrement (1-):

  • The 1+ function is used to increment a numeric value by 1.
  • The 1- function is used to decrement a numeric value by 1.

For example:

(setq x 5)
(setq x (1+ x)) ; Increment x by 1, x will be 6
(setq x (1- x)) ; Decrement x by 1, x will be 5 again

Absolute Value (abs): The abs function returns the absolute value of a number, which is the magnitude of the number without regard to its sign. It ensures that the result is non-negative.

(setq num -10)
(setq absolute-value (abs num)) ; absolute-value will be 10

Fix: The fix function is used to truncate a floating-point number to an integer by removing the fractional part. It essentially rounds towards zero.

(setq x 5.8)
(setq x (fix x)) ; x will be 5 (truncated to an integer)

Float: The float function is used to convert an integer or a string to a floating-point number. It allows you to work with floating-point values.

(setq int-value 10)
(setq float-value (float int-value)) ; float-value will be 10.0 (as a floating-point number)

These basic number manipulation functions are handy when you need to perform simple arithmetic operations, modify numbers, or convert between different numeric types in your AutoLISP programs.


Number Manipulation: Advanced

In AutoLISP, you can perform more advanced number manipulation operations to work with trigonometric functions, logarithms, exponentiation, and other mathematical operations. Here are some common advanced number manipulation functions:

Angle (angle): The angle function returns the angle in radians of a complex number represented in rectangular form. It is commonly used for complex number calculations.

(setq complex-number (complex 3 4)) ; 3 + 4i
(setq angle-radians (angle complex-number))

Cosine (cos): The cos function returns the cosine of an angle in radians.

(setq angle-radians (/ pi 4)) ; 45 degrees in radians
(setq cosine-value (cos angle-radians))

Sine (sin): The sin function returns the sine of an angle in radians.

(setq angle-radians (/ pi 6)) ; 30 degrees in radians
(setq sine-value (sin angle-radians))

Arc Tangent (atan): The atan function returns the arctangent of a number. It is often used to calculate angles.

(setq x 1)
(setq y 1)
(setq angle-radians (atan y x))

Exponential (exp): The exp function calculates the value of the base of natural logarithms (e) raised to the power of a given number.

(setq exponent 2)
(setq result (exp exponent)) ; e^2

Natural Logarithm (log): The log function computes the natural logarithm of a number.

(setq x 10)
(setq natural-log (log x)) ; ln(10)

Square Root (sqrt): The sqrt function calculates the square root of a number.

(setq num 25)
(setq square-root (sqrt num)) ; √25 = 5

Exponentiation (expt): The expt function raises a number to a specified power.

(setq base 2)
(setq exponent 3)
(setq result (expt base exponent)) ; 2^3 = 8

GCD (Greatest Common Divisor) (gcd): The gcd function returns the greatest common divisor of two or more integers.

(setq a 36)
(setq b 48)
(setq common-divisor (gcd a b)) ; GCD of 36 and 48 is 12

Remainder (rem): The rem function calculates the remainder when one number is divided by another.

(setq dividend 10)
(setq divisor 3)
(setq remainder (rem dividend divisor)) ; Remainder of 10 / 3 is 1

These advanced number manipulation functions are useful for solving a wide range of mathematical problems and performing complex calculations in AutoLISP programs.


String Manipulation: Basics

In AutoLISP, you can manipulate strings using various string manipulation functions. Here are some common basic string manipulation functions:

ASCII (ascii): The ascii function returns the ASCII code (integer value) of the first character of a string. This is useful when you want to get the ASCII value of a character.

(setq char “A”)
(setq ascii-value (ascii char)) ; ascii-value will be 65 (ASCII code for ‘A’)

CHR (chr): The chr function converts an ASCII code (integer) into a character. It’s the reverse of the ascii function.

(setq code 65)
(setq character (chr code)) ; character will be “A”

String Case Conversion (strcase): The strcase function is used to convert a string to a specified case. You can use it to convert a string to uppercase, lowercase, or title case.

(setq original-string “Hello, World!”)
(setq uppercase-string (strcase original-string 1)) ; “HELLO, WORLD!”
(setq lowercase-string (strcase original-string 0)) ; “hello, world!”

String Concatenation (strcat): The strcat function is used to concatenate (join) multiple strings together.

(setq str1 “Hello, “)
(setq str2 “AutoLISP!”)
(setq concatenated-string (strcat str1 str2)) ; “Hello, AutoLISP!”

String Length (strlen): The strlen function returns the length (number of characters) of a string.

(setq text “Hello”)
(setq length (strlen text)) ; length will be 5

Substring (substr): The substr function extracts a substring from a given string, starting at a specified position and with a specified length.

(setq original-string “AutoLISP”)
(setq substring (substr original-string 1 4)) ; “utoL”

These basic string manipulation functions are essential for working with text data in AutoLISP programs. They allow you to perform operations such as converting case, extracting substrings, finding the length of strings, and more.


String Manipulation: Conversions

In AutoLISP, you can perform various string conversions to convert between strings and other data types like numbers and angles. Here are some common string conversion functions:

Real to String (rtos): The rtos function converts a real (floating-point) number to a string. You can specify the format of the string using optional arguments.

(setq real-number 3.14159)
(setq str (rtos real-number 2 2)) ; Converts to string with 2 decimal places: “3.14”

Angle to Float (angtof): The angtof function converts an AutoCAD angle to a floating-point number in degrees. This is useful for converting AutoCAD angle values into a format that can be used in calculations.

(setq angle (angle 1.0 1.0))
(setq degrees (angtof angle)) ; Converts AutoCAD angle to degrees as a float

Angle to String (angtos): The angtos function converts an AutoCAD angle to a string. You can specify the angle format using optional arguments.

(setq angle (angle 1.0 1.0))
(setq angle-string (angtos angle 2 2)) ; Converts AutoCAD angle to string with 2 decimal places: “45.00<deg>”

Integer to String (itoa): The itoa function converts an integer to a string.

(setq num 42)
(setq str (itoa num)) ; Converts integer to string: “42”

String to Integer (atoi): The atoi function converts a string containing an integer to an actual integer value.

(setq str “123”)
(setq num (atoi str)) ; Converts string to integer: 123

String to Float (atof): The atof function converts a string containing a floating-point number to an actual floating-point number.

(setq str “3.14”)
(setq num (atof str)) ; Converts string to floating-point number: 3.14

These string conversion functions are useful when you need to work with different data types in your AutoLISP programs or when you need to format and display values as strings. They allow you to seamlessly convert between strings, numbers, and angles as needed.

Introduction to Lists: Basics

Lists are a fundamental data structure in AutoLISP and many other programming languages. In AutoLISP, lists are used to store collections of data, and they can contain various types of elements, including numbers, strings, symbols, and even other lists. Here are some basic list manipulation functions:

List: The list function is used to create a new list. You can pass multiple elements as arguments to create a list containing those elements.

(setq my-list (list 1 2 3 4 5))

In this example, my-list will be assigned a list containing the numbers 1 through 5.

Car: The car function retrieves the first element (head) of a list. It’s often used to access the first item in a list.

(setq first-element (car my-list)) ; first-element will be 1

Cdr: The cdr function retrieves the rest of the list after the first element (tail). It returns a list containing all elements except the first.

(setq rest-of-list (cdr my-list)) ; rest-of-list will be (2 3 4 5)

Cadr: The cadr function is a combination of car and cdr and is used to access the second element of a list.

(setq second-element (cadr my-list)) ; second-element will be 2

Caddr: The caddr function is a combination of car, cdr, and cdr and is used to access the third element of a list.

(setq third-element (caddr my-list)) ; third-element will be 3

Lists can be nested, meaning a list can contain other lists as elements. This nesting allows you to represent structured data in a hierarchical manner. For example:

(setq nested-list (list (list 1 2) (list 3 4)))

In this case, nested-list is a list containing two sublists.

Understanding these basic list functions is crucial for working with complex data structures and navigating through lists in AutoLISP. They form the foundation for more advanced list manipulation and data processing in your programs.

Introduction to Lists: Advanced

Building on the basics of list manipulation in AutoLISP, let’s explore some advanced list functions that allow you to perform more complex operations on lists:

Cons: The cons function is used to add an element to the beginning of a list, effectively creating a new list with the added element.

(setq my-list (list 2 3 4))
(setq new-list (cons 1 my-list)) ; Adds 1 to the beginning of my-list

In this example, new-list will be (1 2 3 4).

Append: The append function is used to concatenate multiple lists together into a single list.

(setq list1 (list 1 2))
(setq list2 (list 3 4))
(setq combined-list (append list1 list2)) ; Concatenates list1 and list2

combined-list will be (1 2 3 4).

Reverse: The reverse function returns a new list with the elements of the original list reversed.

(setq my-list (list 1 2 3))
(setq reversed-list (reverse my-list)) ; Reverses the order of my-list

reversed-list will be (3 2 1).

Last: The last function returns the last element of a list.

(setq my-list (list 1 2 3 4 5))
(setq last-element (last my-list)) ; last-element will be 5

Length: The length function returns the number of elements in a list.

(setq my-list (list 1 2 3))
(setq list-length (length my-list)) ; list-length will be 3

Nth: The nth function is used to access the nth element of a list, where n is an integer.

(setq my-list (list “apple” “banana” “cherry”))
(setq third-element (nth 2 my-list)) ; third-element will be “cherry”

Member: The member function checks if an element is present in a list and returns the sublist starting from that element.

(setq my-list (list 1 2 3 4 5))
(setq result (member 3 my-list)) ; result will be (3 4 5)

These advanced list functions provide you with powerful tools for manipulating and working with lists in AutoLISP. You can use them to build and modify lists, extract specific elements, and perform various operations on list data structures.


Basic Programming & Control Flow

In AutoLISP, you can create and control the flow of your programs using various programming constructs and control flow statements. Here are some of the basic programming and control flow constructs:

Defun: The defun statement is used to define a custom function. It allows you to create your own functions with parameters and a body of code that can be reused throughout your program.

(defun my-function (parameter1 parameter2)
(setq result (+ parameter1 parameter2))
(princ result)
)

Function: The function statement is used to create a lambda (anonymous) function. Lambda functions are often used as arguments to other functions or stored in variables.

(setq add (function (lambda (a b) (+ a b))))

If: The if statement is used for conditional execution. It evaluates a condition and executes one block of code if the condition is true and another block if it’s false.

(if (< x 10)
(princ “x is less than 10”)
(princ “x is not less than 10”)
)

Cond: The cond statement is used for multi-way conditionals. It checks multiple conditions sequentially and executes the first block of code where the condition is true.

(cond
((< x 10) (princ “x is less than 10”))
((> x 10) (princ “x is greater than 10”))
(T (princ “x is equal to 10 or not a number”))
)

Progn: The progn statement is used to group multiple expressions together and execute them sequentially. It is often used when you need to execute several statements in a block.

(progn
(setq a 5)
(setq b 10)
(princ (+ a b))
)

Repeat: The repeat statement is used to repeat a block of code a specific number of times.

(repeat 5
(princ “Hello, AutoLISP! “)
)

Foreach: The foreach statement is used to iterate over a list and execute a block of code for each element in the list.

(foreach item my-list
(princ item)
)

These basic programming and control flow constructs are essential for writing structured and logical AutoLISP programs. They allow you to create functions, make decisions based on conditions, and control the flow of your code.

Advanced Programming & Control Flow

In AutoLISP, advanced programming and control flow constructs enable you to work with variables, expressions, and functions in more flexible and dynamic ways. Here are some advanced programming and control flow constructs:

Quote: The quote function is used to prevent the evaluation of an expression. It returns the expression as-is, without any computation.

(setq x 5)
(setq y (quote x)) ; y will be the symbol ‘x’, not its value

Eval: The eval function is used to evaluate an expression at runtime. It takes a quoted expression as an argument and computes its value.

(setq x 5)
(setq y (eval (quote (+ x 3)))) ; y will be 8

Apply: The apply function is used to call a function with arguments stored in a list. It’s useful when you want to pass a dynamic list of arguments to a function.

(setq args (list 1 2 3))
(setq result (apply ‘+ args)) ; result will be 6

Lambda: The lambda function allows you to create anonymous functions (lambda functions). These functions can be assigned to variables or passed as arguments to other functions.

(setq add (lambda (a b) (+ a b)))

Setq: The setq function is used to assign values to variables. It allows you to create or update variables.

(setq x 10)

Set: The set function is similar to setq but can be used to assign values to symbol property lists or function definitions.

(set ‘x 15)

Boundp: The boundp function checks if a symbol is bound to a value or if it is unbound (undefined).

(setq x 5)
(if (boundp ‘x)
(princ “x is bound to a value”)
(princ “x is unbound”)
)

These advanced programming and control flow constructs provide greater flexibility when working with variables, expressions, and functions in AutoLISP. They enable you to create dynamic code, work with lists of arguments, and manipulate symbols and their properties.


User Interaction: Input

In AutoLISP, you can interact with users to collect input through various get functions. These functions allow you to prompt users for specific types of input, such as angles, distances, keywords, points, real numbers, strings, and integers. Here are some commonly used get functions for user interaction:

Get Angle (getangle): The getangle function allows users to specify an angle by picking two points in the drawing area. It returns the selected angle in radians.

(setq user-angle (getangle “\nSelect an angle: “))

Get Corner (getcorner): The getcorner function prompts users to specify a corner point by picking a point in the drawing area. It returns a list containing the X and Y coordinates of the selected point.

(setq user-corner (getcorner “\nSelect a corner point: “))

Get Distance (getdist): The getdist function allows users to specify a distance by picking two points in the drawing area. It returns the selected distance as a real number.

(setq user-distance (getdist “\nSelect a distance: “))

Get Keyword (getkword): The getkword function prompts users to enter a keyword from a predefined list. It returns the selected keyword as a string.

(setq user-choice (getkword “\nSelect a color (red/green/blue): “))

Get Point (getpoint): The getpoint function allows users to specify a point by picking it in the drawing area. It returns a list containing the X, Y, and Z coordinates of the selected point.

(setq user-point (getpoint “\nSelect a point: “))

Get Real (getreal): The getreal function prompts users to enter a real number. It returns the entered value as a real number.

(setq user-value (getreal “\nEnter a real number: “))

Get String (getstring): The getstring function allows users to input a string. It returns the entered string.

(setq user-text (getstring “\nEnter a string: “))

Get Integer (getint): The getint function prompts users to enter an integer. It returns the entered value as an integer.

(setq user-number (getint “\nEnter an integer: “))

These get functions are essential for creating interactive AutoLISP programs that respond to user input and allow users to specify parameters or make choices within the AutoCAD environment. They enhance the user experience and make your programs more versatile and user-friendly.


User Interaction: Configurations

In AutoLISP, you can interact with configurations and environment variables to customize the behavior of your program or retrieve information about the AutoCAD environment. Here are some commonly used functions for handling configurations and environment variables:

Get Filed (getfiled): The getfiled function is used to display a file dialog box that allows users to select a file. It returns the selected file’s path as a string.

(setq file-path (getfiled “Select a DWG file” “” “dwg”))

Get Config (getcfg): The getcfg function is used to retrieve configuration settings (system variables) in AutoCAD. It returns the current value of the specified configuration variable.

(setq textsize (getcfg “text” “textsize”))

Set Config (setcfg): The setcfg function is used to change the value of a configuration setting (system variable) in AutoCAD.

(setcfg “text” “textsize” 2.5)

Get Variable (getvar): The getvar function is used to retrieve the current value of an AutoCAD system variable.

(setq current-layer (getvar “clayer”))

Set Variable (setvar): The setvar function is used to change the value of an AutoCAD system variable.

(setvar “clayer” “MyLayer”)

Get Environment Variable (getenv): The getenv function is used to retrieve the value of an environment variable on the computer where AutoCAD is running.

(setq user-profile (getenv “USERPROFILE”))

Set Environment Variable (setenv): The setenv function is used to set the value of an environment variable within the AutoCAD session. Note that changes made with setenv are temporary and do not affect the system environment.

(setenv “MYVAR” “myvalue”)

These functions provide a way to interact with configurations, system variables, and environment variables in AutoCAD, allowing you to customize your program’s behavior and access important information about the AutoCAD environment and the user’s system.


Working with Entities in AutoCAD: Basics

Working with entities in AutoCAD is essential for creating, modifying, and managing objects in the drawing. Here are some basic functions and commands for working with entities in AutoCAD using AutoLISP:

Entget (entget): The entget function is used to retrieve the properties and data associated with an entity (object) in the drawing. It returns a list of property-value pairs for the specified entity.

(setq entity-data (entget (car (entsel))))

In this example, the entsel function is used to select an entity, and entget retrieves its properties.

Entlast (entlast): The entlast function returns the entity name of the last created or modified entity in the drawing. It’s often used after creating or modifying entities to reference the most recently processed entity.

(setq last-entity (entlast))

Entmake (entmake): The entmake function allows you to create new entities by specifying a list of property-value pairs in AutoLISP format. It returns the entity name of the newly created entity.

(setq new-line (entmake ‘((0 . “LINE”)(10 0 0 0)(11 1 1 0))))

This example creates a new line entity and stores its entity name in new-line.

Entmod (entmod): The entmod function is used to modify the properties of an existing entity. It takes a list of property-value pairs and applies the changes to the specified entity.

(setq entity-data (entget (car (entsel))))
(setq updated-data (subst ‘(10 5.0 5.0 0.0) (assoc 10 entity-data) entity-data))
(entmod updated-data)

In this example, the code modifies the position of an entity and uses entmod to apply the changes.

Entnext (entnext): The entnext function is used to iterate through entities in the drawing. It returns the entity name of the next entity in the drawing order.

(setq current-entity (entnext last-entity))

This code gets the entity name of the entity following the last processed entity.

These basic functions and commands allow you to interact with and manipulate entities in AutoCAD using AutoLISP. You can retrieve entity data, create new entities, modify existing ones, and iterate through entities in the drawing.


Working with Entities in AutoCAD: Advanced

Working with entities in AutoCAD often involves more advanced operations for tasks such as deletion, updating, selection, handling of entities by their handles, and transformation. Here are some advanced functions and commands for working with entities in AutoCAD using AutoLISP:

Entdel (entdel): The entdel function is used to delete one or more entities from the drawing. You can specify the entity names to be deleted.

(entdel last-entity) ; Deletes the last processed entity

Entupd (entupd): The entupd function is used to update the display of an entity in the drawing after its properties have been modified. It ensures that the changes are reflected in the drawing.

(entupd last-entity) ; Updates the display of the last processed entity

Entsel (entsel): The entsel function is used to interactively select an entity in the drawing. It allows the user to pick an entity, and it returns the selected entity’s entity name.

(setq selected-entity (entsel “\nSelect an entity: “))

Nentsel (nentsel), Nentselp (nentselp): The nentsel and nentselp functions are used to non-interactively select entities based on various filter criteria. Nentsel returns a list of selected entities, while nentselp returns the entity name of the selected entity.

(setq selected-entities (nentsel “Select entities: “))
(setq first-selected-entity (nentselp))

Handent (handent): The handent function is used to retrieve an entity’s handle by specifying its entity name. Handles are unique identifiers for entities.

(setq entity-handle (handent last-entity))

Textbox (textbox): The textbox function allows you to create a text entity with specified properties, such as content, height, and rotation angle.

(setq text-entity (textbox “Sample Text” 5.0 ‘(0 0 0) 45))

Trans (trans): The trans function is used to apply a geometric transformation to an entity. You can translate, rotate, or scale entities using this function.

(setq translation-matrix (list 1.0 0.0 0.0 0.0 1.0 0.0 10.0 10.0 1.0))
(trans last-entity 1 translation-matrix)

These advanced functions and commands provide you with greater control and flexibility for managing entities in AutoCAD using AutoLISP. You can perform tasks such as deletion, updating, interactive and non-interactive selection, handling entities by their handles, creating text entities, and applying transformations to entities.


Selection Sets Management

Selection sets are used in AutoCAD to group and manage specific sets of entities. AutoLISP provides functions for creating, modifying, and working with selection sets. Here are some common selection set management functions:

SSAdd (ssadd): The ssadd function is used to add one or more entities to a selection set. You can specify the selection set name and the entity name(s) to be added.

(setq ss (ssadd))
(ssadd ss last-entity) ; Adds the last processed entity to the selection set

SSDel (ssdel): The ssdel function is used to remove entities from a selection set. You specify the selection set name and the entity name(s) to be removed.

(ssdel ss last-entity) ; Removes the last processed entity from the selection set

SSGet (ssget): The ssget function is used to create a selection set based on specified criteria, such as entity type, layer, or other properties. It returns the selection set as a list of entity names.

(setq ss (ssget “X” ‘((0 . “LINE”))))

SSGetFirst (ssgetfirst): The ssgetfirst function is used to retrieve the first entity in a selection set. It returns the entity name.

(setq first-entity (ssgetfirst))

SSLength (sslength): The sslength function is used to get the number of entities in a selection set.

(setq num-entities (sslength ss))

SSMemb (ssmemb): The ssmemb function checks if an entity is a member of a selection set. It returns T if the entity is in the set, or nil if it is not.

(setq is-member (ssmemb ss last-entity))

SSName (ssname): The ssname function retrieves the name of an entity in a selection set based on its position in the set.

(setq entity-name (ssname ss 0))

SSNameX (ssnamex): The ssnamex function is used to retrieve the entity name and XData (extended entity data) for an entity in a selection set.

(setq entity-data (ssnamex ss 0))

SSSetFirst (sssetfirst): The sssetfirst function is used to set the first entity in a selection set. It takes the entity name to be set as the first entity.

(sssetfirst ss first-entity)

Selection set management functions are crucial for organizing and working with specific groups of entities in AutoCAD. They allow you to create, modify, and query selection sets based on your criteria, improving your control and flexibility when working with entities.


Graphics & Display Functions

In AutoLISP, you can use graphics and display functions to interact with the graphical user interface (GUI) and perform operations related to graphics and text display. Here are some commonly used graphics and display functions in AutoLISP:

Graphscr (graphscr): The graphscr function sets the graphics screen to a specified screen configuration.

(graphscr 1) ; Sets the graphics screen to configuration 1

Grclear (grclear): The grclear function clears the graphics screen and removes all graphics objects.

(grclear)

Grdraw (grdraw): The grdraw function is used to draw graphics objects on the graphics screen. It can draw lines, circles, rectangles, and other shapes.

(grdraw (list (list 0 0) (list 10 10)) :LINE)

Grread (grread): The grread function reads user input and returns the input as a string. It can be used for text input from the user.

(setq user-input (grread “\nEnter text: “))

Grtext (grtext): The grtext function displays text on the graphics screen at a specified location.

(grtext (list 5 5) “Hello, AutoLISP!”)

Grvecs (grvecs): The grvecs function is used to draw 2D vectors or arrows on the graphics screen. It takes the starting and ending points of the vector and other properties.

(grvecs (list 0 0) (list 10 10) :COLOR 1)

Textscr (textscr): The textscr function sets the text screen configuration to a specified value.

(textscr 2) ; Sets the text screen configuration to 2

Redraw (redraw): The redraw function forces a redraw of the graphics screen, updating the displayed graphics.

(redraw)

These graphics and display functions allow you to work with the visual aspects of your AutoLISP programs, including drawing graphics, displaying text, and interacting with the user through graphical input and output.

Dictionaries, Tables & Records

In AutoLISP, you can work with dictionaries, tables, and records to manage and store additional data related to your AutoCAD drawing. Here are some commonly used functions and concepts related to dictionaries, tables, and records:

Dictadd (dictadd): The dictadd function is used to add a new entry (key-value pair) to a dictionary. It takes the dictionary name, key, and value as arguments.

(dictadd my-dict “Key1” “Value1”)

Dictnext (dictnext): The dictnext function is used to iterate through the entries in a dictionary. It returns the next key-value pair in the dictionary.

(setq entry (dictnext my-dict))

Dictremove (dictremove): The dictremove function is used to remove an entry (key-value pair) from a dictionary.

(dictremove my-dict “Key1”)

Dictrename (dictrename): The dictrename function is used to rename a key within a dictionary.

(dictrename my-dict “OldKey” “NewKey”)

Dictsearch (dictsearch): The dictsearch function is used to retrieve the value associated with a specific key in a dictionary.

(setq value (dictsearch my-dict “Key1”))

Named Object Dictionary (namedobjdict): The namedobjdict is a predefined dictionary in AutoCAD that is used to store named objects, such as layers, blocks, and dimension styles.

(setq named-objects (namedobjdict))

Tblnext (tblnext): The tblnext function is used to iterate through the entries in a table (e.g., layer table, block table). It returns the next table record.

(setq layer-record (tblnext “LAYER” nil))

Tblobjname (tblobjname): The tblobjname function returns the name of a table entry (e.g., layer name).

(setq layer-name (tblobjname “LAYER” layer-record))

Tblsearch (tblsearch): The tblsearch function is used to search for a specific entry in a table.

(setq block-record (tblsearch “BLOCK” “MyBlockName”))

Regapp (regapp): The regapp function is used to register a new application name in the “APPID” table. This is often used to define custom object types.

(regapp “MyCustomApp”)

These functions and concepts allow you to work with dictionaries, tables, and records in AutoLISP, providing a way to store and manage additional data related to your AutoCAD drawing, including custom data and object definitions.


File & Memory Management Functions

In AutoLISP, you can work with files and memory to manage data and perform various tasks. Here are some commonly used file and memory management functions:

Open (open): The open function is used to open a file for reading or writing. It returns a file descriptor that can be used for subsequent file operations.

(setq file (open “myfile.txt” “r”))

Close (close): The close function is used to close an open file. It takes a file descriptor as an argument.

(close file)

Load (load): The load function is used to load and execute an external AutoLISP file. It allows you to run code from another file within your current program.

(load “myotherfile.lsp”)

Findfile (findfile): The findfile function is used to locate a file in the AutoCAD support file search path. It returns the full path to the file if found.

(setq filepath (findfile “mysupportfile.pat”))

Autoload (autoload): The autoload function is used to specify that an AutoLISP function should be automatically loaded and executed when it’s called for the first time.

(autoload ‘my-function “mylibrary” ‘myinitfunction)

Autoarxload (autoarxload): The autoarxload function is used to automatically load an ARX (AutoCAD Runtime Extension) module when AutoCAD starts.

(autoarxload “myarxmodule.arx”)

Arxload (arxload): The arxload function is used to manually load an ARX module at runtime.

(arxload “myarxmodule.arx”)

Arxunload (arxunload): The arxunload function is used to manually unload an ARX module at runtime.

(arxunload “myarxmodule.arx”)

Alloc (alloc): The alloc function is used to allocate memory for a new object. It returns a reference to the allocated memory block.

(setq my-memory (alloc 100)) ; Allocates 100 bytes of memory

GC (gc): The gc function is used to initiate garbage collection, which reclaims memory that is no longer in use, improving memory management.

(gc)

Mem (mem): The mem function is used to measure the amount of free memory available.

(setq free-memory (mem))

Exit (exit): The exit function is used to exit AutoCAD. It allows you to programmatically close the AutoCAD application.

(if (setq response (getstring “\nExit AutoCAD? (Yes/No): “))
(if (string= (strcase response) “YES”)
(progn
(princ “\nExiting AutoCAD…”)
(command “_QUIT”)
)
)
)

These file and memory management functions in AutoLISP provide the capability to work with files, load external code, manage memory, and control the execution and exit of AutoCAD.