Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
George Omura. Lisp programing tutorial for AutoCAD customization / 620.0.The ABC's of AutoLISP - Omura, George.pdf
Скачиваний:
140
Добавлен:
02.05.2014
Размер:
1.55 Mб
Скачать

The ABC’s of AutoLISP by George Omura

Once the or function returns a T, the second argument is evaluated which means that a 3dbox is drawn. If or returns nil, then the third argument is evaluated drawing a 2 dimensional box. The and function works in a similar way to or but and requires that all its arguments return non-nil before it returns T. Both or and will accept more that two arguments.

How to Make Several Expressions Act like One

There will be times when you will want several expressions to be evaluated depending on the results of a predicate. As an example, lets assume that you have decided to make the 2 dimensional and 3 dimensional box programs part of the C:MAINBOX program to save memory. You could place the code for these functions directly in the C:MAINBOX program and have a file that looks like figure 5.5. When this program is run, it acts exactly the same way as in the previous exercise. But in figure 5.4, the functions BOX1 and 3DBOX are incorporated into the if expression as arguments.

(defun

C:MAINBOX (/ pt1

pt2 pt3 pt4 h choose)

(setq choose (getstring

"\nDo you want a 3D box <Y=yes/Return=no>? "))

(if

(or (equal choose "y")(equal choose "Y"))

(progn

;if choose = Y or y then draw a 3D

box

 

 

(getinfo)

(setq h (getreal "Enter height of box: ")) (procinfo)

(output)

(command "change" "Last" "" "Properties" "thickness" h "" "3dface" pt1 pt2 pt3 pt4 ""

"3dface" ".xy" pt1 h ".xy" pt2 h ".xy" pt3 h ".xy" pt4 h ""

);end command );end progn

(progn ;if choose /= Y or y then draw a 2D box (getinfo)

(procinfo)

(output) );end progn

);end if );end MAINBOX

Figure 5.4: The C;Mainbox program incorporating the code from Box1 and 3dbox

98

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Figure 5.5: Using the progn function

We are able to do this using the progn function. Progn allows you to have several expression where only one is expected. Its syntax is as follows:

(progn

(expression1) (expression2) (expression3) . . .

)

99

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Figure 5.5 shows how we arrived at the program in figure 5.4. The calls to functions BOX1 and 3DBOX were replaced by the actual expressions used in those functions.

How to Test Multiple Conditions

There is also another function that acts very much like the if function called Cond. Arguments to cond consists of one or more expressions that have as their first element a test expression followed by an object that is to be evaluated if the test returns T. Cond evaluates each test until it comes to one that returns T. It then evaluates the expression or atom associated with that test. If other test expressions follow, they are ignored.

Using the Cond function

The syntax for cond is:

(cond

((test expression)[expression/atom][expression/atom]...) ((test expression)[expression/atom][expression/atom]...) ((test expression)[expression/atom][expression/atom]...) ((test expression)[expression/atom][expression/atom]...)

)

Figure 5.6 shows the cond function used in place of the if function. Cond's syntax also allows for more than one expression for each test expression. This means that you don't have to use the Progn function with cond if you want several expressions evaluated as a result of a test.

(defun C:MAINBOX (/ choose)

(setq choose (getstring "\nDo you want a 3D box <Y=yes/Return=no>? ")) (cond

( (or (equal choose "y") (equal choose "Y")) (3dbox)) ( (or (/= choose "y") (/= choose "Y")) (box1))

)

)

Figure 5.6: Using cond in place of if

100

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Figure 5.7 shows another program called Chaos that uses the Cond function. Chaos is an AutoLISP version of a mathematical game used to demonstrate the creation of fractals through an iterated function. The game works by following the steps shown in Figure 5.8.

;function to find the midpoint between two points (defun mid (a b)

(list (/ (+ (car a)(car b)) 2) (/ (+ (cadr a)(cadr b)) 2)

)

)

;function to generate random number (defun rand (pt / rns rleng lastrn)

(setq rns (rtos (* (car pt)(cadr pt)(getvar "tdusrtimer")))) (setq rnleng (strlen rns))

(setq lastrn (substr rns rnleng 1)) (setq rn (* 0.6 (atof lastrn))) (fix rn)

)

;The Chaos game

(defun C:CHAOS (/ pta ptb ptc rn count lastpt randn key) (setq pta '( 2.0000 1 )) ;define point a

(setq ptb '( 7.1962 10)) ;define point b (setq ptc '(12.3923 1 )) ;define point c

(setq lastpt (getpoint "Pick a start point:")) ;pick a point to start (while (/= key 3) ;while pick button not pushed

(setq randn (rand lastpt)) ;get random number (cond ;find midpoint to a b or c

( (= randn 0)(setq lastpt (mid lastpt pta)) ) ;use corner a if 0 ( (= randn 1)(setq lastpt (mid lastpt pta)) ) ;use corner a if 1 ( (= randn 2)(setq lastpt (mid lastpt ptb)) ) ;use corner b if 2 ( (= randn 3)(setq lastpt (mid lastpt ptb)) ) ;use corner b if 3 ( (= randn 4)(setq lastpt (mid lastpt ptc)) ) ;use corner c if 4 ( (= randn 5)(setq lastpt (mid lastpt ptc)) ) ;use corner c if 5

);end cond

(grdraw lastpt lastpt 5) ;draw midpoint (setq key (car (grread T))) ;test for pick

);end while );end Chaos

Figure 5.7: The Chaos game program

101

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Cond can be used anywhere you would use if. For example:

(if (not C:BOX) (load "box") (princ "Box is already loaded. "))

can be written:

(cond

((not C:BOX) (load "box"))

((not (not C:BOX)) (princ "Box is already loaded. "))

)

Figure 5.8: How to play the Chaos game

102

Copyright © 2001 George Omura,,World rights reserved