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

(setvar "osmode" 0)

is replaced by one that sets the osnap mode to whatever was saved as *osnap:

(setvar "osmode" *osnap)

This same expression appears in the *error* function so in the event of a cancellation by the user, the osnap mode is set back to its previous setting.

Organizing Code to Reduce Errors

The error handling function shown here as an example could be incorporated into your Acad.lsp file so it is available for any AutoLISP error that may occur. You can also enlarge it to include other settings or variables that may require resetting. But the way a program is organized can affect the impact an error has. For example, we could have written the C:SEQ program in a slightly different way. Figure 8.17 shows the program with its expressions in a slightly different order.

(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)))

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

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

(setq last

(getint "\nEnter last number: "))

(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.17: The C:SEQ program in a different order

183

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

The conditional expression:

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

immediately follows the expression that prompts for the spacing distance. This placement seems to be a natural place for this expression since it immediately sets the variable spc or *seqpt to a value once the value of spc is obtained. But what happens if the user decides to cancel the program after this expression is evaluated. If the user inputs a new value for the number spacing, then the global variable *seqpt holds that new value even though the program has been canceled. The next time the user uses the C:SEQ program, the value that was entered previously is the new default value. Even though the program was cancelled the value entered for the number spacing became the new default.

This may or may not be a problem but for many, issuing a cancel means canceling the affects of any data entry made during the command. So to avoid having the global variable *seqpt changed when the program is cancelled, the conditional expression is moved to a position after all the prompts are issued. This way, the user can cancel the program with no affect to the *seqpt variable.

Debugging Programs

While we are on the subject of errors, We should discuss the debugging of your programs. As you begin to write programs on your own, you will probably not get them right the first time. Chances are, you will write a program then run it only to find some error message appear. Then you must review your program to try and find the offending expression.

Common Programming Errors

Most of the time, errors will be due to the wrong number of parentheses or the wrong placement of parentheses within your program. If this is the case, you usually get the error message:

error: malformed list

or

error: extra right paren

There aren't any simple solutions to this problem other than checking your program very carefully for number and placement of parentheses. Perhaps the best thing to do is to print out your program. It is often easier to spot errors on paper than it is to spot them on your computer screen.

Since a misplaces paren can cause a variety of problems, printing out your program and checking the parentheses placement is the best start.

Another common error is to mis-spelled symbols. This is especially a problem with confusing lower case l's with 1's and zeros with o's. The full range of typos is possible and often hard to detect. Again, the best solution is to print out your program and take a careful look.

184

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

If you get the message:

error: Insufficient string space

chances are, you didn't provide a closing double quote in a string value as in the following:

(menucmd "p1=* )

Also, prompt strings cannot exceed 100 characters.

Finally, it is common to try to apply a wrong data type to a function. We have mentioned that one common error is to give a variable a string value which happens to be a number:

(setq str1 "1")

Later, you might attempt to use this string as an integer in another function:

(1+ str1)

This results in a bad argument type error.

For your convenience, we have included appendix B that contains the AutoLISP error messages and their meaning. You may want to refer to it as you debug your programs.

Using Variables as Debugging Tools

AutoLISP helps you find errors by printing to the screen the offending expression along with the error message. But sometimes this is not enough. If you find you are having problems with a program, you can check the variables in the program using the exclamation point to see what values they have obtained before the program aborted. If you have an argument list, you may want to keep it empty until you finish debugging your program. That way, you can check the value of the programs's variables. Otherwise, the values of the variables will be lost before you have a chance to check them.

If you have a particularly lengthy program, you can use the princ function to print variables to the prompt line as th program runs. By placing the princ function in strategic locations within your program, you can see dynamically what your variables are doing as the program runs. You can also have the princ function print messages telling you where within your program it is printing from.

Conclusion

As you begin to write you own program, many of the issues brought to light in this chapter will confront you. We hope that by introducing these topics now, you will have a better grasp of what is required in a program design. By knowing what is possible within AutoCAD, you can develop programs that simplify the users efforts.

185

Copyright © 2001 George Omura,,World rights reserved

The ABC’s of AutoLISP by George Omura

186

Copyright © 2001 George Omura,,World rights reserved