-
Making Decision in Program
Making Decisions
You can use AutoLISP to create macros which are like a predetermined sequence of commands and responses. A macro building facility alone would be quite useful but still limited. Unlike macros, AutoLISP offers the ability to perform optional activities depending on some condition. AutoLISP offers two conditionalfunctions that allow you to build-in some decision making capabilities into your programs. These are the ifand condfunctions. If and cond work in very similar ways with some important differences.
How to Test for Conditions
testexpr
Expression to be tested.
thenexpr
Expression evaluated if testexpr is not nil.
elseexpr
Expression evaluated if testexpr is nil.
FUNCTION |
RETURNS T (TRUE) IF... |
Predicates |
|
< |
a numeric value is less than another |
> |
a numeric value is greater than another |
<= |
a numeric value is less than or equal to another |
>= |
a numeric value is greater than or equal to another |
= |
two numeric or string values are equal |
/= |
two numeric or string values are not equal |
eq |
two values are one in the same |
equal |
two expressions evaluate to the same value |
atom |
an object is an atom (as opposed to a list) |
boundp |
a symbol has a value bound to it |
listp |
an object is a list |
minusp |
a numeric value is negative |
numberp |
an object is a number, real or integer |
zerop |
an object evaluates to zero |
|
|
Logical Operators |
|
and |
all of several expressions or atoms return non-nil |
not |
a symbol is nil |
null |
a list is nil |
or |
one of several expressions or atoms return non-nil |
You may notice that several of the predicates end with a p. The p denotes the term predicate. Also note that we use the term object in the table. When we say object, we mean any lists or atoms which include symbols and numbers. Numeric values can be numbers or symbols that are bound to numbers.
All predicates and logical operators follow the standard format for AutoLISP expressions. They are the first element in an expression followed by the arguments as in the following example:
( > 2 4 )
The greater than predicate compares two numbers to see if the one on the left is greater than the one on the right. The value of this expression is nil since two is not greater than four.
The predicates >,<,>=, <= all allows more than two arguments as in the following:
( > 2 1 5 8 )
When more than two arguments are used, > will return T only if each value is greater than the one to is right. The above expression returns nil since 1 is not greater than 5.
The functions and, not, nulland orare similar to predicates in that they too return T or nil. But these functions, called logical operators, are most often used to test predicates (see table 5.1). For example, you could test to see if a value is greater than another:
(setq val1 1)(zerop val1)
nil
We set the variable val1 to 1 then test to see if val1 is equal to zero. The zerop predicate returns nil. But suppose you want to get a true response whenever val1 does not equal zero. You could use the not logical operator to "reverse" the result of zerop:
(setq val1 1)(not (zerop val1))
T
Since not returns true when its argument returns nil, the end result of the test is T. Not and null can also be used as predicates to test atoms and lists.
Using the If function
This function tests to see if the function C:BOX exists. If C:BOX doesn't exist, it is loaded. This simple program decides to load a program based on whether the program has already been loaded. The test function in this case is not. Lets look at how you might apply ifto another situation. In the following exercise, you will add an expression to decide between drawing a 2 dimensional or 3 dimensional box.
1. Open the Box1.lsp file.
2. Change the first line of the Box program so it reads as follows:
(defun BOX1 (/ pt1 pt2 pt3 pt4)
3. Change the first line of the 3dbox program so it reads as follows:
4. Add the program listed in boldface print in figure 5.2 to the end of the Box1.lsp file. Your Box1.lsp file should look like figure 5.2. You may want to print out Box1.lsp and check it against the figure.
5. Save and Exit Box1.lsp.
6. Open an AutoCAD file called chapt5. Be sure to use the = suffix with the file name.
7. Load the box1.lsp file.
8. Run the Mainbox program by entering mainboxat the command prompt. You will see the following prompt:
9. Enter y. The 3dbox function executes.
(defun getinfo ()
(setq pt1 (getpoint "Pick first corner: "))
(princ "Pick opposite corner: ")
(setq pt3 (rxy))
)
(defun procinfo ()
(setq pt2 (list (car pt3) (cadr pt1)))
(setq pt4 (list (car pt1) (cadr pt3)))
)
(defun output ()
(command "line" pt1 pt2 pt3 pt4 "c" )
)
(defun BOX1 (/ pt1 pt2 pt3 pt4)
(getinfo)
(procinfo)
(output)
)
(defun 3DBOX (/ pt1 pt2 pt3 pt4 h)
(getinfo)
(setq h (getreal "Enter height of box: "))
(procinfo)
(output)
(command "change" "L" "" "P" "th" h "" "3dface" pt1 pt2 pt3 pt4 ""
"3dface" ".xy" pt1 h ".xy" pt2 h ".xy" pt3 h ".xy" pt4 h "")
)
(defun C:3DWEDGE (/ pt1 pt2 pt3 pt4 h)
(getinfo)
(setq h (getreal "Enter height of wedge: "))
(procinfo)
(output)
(command "3dface" pt1 pt4 ".xy" pt4 h ".xy" pt1 h pt2 pt3 ""
"3dface" pt1 pt2 ".xy" pt1 h pt1 "" "copy" "L" "" pt1 pt4)
)
(defun C:MAINBOX ()
(setq choose (getstring "\nDo you want a 3D box <Y=yes/Return=no>? "))
(if (or (equal choose "y")(equal choose "Y"))(3dbox)(box1))
)
Next -> How to Make Several Expressions Act like One