处理打开 Revit 文件时的失败(在任何事务之前)
我正在用 C# 开发一个 Revit 插件,用于从 Revit 文件导出数据。该插件通过设计自动化 API 使用(显然,没有用户界面)。对于某些 RVT 文件,整个过程运行良好。不幸的是,对于其他一些文件,在任何事务开始之前,文档打开都会失败,因此我正在努力了解如何捕获这些打开错误(并可能删除相应的失败元素),类似于官方文档内部使用 IFailuresPreprocessor 的交易中建议的内容。
有人对如何处理文档打开过程中的错误有建议吗?抱歉,如果我错过了一些明显的事情。谢谢!
此处提供了部分 C# 代码(基本代码,如设计自动化教程中的代码)以及错误日志。
if (data == null) throw new ArgumentNullException(nameof(data));
Application rvtApp = data.RevitApp;
if (rvtApp == null) throw new InvalidDataException(nameof(rvtApp));
string modelPath = data.FilePath;
if (String.IsNullOrWhiteSpace(modelPath)) throw new InvalidDataException(nameof(modelPath));
Document doc = data.RevitDoc;
if (doc == null) throw new InvalidOperationException("Could not open document.");
using (Transaction transaction = new Transaction(doc))
{
...
}
来自 Design Automation API 的日志报告:
[03/18/2022 10:18:46] Initialize and get RCE: (VersionBuild) 22.1.21.13 (VersionNumber) 2022 (SubVersionNumber) 2022.1.2
[03/18/2022 10:31:11] Failure #0: FailureDefinitionId-'0d5f227d-a4fd-4bc2-b539-1a13cd9a9173', Severity-'Error', Description-'Line is too short.', Resolution-'Delete Element(s)'.
[03/18/2022 10:31:11] Failure: Unable to continue because of posted errors. Rolling back transaction.
[03/18/2022 10:31:17] Autodesk.Revit.Exceptions.OperationCanceledException: Opening was canceled.
[03/18/2022 10:31:17] at Autodesk.Revit.ApplicationServices.Application.OpenDocumentFile(ModelPath modelPath, OpenOptions openOptions)
[03/18/2022 10:31:17] at DesignAutomationFramework.DesignAutomationData..ctor(Application revitApp, String mainModelPath)
[03/18/2022 10:31:17] at DesignAutomationFramework.DesignAutomationReadyEventArgs..ctor(Application revitApp, String mainModelPath)
[03/18/2022 10:31:17] at DesignAutomationFramework.DesignAutomationBridge.SetDesignAutomationReady(Application revitApp, String mainModelPath)
[03/18/2022 10:31:17] at RevitCoreEngineTest.RceConsoleApplication.Program.UserMain(CommandLineArgs cl)
[03/18/2022 10:31:19] RESULT: Failure - Result of running user app is failure
[03/18/2022 10:31:19] Finished running. Process will return: TestError
I'm developing a Revit plugin in C# for exporting data from Revit files. This plugin is used through the Design Automation API (i.e. without user interface, obviously). The whole process works fine for some RVT files. Unfortunately, for some other files, the document opening fails before any transaction has started, so I'm struggling to see how I can catch these opening errors (and possibly delete the corresponding failed elements), similarly to what is suggested in the official documentation inside transactions using IFailuresPreprocessor.
Does anyone have suggestion on how to handle errors during the document opening itself? Sorry if I miss something obvious. Thanks!
Part of the C# code is available here (basic code, like in the Design Automation tutorials), as well as the error logs.
if (data == null) throw new ArgumentNullException(nameof(data));
Application rvtApp = data.RevitApp;
if (rvtApp == null) throw new InvalidDataException(nameof(rvtApp));
string modelPath = data.FilePath;
if (String.IsNullOrWhiteSpace(modelPath)) throw new InvalidDataException(nameof(modelPath));
Document doc = data.RevitDoc;
if (doc == null) throw new InvalidOperationException("Could not open document.");
using (Transaction transaction = new Transaction(doc))
{
...
}
Log report from Design Automation API:
[03/18/2022 10:18:46] Initialize and get RCE: (VersionBuild) 22.1.21.13 (VersionNumber) 2022 (SubVersionNumber) 2022.1.2
[03/18/2022 10:31:11] Failure #0: FailureDefinitionId-'0d5f227d-a4fd-4bc2-b539-1a13cd9a9173', Severity-'Error', Description-'Line is too short.', Resolution-'Delete Element(s)'.
[03/18/2022 10:31:11] Failure: Unable to continue because of posted errors. Rolling back transaction.
[03/18/2022 10:31:17] Autodesk.Revit.Exceptions.OperationCanceledException: Opening was canceled.
[03/18/2022 10:31:17] at Autodesk.Revit.ApplicationServices.Application.OpenDocumentFile(ModelPath modelPath, OpenOptions openOptions)
[03/18/2022 10:31:17] at DesignAutomationFramework.DesignAutomationData..ctor(Application revitApp, String mainModelPath)
[03/18/2022 10:31:17] at DesignAutomationFramework.DesignAutomationReadyEventArgs..ctor(Application revitApp, String mainModelPath)
[03/18/2022 10:31:17] at DesignAutomationFramework.DesignAutomationBridge.SetDesignAutomationReady(Application revitApp, String mainModelPath)
[03/18/2022 10:31:17] at RevitCoreEngineTest.RceConsoleApplication.Program.UserMain(CommandLineArgs cl)
[03/18/2022 10:31:19] RESULT: Failure - Result of running user app is failure
[03/18/2022 10:31:19] Finished running. Process will return: TestError
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
警告吞噬者 已成功用于 Forge Design Automation for Revit。您还可以查看 The Building Coder 或 检测和处理 的其他建议对话和失败,以及 DA4R – Revit 设计自动化。它包括针对 DA4R 调整警告吞噬器的演示。
The warning swallower has been used successfully in Forge Design Automation for Revit. You can also check out the other suggestions by The Building Coder or Detecting and Handling Dialogues and Failures, as well as the topic group on DA4R – Design Automation for Revit. It includes a demonstration of adapting the warning swallower for DA4R.