PDA

View Full Version : Error Handling



jwf
2004-12-02, 12:20 AM
Hopefully someone can help. We currently use our acaddoc.lsp to load all of our lisp. Within this doc there are two routines as well. The first (initerr) and the second (reset).
INITERR stores some common variables. RESET will restore all of the stored variables in the initerr. These two commands run at the beginning(initerr) and at the end(reset) of our routines.
The problem is, if you esc in the middle of a routine it will not restore your variables because you never reach the RESET command in that routine.

(defun initerr ()
(setq oldlayer (getvar "clayer"))
(setq oldsnap (getvar "osmode"))
(setq oldortho (getvar "orthomode"))
(setq oldang (getvar "snapang"))
(setq oldcol (getvar "cecolor"))
)

(defun reset ()
(setvar "clayer" oldlayer)
(setvar "osmode" oldsnap)
(setvar "orthomode" oldortho)
(setvar "snapang" oldang)
(setvar "dwgcheck" 1)
(setvar "cecolor" oldcol)
(princ)
)

Those are the command in my acaddoc.lsp

A sample Routine is:

;DLEADER.LSP bgceDotLeader (c)2004, Dragon Slayer
(defun c:DLEADER (/ *error*)
(defun *Error* (msg)
(reset)
(print "Error: ")
(print msg)
(princ)
)
(initerr)
(graphscr)
(setvar "CMDECHO" 0)
(setq z3 (getvar "texteval"))
(setq z4 (getvar "clayer"))
(setq z5 (getvar "cecolor"))
(setq z6 (getvar "celtype"))
(setq t1 (substr z4 1 1))
(if (or (= t1 "M") (= t1 "F")) (setvar "clayer" "M-TEXT") ())
(if (= t1 "E") (setvar "clayer" "E-TEXT") ())
(if (= t1 "P") (setvar "clayer" "P-TEXT") ())
(if (and (and (and (/= t1 "M") (/= t1 "P")) (/= t1 "E")) (/= (tblsearch "layer" "G-ANNO-TEXT") nil)) (setvar "clayer" "G-ANNO-TEXT") ())
(setvar "celtype" "BYLAYER")
(setvar "cecolor" "BYLAYER")
(setvar "dimclrd" 256)
(setvar "osmode" 0)
(setvar "orthomode" 0)
(setvar "dimasz" 0.054)
(setvar "texteval" 1)
(COMMAND "DIMLDRBLK" "dot")
(setq ss nil)
(setvar "dimclrd" 7)
(setvar "osmode" 512)
(setq pt1 (getpoint "Select Arrowhead Point (Snap to line: "))
(if (/= (setq ss (ssget pt1)) nil)
(progn
(setq lobj (ssname ss 0))
(setq ldata (entget lobj))
(if (/= (setq objcol (cdr (assoc 62 ldata))) nil)
(setq objcol (cdr (assoc 62 ldata)))
(progn
(setq objlay (cdr (assoc 8 ldata)))
(SETQ LAYERLIST (TBLSEARCH "LAYER" OBJLAY))
(SETQ OBJCOL (CDR (ASSOC 62 LAYERLIST)))
);progn
);if
(if (or (= objcol 1) (= objcol 5))
(setvar "dimasz" (* (getvar "dimasz") 1.5))
)if
);progn
);if
(acet-ql-set '((63 . 1)(61 . 0)(71 . 1)(68 . 1)(69 . 1)(68 . 0)(60 . 4)))
(command "qleader" PT1 pause pause )
(SETQ CURTXT (ENTLAST))
(SETQ TXTDATA (ENTGET CURTXT))
(Setvar "texteval" z3)
(setvar "cecolor" z5)
(setvar "celtype" z6)
(reset)
)
;end dLEADER.lsp

How can I make it so when I esc in the middle it will default to my stored variables. The trick is I would to do it in my ACADDOC.LSP. I do not want to change 100's of routines. We are a MEP Consulting firm. Our menus have been customized alot during the past 7 years. Many lisp routines have been created.

Thanks for any help I receive

jaberwok
2004-12-02, 09:54 AM
Your individual routines do not contain a custom error handler so "reset" never gets called.

This is the (old-fashioned) standard way to add an error handler -

(defun myerror (s) ; If an error (such as CTRL-C) occurs
; while this command is active...
(if (/= s "Function cancelled")
(princ (strcat "\nError: " s))
)
(setvar "cmdecho" ocmd) ; Restore saved modes
(setvar "blipmode" oblp)
(setq *error* olderr) ; Restore old *error* handler
(princ)
)

So you would need something along the lines of -

(defun myerror (s) ; If an error (such as CTRL-C) occurs
;while this command is active...
(if (/= s "Function cancelled")
(reset))
)

- added to each routine.

I'm not a great programmer so don't take this code too literaly.

RobertB
2004-12-03, 07:42 PM
In addition to the edits I made directly to your code, I wanted to attach a copy of an ATP I did a few years ago.