program abort with ESC

Discussion in 'AutoCAD' started by gert, Jul 9, 2004.

  1. gert

    gert Guest

    i wrote a lisp, where i have to set OSNAP to 2, if the user enters ESC
    key, my program stopps and i have OSNAP set to 2, but i want to change
    it back to the value it had at the beginn of my program

    (setq OSNAPOLD (getvar "OSNAP"))
    ..
    ..
    program
    ..
    ..
    *if ESC ist pressed, stop program AND set OSNAP back to OSNAPOLD

    any help welcome

    mfg
    gert
     
    gert, Jul 9, 2004
    #1
  2. gert

    dblaha Guest

    Do a search for "ESC error" in this discussion group and you'll get a wealth of information on this subject.
     
    dblaha, Jul 9, 2004
    #2
  3. gert

    rapidcad Guest

    Gert,

    What you are describing is an error handler. I'm not an expert at these, but I have put together several of them and mine work - other people may have better ideas, but for what it's worth, here's my 2 cents.

    I used to write in an error handler into every program when I was just figuring out the concept, but now I place the code as a modularized function in my Acaddoc.lsp and call it in any routine. Also, Ive got a mode collector and reset-er in there too so now I just call (modes '("osmode" "snapmode" "osmode" "autosnap")) after the start of the defun, then I simply issue a (moder) at the end to return.

    However, to get you started, here's the old way I used to do it.....
    An example of using error handling - HTH This gets placed above the defun :

    (DEFUN arwERR (msg) ;;; error handling function
    (setq *error* olderr)
    (command "UNDO" "E")
    (command "u")
    (resetvars-dbla)
    (princ)
    )
    (defun catchvars-dbla ()
    (setq RL (getvar "CLAYER")) ;;; store current layer
    (setq asm (getvar "autosnap")) ;;; store autosnap settings
    (setq orm (getvar "orthomode")) ;;; store current ortho status
    (setq osm (getvar "osmode")) ;;; store current osnap modes
    (setq ASZ (getvar "DIMASZ"))
    (setq DSCL (getvar "DIMSCALE"))
    )
    (defun resetvars-dbla ()
    (setvar "osmode" osm) ;;; reset osnap modes
    (setvar "orthomode" orm) ;;; reset ortho to previous status
    (setvar "autosnap" asm) ;;; reset autosnap to previous status
    (setvar "clayer" rl) ;;; reset previous current layer
    (setvar "plinewid" 0.0)
    (setvar "DIMASZ" ASZ)
    (setvar "DIMSCALE" DSCL)
    (princ) ;;; quiet exit
    )
    Here's where your defun goes....
    (defun c:blahblah blah ()
    then immediately you issue your variable-catcher, error handler start and undo loop begining ... like this.....
    (catchvars-dbla) ;;; collect current environment settings
    (setq olderr *error* ;;; Use special error handling function in place of AutoCAD standard error.
    *error* arwerr)
    (command "UNDO" "be") ;;; begin undo loop (in case of undo or error)
    Now your program guts go here....
    ( blah blah blah do this, do that...)
    and the reset and undo close go as the last lines before your princ
    (resetvars-dbla) ;;; reset the variables
    (command "UNDO" "E") ;;; end undo marked area (making it one big step)
    (PRINC) ;;; quiet exit
    )

    It may need some tweeking, but it works for me... HTH
     
    rapidcad, Jul 9, 2004
    #3
  4. gert

    Joe Burke Guest

    Gert,

    You want to use an error function like this assuming A2k or later.

    (defun c:functionname ( / *error* local_vars)

    (defun *error* (Msg)
    (cond
    ((or (not Msg)
    (member Msg '("console break"
    "Function cancelled"
    "quit / exit abort"))))
    ((princ (strcat "\nError: " Msg)))
    ) ;cond
    ;reset vars here
    (setvar "osnap" OSNAPOLD)
    (princ)
    ) ;end error

    ;; primary code starts here
    (setq OSNAPOLD (getvar "OSNAP"))
    ;; code
    ;; at the end of code, call the error function:
    (*error* nil)

    The error handler will reset the osnap variable regardless of whether the user
    cancels, or the program runs to completion.

    Joe Burke
     
    Joe Burke, Jul 9, 2004
    #4
  5. Rapidcad (do you have a *real* name? <bg>),

    Your posted example works great for R14 and earlier, but starting with
    AutoCAD 2000 it is possible to declare *error* as a local variable and have
    it remain local when it runs. Joe's example shows a way to use the local
    variable approach.

    Also, you are basically duplicating code, once to handle a normal exit from
    the program, and once for the error handler. Joe's example shows how adding
    a test for a nil error message allows you to use the error handler to handle
    a normal exit also, removing the need for duplicated code.

    --
    R. Robert Bell


    Gert,

    What you are describing is an error handler. I'm not an expert at these,
    but I have put together several of them and mine work - other people may
    have better ideas, but for what it's worth, here's my 2 cents.

    I used to write in an error handler into every program when I was just
    figuring out the concept, but now I place the code as a modularized function
    in my Acaddoc.lsp and call it in any routine. Also, Ive got a mode
    collector and reset-er in there too so now I just call (modes '("osmode"
    "snapmode" "osmode" "autosnap")) after the start of the defun, then I simply
    issue a (moder) at the end to return.

    However, to get you started, here's the old way I used to do it.....
    An example of using error handling - HTH This gets placed above the defun :

    (DEFUN arwERR (msg) ;;; error handling function
    (setq *error* olderr)
    (command "UNDO" "E")
    (command "u")
    (resetvars-dbla)
    (princ)
    )
    (defun catchvars-dbla ()
    (setq RL (getvar "CLAYER")) ;;; store current layer
    (setq asm (getvar "autosnap")) ;;; store autosnap settings
    (setq orm (getvar "orthomode")) ;;; store current ortho status
    (setq osm (getvar "osmode")) ;;; store current osnap modes
    (setq ASZ (getvar "DIMASZ"))
    (setq DSCL (getvar "DIMSCALE"))
    )
    (defun resetvars-dbla ()
    (setvar "osmode" osm) ;;; reset osnap modes
    (setvar "orthomode" orm) ;;; reset ortho to previous status
    (setvar "autosnap" asm) ;;; reset autosnap to previous status
    (setvar "clayer" rl) ;;; reset previous current layer
    (setvar "plinewid" 0.0)
    (setvar "DIMASZ" ASZ)
    (setvar "DIMSCALE" DSCL)
    (princ) ;;; quiet exit
    )
    Here's where your defun goes....
    (defun c:blahblah blah ()
    then immediately you issue your variable-catcher, error handler start and
    undo loop begining ... like this.....
    (catchvars-dbla) ;;; collect current environment settings
    (setq olderr *error* ;;; Use special error handling function in place
    of AutoCAD standard error.
    *error* arwerr)
    (command "UNDO" "be") ;;; begin undo loop (in case of undo or error)
    Now your program guts go here....
    ( blah blah blah do this, do that...)
    and the reset and undo close go as the last lines before your princ
    (resetvars-dbla) ;;; reset the variables
    (command "UNDO" "E") ;;; end undo marked area (making it one big step)
    (PRINC) ;;; quiet exit
    )

    It may need some tweeking, but it works for me... HTH
     
    R. Robert Bell, Jul 9, 2004
    #5
  6. gert

    rapidcad Guest

    Thanks R.
    Your insight is from many years of experience I take it. Thanks for the advice. You've easily exposed the fact that this example of mine is old. It does in fact come from a ver. 12 application that still works flawlessly today (A2K4) As I said
    " I'm not an expert at these, but I have put together several of them and mine work - other people may have better ideas, but for what it's worth, here's my 2 cents. "

    I'll have to try again to get my hands around Joe's example. Since I first wrote this handler, I have figured out that my 3rd party app has an error handler built in so I've been using it.

    It doesn't take much experience to know that my example is pretty antiquated, (and yes, clumsy). I replied to his query in the first place not because I have much to share, but because I figued I could help out some. I've been posting a question all week - today I posted it for the third time - and for the third time I've gotten 0 replies - even with 3 different titles, so I don't want that to happen to him. And partly, I'm hoping the old proverb, "he who refreshes others shall by others be refreshed" might come true for me.

    And yes I do have a "real" name, it might even be the same as "R." (if I knew what that stood for) - thanks for asking...<g>
     
    rapidcad, Jul 9, 2004
    #6
  7. Sometimes we need to support older versions, which is why I said your code
    is certainly valid for those cases. You have also indicated in the past a
    willingness to learn new techniques, which why I pointed Joe's thread out to
    you.

    I've seen your other threads, but haven't had the time to do a heavy debug
    on it. I had a migraine yesterday, which hosed me for the day.

    --
    R. Robert Bell


    Thanks R.
    Your insight is from many years of experience I take it. Thanks for the
    advice. You've easily exposed the fact that this example of mine is old.
    It does in fact come from a ver. 12 application that still works flawlessly
    today (A2K4) As I said
    " I'm not an expert at these, but I have put together several of
    them and mine work - other people may have better ideas, but for what it's
    worth, here's my 2 cents. "

    I'll have to try again to get my hands around Joe's example. Since I first
    wrote this handler, I have figured out that my 3rd party app has an error
    handler built in so I've been using it.

    It doesn't take much experience to know that my example is pretty
    antiquated, (and yes, clumsy). I replied to his query in the first place
    not because I have much to share, but because I figued I could help out
    some. I've been posting a question all week - today I posted it for the
    third time - and for the third time I've gotten 0 replies - even with 3
    different titles, so I don't want that to happen to him. And partly, I'm
    hoping the old proverb, "he who refreshes others shall by others be
    refreshed" might come true for me.

    And yes I do have a "real" name, it might even be the same as "R." (if I
    knew what that stood for) - thanks for asking...<g>
     
    R. Robert Bell, Jul 9, 2004
    #7
  8. gert

    rapidcad Guest

    Sorry to hear of the headaches, Robert. I do appreciate everything I get from this forum, and I try to answer the easier questions (when workload allows) since that's more my speed. Mark Propst did a huge favor and on his own initiave rewrote just about the whole thing in active X for me. It works pretty well now. As far as learning new things, I'll have to chew on the active X stuff for awhile! Thanks for your comments and all that you contriburte to us less experienced programmers....

    Ron Powell
    CAD Manager
    jdh Engineering
    Grandville, MI
     
    rapidcad, Jul 9, 2004
    #8
  9. Thanks Ron!

    --
    R. Robert Bell


    Sorry to hear of the headaches, Robert. I do appreciate everything I get
    from this forum, and I try to answer the easier questions (when workload
    allows) since that's more my speed. Mark Propst did a huge favor and on
    his own initiave rewrote just about the whole thing in active X for me. It
    works pretty well now. As far as learning new things, I'll have to chew on
    the active X stuff for awhile! Thanks for your comments and all that you
    contriburte to us less experienced programmers....

    Ron Powell
    CAD Manager
    jdh Engineering
    Grandville, MI
     
    R. Robert Bell, Jul 9, 2004
    #9
  10. gert

    gert Guest

    thx for your answers, i will try it with the error function

    mfg

    gert
     
    gert, Jul 12, 2004
    #10
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.