在 ObjectARX 中,有一个名为 acedDisableDefaultARXExceptionHandler 的函数。您可以尝试 P/Invoke 它。
// EntryPoint may vary across autocad versions
[DllImport("acad.exe", EntryPoint = "?acedDisableDefaultARXExceptionHandler@@YAXH@Z")]
public static extern void acedDisableDefaultARXExceptionHandler(int value);
您也可以尝试 System.Windows.Forms.Application.ThreadException:http ://through-the-interface.typepad.com/through_the_interface/2008/08/ catching-except.html
最简单的方法是将所有代码包装在 try/catch 块中。在 AutoCAD 中,有两种执行代码的方法:
用一个命令
为避免重复代码,请声明如下接口:
public interface ICommand
{
void Execute();
}
然后将其用于您的命令:
public class MyCommand : ICommand
{
void Execute()
{
// Do your stuff here
}
}
在定义命令的类中,使用此通用方法执行:
void ExecuteCommand<T>() where T : ICommand
{
try
{
var cmd = Activator.CreateInstance<T>();
cmd.Execute();
}
catch (Exception ex)
{
Log(ex);
}
}
现在您的命令如下所示:
[CommandMethod("MYCMD", CommandFlags.Modal)]
public void MyCommand()
{
ExecuteCommand<MyCommand>();
}
在事件处理程序中
在这种情况下,由于您需要事件参数,只需将代码直接包装在 try/catch 中即可。