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

The ABC’s of AutoLISP by George Omura

The while expression does not always include prompts for user input. Figure 5.12 shows a simple program that inserts a sequence of numbers in increasing value. Each number is increased by one and they are spaces 0.5 units apart. The user is prompted for a starting point and a first and last number. Once the user inputs this information, the program calculates the number to be inserted, inserts the number using the text command, calculates the next number and so on until the last number is reached.

;Program to draw sequential numbers -- Seq.lsp

 

(defun C:SEQ (/ pt1 currnt last)

 

(setq pt1 (getpoint "\nPick start point: "))

 

(setq currnt (getint "\nEnter first number: "))

 

(setq last (getint "\nEnter last number: "))

 

(command "text" pt1 "" "" currnt)

;write first number

(while (< currnt last)

;while not last number

(setq currnt (1+ currnt))

;get next number

(command "text" "@.5<0" "" "" currnt)

;write value of currnt

);end while

 

);end seq

 

Figure 5.12: Sequential number program

This program expects the current text style to have a height of 0.

Using the Repeat Function

Another function that performs recursions is the repeat function. Repeat works in a similar way to While but instead of using a predicate to determine whether to evaluate its arguments, repeat uses an integer value to determine the number of times to perform an evaluation. The syntax for repeat is:

(repeat [n]

(expression 1)(expression 2) (expression 3) ...

)

The n above is an integer or a symbols representing an integer.

108

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

The program in Figure 5.13 shows the sequential number program using repeat instead of while. When run, this program appears to the user to act in the same way as the program that uses while.

;Program to write sequential numbers using Repeat

 

(defun C:SEQ (/ pt1 currnt last)

 

(setq pt1 (getpoint "\nPick start point: "))

 

(setq currnt (getint "\nEnter first number: "))

 

(setq last (getint "\nEnter last number: "))

 

(command "text" pt1 "" "" currnt)

;write first number

(repeat (- last currnt)

;repeat last - currnt times

(setq currnt (1+ currnt))

;add 1 to currnt

(command "text" "@.5<0" "" "" currnt)

;write value of currnt

);end repeat

 

);end seq

 

 

 

Figure 5.13: Sequential number program using repeat

Using Test Expressions

So far, we have shown you functions that perform evaluations based on the result of some test. In all the examples, we use predicates and logical operators for testing values. While predicates and logical operators are most commonly used for tests, you are not strictly limited to these functions. Any expression that can evaluate to nil can also be used as a test expression. Since virtually all expressions are capable of returning nil, you can use almost any expression as a test expression. The following function demonstrates this point:

(defun MDIST (/ dstlst dst) (setq dstlst '(+ 0))

(while (setq dst (getdist "\nPick distance or Return to exit: ")) (Setq dstlst (append dstlst (list dst)))

(princ (Eval dstlst)) );end while

);end MDIST

109

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

This function gives the user a running tally of distances. The user is prompted to pick a distance or press Return to exit. if a point is picked, the user is prompted for a second point. The distance between these two points is displayed on the prompt. The Pick distance prompt appears again and if the user picks another pair of points, the second distance is added to the first and the total distance is displayed. This continues until the user presses return. The following discussion examines how this function works.

As usual, the first line defines the function. The second line creates a variable called dstlst and gives it the list value (+ 0).

(defun MDIST (/ dstlst dst) (setq dstlst '(+ 0))

The next line begins the while portion of the program. Instead of a predicate test, however, this expression uses a setq function.

(while (setq dst (getdist "\nPick point or Return to exit: "))

As long as points are being picked, getdist returns a non-nil value to setq and while repeats the evaluation of its arguments. When the user presses return, getdist returns nil and while quits evaluating its arguments. We see that while is really only concerned with nil and non-nil since the test expression in this example returns a value other than T.

The next few lines append the current distance to the list dstlst then evaluates the list to obtain a total:

(Setq dstlst (append dstlst (list dst)))

(princ (eval dstlst))

The function princ prints the value obtained from (eval dstlst) to the prompt (see Figure 5.14).

;Program to measure non-sequential distances -- Mdist.lsp

(Defun C:MDIST (/ dstlst dst)

 

(setq dstlst '(+ ))

;create list with plus

;while a return is not entered ...

 

(while (setq dst (getdist "\nPick point or Return to exit: "))

(Setq dstlst (append dstlst (list dst)))

;append distance value

(princ (Eval dstlst))

;print value of list

);end while

 

);end mdist

Figure 5.14: The Mdist function

110

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Conclusion

You have been introduced to several of the most useful functions available in AutoLISP. You can now begin to create functions and programs that will perform time consuming, repetitive tasks quickly and easily. You can also build-in some intelligence to your programs by using decision making functions. You may want to try your hand at modifying the programs in this chapter. For example, you could try to modify the Mdist function to save the total distance as a global variable you can later recall.

In the next chapter, you will get a brief refresher course in geometry.

111

Copyright © 2001 George Omura,,World rights reserved