Você está na página 1de 25

AutoLISP Course

By Jos van Doorn. AutoCAD specialist and AutoLISP programmer. Also publisher ACAD Newsletter. About AutoCAD and AutoLISP. FREE. To subscribe send an e-mail to: mailto:acadnewsletter-subscribe@topica.com

Jos van Doorn 2003

AutoLISP Course

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

Jos van Doorn 2003

AutoLISP Course

Introduction
Thanks for requesting the AutoLISP course. After reading and studying this course you will be able to write your own AutoLISP routine. AutoLISP is a very powerful feature of AutoCAD. In AutoLISP you can write your own routines that can be used for creating AutoCAD drawings. That's what you will see. AutoLISP is very fast. Using an AutoLISP routine an AutoCAD drawing is created in less than one second. You want to learn programming in AutoLISP. That's why you are doing this course. I think that's a very wise decision. That will help you a lot. 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. I mentioned it before. AutoLISP is very powerful. Anything you can do in AutoCAD can be done using an AutoLISP routine. Anything! You can use AutoLISP routines to create parts of an AutoCAD drawing. But you can also use it to create a complete AutoCAD drawing. To create an AutoCAD drawing by hand takes hours. Sometimes it takes days. An AutoLISP routine can do the job within one second. That's a time saver. This course will help you to get started with programming in AutoLISP. In every lesson is an exercise. The exercise is about what has been explained in the lesson. Maybe you've got a problem. If so come to me. Ask me what's bordering you. Or what you cannot solve. And I'll give you an answer. You can send me an e-mail. Write "Problem" in the subject line. Then I can see where your e-mail is about. This is my e-mail address: mailto: chi@barnsco.com

Steal this e-book


Easy. Give this e-book away to other people that are interested in AutoLISP programming. Or send it to them by e-mail. Or put it on your web site. One thing. This e-book is a no cost e-book. It cannot be sold. It can only be given away. Because Im giving it away. At no cost.

Jos van Doorn 2003

AutoLISP Course

Have a good course.

Jos van Doorn. AutoCAD specialist and AutoLISP programmer. Also publisher ACAD Newsletter. About AutoCAD and AutoLISP. FREE. To subscribe send an e-mail to: mailto:acadnewsletter-subscribe@topica.com

Jos van Doorn 2003

AutoLISP Course

Lesson 01: About AutoLISP


AutoLISP is one of the dialects of the programming language LISP. LISP has been developed by John McCarthy in the fifties. LISP was the second higher programming language after FORTRAN. I was never really popular. That changed in the eighties. LISP is short for LIst PRogramming or LIst PRocessing. But they say it is also short for "Lots of Stupid Parentheses". AutoLISP is a programming language that is interpreted. AutoLISP is not compiled. The advantage is that mistakes can easily be detected.

Why wait for hours or even days? Get your AutoCAD drawings right away! Find out how. Send an e-mail to: mailto:acadconsult@sendfree.com
A further explanation of AutoLISP will be given later. But let's start with an example of how AutoLISP can be used when drawing in AutoCAD. Suppose we want to insert a block. And the block must have a scale of three divided by seven. You can calculate that number or use AutoLISP. This is how you can do it:

For the x-scale (/ 3.0 7) is entered at the prompt. That means three divided by seven. AutoLISP makes the calculation and comes with 0.428571. This is a feature of AutoLISP you can use right away. But there is more to do than division. Here's an overview of other arithmetic functions: Action Addition subtraction multiplication Function (+ <number> <number ..) (- <number> <number ..) (* <number> <number ..)

You can work with two numbers. But you can also put more numbers between the brackets. Then the same operation is done on more numbers.

Jos van Doorn 2003

AutoLISP Course

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.

Jos van Doorn 2003

AutoLISP Course

Lesson 02: Entities


In AutoLISP we work with entities. The names of these entities are: Atom Atoms Description Elements consisting of one or more numbers and characters. Examples are: ABC, 123, A1, P6, etc. In the examples the atoms have a value. If the atom has no value then the value is nil A number of elements placed between brackets. The elements can be atoms or other lists. Examples: (A B C), (1 2 (34) 5). A list can also contain no elements. Then it is represented as () or nil Functions are the bread and butter of AutoLISP. We'll use functions all the time. We already have used a function. Remember the use of (/ 3.0 7) in the previous lesson?

Lists

Functions

This is an explanation: Atom (/ 3.0 7) / 3.0 7 /, 3.0, and 7 Type List Function Real Number Integer Atoms

The function always comes directly behind the opening bracket. Later all AutoLISP functions will be explained

Why wait for hours or even days? Get your AutoCAD drawings right away! Find out how. Send an e-mail to: mailto:acadconsult@sendfree.com
An atom can be: Atom Symbol Description 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 A string is a text placed between quotation marks. Examples of texts are: "This is a text" "Anyone in for AutoLISP"

String

Jos van Doorn 2003

AutoLISP Course

Atom Real or float

Description Reals or floats have a decimal part. Examples are: 3.4256 1.5 Integers are numbers without a decimal part. Examples are: 3 56 These are built-in function. AutoLISP has a number of built-in functions. You don't really want to know these functions. You don't need to know these functions A file descriptor is given to every external file AutoLISP works with. This is an example of a file descriptor: <File #E872> 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: <Entity name: 60000014 > A selection set is created by AutoCAD when the Select objects prompt appears. This is how it may look: <Selection set: 1 >

Integer

Subrs

File descriptor

AutoCAD entity name

AutoCAD selection set

Exercise
What's the type? 1. 2. 3. 4. 5. 6. 3 1.23 (1 2 3) / <Selection set: 5> <File #A315>

Jos van Doorn 2003

AutoLISP Course

Lesson 03: Setting Values


We're now going to talk about one function we'll use a lot in AutoLISP and two more functions. The functions are: 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. The symbol is a number. And we want to store the number 56 in that symbol. This is how we do it:

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. What is displayed at the command prompt is what's given back by the function. In this case 56 is given back by the function. The number 56 is stored in the symbol NR. We can see that that number is stored in the symbol. Type !nr at the command prompt. When that's done the value of the symbol is displayed. The number 56 is displayed at the prompt. That's the number we stored in the symbol. There is something special we can do with the SETQ function. We can shorten the use of the SETQ function significantly. Suppose we want to assign values to three different variables. One way of doing that is if you do it like this:

Here's the shorter way:

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 carriage returns make no difference for AutoLISP.

Jos van Doorn 2003

AutoLISP Course

Why wait for hours or even days? Get your AutoCAD drawings right away! Find out how. Send an e-mail to: mailto:acadconsult@sendfree.com
This is a nice option. Suppose you want to draw a circle with a radius of 56. You stored 56 in the symbol NR. This is how you do it:

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. Watch out with naming symbols. Don't use names of functions for a symbol name. So you cannot say:

With the EVAL function the value of a symbol can be checked. It works the same as placing an exclamation mark before the symbol name. 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 QUOTE function doesn't evaluate a symbol. You can use it with a symbol name. The symbol name is then written out in capitals. See for yourself. Type (quote nr) at the command prompt. Press the Enter button. And NR is displayed at the prompt. Not the value. We've talked about three functions. And what have we seen? This is very important. Each function gives something back. A function always gives something back. 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 sophisticated 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. Go ahead. Type (/ 3.0 (setq nr 7)) at the command prompt and press the Enter key. On the next line the number 0.428571 is written.

Jos van Doorn 2003

10

AutoLISP Course

That number is given back by the division function. But more has happened. The number 7 is stored in the symbol NR. See for yourself. Type !nr at the command prompt. And the number 7 is displayed. That's the value that has been stored in the variable NR. 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. What we've seen here is an example of nesting functions. We'll do that a lot later as we work with AutoLISP. But easy, my dear subscribers. We don't want to go too fast. Let's first take a rest. Next time I'll tell about arithmetic functions.

Exercise
In lesson 1 we did a calculation. Now let's do another calculation. I want to divide the number 9632 by the product of 63 times 13. But let's do it in a proper way. I want to make the calculation in one line. And I want to store values in the CC and the PR variable.

Jos van Doorn 2003

11

AutoLISP Course

Lesson 04: Arithmetic Functions 1


Now it's time to go to AutoLISP functions. Here' we'll talk about arithmetic functions first. AutoLISP has got a lot of them. Here's a list of ten of these functions:

Let me first make some remarks about the notification. Each function starts with an opening bracket. Then comes the function. Next to the function you find written <number>. That means a number is expected. You cannot enter a text or a string there. The three dots indicate that more numbers can be entered between the brackets of the function. For the first function you could write: (+ 1 2 3 4 5) At the end is always a closing bracket. In AutoLISP the number of opening and closing brackets is always equal. Also here.

Why wait for hours or even days? Get your AutoCAD drawings right away! Find out how. Send an e-mail to: mailto:acadconsult@sendfree.com
And now what the functions do. That's not too complicated. The first four functions add, subtract, multiply, or divide numbers. Just to make it a little bit clearer. Suppose you have (* 2 3 4). What does this function give back? It gives back 24. That's 2 times 3 times 4. Or suppose we have (/ 30 5 2). What does the function give back? The function gives back 3. Because that's 30 divided by 5 divided by 2. The function 1+ and 1- add or subtract one from the number. So (1+ 5) would give back 6. And (1- 10) would give back 9. We don't have (2+ <number>).

Jos van Doorn 2003

12

AutoLISP Course

The ABS function gives the absolute value of a number. So (abs -100) gives back 100. Do you see? You can only place one number between the brackets. What the MAX and the MIN function does is clear. They give the maximum number or the minimal number of a list of numbers. The GCD is a very nice function. It gives the biggest common divider of two numbers. The numbers must be integers and positive. Here are some examples: Function (gcd 81 57) (gcd 12 20) Given back 3 4

We've got nine more arithmetic functions. In the next lesson we'll talk about them. But let's first do some AutoLISP programming. You can do it directly at the prompt. Enter (gcd 12 20) at the prompt. And see what happens. The number 4 is displayed. That's given back by the function. 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. Type the following in an ASCII file. Open Notepad and type it in it. Than save the file under the name ARITH.LSP. Use that name with the extension. 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 pulldown 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:

Jos van Doorn 2003

13

AutoLISP Course

Jos van Doorn 2003

14

AutoLISP Course

When finished typing the AutoLISP program you must save it on your hard disk. Save the AutoLISP program under the name ARITH.LSP. 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 number N1 is 12.0. That number is a real number. It has a decimal part. The other numbers are both integers. It have no decimal part. Look at what type of number is given back by the functions. If one number of the function is a real number than a real number is given back. The MAX function even converts the number 20 into a real number. 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 programmer. Nice to have you around, fellow AutoLISP programmers. OK. I need to tell a few things more. But you're on your way to become a number one AutoLISP programmer.

Exercise
Do the following calculations: 1. 2. 3. 4. 5. 12 + 345 + 24.4 96 - 23.7 - 34.4 Absolute value of - 123 Maximum value of the numbers 12 45 23 78 34 Biggest common divider of 2345 and 555

Jos van Doorn 2003

15

AutoLISP Course

Lesson 05: Arithmetic Functions 2


We've got nine more arithmetic functions in AutoLISP. I told you, remember? Well. Here they are. Here's a list of the remaining functions:

The REM function gives the remainder of a division. So (rem 42 16) would give back 10. Because that's left after dividing 42 by 16. The EXP function is created for the people with an academic background around us. It gives back the number e to the power of the number. So (exp 1.0) gives back 2.71828. What's given back is always a real number. And do we want to know how the number e is calculated? 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 LOG function gives back the natural logarithm of a number. Here's an example. The function (log 4.5) gives back 1.50408.

Why wait for hours or even days? Get your AutoCAD drawings right away! Find out how. Send an e-mail to: mailto:acadconsult@sendfree.com
PI is not really a function. It's a number we use for calculating angles and so. PI stands for 3.1415926. You know what it means. 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 expressed 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 radians does a circle have? Wait. You know. You went to university.

Jos van Doorn 2003

16

AutoLISP Course

I also went to university. I met the man at the door. He was very nice. And a full circle ahs got 2 pi radians. I must make one more remark about the notification. I already explained for what the dots stand. But we also have square brackets. What's placed between square brackets is optional. You can place it in the function or leave it out. But when left out the function works differently. OK. Time for a little exercise. Remember the last time I created a little program to demonstrate the working of the functions. Go ahead. You now know how to write a program. So write a program yourself. Write a program to demonstrate the mentioned functions. One more tip. In the last lesson I was talking about using Notepad of Windows. Forget about Notepad. I've got a better idea. Go to HTTP://WWW.TEXTPAD.COM. There you can download a text editor that's much better than Notepad. One thing. They want you to buy it. Still download the program. The download is a fully functional evaluation version. It only has one disadvantage. 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 program. 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. 2. 3. 4. What's the remainder of 45 divide by 8 and divided by 3? What's the square root of 1526? How many radians is an angle of 45 degrees? What's the cosines of an angle of 135 degrees?

Jos van Doorn 2003

17

AutoLISP Course

Lesson 06: Arithmetic Functions 3


Here are five more functions. At least four of them are very important. We'll use them all of the time while programming in AutoLISP. Here are the functions:

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 first function calculates the angle between the two points. It's the angle between the two points and the active UCS. If the points are 3D points then the angle is projected in the XY-plane. So no 3D angle will be given. Only a projected angle will be given. The function gives back the angle in radians. So it doesn't give back the angle in degrees. AutoLISP always works with radians. The second function can be used for measuring a distance between two points. What's given back depends on the value of the FLATLAND system variable. If the FLATLAND system variable is unequal to zero than 2D points are expected. And a 2D distance is given back. Otherwise a 3D distance is given back.

Why wait for hours or even days? Get your AutoCAD drawings right away! Find out how. Send an e-mail to: mailto:acadconsult@sendfree.com
The INTERS function calculates at what point the lines between four points cross. The ON argument is optional. You can use it or not. If the FLATLAND system variable is unequal to zero than 2D points are expected. A 2D point is then calculated. Otherwise a 3D point. If the ON argument is present and equal to nil then the lines going through the points are considered to be without an end. So there is always a crossing then. If the argument is not present or the argument is unequal to nil then the crossing must be on the lines between the points.

Jos van Doorn 2003

18

AutoLISP Course

The POLAR function gives back a point under the specified angle 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. The OSNAP function calculates a point on a line in accordance with the MODE argument. The original point is lying on a line. For the mode the following can be used:

Let's write an AutoLISP program. This program demonstrates the functions that are mentioned here. The program has some new things. 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. Here's the program:

Jos van Doorn 2003

19

AutoLISP Course

Type the program in TextPad or Notepad. And then save it under the name GEOMT.LSP. It's important to add the LSP extension to the name. 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. If they have a crossing with the first line then a donut is drawn on that line. Otherwise a donut is drawn outside the lines. 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. In the next lesson we're going to be more serious. Then we're going to draw something with the help of AutoLISP. Where are my seatbelts?

Exercise
I've got the following four points: Point P1 P2 P3 P4 Value 100,100 200,200 150,10 150,20

I want to know: 1. 2. What's the angle between P1 and P2? What's the angle between P1 and P3?

Jos van Doorn 2003

20

AutoLISP Course

3.

What the intersection between the four points? if the lines are extended if the lines are not extended

Let me give you a tip. We can find the answers by doing something at the command prompt of AutoCAD. But let's write an AutoLISP program. In teh AutoLISP program are four variables for the points. The variables are P1, P2, P3, and P4. You can assign points to the variables. This is how the point 100,100 is assigned to the point P1. In the AutoLISP routine you use the following line for doing that.

The LIST function is used. Two numbers are placed between the brackets of the LIST function. The numbers are not connected. No comma.

Jos van Doorn 2003

21

AutoLISP Course

Lesson 07: Creating Drawings 1


We've done a lot of functions up till now. One aspect of AutoLISP is making drawings. So it's time to get started with making drawings. Wait, wait, my fellow AutoLISP programmers. There are a few things I must explain first. Otherwise you're completely lost. And I don't want that. I knew what you're going to say. Otherwise you're going to complain. You're going to say that I'm trying to confuse you. Not true. I must first tell you something about: Loading an AutoLISP program in AutoCAD Defining a function in AutoLISP Defining a new command in AutoLISP Using standard AutoCAD commands in AutoLISP I'll give an explanation about the above. When that's done I'll come with an AutoLISP program. I'll do that in the next lesson. We can load an AutoLISP program over AutoCAD. We click on Tool on the Menu Br and on Load Application on the pull-down menu.

Why wait for hours or even days? Get your AutoCAD drawings right away! Find out how. Send an e-mail to: mailto:acadconsult@sendfree.com
But there's another way. That's a more direct way. We use an AutoLISP function for doing that. That function is more comfortable. This is the syntax of the function:

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:

We don't have to mention the extension LSP. The file is supposed to be a valid AutoLISP file. But when the file is saved the LSP extension must be given.

Jos van Doorn 2003

22

AutoLISP Course

The LOAD function has the optional NOT PRESENT argument. That's a text. The text is displayed when the file is not present. You don't have this optional argument when loading an AutoLISP file from within AutoCAD over Tools etc. You must select a file. A function in AutoLISP is defined using the DEFUN function. We already have seen how that function is used. This is the syntax of that function:

SYMBOL is the name of the function. That name is used to make a call to the function. The name can never be the name as an AutoCAD command. There are two types of arguments. The first type of arguments are arguments that are used by the function. The second type are used in the function. Those two arguments are divided by a sledge. In front of the sledge and after the sledge is a space. The second type of arguments are special. The second type of arguments only have a value in the function. Outside the function they have no value. These arguments are called local arguments. Here's an overview: Function (defun test (a b) ...) (defun test (/ a b ...) (defun test (a / b) ...) Description the function works with two arguments the function has two local arguments the function works with one argument and has one local argument the function works without arguments and has no local arguments

(defun test () ...)

We must always try to specify arguments in a function as local. That way they have no value outside the function. That saves memory. The DEFUN function always works with an argument list. But the function can work without arguments and have no local arguments. In that case the argument list is empty. For the argument list two brackets are placed in the DEFUN function. This is how a function is defined using the DEFUN function. But the DEFUN function can also be used to define a new command.

Jos van Doorn 2003

23

AutoLISP Course

That works the same as defining a function. The difference is that the symbol name is preceded by C: like in (defun c:nwcmd () ...) Normally a new command doesn't work with arguments. But a new command can have local arguments. They are specified as before. Here is a simple function. See whether you can recognize what have been said.

The function works with the argument NR. That argument is a number. And the function gives back the number plus 10. This is the function used to use AutoCAD commands in an AutoLISP program. The function has an argument. What comes next depends on the command.

Here's how the command function could be used:

ET is an entity. The entity is placed in the command function as an argument. The ERASE command is terminated by using "". 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. Here's how the TEXT command is used in AutoLISP:

Do you recognize all these arguments? If not start the TEXT command in AutoCAD. See what arguments are needed for the TEXT command. Some commands start with a dialog box. But that's not what we want. We want to see all the prompts of the commands.
Jos van Doorn 2003 24

AutoLISP Course

An example is the BHATCH command. Type that command at the command prompt. And see that the Boundary Hatch dialog box is displayed. Instead of BHATCH type -BHATCH at the command prompt. Now no dialog box is. Prompts are displayed. You can find all the arguments. There are also a lot functions for requesting information. We need these functions very often in an AutoLISP program. Later we'll talk about these functions. But first I want to do something with a simple program. I want to give an explanation about this program. With the simple program we'll be drawing something with AutoLISP. And isn't that what we want to do with AutoLISP? We want to use it for making drawings.

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. In the center of the circle a text is written. The text is "TEST". The height of the text is half the radius of the circle. The corner points of the rectangle are 100,100 and 400,400. The radius of the circle is 100. For the text any text style can be used.

Jos van Doorn 2003

25

Você também pode gostar