AutoLisp-Escape exception handling

I have an AutoLisp function that I have wrote that essentially is an endless loop, until the user hits the Escape key to exit.

The problem is that certain settings to the OSNAP, text size, etc. that are made when the function runs and not reset when the user hits the escape key and exits.

Is there a way to trap the escape key so that a handler routine can be run that will reset the users settings before exit?

Thanks Jarvis

Reply to
jarvis
Loading thread data ...

You need to add error trapping to your lisp. Here's an example from one of mine ...

Posting may scramble it a bit, email me if desired and I'll email you a complete copy of my template.lsp with the Error Handler.

;; ;; **************************************** ;; * == Error Handler == * ;; * * ;; **************************************** ;; ;; (defun ETRAP (msg) (if (or (= msg "Function cancelled") ; If user cancelled (= msg "quit / exit abort") ; If user aborted ) ; End or sequence (princ) ; Exit quietly (princ (strcat "\nError: " msg)) ; Otherwise report error message ) ; End if sequence ;; ;; **** Variable Resets **** ;; (setvar "cmddia" CCDI) ; Reset cmddia variable (setvar "cmdecho" CCME) ; Reset cmdecho variable (setvar "cecolor" CCOL) ; Reset color variable (setvar "dimscale" CDMS) ; Reset dimscale variable (setvar "filedia" CFDI) ; Reset fiedia variable (setvar "celtype" CLTY) ; Reset linetype variable (setvar "clayer" CLAY) ; Reset layer variable (setvar "orthomode" CORM) ; Reset ortho variable (setvar "osmode" COSM) ; Reset osnaps variable (setvar "snapang" CSAN) ; Reset snapang variable ;; ;; **** Screen Prompts **** ;; (prompt "\n") ; Clear text line (princ (strcat CLFN " Terminated")) ; Notify Operator (setq *error* PERROR) ; Reset Previous Error Function (princ) ; Nil supression ) ; End ETRAP ;; ;; ;; **************************************** ;; * == End Error Handler == * ;; * * ;; **************************************** ;; ;; ;; ;; **************************************** ;; * == Main Function == * ;; * * ;; **************************************** ;; (defun c:Template ( / CLFN) ;; ;; **** Sets general conditions **** ;; (setq PERROR *error*) ; Get previous error (setq *error* ETRAP) ; Set error trap (setq CLFN "Template") ; Set current lisp function name ;; (setq CCDI (getvar "cmddia")) ; Get cmddia setting (setq CCME (getvar "cmdecho")) ; Get cmdecho setting (setq CCOL (getvar "cecolor")) ; Get current color (setq CDMS (getvar "dimscale")) ; Get dimscale setting (setq CFDI (getvar "filedia")) ; Get filedia setting (setq CLTY (getvar "celtype")) ; Get current linetype (setq CLAY (getvar "clayer")) ; Get current layer (setq CORM (getvar "orthomode")) ; Get ortho setting (setq COSM (getvar "osmode")) ; Get osnap settings (setq CSAN (getvar "snapang")) ; Get snapang setting ;; (setvar "cmdecho" 0) ; Turn cmdecho off ;;

Reply to
Chip Harper

You can put this in your basic program: (setq os (getvar "osmode")) and then in your error handler, put this: (setvar "osmode" os)

For example: (defun c:thefunction ();os is global - not local (setq os (getvar "osmode"));not in the loop (while the loop exists (Do the loop) ) (princ) )

If you put (setq os (getvar "osmode")) in the loop, it will constantly change. That's no good. It has to occur before the loop and independent of it.

Keep in touch Bill DeShawn

formatting link

Reply to
Bill DeShawn

Thanks guys I think that will get me going Jarv

Reply to
jarvis

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.