Test
Functions
AutoLISP has got two text
functions. They are:
- IF function
- COND function
IF
Function
(if
<expression> <then> <else>)
The expression is
evaluated. If it is true, then the THEN part is performed. If not,
then the ELSE part is performed.
Examples:
Function: Gives:
(if (= 1 3) "Yes"
"No") "No"
(if (= 2 (+ 1 1)
"Yes") "Yes"
(if (= 2 (+ 3 4)
"Yes") nil
The THEN part or the ELSE
part of the IF function can only contain one expression. If more
there are more expressions, then PROGN is used.
Example:
(if (= a b)
(progn
(setq a (+ a 10))
(setq b (+ b 10))
)
(progn
(setq a (- a 10))
(setq b (- b 10))
)
)
Here is another example of
how the IF function can be used. Now we have an AutoLISP program. A
line is drawn and a circle and a text.
(defun c:ball1 (/ p1 p2 an
tx aw ht)
(setq p1 (getpoint
"\nFirst point:")
p2 (getpoint p1
"\nSecond point:")
an (angle p1 p2)
)
(initget 1 "Yes"
No")
(setq aw (getkword "Text
(Yes/No)? "))
(if (= aw "Yes")
(setq ht (getdist
"\nHeight: ")
tx (getstring "\nText:
")
)
(setq ht (getdist p1
"\nRadius: "))
)
(command "line"
(polar p1 an ht) p2 ""
"circle" p1
ht
)
(if (= aw "Yes")
(command "text"
"m" p1 ht 0 tx)
)
)
COND
Function
(cond
(<text1> <result1> …)
This function is used to
perform an action depending on the value of a variable. Here is an
AutoLISP program that shows it.
If is the program from
before. But now you can draw a circle, a square, or a triangle. You
choose in the beginning.
(defun c:ball2 (/ p1 p2 an
ht tx aw)
(setq p1 (getpoint
"\nFirst point: ")
p2 (getpoint p1
"\nSecond point: ")
an (angle p1 p2)
ht (getdist
"\nHeight text: ")
tx (getstring
"\nText: ")
)
(initget "Cir Tri
Quadr")
(setq aw (getkword
"\nTriangle/Quadrant/
<Circle>:
")
)
(command "line"
(polar p1 an ht) p2 "")
(cond
((= aw "Tri")
(command
"polygon" 3
p1
"c"
(polar
p1 an ht)
)
)
((= aw "Quadr")
(command
"polygon" 4
p1
"c"
ht
)
)
(T
(command "circle"
p1 ht)
)
)
(command "text"
"m" p1 ht 0 txt)
)
No comments:
Post a Comment