在 Lisp 退出时执行代码
Execute Code On Lisp Exit
我写了一个 lisp,涉及设置变量,然后在循环内选择点。一旦我决定完成选择点,我希望能够将该变量恢复到我按下转义键时的原始状态。
例如
(defun c:df ()
(setq oom (getvar "osmode")) ;store current state
(setq type(getint "\nEnter Type: 1 For Horizontal, 2 For Vertical : "))
(setq startpt (getpoint "\nChoose Start Point : "))
(setq ptx (+ (nth 0 startpt)10))
(setq pty (+ (nth 1 startpt)10))
(setvar "osmode" 2); change state state
(while
(setq nextpt (getpoint "'Pick Mid: ")) ;make selection
(if (null nextpt) ((princ "\nNull Value Error.") return))
(if (= type 1) (command "dimlinear" startpt nextpt "H" (list 0 pty) ))
(if (= type 2) (command "dimlinear" startpt nextpt "V" ptx ))
(setq ptx (+ 5 ptx))
(setq pty (+ 5 pty))
)
;do after escape key is pressed.
(setvar "osmode" oom) ;revert state back to original.
)
我找到了与 "User Input Errors" 有关的可能线索,但无法真正发挥作用。以我的轻描淡写,当按下转义时,lisp 只是退出并且没有完成执行。
提前致谢。
AutoLISP 将取消视为错误。
因此,您可以通过错误处理来管理取消。 AutoLISP 提供了可以在本地重新定义的 *error*
函数。
另外,我想提几点建议:
不要对变量使用 type
符号,它是内置 AutoLISP 函数的名称
在本地声明变量(必须是 *error*
函数)
使用 getkword
and initget
让用户选择一个选项。
(defun c:df (/ *error* oom option startpt ptx pty nextpt) ; local variables
;; *error* local redefinition
(defun *error* (msg)
(if (/= msg "Function cancelled")
(princ (strcat "\nError: " msg))
)
(if oom
(setvar "osmode" oom)
)
(princ)
)
(setq oom (getvar "osmode")) ;store current state
(initget 1 "Horizontal Vertical")
(setq option (getkword "\nChoose an option [Horizontal/Vertical]: "))
(if (setq startpt (getpoint "\nChoose Start Point : "))
(progn
(setq ptx (+ (car startpt) 10))
(setq pty (+ (cadr startpt) 10))
(setvar "osmode" 2) ; change state state
(while (setq nextpt (getpoint "'Pick Mid: ")) ;make selection
(if (= option "Horizontal")
(command "_dimlinear" startpt nextpt "H" (list 0 pty))
(command "_dimlinear" startpt nextpt "V" (list ptx 0))
)
(setq ptx (+ 5 ptx))
(setq pty (+ 5 pty))
)
(setvar "osmode" oom) ;revert state back to original.
)
)
(princ)
)
我写了一个 lisp,涉及设置变量,然后在循环内选择点。一旦我决定完成选择点,我希望能够将该变量恢复到我按下转义键时的原始状态。 例如
(defun c:df ()
(setq oom (getvar "osmode")) ;store current state
(setq type(getint "\nEnter Type: 1 For Horizontal, 2 For Vertical : "))
(setq startpt (getpoint "\nChoose Start Point : "))
(setq ptx (+ (nth 0 startpt)10))
(setq pty (+ (nth 1 startpt)10))
(setvar "osmode" 2); change state state
(while
(setq nextpt (getpoint "'Pick Mid: ")) ;make selection
(if (null nextpt) ((princ "\nNull Value Error.") return))
(if (= type 1) (command "dimlinear" startpt nextpt "H" (list 0 pty) ))
(if (= type 2) (command "dimlinear" startpt nextpt "V" ptx ))
(setq ptx (+ 5 ptx))
(setq pty (+ 5 pty))
)
;do after escape key is pressed.
(setvar "osmode" oom) ;revert state back to original.
)
我找到了与 "User Input Errors" 有关的可能线索,但无法真正发挥作用。以我的轻描淡写,当按下转义时,lisp 只是退出并且没有完成执行。
提前致谢。
AutoLISP 将取消视为错误。
因此,您可以通过错误处理来管理取消。 AutoLISP 提供了可以在本地重新定义的 *error*
函数。
另外,我想提几点建议:
不要对变量使用
type
符号,它是内置 AutoLISP 函数的名称在本地声明变量(必须是
*error*
函数)使用
getkword
andinitget
让用户选择一个选项。(defun c:df (/ *error* oom option startpt ptx pty nextpt) ; local variables ;; *error* local redefinition (defun *error* (msg) (if (/= msg "Function cancelled") (princ (strcat "\nError: " msg)) ) (if oom (setvar "osmode" oom) ) (princ) ) (setq oom (getvar "osmode")) ;store current state (initget 1 "Horizontal Vertical") (setq option (getkword "\nChoose an option [Horizontal/Vertical]: ")) (if (setq startpt (getpoint "\nChoose Start Point : ")) (progn (setq ptx (+ (car startpt) 10)) (setq pty (+ (cadr startpt) 10)) (setvar "osmode" 2) ; change state state (while (setq nextpt (getpoint "'Pick Mid: ")) ;make selection (if (= option "Horizontal") (command "_dimlinear" startpt nextpt "H" (list 0 pty)) (command "_dimlinear" startpt nextpt "V" (list ptx 0)) ) (setq ptx (+ 5 ptx)) (setq pty (+ 5 pty)) ) (setvar "osmode" oom) ;revert state back to original. ) ) (princ) )