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
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.