AutoLisp Chapter - 6

AutoLisp Chapter - 6
Site menu


Login form


Search


Site friends
  • Create your own site


  • Statistics

    Total online: 3
    Guests: 3
    Users: 0


    Welcome, Guest ยท RSS 03 December 2024
    AutoLisp Tutorial for Intermediate
     

    Chapter - 6
     
    Topics Coverd In this Chapter
    • 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

    The if functions works by first testing to see if a condition is met then it performs one option or another depending on the outcome of the test. This sequence of operations is often referred to as an if-then-elseconditional statement. ifa condition is met, thenperform computation A, elseperform computation B. As with all else in AutoLISP, the if function is used as the first element of an expression. It is followed by an expression that provides the test. A second and optional third argument follows the test expression. The second argument is an expressions that is to be evaluated if the test condition is true. If the test returns false or nil, then ifevaluates the third argument if it exists, otherwise if returns nil. The following shows
     
    the syntax of the if function. 
     
    (if testexpr thenexpr [elseexpr])
     
     Arguments

    testexpr

      Expression to be tested.

    thenexpr

      Expression evaluated if testexpr is not nil.

    elseexpr

      Expression evaluated if testexpr is nil.

     
     
     The test expression can be use any function but often you will use two classes of functions called predicates and logical operators. Predicates and logical operators are functions that return either true or false. Since these functions don't return a value the way most functions do, the atom T is used by predicates and logical operators to represents a non-nil or true value. There are several functions that return either a T or nil when evaluated.

    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

     

    Table 5.1 A list of AutoLISP predicates and logical operators.

     

    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

    In chapter 2, you saw briefly how the if function worked with the not logical operator to determine whether to load a program or not. Looking at that expression again (see Figure 5.1), you see a typical use of 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)
    By removing the C: from the function name, Box1 now becomes a function that can be called from another function.

    3. Change the first line of the 3dbox program so it reads as follows:

    (defun 3DBOX (/ pt1 pt2 pt3 pt4)

    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:

    Do you want a 3D box <Y=yes/Return=no>?

    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