## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

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

1

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

2

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 I’m giving it away. At no cost.

© Jos van Doorn 2003

3

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

4

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

5

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

6

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

7

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

8

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

9

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

AutoLISP Course

**Lesson 08: Creating Drawing 2
**

One aspect of AutoLISP is making drawings in AutoCAD with it. You know why we want to do that. AutoLISP is real fast. We will be saving a lot of time. 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. The drawings my programs make can also be made by hand in AutoCAD. If that's done then hours or days are used. So an AutoLISP routine is a big time saver. So when a drawing is made by AutoLISP we save a lot of time. And we save a lot of money. How much is one hour working with AutoCAD? That's what I promised. I promised to come with an AutoLISP program that makes a drawing. And that's what I always say: keep your promises.

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

No more talking. Let's go to the business for which we are here. Let's get on with the AutoLISP program. Well. Here it is:

© Jos van Doorn 2003

26

AutoLISP Course

Type this program in Notepad or TextPad. And save it under the name SDOOR.LSP. Next load it in AutoCAD and type SDOOR at the prompt. The program starts. It asks for the position of the door. Pick a point in the screen. And then three sizes are asked for. 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. After picking a point and entering three numbers the door is drawn. Also the posts are drawn. And an arc is drawn. In the program are a couple of new things. Yes. I know. You were thinking things are easy. Good. But I'll keep you busy. 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. The functions which names starting with GET are functions to get information from the user. I don't want to talk about them now. That's later. Let's go to the SETVAR function and the GETVAR function. You know about system variables. AutoCAD has got a lot of them. The SETVAR function is used to set a system variable. And the GETVAR function is used to get the value of a system variable. I start the program with:

Do you know the CMDECHO system variable? When that system variable is set to zero then no prompts of commands are displayed. To see the effect of that setting place a semi colon in front of that line. Load the program again in AutoCAD and run it.

© Jos van Doorn 2003

27

AutoLISP Course

If a semi colon is placed in front of a line in an AutoLISP program then that line is ignored by AutoLISP. 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. What the heck is the functions of these prompts? And then these prompts will slow down your AutoLISP program very much. I wanted to switch on ORTHO. Because if users want to end distances by picking points the points must be under fixed angles. First I see what the value of the system variable concerned is. I store that value in the OM variable. And then I set ORTHOMODE. At the end of the program I set ORTHOMODE back to its original value. I use the SETVAR function for doing that. OM is in that function. Why did I look up the value of the ORTHOMODE system variable? Well. Maybe ORTHO was switched on. If so I want to switch it back at the end. I didn't look up the value of the CMDECHO system variable. No point. That variable always has got a value of 1. If the system variable would have a value of zero then you cannot work with AutoCAD. You don't know what to do when a command is invoked. I already explained how the COMMAND function works. So the use of the COMMAND function is very clear for you. But there's something to say. This is about programming in AutoLISP. You must always do it in a smart way. Don't use too many variables. Keep it simple. That's what I did as I used the line command. I introduced the PT variable and I used the variable all the time. 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. For setting the value of the PT variable I used the SETQ function. That function gives a value back. That value is used by the LINE command all the time. 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.

© Jos van Doorn 2003

28

AutoLISP Course

I like to place the variables on separate lines. That's also useful if you want to print the program out. No wrapped lines. A few more words about PI. We know where PI stands for. I explained that one in one of the previous lessons. Here it's used for angles. AutoLISP only works with angles in radians. Maybe you cannot imagine yet. But we can make a good use of PI for specifying angles. A circle consists of 2PI radians. We all know that. So 2PI radians stands for 360 degrees. Right? Can you follow me? Do you agree? And now we want to have an angle of 90 degrees. How do we do that? Let's do some calculations. We must do some arithmetic. So get ready. 2PI stands for 360 degrees. So PI stands for 180 degrees. Wait a minute now. If PI stands for 180 degrees then PI times 0.5 stands for 90 degrees. I don't have to tell you. But it must be clear by now how to calculate an angle of 30 degrees or an angle of 45 degrees. Just calculate some more. I showed you an AutoLISP program to make a drawing in AutoCAD. Use that program. Have a good look at it. And make your own program. I don't think it's too difficult to make your own program now. Just follow my program. Use parts of it if you want. It can be done. Next time I'll talk about the functions for getting information. 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.

© Jos van Doorn 2003

29

AutoLISP Course

**Lesson 09: Getting Information 1
**

Now let's talk about functions for getting information from the user. AutoLISP has got a lot of them. We'll talk about them in two lessons. 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. Here are the six straightforward functions:

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. I think it's always a good idea to add a prompt. Otherwise users don't know what information is expected to be given. They are left guessing.

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 GETPOINT function is used for entering a point. We've seen that function in the GEOMT.LSP routine and the SDOOR.LSP routine. If a prompt is added to the function the prompt is displayed at the command prompt of AutoCAD. You could program like (getpoint "\nEnter point: "). If a point is added to the function then a rubber band is drawn from that point. We already have seen how that's done. The GETCORNER function is very much the same as the GETPOINT function. The difference is that the function needs a base point. The function can be used for entering points of a rectangle. The base point is one corner of the rectangle. And then there is the point for the opposite corner.

© Jos van Doorn 2003

30

AutoLISP Course

A rubber band is drawn between the two points. The rubber band has the forma of a rectangle. But the rectangle is not drawn as a point is entered. We have seen the GETDIST function. A point can be added to the function. Then the distance is calculated from the point if a point is picked. Otherwise you must pick two points to give a distance. But there is another way. You can also enter a number for the distance at the prompt. The GETINT and GETREAL functions are used for entering an integer or a real number. The integer can have a value between -32768 and 32767. With the GETANGLE function an angle can be entered. Two points can be picked or a number can be entered at the prompt. The function gives back the angle in radians. So if you enter 30 at the prompt the function gives back the real number 0.523599. 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 considered as the first point. The GETSTRING function is used for entering a string. Who could have thought about that one? But there's something special about the function. That's AutoCAD. A space is the same as a carriage return. The same for AutoLISP function. A space would terminate the GETSTRING function. But there is the CR argument. If that argument is present and unequal to nil then a space doesn't terminate the function. So. Suppose you use the GETSTRING function as:

Then you couldn't enter "Jos van Doorn". How did I come up with that name? If I would like to enter that name I had to use the GETSTRING function as:

Now I can enter a full name with spaces. I must press the ENTER key to terminate the function. Let me tell you something about T. T is short for TRUE. And TRUE is the opposite of nil. A variable can be nil and it can be T.

© Jos van Doorn 2003

31

AutoLISP Course

If the variable is nil then it has no value at all. If it's T then it has a value. You can use T if the value doesn't matter. We wanted to give the CR argument a value in the GETSTRING function. So we use T. We could also write "value". That's the same. We have two more functions for getting information. One of them I've never used. But the other one I use all the time. That's an important one. See you next time.

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 doesn't matter. Because there is zoomed in and out of the text. That's why the size doesn't matter.

© Jos van Doorn 2003

32

AutoLISP Course

**Lesson 10: Getting Information 2
**

Here are two more functions for getting information from the user of your AutoLISP program. I never use the first function. But the second one ...

The GETORIENT function is used when the user has changed the direction of the zero angle. By default that angle points to the east. This function is very much the same as the GETANGLE function. Except GETANGLE measures starting from the zero angle. GETANGLE measures relative angles. GETORIENT measures absolute angles. For GETORIENT the zero angle always points to the east. Why did I never use the GETORIENT function? I don't really mind how the angle is measured. And I can always use an angle given back by the GETANGLE function. Let's go to the GETKWORD function. That's a very nice function. Look at the name. That's short for GET KEY WORD. Isn't that beautiful? The GETKWORD function always works together with the INITGET function. In the INITGET function the keywords are specified. You can specify what input is valid for the GETKWORD function. If the input is not valid then the GETKWORD repeats itself.

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

Here's an example of how the GETKWORD function together with the INIGET function can be used:

"Continue? (Yes/No) " is displayed at the prompt. The user can only give an input of "Yes", "Y", "y", "No', "N", or "n". He must give one of these answers. The user cannot give an empty input by pressing the ENTER key. That's not allowed. In the INITGET function is specified what input is valid. After giving a valid input the value of the AW variable is "Yes" or "No". Even if the user gave the input was "Y", "y", "N", or "n".

© Jos van Doorn 2003

33

AutoLISP Course

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. Let's go to the INITGET function. Let's explain how that function works. The explanation is a lot. But still you can understand it. This is the syntax of the INITGET 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. Here's an overview of the bits that can be used: Bit 1 2 4 8 16 32 Meaning Empty input not allowed Zero value not allowed Negative input not allowed No check of limits, even with LIMCHECK = 1 3D points instead of 2D points Dashes are used to draw a "rubber band"

Here's an example of how the bits can be used:

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. Instead of (initget (+ 1 2 4)) we also could have written (initget 7). The three bits are added together. And the sum of the bits is used. The TEXT specifies the valid entries of the GETKWORD function. We already have seen that one. "Yes No' has been used. The use of capitals is important. The capitals are the letters that can be entered instead of the complete words. 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. The INITGET function is used for the GETKWORD function. You have to use the INITGET function there. You cannot leave that function out. But the INITGET function can also be used for other functions to get information from the user. Here's an overview:

© Jos van Doorn 2003

34

AutoLISP Course

Function

no null X X X X X X X

no zero X X X X X

GETINT GETREAL GETDIST GETANGLE GETORIENT GETPOINT GETCORNER

no negative X X X

no limits

3D points

Use dashes

X

X X

X X

X 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 meaning. 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 ...

© Jos van Doorn 2003

35

AutoLISP Course

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

Nr. 1. 2. 3. 4. 5. 6. Entry 3 1.23 (1 2 3) / <Selection set: 5> <File #A315> Type Integer Real or Float List Function Selection Set File Descriptor

Lesson 3

This is to be typed at the command prompt: (setq cc (/ 9632.0 (setq pr (* 63 13)))) I didn't write 9632. I wrote 9632.0. Now the number is a float. And a float is given back after the calculation. To find the values of the calculation and the product type !CC and !PR at the command prompt. This is given back: Variable !CC !PR Value 11.7607 819

Lesson 4

Nr. 1. 2. 3. 4. 5. Function (+ 12 345 24.4) (- 96 23.7 34.4) (abs -123) (max 12 45 23 78 34) (gcd 2345 555) Value 381.4 37.9 123 78 5

Lesson 5

Nr. 1. 2. 3. 4. Function Value (rem 45 8 3) 2 (sqrt 1526) 39.064 360 degrees is 2 pi radians. So 45 degrees is 2 pi divided by 8. The answer is 0.25 radians (cos (* 0.25 0.731689 3))

© Jos van Doorn 2003

36

AutoLISP Course

Lesson 6

Here's the AutoLISP routine. First the point values are assigned to the variables P1, P2, P3, and P4. And then there is a calculation. What has been calculated is written to the text screen of AutoCAD. To see it completely press the F2 key on the keyboard.

This is given back by the AutoLISP routine: Nr. 1. 2. 3. To measure Angle P1 and P2 Angle P1 and P3 Intersection point: (extension) (no extension) Value 0.785398 5.21949 150.0 150.0) nil

© Jos van Doorn 2003

37

AutoLISP Course

Lesson 7

This AutoLISP routine will draw the rectangle with a width of 2. It will also draw a circle in it and text in the circle. At the start everything in the screen is deleted. That's done by the ERASE command. At the end there is zoomed in and out of the drawing.

© Jos van Doorn 2003

38

AutoLISP Course

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. The GETANGLE function gives back the angle in radians. But that's a problem. Because the text command works with angles in degrees. So in the text command we must make a calculation. We must find out what is the angle in degrees. We must transform the AN variable. At the end we zoom in and put of the word that has been written to the screen.

© Jos van Doorn 2003

39

AutoLISP Course

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. But there is more to AutoLISP then what has been explained in the ten lesson of this course. Much more. This is what more there is in AutoLISP: 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. In almost every lesson is an exercise that goes into what has been explained in the lesson. You will also get the answers to all exercises. And that is a bonus that comes with the complete AutoLISP course. You'll get full support. You can always come to me with your questions. I will help you to get started with programming in AutoLISP. No matter what. If you’ve got a question you can always come to me with your question If you want the complete AutoLISP course then let me know. Send me an e-mail with "Complete Course” in the subject line. This is my e-mail address: mailto: chi@barnsco.com

© Jos van Doorn 2003

40

AutoLISP Course

Looking forward to hearing from you,

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

41

AutoLISP Course

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. In the ACACAD Newsletter articles about paper space have been published. In it were also articles about creating linetypes and hatch patterns. For AutoLISP you'll find many AutoLISP routines. The listings of the AutoLISP routines are given. Complete with a thorough explanation. That was published in the earlier editions of the ACAD Newsletter. In those editions was the BLANK AutoLISP routine. It was for drawing a blank. A blank is a rectangle containing a code. In piping drawings you always find a lot of blanks. The blanks routine was a very useful routine. 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. The ACAD Newsletter was also publishing a profile application. The application worked with dialog boxes. The user could do a lot with it. The user could select the type of profile to be drawn. And then he could enter the size of the profile and the view of the profile. Next the profile was drawn. There is a lot more that can be found in the ACAD Newsletter. In it you'll find AutoLISP tips, feedback, and other tips that make working with AutoCAD easier. The ACAD Newsletter is send out once a week. It has been up for more than two years. And you don't have to pay to get it. It's distributed at no cost. Don't wait any longer. Get all the good information about AutoCAD and AutoLISP. Take action right away. Send a blank email to: mailto:acadnewsletter-subscribe@topica.com

© Jos van Doorn 2003

42

This document was created with Win2PDF available at http://www.daneprairie.com. The unregistered version of Win2PDF is for evaluation or non-commercial use only.

- Advanced AutoLISP Programming[1]
- autolisp
- AutoCAD 2000 Visual LISP Tutorial - 140 Pages
- autocad and lisp
- Visual Lisp
- autolisp2
- Autolisp Programs
- The AutoLISP Crash Course
- VLisp_Developers_Bible
- 2_Programming in AutoLISP
- The+Language+and+Its+Developmen+ +Togores%2C+Reinaldo+N
- ABC's of Autolisp
- AutoLisp
- Acdmac 2013 Autolisp Developers Guide
- Autolisp Reference - Visual Lisp, Autolisp & Dxf - Auto Cad 2004
- Visual Lisp Tutorial (Afra)
- AutoLISP
- Autolisp Notes
- The Visual LISP Developers Bible - 2011 Edition
- AlispRef
- AutoLisp Programming
- Mastering AutoLISP in 80 Minutes
- AutoCAD Lisp Routines and Commands List
- Autolisp Programs & Visual Basic Programs
- HyperPics_ AutoLISP Downloads.pdf
- Autolisp Notes New By Swapnil A. Kale
- AutoLISP Programming Techniques
- Loading AutoLISP Files _ AfraLISP
- autolisp
- AutoLisp

- Lunar Roving Vehicle Navigation System Performance Review
- XII maths
- tmp821B.tmp
- UT Dallas Syllabus for math2312.002.11f taught by Manjula Foley (mxf091000)
- Optimization of Numerically Controlled Oscillator in digital communication systems
- Algebra II/Trigonometry Regents exam
- Berkeley Solar Drone
- High School Mathematics Grade 10-12
- UT Dallas Syllabus for math2312.701.11f taught by Manjula Foley (mxf091000)
- tmpDF31.tmp
- Mathematics-III
- tmp1BFA
- tmp6DA3.tmp
- tmp32DD.tmp
- tmpF183.tmp
- UT Dallas Syllabus for math1316.001.11f taught by Manjula Foley (mxf091000)
- UT Dallas Syllabus for math2312.501.10f taught by Tommy Thompson (txt074000)
- tmp5CAE
- UT Dallas Syllabus for math2312.002.11s taught by Manjula Foley (mxf091000)
- tmpEB53.tmp
- tmpA127
- Numerical Analysis
- Scientific American Supplement, No. 531, March 6, 1886 by Various
- tmpCA9F
- UT Dallas Syllabus for math2312.002.10f taught by Manjula Foley (mxf091000)
- tmp4BA8.tmp
- tmp77F9.tmp
- tmp7C99
- tmp6B20
- tmpA14.tmp

- ISRO Sample Programming Placement Paper Level1
- tmpD747
- tmp664
- tmp2A89.tmp
- As NZS ISO IEC 15437-2003 Information Technology - Open Distributed Processing - Enhancements to LOTOS (E-LOT
- Use of Linear Regression in Machine Learning for Ranking
- frbclv_wp1987-11.pdf
- tmp82D3.tmp
- Mindtree Sample Programming Placement Paper Level-I
- As NZS 4379.2-1996 Information Technology - Open Systems Interconnection - Remote Database Access SQL Special
- As ISO IEC 15444.1-2004 Information Technology - JPEG 2000 Image Coding System Core Coding System
- tmp9B1B.tmp
- R/C Soaring Digest - Sep 2006
- Huawei Sample prograamming Placement Paper Level1
- NIIT Sample Technical Placement Paper Level1
- frbrich_wp15-10.pdf
- SISL Sample Programming Placement Paper Level1
- tmpBDD4.tmp
- As NZS 62026.5-2001 Low-Voltage Switchgear and Controlgear - Controller-Device Interfaces (CDIs) Smart Distri
- L&T Infotech Sample Programming Placement Paper Level1
- Firstsource Sample Technical Placement Paper Level1
- Tcs Technical Placement Paper
- Principal of Programming Language
- As NZS 3827.1-1998 Lighting System Performance - Accuracies and Tolerances Overview and General Recommendatio
- Hawk Technology Systems v. Pennsylvania State University
- HCL Sample Technical Placement Paper
- tmp1E7B.tmp
- Logica CMG Sample Programming Placement Paper Level1
- 3-Axis Motion Control of CNC Machine based on G-Code, M-Code using FPGA and also Apply Bezier Curve
- As NZS 14882-1999 Programming Languages - C Plus Plus

Sign up to vote on this title

UsefulNot usefulClose Dialog## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Close Dialog## This title now requires a credit

Use one of your book credits to continue reading from where you left off, or restart the preview.

Loading