在Visual Lisp中处理自动化错误

时间:2023-03-09 16:05:07
在Visual Lisp中处理自动化错误

Handling Automation errors in Visual LISP

翻译自原文Kean's blog:http://through-the-interface.typepad.com/through_the_interface/2006/08/handling_automa.html

自从VLisp被推广,开发者便利用它的能力优势来引用COM自动化接口。这种使LISP平台增加许多新的扩展的功能,就像你能通过召唤ObjectARX 程序来实现定义LISP函数。但是,自动操作LISP 允许开发者通过其他应用程序决定于COM的API,就像Miscrosoft Excle。

在LISP中快速标注错误处理...

传统做法是在LISP中定义erro函数,来在执行的过程中抓住错误。通过这个函数,他们可以经常报errno 的值-Autocad 使用它来告诉LISP应用程序哪种错误正在发生,这样可以使开发者明确问题的缘由。

这很好,但是这种全局错误抓取使错误之后不容易恢复程序。  

当通过自动操作接口来处理,事情会变得不同。自动处理客户端一半需要当错误发生时抓住它,而不是定义全局错误处理函数。VLisp通过一个叫vl-catch-all-apply的函数来实现的。vl-catch-all-apply是这样一个函数,不同函数引用时,他的函数名被作为内容提要并以列表的方式发送给vl-catch-all-apply函数。vl-catch-all-apply在函数被引用时,尽全力去抓取错误。apply与vl-catch-all-apply的区别就是函数的返回值,既不是函数引用的返回值,也不是错误对象。

举个不包含自动处理的例子,下面使用我最喜欢的代码要求用户输入两个数字做除法,并反馈结果。我们用(vl-catch-all-error-p)看结果是否正确,如果不正确,我们用(vl-catch-all-error-message)看错误信息。

(defun c:div (/ first second result)

  (setq first (getreal "\nEnter the first number: ")

        second (getreal "\nEnter the second number: ")

        result (vl-catch-all-apply '/ (list first second))

  )

  (if (vl-catch-all-error-p result)

    (princ (strcat "\nCaught an exception: "

                   (vl-catch-all-error-message result)

           )

    )

    (princ (strcat "\nSuccess - the result is "

                   (rtos result)

           )

    )

  )

  (princ)

)

当你运行程序时,得到以下结果

Command: div

 Enter the first number: 50

 Enter the second number: 2

 Success - the result is 25.0000

 Command: div

 Enter the first number: 50

 Enter the second number: 0

 Caught an exception: divide by zero

所以,如何把这个技术应用在自动引用上呢?让我们看另一段代码,这次引用一个函数来检查两个立体的冲突,代码定义了名为CHECKINT的命令,请求选择两个立体。假设两个立体已经选择,它会引用CheckInterference Automation方法,指定任何交集应被创建为独立的实体。

; Helper function to check whether an entity is a solid

(defun is-solid-p (ename)

  (= (cdr (assoc 0 (entget ename))) "3DSOLID")

)

 

; The CHECKINT command

(defun c:checkint (/ first second e1 e2 result)

  (vl-load-com)

  (setq first  (entsel "\nSelect the first solid: ")

        second (entsel "\nSelect the second solid: ")

  )

  (if (and first

          second

          (is-solid-p (setq e1 (car first)))

          (is-solid-p (setq e2 (car second)))

      )

    (progn

      (setq result (vl-catch-all-apply

                      'vla-CheckInterference

                      (list (vlax-ename->vla-object e1)

                            (vlax-ename->vla-object e2)

                            :vlax-true

                      )

                  )

      )

      (if (vl-catch-all-error-p result)

        (princ (strcat "\nCaught an exception: "

                      (vl-catch-all-error-message result)

               )

        )

        (progn

          (princ "\nSuccess!")

          ; Highlight the newly created intersection solid

          (vla-Highlight result :vlax-true)

          (vlax-release-object result)

       )

      )

    )

    (princ "\nMust select two solids.")

  )

  (princ)

)