Error handler Questions....

Discussion in 'AutoCAD' started by Court Myers, Jun 30, 2004.

  1. Court Myers

    Court Myers Guest

    I've been reading the posts in this NG for a little while and have tried to
    get an error handler to work but i can't seem to get it to work.... All I
    want it to do is run some cleanup if the user hits "ESC" or something else
    goes wrong below i posted my origanl code w/o my attempts at error handling
    because i didnt save them....

    thanks in advance
    Court

    ; ElevTag01_2005.lsp
    ;copyright - court myers - architectural design group inc. 2004

    (defun c:elevtag01 (/ laykeyset01 scale01 osmode01 clayer01 polarsnapon
    polarsnapoff block01 block02 lp)

    ;-----set Layer Key - load ADG's LayerKeyStyle?????

    (setq laykeyset01 (AecGenerateLayerKey "TEST"))

    ;-----get current variables

    (setq
    scale01 (getvar "dimscale")
    osmode01 (getvar "osmode")
    clayer01 (getvar "clayer")
    )

    ;-----define polar tools

    (defun polarsnapon ()
    (setvar "autosnap" (boole 7 8 (getvar "autosnap")))
    )


    (defun polarsnapoff ()
    (setvar "autosnap" (boole 4 8 (getvar "autosnap")))
    )

    ;-----set variables

    (setq block01
    "ADG_Standards_Content/Blocks_Symbols/2005_Standards/ADG_DetailBubble01.dwg"
    block02
    "ADG_Standards_Content/Blocks_Symbols/2005_Standards/ADG_DetailBubbleArrow01.dwg")
    (setvar "attdia" 1)
    (polarsnapon)
    (setvar "osmode" 0)
    (setvar "clayer" laykeyset01)

    ;-----insert

    (prompt "\nInsertion point: ")
    (command ".-insert" block01 "scale" scale01 pause "0")
    (setq lp (getvar "lastpoint"))
    (prompt (strcat "\nRotation angle <" (rtos 0.0)">: "))
    (command ".-insert" block02 lp scale01 "" pause)

    ;-----reset vars

    (setvar "clayer" clayer01)
    (setvar "osmode" osmode01)
    (polarsnapoff)
    (princ)
    )
    -----end of file
     
    Court Myers, Jun 30, 2004
    #1
  2. Court Myers

    ECCAD Guest

    Place this in your acaddoc.lsp (or acad.lsp) for a universal error trap.
    ;;
    ;; Local Functions
    ;;
    (defun myerror (sxx)
    (if (= sxx nil)
    (setq sxx "console break")
    ); end if
    (if (not (member sxx '("console break" "Function cancelled")))
    (princ (strcat "\nError: " sxx))
    ); end if
    (if (= (member sxx '("console break" "Function cancelled")))
    (progn
    (prompt "\nError: Break...")
    (setvar "MENUECHO" 0)
    (setvar "HIGHLIGHT" 1)
    (setvar "SORTENTS" 1)
    (setvar "ATTDIA" 0); No Attribute Dialog Boxes, please
    (setvar "ATTREQ" 1); Attributes Required
    (setvar "OSMODE" 0); No OSNAP modes
    (setvar "CMDDIA" 1); Plot command dialog on
    ); end progn
    ); end if
    (setvar "CMDECHO" 0)
    (setq sxx nil)
    (princ)
    ); end function err
    (setq olderr *error* *error* myerror)
    ;;

    Tune as desired for setvar's.

    Cheers

    Bob
     
    ECCAD, Jun 30, 2004
    #2
  3. Court Myers

    Court Myers Guest

    thank you... but what if i wanted it localized to the routine itself... so
    my current layer gets put back into place.... and other vars that i might
    change..

    Court
     
    Court Myers, Jun 30, 2004
    #3
  4. Court, search this ng for the string "(*error* nil)" and you should find
    plenty of examples.

    --
    R. Robert Bell


    "Court Myers" <cmyersATadgokcDOTcomNOSPAM> wrote in message
    thank you... but what if i wanted it localized to the routine itself... so
    my current layer gets put back into place.... and other vars that i might
    change..

    Court
     
    R. Robert Bell, Jun 30, 2004
    #4
  5. Court Myers

    Phil Guest

    just for peer review and maybe your benefit I am posting my usual error
    routine.

    I don't save the old error and never have had a problem. Don't know if
    this is just lucky or it is proper. I just use the routine locally. and
    the exit at end will call the error routine


    (defun c:x ( / os )
    (defun *error* (s)
    (command)
    (command "undo" "end")
    (setvar "osmode" os) ; or whatever
    (if (/= s "exit / abort") ;; I forgot the actual message
    (princ s)
    )
    (princ)
    )

    (setq os (getvar "osmode"))
    (command "undo" "begin")

    -do routine-
    (exit) ; this will call error routine and do cleanup
    )
     
    Phil, Jul 1, 2004
    #5
  6. Court Myers

    Tom Smith Guest

    Nowadays you can declare the *error* function local to your routine, and you
    should be doing that, i.e. (defun c:x ( / *error* os ) ...)

    Also, IMHO it's more clear if you call your handler on normal completion,
    e.g. (*error* "\nDone."), rather than calling the all-purpose-crash-and-burn
    (exit), which causes an error event and therefore indirectly triggers the
    local error handler.

    By calling (exit) you're only causing a crash. You could do this just as
    well and have the same effect by calling an unknown function,
    e.g.(bogusfunction). Try it! Again, to me it's more clear to explicity
    invoke the error handler with an appropriate message, or none if desired.
     
    Tom Smith, Jul 1, 2004
    #6
  7. Court Myers

    Phil Guest

    yes, I use to do that, calling the *error* but got away from it. I also
    use to declare *error* local but quit that. It does sound safer maybe
    I better do that again. I think my error's have been acting weird,
    notations like "cmdecho" not available, or error in error-handler
    routine. Declaring it may make it behave.
     
    Phil, Jul 2, 2004
    #7
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.