Roll back a single failed operation in a continuous transaction and disable all failure message boxes (including warnings and errors)
Problem
I tried to implement a failed handler function: Disable all warning message boxes and revoke the actions that cause the warning. However, I cannot find a way to disable message boxes that are not warnings.
My problem scenarios are as follows:
Create multiple Revit elements in an external command. This process may fail for some reason. However, I do not want to bring up a dialog box for the user to choose whether to continue or exit. I want the program to ignore the part of the operation that causes the failure and continue the subsequent operation. However, I need to record all failures and let the user know what happened at the end of the entire external command.
Answer
Two special statements are required in the solution.
-Return FailureProcessingResult. ProceedWithRollBack in the failed processing function;
-Call failureHandlingOptions. SetClearAfterRollback (true) to set transaction failure handling );
First, define a failure handling class:
[Csharp]
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)
{
// If the message is a warning, the message box is disabled.
FailuresAccessor. DeleteWarning (failureMessageAccessor );
}
Else
{
// If it is an error, cancel the operation that causes the error, but continue the entire transaction.
Return FailureProcessingResult. ProceedWithRollBack;
}
}
Catch
{
}
}
Return FailureProcessingResult. Continue;
}
}
Then, when creating a transaction, specify the failure handling settings as follows:
[Csharp]
Transaction transaction = new Transaction (doc );
FailureHandlingOptions failureHandlingOptions = transaction. GetFailureHandlingOptions ();
FailureHandler failureHandler = new FailureHandler ();
FailureHandlingOptions. SetFailuresPreprocessor (failureHandler );
// This sentence is the key
FailureHandlingOptions. SetClearAfterRollback (true );
Transaction. SetFailureHandlingOptions (failureHandlingOptions );
Transaction. Start ("Transaction Name ");
// Do something here that causes the error
//......
Transaction. Commit ();
// This Code simply uses a WinForm dialog box to display the failure records saved in the failed processing function.
// You can perform any processing on these failure records in actual applications.
If (failureHandler. ErrorMessage! = "")
{
System. Windows. Forms. MessageBox. Show (failureHandler. ErrorSeverity + "|" + failureHandler. ErrorMessage );
}