Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
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

setting to change ("snapunit") and a value representing the new settings (12 12). Here we are attempting to use Setvar to change the snap distance setting to 12 by 12.

Remember that AutoLISP evaluates each argument before it is passed to the function. As with numbers, Strings evaluate to themselves, so the string "snapunit" evaluates to "snapunit". But AutoLISP will also try to evaluate the list (12 12). AutoLISP always tries to evaluate lists as if they are expressions. As you saw earlier, the first element in an expression must be a function. Since the first element of the list (12 12) is not a function, AutoLISP will return an error message (see figure 1.5).

Figure 1.5: An error using Setvar

In this situation, we do not want this list (12 12) to be evaluated. We want it to be read "as is". To do this, we must add the Quote function as in the following:

(setvar "snapunit" '(12 12))

Now AutoLISP will not try to evaluate (12 12), and Setvar will apply the list to the snapunit system variable setting.

Quote provides a means to prevent evaluations when they are not desirable. Quote is most often used in situations where a list must be used as an argument to a function. Remember that there are two types of lists, those intended for evaluation and those used to store data. The list (12, 12) stores data, the width and height of the Snap distance. Because (12 12) does not have a function as its first element, it cannot be evaluated. Since AutoLISP blindly evaluates everything, Quote is needed to tell AutoLISP not to evaluate (12 12).

Applying Variables

The variable Golden can now be used within an AutoCAD command to enter a value at a prompt, or within another function to obtain other results. To see how this works, you'll assign the value 25.4 to a variable called Mill.

1. Enter

(setq mill 25.4)

14

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

at the command prompt.

Now find the result of dividing Mill by Golden.

2. Enter

(/ mill golden)

This returns the value 15.698393.

Now assign this value to yet another variable.

3. Enter

(setq B (/ mill golden))

Now you have three variables, Golden, Mill, and B, which are all assigned values that you can later retrieve, either within an AutoCAD command by entering an exclamation point followed by the variable, or as an argument within an expression.

Our examples so far have shown numbers being manipulated, but text can also be manipulated in a similar way. Variables can be assigned text strings that can later be used to enter values in commands that require text input. Strings can also be joined together or concatenated to form new strings. Strings and numeric values cannot be evaluated together, however. This may seem like a simple statement but if you do not consider it carefully, it can lead to confusion. For example, it is possible to assign the number 1 to a variable as a text string by entering

(setq text1 "1'')

Later, if you try to add this string variable to an integer or real number, AutoCAD will return an error message.

The examples used Setq and the addition and division functions. These are three functions out of many available to you. All the usual math functions are available, plus many other functions used to test and manipulate variables. Table 1.1 shows some of the math functions available.

15

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Table 1.1: A partial list of AutoLISP functions

MATH FUNCTIONS THAT ACCEPT MULTIPLE ARGUMENTS

(+ number number

...)

 

add

(- number number ...

)

 

subtract

(* number number ...

)

 

multiply

(/ number number ...

)

 

divide

(max number number

...)

find largest of numbers given

(min number number ...

)

find smallest of numbers given

(rem number number ...

)

find the remainder of numbers

MATH FUNCTIONS THAT ACCEPT SINGLE ARGUMENTS

(1+ number)

add 1

(1© number)

subtract 1

(abs number)

find the absolute value

(exp nth)

e raised to the nth power

(expt number nth)

number raised to the nth power

(fix real)

convert real to integer

(float integer)

convert integer to real

(gcd integer integer)

find greatest common denominator

(log number)

find natural log of number

(sqrt number)

find square root of number

16

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

FUNCTIONS FOR BINARY OPERATIONS

(~ integer)

(logand int. int. ...)

(logior int. int. ...)

(lsh int. bits)

find logical bitwise NOT of integer

find logical bitwise AND of integers

find logical bitwise OR of integers

find logical bitwise shift of int.by bits

Since AutoLISP will perform mathematical calculations, you can use it as a calculator while you are drawing. For example, if you need to convert a distance of 132 feet 6 inches to inches, you could enter

(setq inch1 (+ 6 (* 132 12)))

at the command prompt. The result of this expression is returned as 1590. The asterisk is the symbol for the multiplication function. The value 1590 is assigned to the variable Inch1, which can later be used as input to prompts that accept numeric values. This is a very simple but useful application of AutoLISP. In the next section, you will explore some of its more complex uses.

Accessing Single Elements of a List

When you draw, you are actually specifying points on the drawing area in coordinates. Because a coordinate is a group of values rather than a single value, it must be handled as a list in AutoLISP. You must use special functions to access single elements of a list. Two of these functions are Car and Cadr. The following example illustrates their use.

Suppose you want to store two point locations as variables called Pt1 and Pt2.

1. Enter the following two lines the command Prompt:

(setq pt1 (list 5 6)) (setq pt2 (list 10 12))

The List function in these expressions combines the arguments to form a list. (You can see this in Figure 1.6).

17

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Figure 1.6: The list function.

These lists are assigned to the variable Pt1 and Pt2. As we have just seen, variables accept not only single objects as their value but also lists. In fact, variables can accept any data type as their value, even other symbols representing other variables and expressions.

2. To see the new value for pt1, enter the following:

!pt1

The list (5 6) appears.

Now suppose you want to get only the x coordinate value from this example.

3. Enter:

(car pt1)

The value 5 appears.

18

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

4. To get the y value, enter:

(cadr pt1)

which returns the value 6. These values can in turn be assigned to variables, as in the line

(setq x (car pt1))

Figure 1.7 may help you visualize what Car and Cadr are doing.

Figure 1.7: Car and Cadr of pt1

By using the List function, you can construct a point variable using x and y components of other point variables. For example, you may want to combine the y value of the variable Pt1 with the x value of a point variable Pt2.

5. Enter the following:

19

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

(list (car pt2) (cadr pt1))

You get the list (10 6) (see figure 1.8).

Figure 1.8: Deriving a new list from pt1 and pt2

These lists can be used to enter values during any AutoCAD command that prompts for points.

Actually, we have misled you slightly. The two primary functions for accessing elements of a list are CAR and CDR (pronounced could-er). You know that Car extracts the first element of a list. CDR, on the other hand, returns the value of a list with its first element removed.

6. Enter the following at the command prompt:

(cdr '(A B C))

The list (B C) is returned (see figure 1.9).

20

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Figure 1.9: Using Cdr to remove the first element of a list.

When CDR is applied to the list (A B C) you get (B C) which is the equal to the list (A B C) with the first element, A, removed. Notice that in the above example, the list (A B C) was quoted. If the quote were left out, AutoLISP would try to evaluate (A B C). Remember that AutoLISP expect the first element of a list to be a function. Since A is variable and not a function, you would get the error message:

error: null function (A B C)

Now try using CDR with the variable pt1.

7. Enter

(cdr pt1)

The list (6) is returned.

Remember that anything within a pair of parentheses is considered a list, even () is considered a list of zero elements. Since the value returned by CDR is a list, it cannot be used where a number is expected. Try replacing the CADR in the earlier example with a CDR:

8. Enter:

(list (car pt2) (cdr pt1)) (10 (6))

You get a list of 2 elements, 10 and (6) (see figure 1.10). Though this is a perfectly legal list, it cannot be used

21

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

as a coordinate list.

Figure 1.10: Using Car and Cdr together

So what exactly is CADR then. CADR is the contraction of CAR and CDR. You now know that CDR returns a list with its first element removed and that CAR returns the first element of a list. So to get 6 from the list held by the variable pt1, you apply CDR to pt1 to get (6) the apply car to (6) as in the following example:

(car (cdr pt1)) 6

This CAR-CDR combination is abbreviated to CADR.

(cadr (pt1)) 6

Figure 1.11 shows graphically how this works. You can combine CAR and CDR in a variety of ways to break down nested lists. Figure 1.12 shows some examples of other CAR and CDR contractions.

22

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Figure 1.11: How CADR works

23

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

Figure 1.12: The CAR and CDR functions combined to extract elements of nested lists

24

Copyright © 2001 George Omura,,World rights reserved