Autodesk - Free AutoLISP Course
Autodesk - Free AutoLISP Course
mailto:[email protected]
Overview
Overview ....................................................2
Introduction ................................................3
Lesson 01: About AutoLISP ...................................5
Lesson 02: Entities .........................................7
Lesson 03: Setting Values ...................................9
Lesson 04: Arithmetic Functions 1 ..........................12
Lesson 05: Arithmetic Functions 2 ..........................16
Lesson 06: Arithmetic Functions 3 ..........................18
Lesson 07: Creating Drawings 1 .............................22
Lesson 08: Creating Drawing 2 ..............................26
Lesson 09: Getting Information 1 ...........................30
Lesson 10: Getting Information 2 ...........................33
Answers ....................................................36
What next? .................................................40
ACAD Newsletter ............................................42
Introduction
Thanks for requesting the AutoLISP course. After reading and
studying this course you will be able to write your own Auto-
LISP routine.
You know how it is. You're working with AutoCAD. All the time
you've got these repetitive tasks. Don't do them by hand. Use
an AutoLISP routine.
mailto: [email protected]
Easy. Give this e-book away to other people that are inter-
ested in AutoLISP programming. Or send it to them by e-mail.
Or put it on your web site.
mailto:[email protected]
Action Function
Addition (+ <number> <number ..)
subtraction (- <number> <number ..)
multiplication (* <number> <number ..)
You can work with two numbers. But you can also put more num-
bers between the brackets. Then the same operation is done on
more numbers.
Exercise
I want you to make a calculation in AutoCAD. You're a farmer
and you've got 96 cows. You are giving them away to eight
people.
Atom Description
Atoms Elements consisting of one or more numbers and
characters. Examples are: ABC, 123, A1, P6, etc.
This is an explanation:
Atom Type
(/ 3.0 7) List
/ Function
3.0 Real Number
7 Integer
/, 3.0, and 7 Atoms
Atom Description
Symbol A symbol is used to store values in
it. The name of the symbol can be
made up. But the longer the name the
more memory is used
String A string is a text placed between
quotation marks. Examples of texts
are:
"This is a text"
"Anyone in for AutoLISP"
Atom Description
Real or float Reals or floats have a decimal part.
Examples are:
3.4256
1.5
Integer Integers are numbers without a deci-
mal part. Examples are:
3
56
Subrs These are built-in function. Auto-
LISP has a number of built-in func-
tions. You don't really want to know
these functions. You don't need to
know these functions
File descriptor A file descriptor is given to every
external file AutoLISP works with.
This is an example of a file de-
scriptor:
<File #E872>
AutoCAD entity name Every element that is placed in an
AutoCAD drawing gets an entity name.
That's done automatically. This is
an example of such a name:
Exercise
What's the type?
1. 3
2. 1.23
3. (1 2 3)
4. /
5. <Selection set: 5>
6. <File #A315>
SETQ
EVAL
QUOTE
Let's start with the SETQ function. We have a symbol and now
we want to store a value in that symbol. Let's say we have
the symbol NR.
Type this at the command line of AutoCAD. Press the Enter key
when done. What do we see? The number 56 is displayed at the
command prompt.
Now the name of the SETQ function is used only once. And
there is only one opening bracket and one closing bracket.
But there is more.
A lot have spaces have been added to the second listing. And
also carriage returns have been added. Those spaces and car-
riage returns make no difference for AutoLISP.
At the second prompt you enter !nr. The value that has been
stored in the NR symbol will be used. A circle with a radius
of 56 is drawn.
We have stored 56 in the symbol NR. Now type (eval nr) at the
command prompt. What do we get? We'll get 56. That the value
stored in the symbol.
The SETQ function gives a value back. It gives back the value
that is stored. The EVAL function gives back a value. And the
Quote function a name.
Let's use this feature now in a proper and a more or less so-
phisticated AutoLISP way. We have used functions in a list.
We're going to do it more.
We've used (/ 3.0 7). But also (/ 3.0 (setq nr 7)) can be
used. If we do a couple of things happen. I'll tell. Try to
find out why.
The SETQ function has given back the number 7. That number is
now used to divide the number 3.0. That's why 0.428571 is
displayed.
Exercise
In lesson 1 we did a calculation. Now let's do another calcu-
lation. I want to divide the number 9632 by the product of 63
times 13.
The three dots indicate that more numbers can be entered be-
tween the brackets of the function. For the first function
you could write:
(+ 1 2 3 4 5)
And now what the functions do. That's not too complicated.
The first four functions add, subtract, multiply, or divide
numbers.
What the MAX and the MIN function does is clear. They give
the maximum number or the minimal number of a list of num-
bers.
But you can also put the function in an ASCII file. Load the
file in AutoCAD and run it. Let's go a little bit further
than what has been explained.
Next load the file in AutoCAD. You know how to do it. Click
on Tools on the menu bar and on Load Application on the pull-
down menu.
The Load AutoLISP, ADS, and ARX file dialog box opens. Click
on the File button and locate the AutoLISP file in the Select
AutoLISP, ADS, or ARX file dialog box.
When that's done click on the Open button. The dialog box
closes. Next click on the Load button of the first dialog
box. And the AutoLISP file is loaded.
Type ARITH at the command prompt. And the AutoLISP file runs.
A lot of text is displayed at the command prompt. See for
yourself.
But first let's type our AutoLISP file. This is how it looks:
Forget about the PRINC function, about "\n", and about "\t".
We'll talks about them later. Just concentrate on the numbers
N1, N2, and N3.
The MAX function even converts the number 20 into a real num-
ber. It gives back 20.0 as the maximum number of the two.
One more thing we've done now. And that's good. That saves me
a lot of work. I've shown how to write an AutoLISP program in
an ASCII file.
And did you write the program in a Notepad file? Very good.
Congratulations. You can now call yourself an AutoLISP pro-
grammer.
Exercise
Do the following calculations:
1. 12 + 345 + 24.4
2. 96 - 23.7 - 34.4
3. Absolute value of - 123
4. Maximum value of the numbers 12 45 23 78 34
5. Biggest common divider of 2345 and 555
The EXPT function gives back the base number to the power of
the exponent number. So (expt 2 4) would give back 16.
The SQRT function gives back the root of the number. Couldn't
we have guessed it? So (sqrt 4) gives back the number 2.
The meaning of the COS and the SIN function is clear. And now
we want the tangent of an angle. That's the sinus dived by
the cosines. Right?
For using the COS and the SIN functions the angle must be ex-
pressed in radians. And let's forget about the ATAN function.
I never use it.
I told you. The angle must be given in radians. How many ra-
dians does a circle have? Wait. You know. You went to univer-
sity.
OK. Time for a little exercise. Remember the last time I cre-
ated a little program to demonstrate the working of the func-
tions.
One more tip. In the last lesson I was talking about using
Notepad of Windows. Forget about Notepad. I've got a better
idea.
Every now and then when you save a file a dialog box shows
up. The dialog box asks you whether you want to buy the pro-
gram.
When the dialog box shows up you have two choices: Purchse
Now and Continue Evaluation. Up to you. But I always click on
Continue Evaluation.
Exercise
1. What's the remainder of 45 divide by 8 and divided by 3?
2. What's the square root of 1526?
3. How many radians is an angle of 45 degrees?
4. What's the cosines of an angle of 135 degrees?
The first four functions are very important. Those are the
functions we use all the time. Those functions help us to
write some good AutoLISP programs.
The POLAR function gives back a point under the specified an-
gle from the given point and on the specified distance. The
angle is given in radians.
What has been said about the FLATLAND system variable is also
valid for this function. So a 2D point or a 3D point is given
back.
Don't worry too much about the new things. Later I'll explain
how these new tings work. For now let's just make a good use
of them.
Load the program in AutoCAD and run it. You run the program
by typing GEOMT at the command prompt. Then is asked for
points.
What does the program do? First it asks for two points. A
line is drawn between the two points. And the angle and the
distance is displayed.
Next two more points are entered. You're free to enter the
points where you want. The additional points can have a
crossing with the first line or not.
As you can see I've used the PRINC function again. That's
what I also did in the first program. Don't worry. Next we
talk about that function.
Exercise
I've got the following four points:
Point Value
P1 100,100
P2 200,200
P3 150,10
P4 150,20
I want to know:
Let me give you a tip. We can find the answers by doing some-
thing at the command prompt of AutoCAD. But let's write an
AutoLISP program.
The LIST function is used. Two numbers are placed between the
brackets of the LIST function. The numbers are not connected.
No comma.
A file name is entered. That can be the sole file name if the
file is in the search path of AutoCAD. Otherwise a path must
be specified.
But watch out. Use sledges for specifying a path and not back
sledges. This is how a path could be specified in the LOAD
function:
Here's an overview:
Function Description
(defun test (a b) ...) the function works with
two arguments
(defun test (/ a b ...) the function has two
local arguments
(defun test (a / b) ...) the function works with
one argument and has one
local argument
(defun test () ...) the function works
without arguments and has
no local arguments
In this way we can use all the AutoCAD commands. What? You
don't know the names of all commands. But you know how to
start all the AutoCAD commands.
If you don't know all the AutoCAD commands then just start
them. After starting them look at the command prompt. There
the name of the command is displayed.
Each command works with arguments. How can you find all the
arguments. Again start the command and look at the command
prompt.
Some commands start with a dialog box. But that's not what we
want. We want to see all the prompts of the commands.
Exercise
Create an AutoLISP routine that draws a rectangle. The width
of the rectangle is 2. In the middle of the rectangle is a
circle.
That's what I'm doing. And you can do it too when you know
AutoLISP. My AutoLISP programs make AutoCAD drawings within
one minute.
You can enter the sizes in two ways. You can pick points or
you can enter a number. Up to you. But entering numbers is
the easiest way.
First we see the SETVAR function and the GETVAR function. And
then there are functions which names start with GET. And we
see the PRINC function.
Now you see a lot of lines displayed. The lines come from the
LINE command and the CIRCLE command. We don't want those
prompts.
I also could have calculated nine points and draw lines and a
circle between these points. But that would be a waste. No
smart programming.
One more thing I did. You don't have to do that. But I placed
all variables that are used by the commands on a different
line.
You can place the variables on a single line. You can place
them on the same line as the command. But then you'll get a
mesh.
A few more words about PI. We know where PI stands for. I ex-
plained that one in one of the previous lessons. Here it's
used for angles.
Next time I'll talk about the functions for getting informa-
tion. I'll do it in two lessons. We have too many for doing
it in one lesson.
Exercise
No exercise in this lesson. You've had enough information.
You are exhausted now. So an exercise is not such a good
idea.
But this is what you do. Copy the AutoLISP routine from this
lesson in a file. Save the file and start working with the
AutoLISP routine.
First let's talk about six of them. These six are pretty
straight forward. There are two more functions. They are a
little bit complicated.
This is what we can see right away. The names of all these
functions start with GET. Of course. The functions are for
getting information.
And then you can add a prompt to each function. You can also
leave the prompt out. Adding a prompt is optional. You're
free to add a prompt.
The GETINT and GETREAL functions are used for entering an in-
teger or a real number. The integer can have a value between
-32768 and 32767.
You can also add a point to the function. If that's done you
don't have to point a second point. The point is to be con-
sidered as the first point.
Then you couldn't enter "Jos van Doorn". How did I come up
with that name?
Now I can enter a full name with spaces. I must press the EN-
TER key to terminate the function.
Exercise
The user is asked for a text. He can only enter a word. After
entering the word he is asked for an insertion point and an
angle.
The text is written to the screen. The size of the text does-
n't matter. Because there is zoomed in and out of the text.
That's why the size doesn't matter.
The GETORIENT function is used when the user has changed the
direction of the zero angle. By default that angle points to
the east.
You can specify what input is valid for the GETKWORD func-
tion. If the input is not valid then the GETKWORD repeats it-
self.
Isn't that a nice feature? Yes. It is. That's why I use this
function all the time. I very often use the INITGET function
together with the GERTKWORD function.
You can add bits to the INITGET function. You can also omit
adding bits to the INITGET function. But the integers are
added together.
Bit Meaning
1 Empty input not allowed
2 Zero value not allowed
4 Negative input not allowed
8 No check of limits, even with LIMCHECK = 1
16 3D points instead of 2D points
32 Dashes are used to draw a "rubber band"
The user must enter a value. He cannot press the ENTER key.
Zero can not be entered. And a negative input is not allowed
either.
One more thing. Suppose we can make a choice between LEFT and
LTYPE. If we type L then is not clear what choice we want to
make.
In the text for the INIGET function you write "LEft LType".
Now LE and LT re valid entries. L is not a valid entry.
But the INITGET function can also be used for other functions
to get information from the user. Here's an overview:
Function no no no no 3D Use
null zero nega- limits points dashes
tive
GETINT X X X
GETREAL X X X
GETDIST X X X X X
GETANGLE X X X
GETORIENT X X X
GETPOINT X X X X
GETCORNER X X X X
It's clear what this means. For the GETINT function you can
only use the 1, 2, and 4 bits. The other bits have no mean-
ing.
Now you've got all the functions for getting user input. Now
you're ready for programming in AutoLISP. You know how to use
the AutoCAD commands.
Exercise
No exercise this time. I mean. What exercise could I possibly
add to the lesson? We only talked about two functions. If we
were talking about more functions ...
Answers
Lesson 1
Type (/ 96 8) at the command prompt and press the Enter key.
That will give you the correct answer. The correct answer is
12.
Lesson 2
Lesson 3
This is to be typed at the command prompt:
Variable Value
!CC 11.7607
!PR 819
Lesson 4
Lesson 5
Lesson 6
Here's the AutoLISP routine. First the point values are as-
signed to the variables P1, P2, P3, and P4. And then there is
a calculation.
Lesson 7
Lesson 9
Here's the AutoLISP routine with all the magic. First there
is asked for a word. And then there is asked for an insertion
point and an angle.
For getting the angle the GETANGLE function is used. The user
can pick a point or he can enter an angle in degrees.
At the end we zoom in and put of the word that has been writ-
ten to the screen.
What next?
You can consider this course as an introduction to AutoLISP.
After doing this course you will be able to write your own
AutoLISP routine.
Predicates
Test Functions
Repetition Functions
System Variables
Text Screen Functions
Graphical Screen Functions
Text Functions
Conversion Functions 1
List Functions 1
External Files
AutoCAD Database
Entity Name Functions
Selection Set Functions
Entity Data Functions
Tables In AutoCAD Database
System Management
This course only gives the first ten lessons of the AutoLISP
course I wrote. The complete AutoLISP course consists of 30
lessons.
If you want it. You can get the complete course. The complete
course is only $ 10. And it is very much the same as this
course.
mailto: [email protected]
mailto:[email protected]
ACAD Newsletter
The ACAD Newsletter is about AutoCAD and AutoLISP. In the
ACAD Newsletter you find AutoCAD articles. In the articles
aspects of AutoCAD are explained.
The code was entered. Next the user could pick the position
of the blank. The blank was hanging on the cursor. The user
could move the blank and then pick a point.
Don't wait any longer. Get all the good information about
AutoCAD and AutoLISP. Take action right away. Send a blank e-
mail to:
mailto:[email protected]