Many aspects of detecting and handling dialogues and failures have been discussed in the past.
Let's look at this area again, based on two recent questions, a basic and an advanced one, and also mention some Revit 2015 enhancements to the Revit Developer Centre:
Question: Is it possible with the Revit MEP 2011 API to handle warning messages?
For instance, when batch processing files, if a warning message occurs, i.e. a modal dialogue halting the program is displayed, I would like to intercept the event, capture the warning message and disable the display of the modal dialog.
Furthermore, if warning messages can be handled, can error messages be handled via the API as well? For instance, if an error occurs within the application while processing files, can I programmatically determine the error number, take appropriate action and then cancel the modal error message that would normally be displayed interactively?
Answer: Yes, this handling of warning messages as you describe is possible, and more so, absolutely common, in three different levels of increasing complexity and power:
Here is a grand overview over all three approaches to detect and handle warning and error messages and failures.
The answer is to the second question is yes as well, and the explanation is also included in that material.
For handling errors gracefully, the Failure API is probably the most suitable approach, as illustrated by the following situation.
Question: In my add-in, editing a door family via Revit API throws an error message.
The failure API cannot resolve the error and the error dialog is shown to the user.
In Revit 2014, I was able to silently resolve the error, and no message was shown to the user.
Answer: You need to call the SetProcessingResult method on the event argument and specify ProceedWithCommit like this:
public void DoFailureProcessing( object sender, FailuresProcessingEventArgs args ) { FailuresAccessor fa = args.GetFailuresAccessor(); // Inside event handler, get all warnings IList<FailureMessageAccessor> a = fa.GetFailureMessages(); int count = 0; foreach( FailureMessageAccessor failure in a ) { TaskDialog.Show( "Failure", failure.GetDescriptionText() ); fa.ResolveFailure( failure ); ++count; } if( 0 < count && args.GetProcessingResult() == FailureProcessingResult.Continue ) { args.SetProcessingResult( FailureProcessingResult.ProceedWithCommit ); } }
The code snippet in the Revit API developer guide on handling failures does not make that call to SetProcessingResult, because it is only deleting warnings.
That works fine, because deleting a warning is an "instant" operation: leaving the event result unchanged (it defaults to "continue") produces the desired result – there are no warnings left to display.
Errors are not removed by resolveErrors – only subsequent regeneration will actually remove them. The removal may also fail – resolution is not guaranteed to succeed. So a call to SetProcessingResult with FailureProcessingResult.ProceedWithCommit is required.
As you certainly know, the most up-to-date version of the Revit SDK is always provided in the Revit Developer Centre.
Besides that, several other important learning resources provided there have now also been updated for Revit 2015:
I just noticed that the Revit Developer Centre link to the Developers Guide is currently still pointing to the Revit 2014 version.
As I mentioned when discussing What's New in the Revit 2015 API, the updated Revit 2015 API Developers Guide is already online as well, and the link on the Developer Centre needs to be updated to point to that instead.