本帖最后由 叶曲冰寒 于 2015-6-7 21:33 编辑
关于命令是否存在的问题之前在网上找了很多地方都没完全搞定。关于命令是否存在的判断结合众牛人的看法,和自己的总结写出来供各位看官瞅瞅。此函数能够判断CAD内置命令和用户定义的命令。
 - ;;功 能:命令是否存在
- ;;参 数:命令字符串
- ;;返回值:存在为T,否则为nil
- (defun isCommandExist(commandstr / cAndCommandStr isexist myvalue)
- (setq cAndCommandStr (strcat "c:" commandStr))
- (if (equal (type (eval (read cAndCommandStr))) 'subr)
- ;;说明是用lisp的defun定义的命令
- (progn
- (setq isExist T)
- (print "命令存在!")
- )
- ;;不是lisp中defun定义的命令
- (progn
- (if
- ;;if判断的条件
- (not
- ;;捕捉错误,错误存在为T,不存在为FALSE
- (vl-catch-all-error-p
- (setq myvalue
- ;;执行语句,并且用vl-catch-all-apply捕捉错误
- (vl-catch-all-apply '(lambda (x) (progn (command x) (setq lastCommand (getvar "LASTPROMPT")) (if (not (wcmatch lastCommand "*未知命令*")) (command "ESC")))) (list commandStr))
- )
- )
- )
- ;;if条件后的第一条语句
- ;;如果执行出错就执行下面这句
- (progn
- (setq isExist nil)
- (print "命令不存在!")
- )
- ;;if条件后的第二条语句
- ;;如果执行出错,就捕捉错误,执行下面这句
- (progn
- (setq isExist T)
- (print "命令存在!")
- )
- );;end if
- ;;取消命令操作
- (if isExist
- (progn
- (command)
- )
- );;end if
- );;end progn
- );;end if
-
- ;;输出值,命令存在时是T,否则是nil
- isExist
- )
|