default settings if lisp doesn't finish

Discussion in 'AutoCAD' started by Mack Attack, Jul 29, 2004.

  1. Mack Attack

    Mack Attack Guest

    I have been doing simple lisp routines for some time and want to start
    making it so if the routine doesn't finish properly that all the variable
    that were changed at the beginning of it don't stay changed and go back to
    what the user had before the routine. I know this can be done but have
    absolutely no idea how to do it. All help will be appreciated but remember
    I do simple routines because I have a simple mind so please try not to blow
    me out of the water to bad.

    Thank as always
    Mack
     
    Mack Attack, Jul 29, 2004
    #1
  2. Mack Attack

    MP Guest

    well in the time it took me to do this little example i see you've already
    gotten two good answers
    but i guess i'll send it anyway, just for the sake of overkill
    :)

    The process is not difficult to understand.
    You'll just need to spend some time digesting it.

    in general terms:
    save settings first
    write localized error trap
    routine body
    reset settings

    do a google search on error or *error* and you'll get a ton of info on how
    to do it.

    simple example:

    (defun test( / *error* cleanup oldos newos bombout)
    ;local error trap
    (defun *error*(msg)
    (if msg
    (princ(strcat "\nError msg: " msg))
    (princ "\nCleanup mode - not really an error")
    )
    (if oldos
    (progn
    (setvar"OSMODE" oldos)
    (setq newos(Getvar"OSMODE"))
    (princ(strcat"\nOsmode reset in error trap to: " (itoa newos)))
    );progn
    );if
    (princ)
    )
    ;local cleanup routine
    ;inspired by Tony Tanzillo
    (defun Cleanup()
    (*error* nil)
    )

    ;just to make sure you can see a difference
    (setvar"OSMODE" 185);represents current running osnap at time of routine

    ;get existing setting

    (setq oldos(Getvar"OSMODE"))
    (princ(strcat"\nOsmode starts: " (itoa oldos)))

    ;reset for routine
    (setvar"OSMODE" 1);or whatever setting required by routine
    (setq newos(Getvar"OSMODE"))
    (princ(strcat"\nOsmode changed to: " (itoa newos)))

    ;...routine body...

    ;run with next line not commented out
    (setq bombout(/ 1 0));cause divide by zero error
    ;then comment out above line and run again


    (setq bombout(/ 1 0.5))
    (princ"\nNow that's okay")


    (cleanup)
    );end function
     
    MP, Jul 29, 2004
    #2
  3. Mack Attack

    Tom Smith Guest

    As mentioned, this has been covered in vast detail before.

    I humbly submit my own version, which I have posted previously. I believe
    it's shorter, easier to use, and more flexible than other examples which
    have been posted.

    First, in a file which is automatically loaded in each document on
    startup -- such as an acaddoc.lsp or a mnl file -- I define a function which
    will create an error handler on the fly:

    (defun *init-error* (varlist / oce)
    (setq *error*
    (append
    '((msg / oce))
    (mapcar
    '(lambda (var) (list 'setvar var (getvar var)))
    varlist)
    '((setq oce (getvar "cmdecho")))
    '((setvar "cmdecho" 0))
    '((command "undo" "end"))
    '((setvar "cmdecho" oce))
    '((princ))))
    (setq oce (getvar "cmdecho"))
    (setvar "cmdecho" 0)
    (command "undo" "begin")
    (setvar "cmdecho" oce)
    (princ))

    This function takes a list of variables to be saved, and defines an *error*
    function which will restore them to their saved values. Here's how to use it
    in a typical lisp file:

    (defun c:mylisp (/ *error* var1 var2) ;declare *error* local along with
    other local variables
    (*init-error* '("cmdecho" "osmode" "etc" "etc")) ;list of variables to be
    saved
    (setvar "osmode" 0) ;change variables as needed in this lisp
    (do-whatever) ;your code goes here
    (*error* nil)) ;reset variables

    You can list whatever variables your lisp needs to temporarily alter - it is
    not limited to a specific list of them as many error handler examples are.
    Also, this avoids the need to include pretty much the same block of
    repetitive code in every single lisp, as most examples show. Finally, it
    avoids the unneccessary complication of a separate "cleanup" or "restore"
    function to cover normal termination -- the same error-handling function is
    called whether your lisp crashes or finishes normally.
     
    Tom Smith, Jul 29, 2004
    #3
  4. Mack Attack

    Mack Attack Guest

    I thank everyone for their examples and pointing me in the direction I need
    to go. I figured this had been discussed but did not know which direction
    to start. I will work on figuring this out.

    Thanks again to all
    Mack
     
    Mack Attack, Jul 29, 2004
    #4
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.