Jump to content

ESC button pressed


brianhiroshi

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...