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

How to Get Text Input

You can prompt the user for text input using the Getstring and Getkword functions. These two functions always return String variables even if a number is entered as a response. Though both Getstring and Getkword are used for obtaining String values, They operate in slightly ways. Getstring will accept almost anything as input so it can be used to acquire words or sentences of up to 132 characters long with no particular restriction to content. Getkword, however, is designed to work in conjunction with the Initget function, (described in detail later in this chapter), to accept only specific strings, or keywords, as input. If the user tries to enter a string to Getkword that is not a predetermined keyword, the user is asked to try again.

Using Getstring

The syntax for Getstring is:

(getstring [optional variable or number] [optional prompt string])

You can optionally supply one or two arguments to Getstring. You can supply a string value for a prompt to the user or you can also supply a non-nil variable or number, to signify that Getstring is to accept spaces in the user input. For example, in many AutoCAD commands, pressing the space bar on your keyboard is the same as pressing the return key. Normally, Getstring will also read a space bar as a return thus disallowing more than a single word to be input to the Getstring function. Enter the following expression:

(setq str1 (getstring "Enter a word: " ))

the following prompt appears:

Enter a word:

Try entering your first and last name. As soon as you press the space bar, the getstring function and the command prompt returns read your first name. Display the value of str1 by entering:

!str1

Since the space bar is read as a return, once it is pressed, the entered string is read and AutoLISP goes on to other business. If you provide non-nil variable or number argument, however, getstring will read a space bar input as spaces in a text string thereby allowing the user to enter whole sentences or phrases rather than just single words. Enter the following:

(setq str2 (getstring T "Enter a sentence: "))

The following prompt appears:

Enter a sentence:

Try entering your first and last name again. This time you are able to complete your name. Display the value of str2 by entering:

80

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

!str2

An integer or real value or anything that will not evaluate to nil could replace the T argument in the above example.

We would like to restate that you can enter numbers in response to Getstring but they will be treated as strings therefore, you won't be able to apply math functions to them. You can, however, convert a number that is a string variable type into a real or integer type using the Atof, Atoi and Ascii functions (see Chapter 7 for details).

Using Getkword

The syntax for Getkword is:

(getkword [options prompt string])

Getkword offers only one argument option, the string prompt. Unlike most other functions, Getkword must be used in conjunction with the Initget function. Initget is used to restrict the user by allowing only certain values for input. If a disallowed value is entered, the user is prompted to try again. In the case of Getkword, Initget allows you to specify special words that you expect as input. The most common example of this situation would be the Yes/No prompt. For example, you may want to have your program perform one function or another based on the user entering a Yes or No to a prompt. Enter the following:

(initget "Yes No")

(setq str1 (getkword "Are you sure? <Yes/No>: "))

The following prompt appears:

Are you sure? <Yes/No>:

If the user does not enter a Yes, Y, No or N, Getkword will continue to prompt the user until one of the keywords is entered. Try entering MAYBE. You get the message:

Invalid option keyword.

Are you sure? <Yes/No>:

Now try entering Y. Getkword accepts the Y as a valid keyword and the whole keyword Yes is assigned to the variable str1. If you had entered n or N for No, then the entire keyword No would have been assigned to str1.

The words Yes and No are first established as keywords using the initget function. Getkword then issues the prompt and waits for the proper keyword to be entered. If capital letters are used in the Initget function's string argument, they too become keywords. This is why Y is also allowed as keyword in the above example (see Initget for more details). It doesn't matter if you enter a capital or lower case Y. It only matters that you enter the letter that is capitalized in the Initget string argument.

81

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

How to Get Numeric Values

At times, you may want to prompt the user for a numeric value such as a size in decimal units or the number of times a functions is to be performed. Getreal and Getint are the functions you would use to obtain numeric input.

Using Getreal and Getint

Getreal always returns reals and Getint always returns integer values. The syntax for Getint is:

(getint [optional prompt string])

The syntax for Getreal is:

(getreal [optional prompt string])

Getreal and getint can be supplied an optional argument in the form of a prompt string. Just as with all the previous get functions, this prompt string is displayed as a prompt when an expression using getreal or getint is evaluated.

How to Control User Input

You can add some additional control to the Get functions described in this chapter by using the Initget function. In version 2.6 of AutoCAD, Initget was only used to provide keywords for the Getkword function (see Getkword earlier in this chapter). With Version 9, other functions are added to give more control over input to the other Get functions. For example, with Initget, you can force a Get function not to accept zero or negative values. Unlike most functions, Initget always returns nil.

Using Initget

The syntax for Initget is:

(initget [optional bit code] [optional string input list])

You must supply at least one argument to initget. The bit code option controls the kinds of input that are restricted, or, in some cases, how rubber-banding lines or windows are displayed. Table 4.1 lists the bit codes and their meaning.

82

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

CODE MEANING

1null input not allowed

2zero values not allowed

4

negative values not allowed

8

do not check limits for point values

16

return 3D point rather than 2D point

32

Use dashed lines for rubber-banding lines and windows

Table 4.1: The Initget bit codes and what they mean

Bit codes can be used individually or added together to affect several options at once. For example, if you do not want a Get function to accept null, zero and negative values for input, you can use the following:

(initget 7)

(setq int1 (getint "Enter an integer: "))

In this example, though there is no formal 7 bit code value, for initget, 7 is 1 plus 2 plus 4 so the restrictions associated with bit codes 1, 2, and 4 are applied to the Getint that follows. You can also write the Initget expression above as:

(initget (+ 1 2 4))

Not all initget bit codes are applicable to all Get functions. Table 4.2 shows which code works with which Get function.

83

Copyright © 2001 George Omura,,World rights reserved