I am in full blast preparing and updating material for the Revit API training in Munich starting the day after tomorrow, as well as the upcoming AEC DevCamp conference and another event that I have not even mentioned here yet:
Revit 2013 API Webcast
We will be holding a webcast on the Revit 2013 API on May 17, Ascension Day (register). A list of all the ADN DevTech training opportunities is provided by the Training Course Schedule, entering "Revit API" as the course topic. I'll write something about the webcast topics anon.
Another important piece of news from the ADN team is the launch of a new series of blogs:
ADN DevBlogs
The Autodesk Developer Network ADN team started the launch of a series of new blogs, marking a significant shift from material that's only available to ADN members, like technical solutions known as DevNotes on the ADN extranet, to creating material that's publicly available to everyone. The first two are up and running now:
More ADN DevBlogs for AEC, Manufacturing and Media & Entertainment will coming soon. For more information, please refer to Stephen Preston's welcome post.
Back to the nuts and bolts of the Revit API, here is a case brought up and eventually solved by Mario Guttman of Perkins+Will:
Roll Back Failure Suppressing all Messages, both Warnings and Errors
Question: I am trying to implement a failure handler that will silently respond to all warnings. I am happy to roll back the transaction or delete the elements but I can't seem to get rid of the dialogue box with failures other than warnings.
Answer: I figured it out.
In case anyone is interested, I needed an additional statement in the FailureHandler class:
return FailureProcessingResult .ProceedWithRollBack;
I also needed another statement with the transaction start:
failureHandlingOptions .SetClearAfterRollback( true );
Here is a more complete description and solution:
I'm running a program that creates a lot of new Revit elements. The process may fail in the middle for some reason. I don't want the user to have to respond to a dialogue each time; I just want the program to keep going with the next element.
Specifically, if it is a warning, I want to ignore it, and if it is any other kind of error, I want to cancel creating that element and continue with the next one.
I do want to record the errors so I can inform the user at the end about what problems were encountered.
I looked at your posting on the Failure API about how to handle failure messages, and also the Revit 2012 API Developer Guide and sample code, but didn't quite find what I was looking for.
I finally figured it out and thought you might like to share this:
To begin with, you need to create a FailureHandler class; mine looks like this:
public class FailureHandler : IFailuresPreprocessor { public string ErrorMessage { set; get; } public string ErrorSeverity { set; get; } public FailureHandler() { ErrorMessage = ""; ErrorSeverity = ""; } public FailureProcessingResult PreprocessFailures( FailuresAccessor failuresAccessor ) { IList<FailureMessageAccessor> failureMessages = failuresAccessor.GetFailureMessages(); foreach( FailureMessageAccessor failureMessageAccessor in failureMessages ) { // We're just deleting all of the warning level // failures and rolling back any others FailureDefinitionId id = failureMessageAccessor .GetFailureDefinitionId(); try { ErrorMessage = failureMessageAccessor .GetDescriptionText(); } catch { ErrorMessage = "Unknown Error"; } try { FailureSeverity failureSeverity = failureMessageAccessor.GetSeverity(); ErrorSeverity = failureSeverity.ToString(); if( failureSeverity == FailureSeverity.Warning ) { failuresAccessor.DeleteWarning( failureMessageAccessor ); } else { return FailureProcessingResult .ProceedWithRollBack; } } catch { } } return FailureProcessingResult.Continue; } }
Then, in the body of the main program you need something like:
Transaction transaction = new Transaction( doc ); FailureHandlingOptions failureHandlingOptions = transaction.GetFailureHandlingOptions(); FailureHandler failureHandler = new FailureHandler(); failureHandlingOptions.SetFailuresPreprocessor( failureHandler ); failureHandlingOptions.SetClearAfterRollback( true ); transaction.SetFailureHandlingOptions( failureHandlingOptions ); transaction.Start( "Transaction Name" ); // Do something here that causes the error transaction.Commit(); // The following is just illustrative. // In reality we would collect the errors // to show later. if( failureHandler.ErrorMessage != "" ) { System.Windows.Forms.MessageBox.Show( failureHandler.ErrorSeverity + " || " + failureHandler.ErrorMessage ); }
Many thanks to Mario for all his research and friendly sharing!
hi Jeremy,
refer to Roll Back Failure Suppressing all messages, Mario's solution is great, most of the warning messages are gone except a few, is it normal or did i miss something?
i even used failuresAccessor.DeleteAllWarnings(); maybe it's because i have only one transaction w/ multiple start() + commit() for different cases of the elements modification, should i use sub-tranaction or something else?
also, is there a way to supress Error messages too, i mean accept default?
cheers w/ many thanks,
Ning
Posted by: Ning Zhou | January 24, 2013 at 02:23
figured out that it need seperate transactions for some cases, thanks.
Posted by: Ning Zhou | January 30, 2013 at 20:17
Dear Ning,
Oh, wow, yes, of course.
That is definitely a recurring topic that I already discussed repeatedly.
Thank you for letting us know.
Congratulation on solving it.
Cheers, Jeremy.
Posted by: Jeremy Tammik | January 31, 2013 at 05:30
Hi Jeremy
That is Amazing, i was searching for this for a while.
Really Thanks for Sharing.
Posted by: Moustafa Khalil | January 07, 2014 at 07:48
Dear Moustafa,
Thank you very much for your appreciation!
Glad you found it!
Good luck making use of it!
Cheers, Jeremy.
Posted by: Jeremy Tammik | January 10, 2014 at 11:39