Anda di halaman 1dari 18

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

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 youve 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.

Anda mungkin juga menyukai