AutoLISP.3: Expressions, variables and data types
Print This Post
![]() |
Author | Orhan Toker |
| Profession | Architect M.Sc. | |
| Autodesk Authorized Consultant | ||
| Database Specialist | ||
All Autocad versions
An AutoLISP program is composed of series of expressions, one under another. Simplest type of an LISP expression is shown below.
(function arguments)
Each LISP expression starts with an opening parenthesis. This is followed by function and arguments related to it (parameters) and ends with a closing parenthesis. Each expression creates a return value which is to be used by another expression that includes that expression. Value of the last evaluated expression is returned to the expression which calls that last evaluated expression. For example:
(* 2 (+ 5 3))
16
First argument of the multiplication operation is number one. Second argument is the result of summation operation. Editor first evaluates the expression that is inside the inner most parentheses and the result value is returned as the second argument of the expression which includes the multiplication operation. If there are unclosed parentheses, you get the following result, not 16:
(* 2 (+ 5 3)
(_>
In such case; if you immediately close the parenthesis, the editor continues properly:
(* 2 (+ 5 3)
(_>)
16
AutoLISP Data Types
AutoLISP expressions function according to the sequence and attributes of data types inside the parentheses. In order to use AutoLISP with full functionality, you have to understand the data types very well.
Integer (Integer Numbers)
They are numbers that have no decimal parts. These 32 bit numbers are all integers between -2,147,483,647 and +2,147,483,648. If you enter an integer outside these limits, the editor will automatically change them into real numbers. However, for example, if the sum of two appropriate numbers exceeds the maximum limit in 32 bit system, the result will be invalid. For example:
(+ 2147483646 3)
-2147483647
Result will be negative integer.
Real (Real Numbers)
In AutoLISP, real numbers are numbers having floating point format and 14 digits of precision after “.”. AS an example for real numbers for AutoLISP editor can be 3.1 -5.4 0.0000013 21,000,000.0
String (array of characters)
They are combination of alphanumerical characters, i.e. “Word 1”, “ book”, “H&B” “?*_i” are valid strings for AutoLISP editor. \ character enables you to enter control character for AutoLISP strings. List of these control characters is shown below.
AutoLISP control characters:

Table.1 AutoLISP control characters in strings.
Lists
A sequence of values that is separated from each other with a space is called list. The best way of storing all together a lot of numbers that is related to each other, is using lists. For example, a point can be stored as (13.45 23.50 0.00) list. In future, we will share much more information about lists.
Selection Sets
It is a group of one or more AutoCAD entities. By using AutoLISP, you can add entities to selection sets or subtract from them.
Entity Name
Numerical labels given to drawing entities by AutoCAD is called an entity name. Entity names can be used to access the entities in the drawing database. There are numerous LISP routines to make changes on the selected entities. We will handle this subject in detail in our future lessons.
AutoLISP Variables
As it is in all programming languages, there are also variables in AutoLISP programming language. There is no need to define a variable in AutoLISP in the beginning. Variable is equalized as soon as it is assigned and it is created as the data type of the variable it is equalized. Let’s create some variables. For creating, defining and assigning of AutoLISP variables, one single function is used.
(setq name "John")
Above expression immediately creates a variable named as “name”, defined as string and value of “John” is assigned to it. As AutoLISP is an editor, all of the variables ( except the function arguments ) are recognizable by other global functions. So, as all of the AutoLISP variables are global variables, we must be careful while naming them.
(setq intNumberOfPoints 5)
(setq strName "Orhan" strSurName "Toker")
(setq ptStartingPoint (getpoint "\nPick a Point "))
In all of the example variables above, attention was paid for assigning names to them. Due to the fact that, variables types are not pre-defines and all of the variables are global, it will be good for you if you put prefixes in front of the names of the variables according to the data type of that variable.
Watching the value of a variable
If you want to learn the immediate value of a variable, you can do it though the console or command line. There is very small difference between them.
$_strName ;watching from console
"Orhan"
Command: !strName;watching from command line
"Orhan"
As you can see, it is enough for you to place “!” sign in front of the name of the variable, value of which you want to learn.
Pre-defined variables
There are 3 pre-defined variables, name of which we can’t use as variable name in AutoLISP.
PI Pi constant
T .True
PAUSE command pause
Forcing
Used to obtain the division of two integers as real numbers
(/ 12 5); without Forcing
2
(/ 12 5.0); with forcing
2.4
That’s all for this lesson. Good bye.
Technorati Tags: AutoCAD, AutoLISP, programming, expressions, variables, Daily AutoCAD
Similar Posts
Rate this article:


(1 votes, average: 4 out of 5)
February 23rd, 2007 17:29
thank you
merci beaucoup
July 19th, 2007 03:19
Thanks,
can you point to the more advance use of variables I would like to pick from the screen a txt entity that is a real and make it the z value of a point I think that would be pretty neat
so basically if I ever get a dwg from a consultant where someone has zero’d all the points but miraculously has left the z value as a txt value - you see something like it on surveyors detail plans all the time - I could write a routine that would “use” the text height (Z piece of text) to create the z value for the point - simple but challenging for the likes of me
July 19th, 2007 09:06
Dou you want to set text entities’ z value to 0 (zero) with an AutoLISP program?
March 28th, 2008 17:12
Hi.. i am vijay from India. wel, i wanna know that how to calculate angle from the programme i.e AutoLisp.
March 30th, 2008 08:12
We already have the article you want. Please see
http://www.dailyautocad.com/autocad/visuallisp-calculating-slope-between-two-points/
June 16th, 2008 11:12
Dear sir,
As im new to Autolisp,i need some of the lisp programs for my daily use,will you please support me in this regard.
Regards.
B.Saradhi
July 23rd, 2008 11:29
is their any software program (autocad) or autodesk that compatible to touchsreen cellular phone
or that can be install to cellular phone.
July 23rd, 2008 11:34
office program like (excel ) are usefull to cellular phone cause its handy. programmer of autocad must create to install at cellular phone
thanks