- UID
- 14
- 积分
- 8264
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-4
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
- ;;;出错处理例程
- ;; (catch-error <Protected> <OnError>)
- ;;
- ;; (catch-error
- ;; '(lambda () <code to protect>...)
- ;; '(lambda (error) <error handling code>...)
- ;; )
- ;;
- ;; A wrapper for vl-catch-all-xxxxxxx
- ;;
- ;; This function provides a high-level exception-handling
- ;; construct that allows the programmer to factor out some
- ;; of the testing and flow control required when using the
- ;; vl-catch-all-xxxxx functions directly (which is messy).
- ;;
- ;; (catch-error) takes two arguments, both of which are
- ;; quoted function names or lambda lists:
- ;;
- ;; <Protected> is a function that takes no arguments
- ;; (usually a lambda list). It is evaluated, and if an
- ;; error occurs during its evaluation, then <OnError> is
- ;; called and passed the error string.
- ;;
- ;; <OnError> is a function that is called only if an error
- ;; occurs during the evaluation of <Protected>. In that case,
- ;; the message returned by (vl-catch-all-error-message) is
- ;; passed to <OnError> as its only argument.
- ;;
- ;; If no error occurs during evaluation of <Protected>, then
- ;; (catch-error) returns the result of <Protected>. If an error
- ;; occurs during evaluation of <Protected>, then (catch-error)
- ;; returns the result of <OnError> if supplied. If an error
- ;; occurs and <OnError> is not supplied (e.g., nil), then
- ;; (catch-error) returns the Visual LISP error object that
- ;; triggered the error.
- ;;
- ;; By default, errors are always handled. The only way to
- ;; re-raise the error is to call (exit) from within <OnError>,
- ;; and is generally what should be done in cases where the
- ;; error is unknown or the code in the <OnError> error handler
- ;; is not equipped to deal with a particular type of error.
- ;;
- ;; Note that calls to (catch-error) can be nested, such that
- ;; if a nested call does not handle an error (by calling exit
- ;; or vl-exit-with-error), then one or more outer calls can
- ;; also examine and/or handle the error, with the caveat that
- ;; the error message is not the same as the original one, if
- ;; you use (exit). If you use (vl-exit-with-error), then the
- ;; error message is correct, but in that case the outer most
- ;; call to (catch-all) must handle the error because it cannot
- ;; call (exit) to invoke the *error* function.
- ;;
- ;; In essence, you can think of each call to
- ;; catch error to be a temporary and locally-bound
- ;; redefinition of the *error* function (where the
- ;; <OnError> argument represents the body of *error*),
- ;; with the added benefit of being able to resume
- ;; execution if desired.
- ;;
- ;;
- ;; Example:
- ;;
- ;; The following code obtains a point or the
- ;; option keyword "foo", and traps the error
- ;; that occurs if the user responds with the
- ;; option keyword.
- ;;
- ;; If the error is not a result of the user
- ;; entering the keyword, then something else
- ;; has gone wrong. In that case, the error
- ;; handler displays the error message, and
- ;; aborts by calling (exit).
- ;;
- ;; If no error occurs, then the error handler
- ;; is not called, and the user-supplied point
- ;; is assigned to Result
- ;;
- ;; (defun C:CATCH-ERROR-TEST ()
- ;;
- ;; (setq eKeyWordInput
- ;; "Automation Error. User input is a keyword"
- ;; )
- ;;
- ;; (setq Util
- ;; (vla-get-utility
- ;; (vla-get-activeDocument
- ;; (vlax-get-acad-object)
- ;; )
- ;; )
- ;; )
- ;;
- ;;
- ;; (vla-InitializeUserInput Util 0 "Foo")
- ;;
- ;; (setq result
- ;; (catch-error
- ;; '(lambda ()
- ;; (vla-Getpoint
- ;; Util nil "Enter point/Foo: "
- ;; )
- ;; )
- ;; '(lambda (error)
- ;; (if (eq error eKeyWordInput)
- ;; (vla-GetInput Util)
- ;; (progn
- ;; (alert (strcat "Error: " errmsg))
- ;; (exit)
- ;; )
- ;; )
- ;; )
- ;; )
- ;; )
- ;;
- ;; (if (eq (type Result) 'Str)
- ;; (princ "\nUser entered 'FOO'")
- ;; (princ "\nUser entered a point")
- ;; )
- ;;
- ;; (princ)
- ;;
- ;; )
- ;;
- (defun thisdrawing ()
- (vlax-get-property (vlax-get-acad-object) 'activedocument)
- )
- (defun catch-error (catch:Protected catch:OnError / catch:err)
- (setq catch:err (vl-catch-all-apply catch:Protected))
- (if (and (vl-catch-all-error-p catch:err) catch:OnError)
- (Apply catch:OnError (list (vl-catch-all-error-message catch:err)))
- catch:err
- ) ;_ end of if
- ) ;_ end of defun
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (defun DefaultOnError (s) ;by Qiu Feng
- (if
- (not (member s '("Function cancelled" "console break" "quit / exit abort")))
- (princ (strcat "\nError: " s))
- )
- (princ)
- )
- ;;;ChangeVars
- ;;;修改指定系统变量并返回其先前值
- ;;;参数
- ;;;一个包含要更改的系统变量和他们的新值的点对列表
- ;;;示例
- ;;;(setq ret (changevars '(("filedia" . 0)("cmdecho" . 0)("osmode" . 512))))
- ;;;注意
- ;;;CHANGEVARS 返回一个包含所有指定的系统变量和他们先前值的列表。要恢复他们,可简单地将该返回的列表提供给CHANGEVARS 函数。
- (defun changevars (lst)
- (mapcar '(lambda (x / tmp var)
- (setq tmp (cons (car x)
- (if (= (type (setq var (getvar (car x)))) 'list)
- (list var)
- var
- )
- )
- ) ;_ end of setq
- (setvar (car x)
- (if (= (type (cdr x)) 'list)
- (cadr x)
- (cdr x)
- )
- )
- tmp
- ) ;_ end of lambda
- lst
- ) ;_ end of mapcar
- ) ;_ end of defun
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;
- ;;; Error routine.
- ;;;
- (defun ai_error (s) ; If an error (such as CTRL-C) occurs
- (if (not (member s '("Function cancelled" "console break")))
- (princ (strcat "\nError: " s))
- )
- (vla-endundomark (thisdrawing)) ; Deal with UNDO
- (if old_error
- (setq *error* old_error)
- ) ; Restore old *error* handler
- (if old_cmd
- (setvar "cmdecho" old_cmd)
- ) ; Restore cmdecho value
- (princ)
- )
- ;;; ====================================================================
- ;;; 出错处理包装函数: ErrorWrap
- ;;; 用法: (ErrorWrap MainFunction SysVariableListToProtect ErrorHandle)
- ;;; 例如: (ErrorWrap 'mymain '("osmode" "clayer" "highlight") 'myhandle)
- ;;; 如果没有给出errorhandle, 则用一个默认的处理程序代替
- ;;; 完整示例:
- ;;; (defun c:testErrorWrap ()
- ;;; (ErrorWrap
- ;;; '(lambda (/ pt1 pt2 pt3) ; main program function
- ;;; (setvar "osmode" 0)
- ;;; (setvar "orthomode" 0)
- ;;; (setq pt1 (getpoint "specify point 1:"))
- ;;; (setq pt2 (getpoint pt1 "specify point 2:"))
- ;;; (setq pt3 (getpoint pt2 "specify point 3:"))
- ;;; (command "_line" pt1 pt2 pt3 "close")
- ;;; )
- ;;; '("osmode" "orthomode") ; system variable to protect
- ;;; '(lambda (s) ; error handle, nil for default
- ;;; (alert (strcat "错误:" s))
- ;;; )
- ;;; )
- ;;; )
- ;;; 默认的出错处理函数为
- ;;; (defun DefaultOnError (s)
- ;;; (if
- ;;; (not (member s
- ;;; '("Function cancelled"
- ;;; "console break"
- ;;; "quit / exit abort"
- ;;; )
- ;;; )
- ;;; )
- ;;; (princ (strcat "\nError: " s))
- ;;; )
- ;;; (princ)
- ;;; )
- ;;; 说明:此函数在 Visual LISP 的下运行
- ;;; 作者:秋枫,19-Sep-02
- ;;; ====================================================================
- (defun ErrorWrap (EW:Main EW:SysVarList EW:OnError /
- undo_on undo_off undo_push undo_pop
- undo_setting undo_init catch-error DefaultOnError
- modes moder mlst old_cmd
- )
-
- (defun catch-error (catch:Protected catch:OnError / catch:err)
- (setq catch:err (vl-catch-all-apply catch:Protected))
- (if (and (vl-catch-all-error-p catch:err) catch:OnError)
- (Apply catch:OnError (list (vl-catch-all-error-message catch:err)))
- catch:err
- ) ;_ end of if
- ) ;_ end of defun
- (defun DefaultOnError (s) ;by Qiu Feng
- (if
- (not (member s '("Function cancelled" "console break" "quit / exit abort"))
- )
- (princ (strcat "\nError: " s))
- )
- (princ)
- )
- (defun modes (a)
- (setq MLST nil)
- (repeat (length a)
- (setq MLST (append MLST (list (list (car a) (getvar (car a))))))
- (setq a (cdr a))
- )
- )
- (defun moder ()
- (repeat (length MLST)
- (setvar (caar MLST) (cadar MLST))
- (setq MLST (cdr MLST))
- )
- )
- ;; ErrorWrap Main
- ;; ====================================
- (setq old_cmd (getvar "cmdecho"))
- (setvar "cmdecho" 0)
- (modes EW:SysVarList)
- (vla-startundomark (thisdrawing))
- (setvar "cmdecho" old_cmd)
- (if EW:OnError
- (catch-error EW:Main EW:OnError)
- (catch-error EW:Main 'DefaultOnError)
- )
- (setvar "cmdecho" 0)
- (vla-endundomark (thisdrawing))
- (moder)
- (setvar "cmdecho" old_cmd)
- (princ)
- ;; ====================================
- )
|
评分
-
查看全部评分
|