Auto LISP
Auto LISP
Auto LISP
Slide: 1
AutoLISP
Contents:
6. Functions
Slide: 2
Introduction:
• AutoLISP is AutoCAD's inbuilt programming language.
• AutoLISP is used to create programs that will automatically generate drawings.
• This is a procedural programming language.
• User defined customized commands can be added to AutoCAD using AutoLISP.
• These customized commands will perform the activities that we want to automate.
• The very simple example can be creating parametric shapes of any complexity. These shapes will be
generated considering the parameters defined and values provided at the time of generation.
Slide: 3
Introduction
Those who have some proficiency in AutoCAD know how it is possible to draw lines, curves, polyline, circle, sphere,
box and various other objects by using AutoCAD command prompt.
All these may be parts of the drawing of a big engineering object. To draw a large object a user must replicate
several smaller object which is time consuming and monotonous.
Example: You have drawn the front view of a steam stop valve by AutoCAD. Now if it is required to draw the same
object with some modification in dimension or shape or something else, you must start a new and do almost the
same repetitive job. To avoid such tedious works, you can write a computer a program , which may to do all sorts of
modifications.
AutoLISP is such a high-level computer language supported by AutoCAD.
AutoLISP programs are parametric. All functions are used in parenthesis (round brackets ’()’). All brackets should be
evenly closed. Function and symbol names are not case sensitive and thus both upper- and lower-case letters can
be used. The expressions can be written in multiple lines.
Slide: 4
Expressions and Syntax:
An expression is the basic structure that is used when working with AutoLISP.
Each expression:
i. Begins with an open (left) parenthesis.
ii. Consists of a function name and optional arguments for that function. Each argument can also be an
expression.
iii. Ends with a close (right) parenthesis.
iv. Returns a value that can be used by a surrounding expression. The value of the last interpreted expression is
returned to the calling expression.
In this example, the foo function has one requirement argument, string of the string data type, and one or more
optional arguments of numeric value for number. The number arguments can be of the integer or real data types.
Frequently, the name of the argument indicates the expected data type.
Slide: 5
The AutoLISP Environment:
A namespace is a LISP environment containing a set of symbols (for example, variables and functions).
The AutoLISP environment is entered when AutoCAD receives a "(" symbol at the command line. When AutoCAD
sees the opening parentheses, it runs the AutoLISP interpreter. The interpreter then evaluates, as a list, what is
between the opening "(" symbol and the closing ")" symbol.
Data Types: A data type is a (possibly infinite) set of Lisp objects. There are three basic data types in AutoLISP.
They are functions, symbols, and lists.
Functions: It is the first item in every list. The remainder of the list is used as the parameters or arguments to the
function. If the first item in the list is not a function, you will receive an error message stating: "bad function.“
Variables: A variable in LISP however can be a whole lot more than just a value. A variable can contain a list or a
function definition. A variable has a value bound to it. If it has no binding it is said to be "nil". All variables should be
set to nil upon ending a program or encountering an error.
Lists: The list function can take any number of arguments and as it is a function, it evaluates its arguments.
Slide: 6
Data Types:
AutoLISP expressions are processed according to the order and data type of the code within the parentheses. Before
you can fully utilize AutoLISP, you must understand the differences among the data types and how to use them.
Below are the various Data types:
Integers:
Integers are whole numbers; numbers that do not contain a decimal point.
AutoLISP integers are 32-bit signed numbers with values ranging from +2,147,483,647 to -2,147,483,648.
Some functions through, only accept 16-bit numbers ranging from +32767 to -32678.
When you explicitly use an integer, that value is known as a constant.
Numbers such as 2, -56, and 1,200,196 are valid integers.
If you enter a number that is greater than the maximum integer allowed (resulting in integer overflow), AutoLISP
converts the integer to a real number.
However, if you perform an arithmetic operation on two valid integers, and the result is greater than the maximum
allowable integer, the resulting number will be invalid.
Slide: 7
Data Types:
Reals:
A real is a number containing a decimal point.
Numbers between -1 and 1 must contain a leading zero.
Real numbers are stored in double-precision floating-point format, providing at least 14 significant digits of precision.
Note that AutoLISP does not show you all the significant digits.
Reals can be expressed in scientific notation, which has an optional e or E followed by the exponent of the number
(for example, 0.0000041 is the same as 4.1e-6).
Numbers such as 3.1, 0.23, -56.123, and 21,000,000.0 are all valid AutoLISP real numbers.
Strings:
A string is a group of characters surrounded by quotation marks.
Within quoted strings the backslash (\) character allows control characters (or escape codes) to be included.
When you explicitly use a quoted string in an AutoLISP expression, that value is known as a literal string or a string
constant.
Examples of valid strings are “string 1” and “\nEnter first point:”.
Slide: 8
Data Types:
Lists:
A list is a group of related values separated by spaces and enclosed in parentheses.
Lists provide an efficient method of storing numerous related values. After all, LISP is so-named because it is the
LISt Processing language.
Lists are used to represent 2D and 3D coordinate values, and entity data.
Examples of lists are (1.0 1.0 0.0), ("this" "that" "the other"), and (1 . "ONE").
Selection Sets:
Selection sets are groups of one or more objects (entities).
You can interactively add objects to, or remove objects from, selection sets with AutoLISP routines.
Entity Names:
An entity name is a numeric label assigned to objects in a drawing.
It is a pointer into a file maintained by AutoCAD and can be used to find the object's database record and its vectors
(if they are displayed).
This label can be referenced by AutoLISP functions to allow selection of objects for processing in various ways.
Internally, AutoCAD refers to objects as entities.
Slide: 9
Data Types:
File Descriptors:
A file descriptor is a pointer to a file opened by the AutoLISP open function.
The open function returns this pointer as an alphanumeric label. You supply the file descriptor as an argument to
other AutoLISP functions that read, write, or close the file.
Files remain open until you explicitly close them in your AutoLISP program. The close function closes a file.
Symbols:
AutoLISP uses symbols to refer to functions and data holders.
Symbol names are not case sensitive and may consist of any sequence of alphanumeric and notation characters.
A symbol name cannot consist only of numeric characters.
Slide: 10
Symbols and Special Character:
There are some character in AutoLISP, which cannot be used under certain circumstances.
Symbols names can contain any sequence of printable characters expect the following: () . ‘ “ ;
A minus (-) sign can be used to signify negative integers or real.
Same goes with the plus sign (+) which signifies positive integers or reals.
Within quotes (“”) strings, the backlash (\) character may be used to enter control characters.
The control character are as follows:
\\ Gives a normal backlash character.
\” Gives a normal double quote character.
\e Gives an escape character.
\n Gives a new line character.
\r Gives a return character.
\t Gives the tab character.
Every comment is AutoLISp must start with the semicolon character ;This is a comment
Slide: 11
Functions:
A function is a group of statements that together perform a task.
You can divide up your code into separate functions. How you divide up your code among different functions is up to
you, but logically the division usually is so each function performs a specific task.
It is the first item in every list.
The remainder of the list is used as the parameters or arguments to the function.
If the first item in the list is not a function, you will receive an error.
Math Functions:
(+ number number…) Add
(- number number…) Subtract
(* number number…) Multiply
(/ number number…) Divide
(max number number…) Find largest of numbers given
(min number number…) Find smallest of numbers given
(rem number number…) Find the remainder of numbers
Slide: 12
Function: Syntax
• Function Syntax used to create the routine:
Function name
Required argument(s)
optional argument(s) in
brackets
• In this example, the setq function has one required argument, string of the string data type, and one or more
optional arguments of numeric value for number. The number arguments can be of the integer or real data types.
Frequently, the name of the argument indicates the data type.
Slide: 13
Functions:
Lists:
• This function takes any number of expressions and makes a list out of them and returns the result.
• The coordinate of a point is a commonly used list in AutoLISP. Coordinates can be 2-dimensional or 3-dimensional.
• Following syntax is used to create lists:
Ex: Command: (setq p (list 1 2 3))
Returns: (1 2 3)
cdr: This function separates the remaining elements other than first. If the list is empty, cdr returns nil.
Ex: Command: (setq p (list 1 2 3))
(setq t (cdr p)) here ‘t’ is assigned the remaining element (2 3) of ‘p’
Returns: (2 3)
Slide: 14
Functions:
cadr: This function car & cdr can be used in a combined form that returns the second element of the list.
Ex: Command: (setq p (list 1 2 3))
(setq u (car (cdr p))) here ‘u’ is assigned as the second element (2) of ‘p’
Return: 2
append: This function appends values to an existing list and returns a new list.
Ex: Command: (setq p (list 1 2 3))
(setq o (append p ‘(4))) here a new list is form by adding ‘4’ in the existing list.
Return: (1 2 3 4)
cons: This function adds an element to the beginning of a list or constructs a dotted list.
Ex: Command: (setq p (list 1 2 3))
(setq l (cons 0 p)) here a new list is form by adding ‘0’ in the beginning of the list.
Return: ( 0 1 2 3 )
Slide: 15
Functions:
assoc: This functions searches an association list for an element and returns the association list entry.
Ex: Command: (assoc element alist)
element - Key of an element in an association list.
alist - An association list to be searched.
Entity Name:
• An entity name is a numeric label assigned to objects in a drawing.
• It is a pointer into a file maintained by AutoCAD and can be used to find the object's database record and its vectors
(if they are displayed).
• This label can be referenced by AutoLISP functions to allow selection of objects for processing in various ways.
• Internally, AutoCAD refers to objects as entities.
Slide: 16
Functions:
entsel: This functions prompts the user to select a single object(entity) by specifying a point.
Ex: Command: (setq name (entsel “Select an object: “))
here the function prompts in the AutoCAD interface to select the object.
entmake: This function creates a new entity in the drawing using DXF Codes.
Ex: Command: (entmake (list (0. “CIRCLE”) (62 . 1) (10 4.0 4.0 0.0) (40 . 1.0)))
here a circle is created with group code 62 is for color and 1 is for white, centered
t at (4,4) with a radius of 1.
terpri: This function prints a new line to the command line. (like \n character)
Slide: 17
Functions:
command: This function is used to call up a standard AutoCAD command.
getpoint: This function requests an input of a point. It also echos a preassigned message.
Ex: Command: (getpoint “Pick 1st point:”)
here the user can specify a point by pointing or by entering a coordinate in the
current units' format.
Return: Pick 1st point:
Slide: 18
Functions:
getreal: This functions pauses for user input of a real number and returns that real number.
Ex: Command: (setq a (getreal “Type a number:”))
here “Type a number:” is the message to be displayed to prompt the user and
the input is expressed as a real.
Return: Type a number: (the number user typed in)
distance: This function will return a real number based on 2 input points.
Ex: Command: (dist pt1 pt2)
here the function will determine the distance between 2 points.
Slide: 19
Example : Rectangle (Box)
Slide: 20
Example : Diagonal Rectangle (Box)
Slide: 21
DXF Codes- Data Exchange Format codes:
DXF codes are used to indicate the type of the value of the group, and to indicate the general use of the group. The
specific function of the group code depends on the actual variable, table item, or entity description. This section
indicates the general use of groups, noting as “(fixed)” any that always have the same function.
Slide: 22
DXF Codes- Data Exchange Format codes
Slide: 23
DCL- Dialog Control Language:
Dialog Control Language or DCL for short is a simple markup language that enables programmers in AutoLISP and
Visual LISP to create dialog boxes that can be integrated into their routines.
This means that an AutoLISP routine can gather a range of input data from a user through a familiar and logical
interface. Without DCL, AutoLISP can only take user input from the command line.
AutoLISP includes a few special functions that allow routines to load, interact with and unload dialog boxes written in
DCL.
DCL is specifically for use with AutoCAD in conjunction with AutoLISP and Visual LISP.
Slide: 24
Example: Flange DCL file
Flange DCL
Slide: 25
CUI- Customize User Interface:
The Customize User Interface (CUI) Editor allows you to modify many of the user interface elements that are used
to start a command in the program.
Customizing the user interface allows you to change the placement of the tools that you commonly use, and to add
new tools that can improve your productivity. For example, if you want to add a ribbon panel containing the
commands that you use most often, you can create a new Favorites ribbon panel in the CUI Editor and add it to the
Home tab. The CUI Editor is divided into two parts:
Customize. The Customize tab allows you to create and manage the commands and elements that make up many
of the features in the user interface
Transfer. The Transfer tab allows you to create and save a CUIx file and copy user interface elements between CUIx
files.
Slide: 26
CUI- Customize User Interface:
AutoLISP application files contain custom commands and functions from
the user interface and must be loaded before they can be used.
When using AutoCAD or an AutoCAD-based program, not AutoCAD LT,
AutoLISP programs can be loaded automatically when a customization
(CUIx) file is loaded using one of these methods:
AutoLISP Menu Source (MNL) file that has the same name and in the
location as the main, enterprise, or partial customization (CUIx) files being
loaded
AutoLISP (LSP) files added to the LISP Files node of a CUIx file under the
Customizations In pane of the Customize User Interface (CUI) Editor
Tip: While only AutoLISP (LSP) files can be added the LISP Files node of
a customization (CUIx) file, you can create a LSP file that loads FAS, VLX,
and other types of custom program files. Other types of custom program
files can be loaded using the NETLOAD, VBALOAD, or ARX commands
along with the AutoLISP functions load, arxload, and vl-vbaload.
Slide: 27
LISP File:
LSP files or files with .LSP extension are the files written in LISP programming language.
The LSP files contain source code for AutoLISP in the form of plain text program code which is executed with the
help of an LSP interpreter.
These files can be opened using any text editor. LSP files are used with Popular CAD programs such as AutoCAD,
BricsCAD and ZWCAD.
You can create a LISP program in the VLISP window.
To open the VLISP window, from the AutoCAD window, in the ‘Tools’ menu, select ‘AutoLISP’ and then select the
‘Visual LISP Editor.’
In the VLISP window that opens, you can type your LISP program in the console window.
You can also launch the VLISP text editor by selecting ‘File’, then ‘New.’
The text editor enables you to write and save complete LISP programs, or routines.
Slide: 28
LISP File:
Slide: 29
Example: Nuts and Bolts
Slide: 30
Example: Flange
• Flange LSP
Slide: 31