Description
Key Learnings
- Explain common AutoLISP programming terms
- Identify the common data types encountered within AutoCAD® software and AutoLISP
- Use the Visual LISP Editor to write and format simple LISP routines
- Load, use, and make available for future use those programs that will automate workflow
Speaker
- CBCraig BlackCraig Black has been working with Autodesk, Inc., products since 1985. He has been teaching in the Mechanical Design Technologies programs of Fox Valley Technical College (FVTC) since 1990. At FVTC, he has developed 2 programs within his department, the CAD Customization Advanced Certificate and the Mechanical CAD Drafting Technical Diploma. He has been presenting at Autodesk University nearly since its inception, offering classes on diverse topics ranging from programming and customization to solid modeling and CAD standards. He is a co-author of the widely acclaimed AutoCAD and Its Applications textbook, published by Goodheart-Wilcox, and he has been contracted to do training all over the United States. When not teaching, he works as a programmer/consultant for a large furniture manufacturer based in Wisconsin. Time and weather permitting, Black likes to squeeze in a few rounds of golf each week, as well as cheer for his beloved 13-time NFL World Champion Green Bay Packers.
CRAIG P. BLACK: How we all doing this morning? Good. Is this your first session? No? Go to something else? Good stuff so far? All right, cool. We'll try to keep the ball rolling, all right?
Oh, does anybody want a paper handout? I got about five extra. I'll auction them off. Yeah, lovely lab assistants helping me. All right, cool. I printed them off for my lab assistants and then they did it all themselves anyway. They're good. They're good.
All right. Let's get rocking. I was talking to Scott, one of our lab assistants, a little bit earlier, and I said, yes, this is a basics course and it's not real deep by any means. But I got 90 minutes to get it across to you. And if I really wanted to, I could drag it out for about eight hours. It would be not a problem because I talk a lot. So my job is to shut up and let you guys learn some of this.
All right. Pressed the wrong button. This is me. They wanted me to put a picture, but I couldn't see any reason to make a picture that big so I put six of them. This is the first Autodesk University that I taught at in 1995 I think San Francisco. The one next to it and the one just below that, were here in Vegas. I don't remember what years that was.
This was a year that two different people stopped me as I was walking around and asked me if I was Tommy Chong. I said no, but that was cool. Well, that's because I kind of looked like this. My wife loves it when I make that my profile picture on Facebook. She changes her last name.
And then there's me snow blowing snow last year. I grew up in Green Bay, Wisconsin, and still live there. If you jump my backyard fence you're in Lambeau Field parking lot. So I'm a Packer fan. And then this is me just a couple of summers ago. Kind of looks like me, but I realize now even then I had more hair than what I have right now.
And then you can see some other stuff about me up there. I will walk around up here mostly because I did actually add some notes to PowerPoint. I've been using AutoCAD since 1985; programming with AutoCAD since probably '88, something like that. Working in the educational area now. I teach at Fox Valley Technical College. I was the ATC manager for a number of years and then I moved into the mechanical CAD drafting area. And I've been teaching in there since like 2004 or something like that. So got a lot of experience with AutoCAD. I do a lot of side work programming within AutoCAD. And that's why I'm here.
Got to remember which button to press here. Our lab assistants today are Lee Ambrosius over here. The only ones clapping are the other two lab assistants. I do like being down here. I don't even know what I wrote in my notes in that PowerPoint. So I don't like using PowerPoint, but it works great for this. I'm not a PowerPointer, but we'll get through it.
Lee lives in Green Bay also, or De Pere actually we live, De Pere. And then I mean Chris Lindner, sorry, is also here, and Scott Wilcox. Proud to call them colleagues and friends. We've been working together for a number of years. They've helped out in past lab sessions.
Two of our people here have one Auggie's Top Dog award. You won the very first one, right? That was 2006. Man, we are old. Scott, when did you win? Me neither. Me neither. It's not right. I did finish second twice and it's not really cool to win second when how many people are here at Autodesk University? Like 10,000, something like that? When there's 10,000 people here and they all have the chance to take that Auggie Top Dog contest questionnaire and you lose to a guy that lives four houses away from you, he doesn't rub it in too much, so it's all right.
Anyway, so we got very capable helpers. If you've got any questions-- I typically don't stop for questions because I like to talk. But raise your hand and these guys will come over and get you unstuck. One thing I forgot to tell you guys was if somebody gets messed up, just open up the previous one from the data set. So if we're working on exercise 4, just open up exercise 3 completed and you'll be caught up. Now, that's not a sign for you guys to go in and open up exercise 8 completed and then hit the door. We won't learn anything like that.
All right. Here's what we want to work through. This is why I don't like PowerPoint because I'm going to read to you right now. Explain common AutoLISP programming terms. This stuff is logical. It only works one way. We have to learn how to talk the talk. We can't get help by going, well, this thing right here isn't really doing. We have to know what that thing is and we have to refer to it correctly to get the help that we need. So getting the terms down is a big thing.
Identify common data types encountered within Autodesk, AutoCAD software-- all those R's throw me all the time. --software. Data types, programming terms. Yep, data types. Even a little bit more important that we understand what those data types are. And we will drill that into your head. Again, the working word for the next 88 minutes or however long I haven't been talking at is logical. We want to understand that this stuff is logical. So if we get the terms down and we understand the data types, we'll have it.
Visual LISP Editor to write and format simple LISP routines. We're going to type in stuff right at the command prompt. Just kind of do a line of code at a time until about the last 15 minutes of the day, then we will open up the editor and create a file, excuse me, of all that data. I just caught a very bad cold yesterday, so I'm all out of wind.
All right. And then load, use, and make available for future use, those programs that we just got done creating with the Visual LISP Editor so that we can use those in other drawings. You're going to be upset. When we get done today, you will know how to pick four points and connect them with the line command. I know, I know. Just relax. But we will have gathered an awful lot of confidence in how that actually happened and understand how to apply that in our own world.
I don't know what you guys are doing. I came from a contracting background. I was a drafter for whoever needed me. I did civil drafting. I did electrical drafting. I did architectural drafting. I did mechanical drafting. I did structural drafting. Everybody's going to be using this stuff differently, and how you use it is up to you.
All right, here's the terms that we have to get down. And I'm going to kind of skip around because I don't like just reading PowerPoints to you. Terms that we have to get down. First of all, we are working with AutoLISP. Everything is inside of a set of parentheses. My whole goal is by the end of the course for you to not be able to say AutoLISP. And anytime you use the word parentheses, you need to put your hands up and put your hands out like that and make the sign of parentheses because that's all this is all about. We take away AutoCADs tools as soon as we type in an opening parentheses. Soon as we type in that opening parentheses. See how I did that? We have to teach five years to remember to use the right hand.
Thank you. She says don't trip over the projector again. what's that? Yes, not being Livestreamed, so that's all right. But everything we use, the term list and everything or any list is anything inside of a set of parentheses. Exactly. Even before I said the word parentheses, he was doing that.
If you'd skip that second term and go down to the third one, function. The term function applies to a specific atom. Let's go back to the one previous to it. Everything that's inside of a list is typically called an atom. This is where most of you are going to say thing. Well, the first thing is this, and the second thing is that. Now it turns into a chemistry class because I always use the term element also to refer to the different things inside of a set of parentheses.
Element. The first element in-- oh no, we're going to go to another one below. Argument is the word expression. An expression is a very specific type of list. A list are anything inside of a set of parentheses, and there are atoms inside of a set of parentheses. The first atom inside of a function list is always going to be-- excuse me, the first atom inside of an expression list is always going to be a function. And then other things that are inside the list are other arguments that that function needs.
We're done. That's all you have to keep track of. We need a list. We need a function to be the first element atom inside that list, and we need to give that function the things that it needs and will have a good expression. There's other stuff on there.
Data types. Those arguments that we have to pass to the functions that we use inside of AutoLISP need to be of a specific data type. You never really think about this when you're using AutoCAD every day, but the line command-- now we turn it into a 12 step program. What does a user have to give to the line command in order to create a line?
No, don't start hitting buzzwords just because I said that. Think about it. Don't think about all your vast knowledge of AutoLISP right now. You need a point. Here's a secret. A point is just a list, another specific type of list. That one doesn't have a function as the first atom within the list. It's just got the x value, and then the second atom inside of a point list is the y value, and the third atom is always zero because I never work with 3D and AutoCAD.
All right. So the line command needs a point. It needs that type of data. Notice I don't have point as one of my data types, because a point is stored as a list in AutoLISP. What else does a line command need? Needs another point, another list. If we supply two points to the line command we get a line between those two points. What are some other things that line command needs, or another way of picking points? We don't need another way of drawing lines. We don't always just pick, pick, pick, pick.
Length and angle, numbers. We got two separate types of data types for numbers. We've got integers and we've got reals. The difference between the two is nothing more than a dot. Integer doesn't have a period or a decimal point. Reals do have a decimal point.
Think about the circle command. What's a circle command mean? Don't give me all that tangent, tangent, radius stuff and all that thing. Needs a point, right? Center point needs a radius, or it needs us to say diameter. In order to say diameter with AutoLISP, we have to supply a string. I'm not going to even look back there. We have to supply another type of data, and that would be a piece of string data. String is just another name for text. We have to supply the D that switches AutoCAD over to the diameter method or prompting us for a diameter, and then we're back to typically a real again.
We're done again. Not much more to it. So if we keep terms like expression, that's a specific type of list and list point list-- maybe I should put point list on there. Point coordinates, a point list and an expression are the two types of lists that we're going to focus on today. Expressions are lists, stuff inside of parentheses, that start off with a function and take different arguments in AutoCAD. Hang on. Hang on. Hang on.
Before we get started, we're going to start typing pretty quick at command prompt. I want you to set up AutoCAD. You don't have to do this, but do this. Turn off Dynamic Input where that-- if you don't have AutoCAD, get AutoCAD open and turn off Dynamic Input. Down along on the right hand side, you've got that set of tools or toggles, and this button right here we want to turn that off. That should be present. If it's not present you'll have to go to that little three bars and talk about what?
Here, just you take over this. Yeah. If you can't get this toggled off, let Lee or Scott or Chris know and they'll help you with that. And the other thing I would like you to do is expand your command prompt window. I love this. They're paying me to teach this class, and I'm going to do nothing but trash them.
Autodesk in their infinite wisdom made the command line a few years ago just a single line of text. And it'll show you for a couple of seconds what you just did, but that goes away. That's stupid. You need to rely on things that you did a step or two ago. Dock your command prompt, make it three or four lines long. For programming sake is we're going to test things out. I like to drag it and make it about eight or 10 lines long. It just helps us see stuff.
Everybody pretty much there? If you can't figure this out, give the keyboard to the other person. I didn't just say that. That's not nice. OK, I'm sorry.
All right. Basic syntax of expressions is just what I've already told you. Opening parentheses, first atom within the list is going to be a function name. We'll learn a number of different functions today. And then we've got different arguments. Whoops. Oh, I did do the right one.
This is the basic syntax of functions in AutoLISP. To clarify that a little bit, you're used to using commands in AutoCAD and supplying the correct information to the AutoCAD commands that you know and love. AutoLISP doesn't have commands. AutoLISP has functions, and it takes only certain pieces of data. And there's very strict rules on how to supply that data. And that's all we have to do is memorize those rules. And we're done again.
We typically provide input from the keyboard or the mouse in AutoCAD to whatever AutoCAD command we're using, whatever it might need. We will supply arguments to the different functions that we need by typing in specific data types to those functions. It's not quite that easy because we have to just remember that just like you can go on forever with the line command. It never ends till you hit the Enter key. Whereas the circle command, you pick a point, pick another point, you're done.
Just like commands need different amounts of information, AutoLISP functions need different amounts of information. Some functions don't require any arguments. Some functions take only one argument. Some functions take two arguments. Some functions will take as much crap as you can throw at it. And we just have to learn what those tools or what those functions do for us.
All right. We're at AutoCAD. We got AutoCAD open. We've got a bigger command prompt. We got direct, not direct distance, dynamic input. I knew I brought you along for something. Dynamic input is off. Type some of this stuff in. Oh, hang on. I need to see what page we're on. I write the handout because they make me. We are on page five. Page five, should be five. Yeah. No, page five explains these function. That's fine. That's fine. That's good.
Here are our first functions that we're going to use. we're going to use the plus function or the addition function. I don't care what you call it as long as you know what it does. In the minus or the subtraction function and the multiply function and the divide function-- and notice that I've got num, num, num, num, num. I'm not feeling good. So it's kind of making me sick on the food. Notice that we've got num, num. That's the type of argument it needs. And I didn't get as specific as I could have done that.
What two types of data could we plug in there? Oh, oh, oh. And? There you go. Was an incomplete answer until that point. First hard question of the day she gets price you can carry this thing around because I'm tired of-- that's a book I'm co-author on. What's that? No problem. That's a book called AutoCAD and Its Applications Comprehensive. I happen to be a co-author on it. And Goodheart-Willcox was nice enough to donate some copies to me, so I can give them out. The bad news, there's nothing about AutoLISP in there.
This is a great story. Terence Shumaker is one of the main authors of the book, and he asked me in 2002 at Autodesk University. He says I don't know how I'm to get some of the stuff written. There's so many new features in AutoCAD 2002. He says I need time. I got to write the CAD standard stuff. Well, I can write that. I had no idea what CAD standards were. But I wanted to become an author, so I wrote the CAD standards chapter. It's not in the book anymore. Then I wrote some modules chapters that's not in the book anymore DCL not in the book anymore. Lots of stuff that's just not-- it's available online on their website. So you look at some of that stuff. But all of this stuff is really available through Google. And the help screen. We'll talk about that.
Anyway, here's what I want you to do. Type some of that stuff in. We need to supply numbers. Notice that we don't have to add just two numbers together. We can add as many numbers together as we want. And you're not done with the function until you-- hang on. --until you close it off. You open it up, give it a function, one of those four functions that we're talking about, give it some numbers, close it off, hit the Enter key, and the magic just occurs.
Now, some things could go wrong. Question. Good question, if I heard you right. Really stuffed up my ears too, and I'm old and can't hear.
The addition function, of course, many numbers as you pass to it. Whether they're reals or integers, it'll add them up, give you the answer. Subtraction, little bit different. The first number is thought of as the base number and then we're just going to successively-- even with a cold I said that weird. That was good. --successively subtract any other arguments from that base number. Multiply. Just multiply as many numbers as we pass it together, and we get the answer. Divide, same thing as subtract. The first number is the base number. We'll divide that by whatever the second argument is. We'll divide the result of that by whatever the third argument is, then we close it off and hit the Enter key.
Now, to your point. We can supply integers and we can supply reals. If you supply nothing but integers, your result is going to be an integer. This is only semi problematic with the divide function. If I divide 10, excuse me, integer, no decimal place, by 3, an integer, no decimal place, I'm going to get 3. What should I do here? Not the real answer. And you're still here, this is good. Not the real answer.
If you make any of the arguments to any of these math functions, an integer-- no, dammit. --a real, you're going to get a real number returned to you. If we divide 10 by 3.0, we're going to get the real answer.
Significant things. Any number less than 1 and greater than negative 1 has to have a 0 in front of the decimal place. .25 isn't good. 0.25 is perfect. You'll get an error message saying invalid dotted pair or something like that. We are not getting into what are valid dotted pairs. An integer will be turned if all arguments are integers. We just went through that. And then the last one is, if we do divide 10 by 3.0, we're going to see 3.33333.
Autodesk or AutoLISP or whoever wrote all this junk, many of the textbooks about this stuff will call that significant digits. It's going to show you six significant digits. That's not the correct term for significant. It's just going to show you six places of accuracy. Has nothing to do with whatever your units might be set to. Realize that the computer is keeping track of it out there to 15 or 16 decimal places, whatever it can do now. I don't know. But it's really keeping track of it that far, but it's only going to show it at the command prompt rounded off, excuse me, to the sixth digit.
Make sense? Type, type, type. I want to hear keyboards click. Type in some of this stuff and feel free to go rogue on me and type in some different numbers than what are on the screen.
Yeah? Oh yeah, we can do other screw ups too. Hang on. You'll get this thing, right? Yep. Looks like a bird beak. That means you're missing a parenthesis. It's saying you got one open, you got to close it. We have to have as many opening parentheses as-- or excuse me, we have to have as many closing parentheses as we have opening parentheses.
Another error that might happen is you're going to forget to separate your function from the arguments and the arguments from each other. It's just typos. We just make that. We don't know this stuff yet. Five minutes we'd be good at it.
Plus sign space, 3 space, 4, closing parentheses and you'll get-- hang on. --you'll get 7. I got students that do that all the time. Just divide 100 by 10. Well, wait a second. And I go and say you know the computer has a calculator right on it. How we doing? Oh, good.
We're on slide 8 and 9. I got an hour left. You guys doing anything for lunch? We can just kind of work straight-- well, since the keyboards are really not clicking, I guess you're just going to trust me that this stuff works or do they got really quiet keyboards in here? Pretty quiet. Well, it's got a straightforward in your board. I get it.
Questions? We cool? We now know what a function is? We now know four functions. Almost held up the wrong number of fingers. That wouldn't have been good. Question? It depends upon what you're trying to do and how you're doing it. Are you trying to do nesting? Just hold on. No problem. I'm not very good at this, but you're all listening. That's cool. No problem, we'll get there. Exactly.
Oh, yeah? Pardon me? Oh, because there's functions that we'll talk about. Oh, I got to apologize to you guys too. But in Thursday's session we'll talk about functions for looping. Maybe we want to do a whole series of expressions a certain number of times. We don't want to do it 3.7 times. We want to do it 4. So there are specific times when we want to make sure that we got an integer most of the time. No, most of the time we need reals. We cool with this, folks?
All right, storing data. Now, it's really cool to know that we can do things. I use this all the time when I can't find my calculator and I'm working within AutoCAD. And I need to know the answer to something, I just type it in. The open parentheses plus sign will give it three, four numbers, and close parentheses. Oh, that's what it comes to, and we get to see it at the command prompt. It gets returned to you. That's the term us programmers, we use that term. Well, that expression returns this-- I don't know what that means. It just spits it out at the command prompt. And we can't do anything with it except just admire the fact that we typed in a function validly and we returned a result. We can't do anything with it though. It's just sitting there.
Take a screenshot? No. We need to store that stuff. There was a reason we went through that calculation. We need that number. So we will use a function called setq. What does setq stand for? I don't have a clue. I just know what it does. I don't need to know what it stands for. I do know, but we don't want to go into it.
All we need to know is that setq is the function that we use to store values in a variable. And to even be a little bit more specific-- I had a high school teacher that used to say Pacific every time, and I have to make sure I go specific so I don't make that mistake. We have to be sure that we store specific types of data in a variable.
And another term for variable is symbol. In our case up there, symbol A or variable A, I really don't care what you call it, A is going to be holding on to 1. If we were to type in the next line, B is going to be holding onto 2.5, and C is going to be holding on to the word or the string of AutoLISP. Oh my god. Every time we say list in parentheses, we got to go like this and your job is every time we talk about string C, strings are contained in quotation marks. And my goal is to have everybody doing the chicken dance by the time we're done.
All right. The setq function requires two arguments. If we remember back to the plus function and the minus function and those other couple of math functions, the word num was in italics. It just means they're optional. Here they're in normal-- I don't know what you call normal. They're not in italics. Means that they're required arguments. There's other rules for setq. We're not going to focus on any of them.
Symbol, value. It's that simple, and we're done again. Let that sink in. There's nothing to this. Very good. Oh, that's a prize. I'm sorry, hang on. She put her fingers up and did this, which is if that's not worthy of a book, I don't know what is.
All right. What's your question then? No. No. Because you typed that in and found out. She wouldn't give me the keyboard. I am a smart aleck like that. I can't tell you how many times I've told students-- and when you googled that, what did you find out? After you return it or when it's returned to you? Of course it does.
Yeah, it'll look just like this. Of course you'll have quotation. I should've googled that. Yeah. Yeah, of course, it does. I'm getting mixed up with the proper function and the [INAUDIBLE] function. All right, great. Make sense?
Now, the key is that we put value in there. But remember, value wasn't really one of our data types. It could be any type of data, including a list. I got those slides out of order.
Nesting expression. Well no, I guess I really don't. Let's go back. I'll talk about this stuff in just a second. But let's talk about this. We've got four different data types that we're concerned about. Integers, reals, strings, and lists. And the lists that we typically want to store are actually point lists, and we've got functions that specifically are used to get from the user that type of data so that we can plug it into our expressions.
We've got getint. I'm all out of books. But what type of data will be returned from that function? Integer. What do we got next? Get real? Get real. Never mind.
Get point. The get point function will return a point list. And get string will return a string, which will be in quote. Chris was guiding me there. Do the string thing. We cool?
I was teasing about googling, but not really. You can talk to somebody like me because we're in class and ask questions about this stuff, but a lot of help is out there. There's a lot of AutoLISP-devoted websites. Also, per Lee, whose job is to write a lot of this stuff, the help file inside of AutoCAD, if you hit the F1 key-- don't do this because we don't want to chew up time. If you hit the F1 key, this screen pops up.
If you then pick Developer Document-- oh, this is cool because now that you know what a function is, you're a developer; you're going to write your own programs; that's all a developer does-- and you pick Developer Documentation, then this screen pops up, and we got a lot of tools for developing programs. LISP is just one of them, and if you pick the reference guide, a reference screen, another screen pops up, which basically just says all the letters of the alphabet.
And you can just pick on G, and you'll see all the different functions in AutoLISP that start with G. Now here's the really cool thing. We're talking about some very basic stuff, but because you now have a very firm grip on an integer, in a real, in a string, and point lists, you're going to be able to read that help documentation and understand how it works, and it uses, does it not, the syntax of non-italicized and italicized for optional arguments and--
AUDIENCE: [INAUDIBLE]
CRAIG P. BLACK: You know the bad news? He's helping out on Thursday too. All right. So those are additional functions that are available to us that can get data. We actually are getting data with the plus function and the divide function. We're getting data. Data is being returned to us. Here is a different way. Instead of-- I don't want to try to create a point by math, I just want somebody to pick a point. Yes, sir.
AUDIENCE: There could be some confusion as to the getint function just talking about integers, but the argument you're providing is just a string. Talk to [INAUDIBLE].
CRAIG P. BLACK: Oh, very good. Very good. Very good. I didn't really explain those arguments. Do this for me. At your command prompt, type in open parenthesis getint. Notice that all of our arguments here are optional. We don't really have to provide any data, and then a close parenthesis. Just open parenthesis, getint, close parenthesis. And just sit there.
Because right now you have no idea why the computer is just sitting there. Imagine that this line of code, this phenomenal line of code of open parenthesis, getint, close parenthesis-- is in a program, and your program is running. The program is just going to sit there waiting for you to type in something that will be an integer. So type in an integer, not A-N space integer. That's a string, and they're in quotes.
Makes sense? It just returned it, right? Now, if you wanted to say, hey, how about I let the user know what the heck this program is sitting here pausing for, we're going to nest these expressions. Once we do get-- what can you do it that integer that you just typed in? Nothing more than admire it, sitting there at the command prompt.
You want to store that. There was a reason I needed an integer. I want to store that. So we're going to nest. Here's another possible application for integers. Maybe I'm going to be doing something with the array command. We're going to store that in a symbol called rows. But the string is the argument. All of these get functions can take a string argument that will just be used as a prompt on your screen, and now the user knows why--
AUDIENCE: Using stochastic?
CRAIG P. BLACK: Yeah. --why it's sitting there. It's still just sitting there, but the prompted sitting there too.
AUDIENCE: [INAUDIBLE]
CRAIG P. BLACK: No. Good question. In setq, does the case matter, uppercase or lowercase? No, it doesn't. AutoLISP is not case sensitive. There's only a couple of times when you have to use a lowercase letter. Don't use uppercase because it's easy. Get in in the morning, type in-- or hit the cap lock and just work all day. When we're in mechanical design-- because all of our notes, everything we do is in caps-- I do that.
But when I'm programming, nope, I always do lowercase. My way of doing it is all function names get typed in lowercase, and every time I use a symbol, I type it in an uppercase. That's just me being nice to myself so I like the way it looks. You can actually setup the Visual LISP Editor when we get into it to always do it that way. All you gotta do is hit format and everything gets uppercased and lowercased like it's supposed to. That makes sense?
If we look back here-- dammit-- we've got another possible argument for the getstring function. It's just a flag, and we didn't talk about flags at all. We have to talk about another term that we saw and used a little bit earlier. We have to talk about value.
Who's got programming experience in any other languages? We often do conditional testing, and we like to think about true and false. If this is true, do that. If this is not true, do that. And it's really not a true-false thing.
When we're doing conditional testing-- and I really don't know any other language, so I won't say that's the case in other programming languages, but in LISP, we're never checking for true or false. We're just checking to see whether something has a value or something doesn't have a value. If it doesn't have a value, that's nil. We'll go ahead and say that's false. No means false.
If it has value, that's true. It's an easy way to just teach yourself how to use conditional expressions. There is one symbol in LISP that always has value, and that's T. 6 has value. 3.14159 has value. I want to say that. You don't want to screw around with T.
So when you're setq-ing things, you don't want to use T. Setq A to 1, B to 3. We don't want to setq T to anything because there's a way that we could setq T to nil and that just screws AutoLISP completely up. So T is a symbol that we stay away from.
Pi always holds value, 3.14159-- I think that's six decimal places. Don't set setq pi, and I've done this like I'm writing a program for block insertions, like point of insertions. I just call that pi. That makes sense. And then I use the angle function and try to plug a pi in there, and it doesn't work anymore. So stay away from T stay away from pi.
AUDIENCE: [INAUDIBLE]
CRAIG P. BLACK: e, yeah. You're mechanical engineering, aren't you? And it's lowercase e, right? Yeah. I know it means. It's a constant, but I don't remember what it is, but I know it's usable. You can use e. And then I just said, like I don't have a clue. Maybe stay away from e also. I cannot tell you how many times I've taught class and go, no, you can't do that. Then I try it and I'm like, that works. We cool?
AUDIENCE: [INAUDIBLE]
CRAIG P. BLACK: What's that?
AUDIENCE: [INAUDIBLE]
CRAIG P. BLACK: Oh, if you wanted a to not be 1 anymore?
AUDIENCE: Yeah.
CRAIG P. BLACK: Setq A to nil. True. True story. Or just close AutoCAD and start AutoCAD back up. Start a new drawing. All this stuff only works in one drawing, the drawing that you're working in. A little deeper than what we want to go right now.
Lastly, if you have played with or haven't played with yet, do so. Oh, dang it. We can supply a point list argument to the getpoint function. Let's say I did getpoint in quotes-- pick a point-- setq that to PT 1. So I got open parenthesis setq PT 1, open parenthesis getpoint quotation mark, pick a point colon space-- we'll talk about that, quotes, close parenthesis, forget point, close parenthesis for setq. That's cool. I said that right.
Make sense? Now, pt 1 is holding onto a point value. You've picked a point somewhere. Now, if I setq p2 to getpoint and supply PT 1 as an argument, you're going to get the little drag line that AutoCAD always gives you with the line command. You're not going to draw a line, it just makes your program look pretty. It's still going to just return a point list, but it's another way of using that tool. Make sense?
Nesting expressions, we just talked about that. And I always do this one to help demonstrate-- oh, that T. I got off track, didn't I? T, I'm using T as a flag. T always has value. If the first argument that you passed to getstring has value, you have disabled the spacebar from being the Enter key.
Whenever I'm teaching AutoCAD to begin with, I was telling there's three enters on the keyboard-- the horizontal one, the vertical one, and that big, old spacebar. All of those work as an Enter key in most situations, including AutoLISP. If I use getstring without a flag, the spacebar is the same as enter. I could only type in Craig, and as soon as I hit space, that's what I got.
If I setq full name to getstring T and then a prompting string of some sort, now I can type in Craig and the spacebar is fair game. It will actually enter a space, then I can type in my last name, Black, and move on.
Common errors. When you get things like bad function, no function definition, it just means that you flopped some letters around. You screwed something up, and it'll usually tell you exactly what you screwed up. I just spelled setq wrong in this instance. Missing parenthesis. We're only writing single line stuff right now, but when we get into a program, sometimes we write a longer program and then run the program, and we forgot to close off two or three expressions. You're going to see two or three open parenthesis here. Now, you can get out of it by just typing in three or four closing parenthesis or however many you need to close it off. Probably ain't going to work right, but at least you'll be back to a blank command prompt.
By the way, hitting escape just gets rid of it too then you can start over. Not the way it was a long time ago. How many times did you reboot AutoCAD because you forgot a parenthesis? You get a misplaced dot on input. Typically means that you forgot to put a 0 in front of 0.25 or 0.125.
Sometimes let's say you go oh, yeah, yeah, yeah, yeah, I want to do that 0.1 thing to have that rubber band line drag out and you did setq PT 2 to PT 1, enter another point or getpoint PT 1, enter another point, close parenthesis, close parenthesis. And the LISP could crash or just say, nah. Because PT 1 is supposed to have a point in it, and if it just doesn't happen to have a point in it, it's going to network. It's looking for something. It's looking for a point. So you'll get an error bad argument type. Just telling you how to read the errors that you're going to do.
The good news is with this vast amount of knowledge that you now have, usually if you just look at your lines of code, you can kind of-- oh, yeah. I spelled setq wrong, or, oh, yeah, I'm using PT 1. And sometimes I use PN for points. I don't know why. And I wrote PT 3 five times-- PT 1, PT 2, PT 3, and then I plugged in PN 1. Well, PN 1 doesn't have a value, so my getpoint function is going to crash on me.
Using stored data. Just look at this quickly. If we setq X to 1.25, at the command prompt, if I type in exclamation point X, it's going to tell me what X is holding on to. If I wanted to use that value over and over again, I want to draw a circle with a radius of 1.25, I could do circle, pick a point, at the radius prompt, type in exclamation point X, and it'll be evaluated and returned to the circle command.
If I do it with offset, it'll work. Isn't that cool? Yeah, great. You'll never do it. All that really is, folks, is a troubleshooting tool. Why the heck did my program crash? Why didn't I get my rubber band line? Oh, I got PN 1 in there. If I type in exclamation point PN 1 and it returns nil, I now know why it crashed.
So you just use it to check and see if it's holding the value that you're expecting it to hold. Using stored data from AutoLISP. This right here, folks, this is why we're here. Hang on. Oh, my god. No, we got till noon, right? Cool, I can talk for a long time yet. I was scared for a second.
This is why we're here. We're learning how to drive the command function. Oh, now, we're kind of mixing things up a little bit. We all know how to use AutoCAD. That's why we're here at Autodesk University, or at least in this class. Because LISP, you were asking about LISP or other programs that it works with. But then AutoLISP really only works inside of AutoCAD or Civil 3D or the other verticals. It doesn't work inside of Inventor. You need other programming languages for that.
But we're learning how to do the AutoCAD stuff that we already know how to do over and over again, with fewer steps. That's all we're really trying to learn here. So we want to learn this syntax for this expression. This expression is using the command function, and the command function needs as its first argument-- oh, my god this is like-- needs a string, strings are in quotes, and that string has to be an AutoCAD command.
If we want to use the offset command inside of AutoLISP, we need open parenthesis command space quotation mark offset close quotation mark, and then give it some other stuff. If we want to use a circle command, open parentheses command space in quotation marks circle close quotation mark PT 1 X, close paren-- look, we're done again. It's all we're here to do.
AUDIENCE: Why wouldn't I put an exclamation mark in front of--
CRAIG P. BLACK: Because we're now-- good question-- now, that exclamation point variable name is an AutoCAD tool. As soon as we type in that open parenthesis, AutoCAD is going, cool, I'm on break. As soon as we type in the open parentheses, AutoLISP is working for us. Exclamation point variable is not needed inside of AutoLISP. AutoLISP just needs the variable all by itself.
So if we've got this line of code right here, setq RAD to getreal, enter circle radius. Sorry, but the font that I'm using doesn't do the flippy in the other direction, OK? Oh, well. You know what that means. Close parenthesis, which is closing getreal and then another closing parenthesis because it's closing setq.
Now, RAD is holding on to whatever number you typed in. By the way, if you typed in two, it's holding on to 2.0, because you're using-- I'm using getreal, right? Yeah. Because I'm using getreal, it'll automatically convert whatever you type into a real number. Don't type in Craig because it's not going to do that. You'll get an error message of invalid data type.
Then we're going to do a setq CPT to getpoint enter circle center point. Close. Close. Now, RAD and CPT are holding on to all the data that the circle command needs. And I'm going to-- we'd like to use the term call-- I'm going to call the circle command with the command function, and I'm going to supply CPT because the first argument-- not argument-- the first type of data that the circle command needs is a point, and we've got that's stored in CPT.
We can't just say, hey, I got what I need, and I'm just going to throw that out. We can't reverse RAD and CPT. They've got to be in the right order. It's logical. Everything that we know about AutoCAD, we don't forget. We have to know AutoCAD in order to want to do this stuff in the first place, and we just got to reply or respond to the prompts in the same order with the same type of data, and we've stored the correct data in those symbols, or variables.
We're done again. OK, question?
AUDIENCE: [INAUDIBLE] you have a backslash [INAUDIBLE].
CRAIG P. BLACK: Dammit. Good question. We'll be talking about that in a minute. Yeah, we will.
AUDIENCE: I'll pull it up.
CRAIG P. BLACK: I thought I'd cleaned that up. That's my bad. I've got it on here. You see how well I'm doing with the handouts I printed out, right? I'll just leave that there so I don't trip over it anymore. Make sense? We will talk about that.
Putting it all together. Type these four lines of code into AutoCAD. The backslash n is now here. I have to tell you about that. If I didn't have it there, that string argument to the getpoint function will be shown to you on the command prompt, and I'll pick a point and then the next line of code happens, and when I type in getpoint, whatever the next thing is, that next prompt is going to show up, and it'll show up right next to the previous string that was there.
Backslash lower case n, this is the one time where AutoLISP is case sensitive. It's got to be a lower case n, and that just forces a new line. Some programmers say, oh, you don't need that on the first prompt, but if I don't put it in every single time, I'll never remember on the other ones. So I always start-- I cannot type in when I know I'm typing in an opening quotation mark, I can't not type in backslash lowercase n right away. Fingers do that automatically.
The other thing that you want to do is do this. This is kind of like subversive, is that the word? Whenever I write programs that my users are going to use, I don't want them to know that I wrote it. I want them to think it's AutoCAD. You know why? Because if it doesn't work-- or if they know I wrote it, they're like, oh, I think it should have this option or that option. Craig, can you add this to your code? No. I have other things to do.
So AutoCAD never asks you a question-- a couple of times it does, but for the most part, no. The best one, do you want fast zooms? Just bothers the hell out of me. Anyway, AutoCAD always prompts you with statements.
Sometimes you're going to answer or reply to that prompt with a pick from the mouse. Other times you're going to reply to that prompt by typing values in. Whatever you type in would be smack dab right next to that colon if you didn't put the space in there.
So just like I start every prompting string out with open quotation marks backslash n, capitalized letter all lowercase after that-- because that's the way AutoCAD works-- colon space closing parenthesis. That's the way I was do it. No, closing quote. Always do it that way.
So some of you are listening to me, others were typing, and that's good. And ultimately, what we've got is four points stored in four symbols, and we're passing those four symbols. Kind of looks muddled up here. I should have put a little bit more space in there. You just need a space, but for big screen sake, I probably should have put some extra space in there.
Did it work? What does a C do? Close option for the line command, just draws it back to the first point. We could have-- well, let's talk about this because we got time, so I can still talk. We could have, instead of putting that quotation C close quotation, we could have just typed in PT 1 again, right?
Don't make me laugh. You're going to kill me. But how do you end the line command? I still think this is so stupid. The line command, like the first thing when somebody said, let's make a CAD program. We should probably have a line command. How do you end the line command?
AUDIENCE: [INAUDIBLE]
CRAIG P. BLACK: Just hit the Enter key, right? How do you know that? Because the first guy that taught you how to use AutoCAD or the gal that taught you how to use AutoCAD told you. Is it really that hard to put the word exit in there to let me know or enter? Just make that a default. Put it in those angle braces, put the word exit in there. Oh, if I want to get out of here, I could end.
Here's how you mimic the Enter key as a string-- Opening quotation mark, closing quotation mark. Open closed nothing in between just-- and it's entirely up to you whether you do the sound effects. So I could have did PT 1, PT 2, PT 3, PT 4, PT 1, and it would work too.
AUDIENCE: We're replacing the quotations too. [INAUDIBLE].
CRAIG P. BLACK: Yep. It depends upon the purpose. Remember, just because you picked four points doesn't mean you connected all four points together. You went from PT 1 to PT 2 to PT 3 to PT 4. If I just hit enter now, great, I got a big, old C because remember, we don't have any structure to this box yet. That's Thursday. We cool?
All right. Now, go to the Manage tab up along the top, and there's an Applications panel and Visual LISP Editor, not Visual Basic Editor, Visual LISP Editor. Pick that, and a window will open up. Cause you know why? Like I always tell my students, because I told you to. But you know why? Because you guys are now developers, and every time I want to draw a box, which I could just do with the line command, but never mind that.
I don't want to type in those five lines of code in order to do it. I want to store those five lines of code just like I stored a point using setq or I stored a string using setq with getstring, just like I can store individual pieces of data. A few years ago, I thought about it, and I'm like, you know what, this defun function is just like a major setq.
What I'm really doing is I'm setq-ing this symbol. We'll talk about the significance of that in just a minute. I'm setq-ing that symbol to this line, this line, this line, this line, that line, and then I'm closing off defun.
All my parentheses are matched. I've got two openers, two closers for all these lines, one opener, one closer. Defun is open. Let's just do this with-- the first argument to defun is the symbol that we want to store our lines of code under. If it starts with a C colon, that means I'm making my own command. I can type whatever is after C colon in and make those five lines of code happen automatically.
The second argument after the symbol has to be a list, and that's all I'm going to say because I'm trying to keep it simple because this is where-- if you want to work through lunch, we can do it. There's a reason that we leave it empty right now. We're just learning. But there's stuff that we can jam into that list later on when we get a little more familiar with LISP. Makes sense?
So in the editor, for those of you that actually were listening to me while I was talking about-- type that in. If you were typing it in, you're fine. That's OK. This ought to be fun. I'm going to bend over to pick this up. Chris, come and get this for me. I can't bend over. I am so arthritic. Thank you, sir.
AUDIENCE: So I'm sure copy and paste qualifies a part--
CRAIG P. BLACK: Sure.
AUDIENCE: That doesn't seem to work. [INAUDIBLE]
CRAIG P. BLACK: It should work.
AUDIENCE: It worked for me. You've got to start [INAUDIBLE].
CRAIG P. BLACK: Oh, yeah, cutting and pasting is a beautiful thing. If you notice, these three lines are exactly like the first line except for 2, 3, 4 and first is next, next, next. If you were to-- here's a little secret-- type in setq P2-- all that right up to there, hit the Enter key. Now, use your arrows. Arrow up because you now just hit Enter, and so now your cursor is flashing right here. Use your up arrow, and you'll be right here.
Hold the Shift key down and hit the down arrow, and you will have highlighted that whole line and the Enter that got you to the next line, and do a Control-C, and that copies everything that's highlighted to the clipboard. And then you Control-V, and that just pastes it over the top of itself, but you do that three more times and you got four lines of code that look identical, and just go change the 1's to 2, 3, 4, and change first to next and you've got it all handled. Not a typing class.
And don't do it with the mouse. It sucks. You're a typer, you're a programmer, you're a developer now. Type that stuff in. You should be using your keyboard.
AUDIENCE: I was using it earlier on the command line--
CRAIG P. BLACK: Oh, I forgot. You need to pick New.
AUDIENCE: New.
CRAIG P. BLACK: My bad. Type that all in there. Now here's the cool part, go down here--
AUDIENCE: There we go.
CRAIG P. BLACK: Go back there. Grab all that, do a Control-C, pick up there, Control-V, and now get rid of all these little opener beat things.
AUDIENCE: Thank you.
CRAIG P. BLACK: Yeah, my bad. I skipped the Start a New File direction.
AUDIENCE: [INAUDIBLE]
CRAIG P. BLACK: If they're like me, they're really not using the handout. I only did this handout because they make me, just why I use the PowerPoint. Once you've got that all typed in-- damn it. Once you've got that-- what did I do? Once you've got that all typed in, you want to test it.
Instead of typing each line in separately, what you'll do is you'll go over to this button right here, inside the Visual LISP Editor, and that loads all of the code that you've got typed into that editor window that just loads it into AutoCAD. And you should see a little line in your console down below saying one form loaded.
Then pick this little button right here because that pretty little picture used to mean AutoCAD. No, it's way outdated, isn't it? Pick this, or if you've got a chunk of the AutoCAD window in the background, just pick that. Doesn't matter. Get back to AutoCAD. And at the command prompt, type in box 1, and your prompting should come up. Pick, pick pick, pick, pick, and you should end up with a reasonable facsimile of a box.
AUDIENCE: [INAUDIBLE]
CRAIG P. BLACK: What's that?
AUDIENCE: [INAUDIBLE]
CRAIG P. BLACK: Oh, yeah. That's kind of cool too. Very good. Once you've done this load, load the editor into AutoCAD, and then go on back to AutoCAD. Now, AutoCAD knows, hey, I got another command under my belt. And you type in B-O and you'll start getting some of the things that start with a B and an O. Box 1 is probably on the list. You can just toggle down to it, or you can just go crazy and type in an X and a 1 also. Yeah.
AUDIENCE: If you happen to be in the Visual LISP Console window at the bottom, those buttons aren't active. So if you see that they're not active, you can't click on one of the ones just because you're current window is still visible in the console and doesn't apply.
CRAIG P. BLACK: In this time, I told you I hate reading PowerPoint to you, and in this instance, we could've saved a little headache having if I would have read that to you. You need to open the Visual LISP Editor, start a new file, and then type all this junk in. Some of you typed it into that command prompt. What's that?
AUDIENCE: Now, do you pick that after you typed all that?
CRAIG P. BLACK: Then after you typed all that, then pick this button.
AUDIENCE: Oh, that button.
CRAIG P. BLACK: And then pick that one, and you'll be back in AutoCAD.
AUDIENCE: There we go. That's what we're missing. Because we kept picking Box 1, and then they kept picking the box.
AUDIENCE: Kept switching it to box. Here we go.
CRAIG P. BLACK: Look what you did. High five. If I had another buck, I'd give it to, and you'd just give it away anyway. Now, I have to apologize for some emails that you got from me. If you didn't get two and you only got one, that's a good thing because I'm only going to apologize once. I noticed in the Autodesk University Speaker area that I got to go into check on my classes that I had 82 people registered for this course. I know that's full. But for the one that I'm doing on Thursday, I only had 42.
And I'm like, these people don't even know that they don't like me yet, so why didn't they sign up the second one? I'm like why? Because every single class I've ever taught since they moved to 80 people has always been 80 people.
Well, when I was taking up my laptop in the two rooms that I'm teaching in, when I went into the other room, I'm like, oh, this isn't working, and it never just goes smooth. So as I'm whining about the PowerPoint not working the way I want it to, I'm kind of like, what the heck? And I look around the room and I'm like, this is a lot smaller room. That's why there's only 42 people signed up. They just stuck me in a smaller room. So I apologize. That class is full, but it's Thursday. Is the party Wednesday night?
AUDIENCE: Thursday night.
CRAIG P. BLACK: Thursday it is? OK, never mind. You don't have any excuse. They used to do the party the night before the last day, and they gave me a 7:30 class on CAD standards right after the party. Half the class didn't show up, and the ones that did weren't the least bit interested in CAD standards. So I'm happy. And I thought I was at the party way too long.
If this interests you, we're going to actually turn it into a box command and we're going to just keep moving on Thursday. If this interests you, I suggest coming to class and seeing if there's open seats and sneak in the back. And a lot of times we let people sit and just observe along the back wall. So feel free.
All right, that is the basics. We know what a list is. We know what a function is. We haven't used it too much, but an expression, all those lines of code that setq PT 1, getpoint, setq PT 2, getpoint, those are successive expressions.
And all those expressions are stored inside of a function that we defined. Auto LISP already knows how to setq, it already knows how to add, and it already knows how to getpoint. We taught it how to Box 1. We defined our own function.
That-- because we put a C colon in front of it-- can be used as a command. We understand what an integer is. We understand what a real is. We know what a point list is. We know essentially, there's only two kind of list that we ever have to deal with, a point list or an expression. And an expression is a list that starts out with a function, has a number of different arguments that have to be of specific data types. And those data types have to be an integer, a real, a string, or another symbol, or another list.
Those are the functions that we learned how to use today. We didn't use some of them, but we talked about them. I think we really are done. Questions? Concerns? Comments?
AUDIENCE: Can everybody raise your right hand.
CRAIG P. BLACK: I am a devout--
AUDIENCE: Turn around. Give yourself a pat on the back.
CRAIG P. BLACK: I appreciate your attention over the last 84 minutes. I'm not done thanking you. Sit down. I'll be in the hall. I don't really think a class is in here right away, so we can probably hang around here for-- if you guys got any questions, I will hang until you're not asking me any more questions. Thank you. Thank you. Thank you.