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

Using Defaults in a Program

Virtually every AutoCAD command offers a default value. For example, the line command will continue a line from the last point selected if no point is selected at the First point prompt. Defaults can be a great time saver especially when the user is in a hurry. In this section, you will see first hand how you can add defaults to your own programs.

Adding Default Responses to your Program

In the C:QZOOM program, a default response was added. If the user presses return without picking a point, the program goes into a pan mode allowing the use to select a new view center. By giving the user the pan option in this way, the program becomes easier to use and more flexible. Other AutoCAD commands also provide default values for options. For example, the offset command will offer the last offset distance as a default value for the current offset distance. If the user decides he or she can use that value, he or she only needs to press return to go on to the next part of the command.

You can incorporate similar functionality into your programs by using global variables. Figure 8.11 shows the sequential number program created in chapter 5 with code added to include a default value for the number spacing.

(defun C:SEQ (/ pt1 currnt last spc)

 

(if (not *seqpt)(setq *seqpt 2.0))

;setup global default

(setq pt1

(getpoint "\nPick start point: "))

;get start point

(princ "\nEnter number spacing <")

;first part of prompt

(princ *seqpt)

;print default part of prompt

(setq spc

(getdist pt1 ">: "))

;finish prompt - get spac'g

(setq currnt

(getint "\nEnter first number: "))

;get first number

(setq last

(getint "\nEnter last number: "))

;get second number

(if (not spc)(setq spc *seqpt)(setq *seqpt spc));set global variable

(setq stspc

(rtos spc 2 2))

;convert spacing to string

(setq stspc

(strcat "@" stspc "<0" ))

;create spacing string

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

;place first number

(repeat (- last currnt)

;start repeat 'till last

(setq currnt (1+ currnt))

;add 1 to current number

(command "text" stspc "" "" currnt)

;place text

)

;end defun

;end repeat

)

 

Figure 8.11: The modified C:SEQ program

Make the changes to your copy of the C:SEQ program so it looks like figure 8.11. Open a new file in AutoCAD,

175

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

load the newly modified C:SEQ program, and then run it. The program will run as it has before but it now offers a default value of 2.0 at the Enter number spacing prompt:

Enter number spacing <2.0>:

Press return at this prompt. The default value of 2.0 will be applied to the number spacing. If you enter a different value, .5 for example, this new value becomes the default. The next time you run the program, .5 will appear as the default value for the number spacing:

Enter number spacing <0.5>:

There are actually several expressions that are added to make this default option possible. First is a conditional expression that test to see if a global variable called *seqpt is non-nil:

(defun C:SEQ (/ pt1 currnt last spc)

(if (not *seqpt)(setq *seqpt 2.0))

It its' value is nil, it is given the value of 2.0. This is just an arbitrary value. You can make it anything you like.

Next, the user is prompted to pick a point just as in the previous version of the program:

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

The next set of expressions does the work of displaying the default value to the AutoCAD prompt.

(princ "\nEnter number spacing <")

(princ *seqpt)

(setq spc (getdist pt1 ">: "))

The prompt is broken into three parts. The first expression above prints everything before the default value. The second expression prints the default value. The third expression uses the getdist function to obtain a new distance value from the user. The end of the prompt is included as the prompt string to the getdist function. The net result is a single line appearing at the AutoCAD prompt (see figure 8.12).

Figure 8.12: Using princ to construct a prompt

176

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

The next two lines are unchanged from the earlier version of the program:

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

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

The next line is a new conditional expression that tests to see if a value was entered at the Enter number spacing prompt:

(if (not spc)(setq spc *seqpt)(setq *seqpt spc))

This expression test the variable spc to see if it value is non-nil. If it is nil, indicating the user pressed return without entering a value, spc is assigned the value of the global variable *seqpt. This is the default value that appears in the Enter number spacing prompt. If spc does have a value, then its value is assigned to *seqpt thus making the value of spc the new default value.

The rest of the program is unchanged:

(setq stspc (rtos spc 2 2))

(setq stspc (strcat "@" stspc "<0" )) (command "text" pt1 "" "" currnt) (repeat (- last currnt)

(setq currnt (1+ currnt))

(command "text" stspc "" "" currnt)

)

)

You may wonder why the global variable *seqpt starts with an asterisk. Names given to global variables don't have to be different from other symbol but you may want to set them off by preceding them with an asterisk. This is a convention used in Common LISP which we have carried over to AutoLISP.

Creating a Function to Handle Defaults

If you find that many of your programs utilize defaults, it may be worthwhile to create a function that creates the default prompts for you. Figure 8.13 Shows the C:SEQ program with a function added to handle default prompts.

177

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

(defun deflt

(str1 def)

 

(strcat str1 " <" (rtos def 2 4) ">: ")

 

)

 

 

(defun C:SEQ (/ pt1 currnt last spc)

 

(if (not *seqpt)(setq *seqpt 2.0))

;setup global default

(setq pt1

(getpoint "\nPick start point: "))

 

(setq spc

(getdist (deflt "\nEnter spacing" *seqpt)))

(setq currnt

(getint "\nEnter first number: "))

 

(setq last

(getint "\nEnter last number: "))

 

(if (not spc)(setq spc *seqpt)(setq *seqpt spc)) ;set global variable (setq stspc (rtos spc 2 2))

(setq stspc (strcat "@" stspc "<0" )) (command "text" pt1 "" "" currnt)

(repeat (- last currnt) (setq currnt (1+ currnt))

(command "text" stspc "" "" currnt)

)

)

Figure 8.13: The C:SEQ program with a default handling function.

The function deflt takes two arguments. The first is the beginning text of the prompt and the second is the default value.

(defun deflt (str1 def / lunts)

(setq lunts (getvar "lunits"))

(strcat str1 " <" (rtos def lunts 4) ">: ")

)

The arguments are concatenated to form a single string which is the value returned by the function. Since the default value is a real data type, it is converted to a string using the rtos function. The getvar expression at the beginning of the function finds the current unit style which is used in the rtos function to control the unit style created by rtos.

The C:SEQ function uses this function in the expression:

(setq spc (getdist (deflt "\nEnter spacing" *seqpt)))

Here, the function is place where the prompt string normally appears in a getdist expression. When deflt is evaluated, it returns the string:

"\nEnter spacing <2.0000>: "

This string is then supplied to the getdist function as the prompt string argument.

178

Copyright © 2001 George Omura,,World rights reserved