如何从 WPF 应用程序在 Revit API 中引发外部事件?
How do I raise an external event in the Revit API from a WPF app?
我一直在尝试创建一个 Revit 插件,允许用户在文件的本地版本上查看存储在远程服务器上的问题。它应该从服务器上存储的数据创建一个新的 3D 透视图,并在 Revit 中打开它。但是,每当我尝试 运行 它时,我都会收到异常警告:
Exception thrown: 'Autodesk.Revit.Exceptions.InvalidOperationException' in RevitAPIUI.dll
An unhandled exception of type 'Autodesk.Revit.Exceptions.InvalidOperationException' occurred in RevitAPIUI.dll
Attempting to create an ExternalEvent outside of a standard API execution
我想我模糊地理解这意味着什么,但我不确定到底需要更改什么才能修复它。我正在定义自定义 ExternalEventHandler 并实现其 Execute 方法:
class CameraEventHandler : IExternalEventHandler
{
Issue issue;
int i;
public CameraEventHandler(Issue issue, int index)
{
this.issue = issue;
this.i = index;
}
public void Execute(UIApplication app)
{
Document doc = app.ActiveUIDocument.Document;
using (Transaction t = new Transaction(doc, "CameraTransaction"))
{
t.Start();
...
//Irrelevant code to set camera position programmatically
...
t.Commit();
}
}
public string GetName()
{
return "Camera event handler";
}
}
然后在我的一个 WPF 表单中,我创建了一个 ExternalEvent 并调用了 Raise 方法:
private void RevitViewButton_Click(object sender, RoutedEventArgs e)
{
CameraEventHandler handler = new CameraEventHandler(issue, issueIndex);
ExternalEvent cameraEvent = ExternalEvent.Create(handler);
cameraEvent.Raise();
}
但是,到达ExternalEvent.Create方法时抛出异常
编辑:我觉得值得一提的是,我使用的 WPF 应用程序是作为 Revit 插件启动的。
阅读 this blog 它似乎是 Revit 中的错误。
解决方案似乎是在 IExternalCommand.Execute
或 IExternalApplication.OnStartup
期间创建您的自定义处理程序,而不是在引发事件时创建。
Revit API 不能在有效的 Revit API 上下文之外使用:
这不是错误,Gaz,这是设计使然。
Gaz建议的方案完全正确!
我一直在尝试创建一个 Revit 插件,允许用户在文件的本地版本上查看存储在远程服务器上的问题。它应该从服务器上存储的数据创建一个新的 3D 透视图,并在 Revit 中打开它。但是,每当我尝试 运行 它时,我都会收到异常警告:
Exception thrown: 'Autodesk.Revit.Exceptions.InvalidOperationException' in RevitAPIUI.dll
An unhandled exception of type 'Autodesk.Revit.Exceptions.InvalidOperationException' occurred in RevitAPIUI.dll
Attempting to create an ExternalEvent outside of a standard API execution
我想我模糊地理解这意味着什么,但我不确定到底需要更改什么才能修复它。我正在定义自定义 ExternalEventHandler 并实现其 Execute 方法:
class CameraEventHandler : IExternalEventHandler
{
Issue issue;
int i;
public CameraEventHandler(Issue issue, int index)
{
this.issue = issue;
this.i = index;
}
public void Execute(UIApplication app)
{
Document doc = app.ActiveUIDocument.Document;
using (Transaction t = new Transaction(doc, "CameraTransaction"))
{
t.Start();
...
//Irrelevant code to set camera position programmatically
...
t.Commit();
}
}
public string GetName()
{
return "Camera event handler";
}
}
然后在我的一个 WPF 表单中,我创建了一个 ExternalEvent 并调用了 Raise 方法:
private void RevitViewButton_Click(object sender, RoutedEventArgs e)
{
CameraEventHandler handler = new CameraEventHandler(issue, issueIndex);
ExternalEvent cameraEvent = ExternalEvent.Create(handler);
cameraEvent.Raise();
}
但是,到达ExternalEvent.Create方法时抛出异常
编辑:我觉得值得一提的是,我使用的 WPF 应用程序是作为 Revit 插件启动的。
阅读 this blog 它似乎是 Revit 中的错误。
解决方案似乎是在 IExternalCommand.Execute
或 IExternalApplication.OnStartup
期间创建您的自定义处理程序,而不是在引发事件时创建。
Revit API 不能在有效的 Revit API 上下文之外使用:
这不是错误,Gaz,这是设计使然。
Gaz建议的方案完全正确!