Join Josh Modglin for an in-depth discussion in this video Using an if condition function, part of Learning AutoLISP & Visual LISP.
- [Lecturer] Most software languages provide you the ability to accomplish certain tasks only if a specific condition is met and LISP is no different. There's a few LISP functions we're going to take a look at now that allow us to only do certain actions when conditions meet or match what we've specified. The most common LISP function that is conditional is the if function. Let's go ahead and create a new named function and we'll call it IFCON and no specific local variables at this point and we're just going to use the if LISP function.
Now the if LISP function needs to have the condition specified as an argument. What is the condition that needs to be met that will specify or tell us that this condition is met or is not met? So the condition is going to be if the results of the value returned from GETVAR on the SELECTIONANNODISPLAY equals one, then the Annotative selection display is on.
Now, what about if value of the variable SELECTIONANNODISPLAY does not equal one? It simply going to go to the next list argument provided to the if condition, and so we're going to create another line of text that will be written out if the value is zero, which tells us SELECTIONANNODISPLAY is off.
I have to provide a closing parenthesis for the function if and then of course we have a closing parenthesis for our entire defined function, and remember the good practice of defining a empty prints at the end. This again keeps us very clean and no value is going to be returned, it's like saying nothing at the end, all we're doing is going to be writing out to the command line the specific text on or off, depending on whether or not this condition's met.
If it is met, then it's on, if it's false, then it writes this. Let's go ahead and load our command or our function into the AutoCAD drawing. We'll go to our AutoCAD drawing, we'll call IFCON and notice that it wrote, "Annotative selection display is off." Let's go ahead and turn it on, SELECTIONANNODISPLAY, set it to one and let's call our LISP function again.
And notice now it writes out, "Annotative selection display is on." So it provides us the ability to have a lot of different controls with the if. Now let's go back to our code just briefly, I want to show you one key factor here, do you notice that my strings that I'm writing out to the command line start with a backslash and a lowercase n? What I'm doing is I'm creating a new line before I write this, that way the text is clearly visible and is not blended or mixed in any way with a line of text already there, such as the IFCON call or any other texts.
We created a new line in the command window to write our text. Let's take this a bit further. Let's say we want to do more than just one action if a condition is met. Let's create a new if. If a specific variable is greater than the value we provide then we're going to do something.
Now I just wrote a lot of text, let's kind of break down what I just wrote. Here is our if condition, but inside our if condition we have two additional LISP functions, GETVAR, we know what that is, it returns the value of a variable, the variable that we're looking at is ACADVER, which really, simply stores the version of the AutoCAD that we're running and notice the version starts with a number, but it is in quotation marks, so what it returns back to us is a string when we get this variable.
So we need to turn this string into the number, we're going to use the LISP function, atof, it takes a string and turns it into a real number, if it can. Can it do it with this? Let's inspect, the results are 21.0. So it takes that first portion of the string, found a number, converted it over, and now it's a real number. Now we can run and check whether or not this number here is greater than this number, this is the version where SELECTIONANNODISPLAY first shows up, so as long as we're greater than this version, we know that we will have some type of result when we call this variable.
What was the whole point of this? We were showing that we can run multiple activities whether a situation is found, such as condition is marked true, let's go ahead to the next one, we're going to create a nested condition, we're going to get some formatting going on, that's the wonderful thing of our Visual LISP IDE, we could also reformat using some of these extra commands as well. So what do we have? We have a lot of parentheses, but the result if this condition is met, this will run in the true scenario, nothing is occurring if it's false.
In the true scenario, we have a nested condition to then write out whether or not the Annotative selection display is on or off. If we ran this, the results would be the same at our command line, but if we were running AutoCAD 2004, which this code would still work in, the result would simply be nothing. And so we see the power of the if or the conditionals, we see that we also, in any time, have more than just one line of text, or one argument that we're really accomplishing in a true or false scenario, we enclose in the LISP function, PROGN, and then we also see that we can nest.
Power of conditionals will continue to be used throughout our course, but let's take a look at one more means to check a condition.
- Reviewing LISP formatting
- Creating a defined function
- Getting and setting variables
- Using an if condition function
- Using a cond condition function
- Working with strings and lists
- Getting data from users
- Working with loops, selection sets, and geometry
- Creating and editing graphic entities
- Reviewing Visual LISP director functions
- Reading and writing to the registry
- Loading LISP files
- Error trapping