brianhiroshi Posted February 18, 2009 Posted February 18, 2009 Hi! At the beginning of my function I set some variables like snapmode to 0 abnd restore the values at the. What do I do if the person using an AutoLisp routine press the ESC button before the end of the function? Is there any way to restore the variables when this happens? Thanks! Quote
Lee Mac Posted February 18, 2009 Posted February 18, 2009 You will need to use an error handler in the code - check AfraLISP.net for topics on how this can be done Quote
Lee Mac Posted February 18, 2009 Posted February 18, 2009 Something like: (setq olderr *error* *error* errtrap) (defun errtrap (msg) (if oldvars (mapcar 'setvar vlst oldvars)) (setq *error* olderr) ) (setq vlst (list "OSMODE" "CLAYER") ; list changed system vars here oldvars (mapcar 'getvar vlst)) Quote
brianhiroshi Posted February 20, 2009 Author Posted February 20, 2009 Thanks! I'll see if it works on my program! Quote
alanjt Posted February 21, 2009 Posted February 21, 2009 you can also just have the error handler as a localized subroutine. you just have to name it *error*. e.g. (defun c:my_line ( / *error* old_osmode pt1 pt2 ) (defun *error* (msg) (if old_osmode (setvar "osmode" old_osmode) ) (princ (strcat "\nError: " msg)) ) (setq old_osmode (getvar "osmode")) (setvar "osmode" 0) (if (and (setq pt1 "\nSelect 1st point: ")) (setq pt2 "\nSelect 2nd point: ")) ) (command "_.line" pt1 pt2 "") ) (setvar "osmode" old_osmode) ) nothing special, just a little easier. Quote
Lee Mac Posted February 21, 2009 Posted February 21, 2009 Alan, the above may be a little easier, but I wouldn't recommend redefining the in-built ACAD *error* routine.... One should restore the previous *error* setting after the program has completed. Lee Quote
alanjt Posted February 22, 2009 Posted February 22, 2009 Alan, the above may be a little easier, but I wouldn't recommend redefining the in-built ACAD *error* routine.... One should restore the previous *error* setting after the program has completed. Lee by localizing the *error*, you are doing just that. when i exit the routine (even if i have to execute the *error* function), the original *error* will be restored. as long as it's localized, it only last as long as the routine does. Quote
alanjt Posted February 22, 2009 Posted February 22, 2009 i would say that it's even a safer route to take, since if you somehow manage to escape while the error is restoring, you won't have the problem of it not restoring the original error routine by not running the expression: (setq *error* orig_error) i could be wrong, so i'm not saying this IS the way to go. if someone else would like to chime in, i'm all ears. if i'm doing it wrong, i want to know. Quote
Lee Mac Posted February 22, 2009 Posted February 22, 2009 Well, tbh, I used to make all my error handlers almost identical to the one you posted, but I changed recently due to this: http://www.cadtutor.net/forum/showpost.php?p=213140&postcount=19 Check out third bullet-point Cheers Lee Quote
CALCAD Posted February 25, 2009 Posted February 25, 2009 After playing with this for awhile and trying many variations, I would recommend alanjt's method of error trapping. It's the shortest, simplest and I daresay the safest method. JohnM's recommendations referred to by Lee Mac are worth consideration, but the third item about *error* is confusing because the AutoCAD documentation refers to *error* as the "user definable error handling function." You're SUPPOSED to redefine it - then restore the default. Well, by localising *error* in the main function, that is what happens, with the least amount of code. Lee, if you recall a few months back, CAB rewrote some of your Centre-Line program using just this kind of error trap. There may be a problem with this method, but so far I've been unable to find it. It LOOKS wrong because *error* is always referred to as a function, and putting it in the local var list suggests it's a variable name. Perhaps it's actually something like a pointer to the function. In any case, it seems to work. Quote
Lee Mac Posted February 25, 2009 Posted February 25, 2009 After playing with this for awhile and trying many variations, I would recommend alanjt's method of error trapping. It's the shortest, simplest and I daresay the safest method. JohnM's recommendations referred to by Lee Mac are worth consideration, but the third item about *error* is confusing because the AutoCAD documentation refers to *error* as the "user definable error handling function." You're SUPPOSED to redefine it - then restore the default. Well, by localising *error* in the main function, that is what happens, with the least amount of code. Lee, if you recall a few months back, CAB rewrote some of your Centre-Line program using just this kind of error trap. There may be a problem with this method, but so far I've been unable to find it. It LOOKS wrong because *error* is always referred to as a function, and putting it in the local var list suggests it's a variable name. Perhaps it's actually something like a pointer to the function. In any case, it seems to work. Thanks CALCAD. As I say, I used to use the example posted by Alanjt in all my LISPs, and thought it was the right way to go - after seeing it being used by more experienced users such as CAB and ASMI. But then I took note of JohnM suggestion and also saw something on AfraLISP in the same tone as JohnM, and reconsidered my approach. http://www.afralisp.net/lispa/lisp6.htm Thanks for clarifying things. Lee Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.