The following wizard pages will guide you to set options and create the failure processor:
The whole failure processor class generated by the wizard has been appended below:
using System;
using System.Text;
using System.Xml;
using System.Linq;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
namespace RevitAddinCS2
{
public class FailureProcessor1 : IFailuresProcessor
{
private List<KeyValuePair<string, FailureProcessingResult>> TransNameFailureResultPairList
{
get
{
List<KeyValuePair<string, FailureProcessingResult>> list = new List<KeyValuePair<string, FailureProcessingResult>>();
list.Add(new KeyValuePair<string, FailureProcessingResult>("TestTransaction", FailureProcessingResult.ProceedWithCommit));
return list;
}
}
public static FailureDefinitionId FailureProcessor1_FailureDefinition = new FailureDefinitionId(new Guid("32609fdb-89c0-47ff-8b31-f238e1438086"));
public static FailureDefinition FailureDefinitionOfFailureProcessor1_FailureDefinition
{
get
{
FailureDefinition failDef = FailureDefinition.CreateFailureDefinition(FailureProcessor1_FailureDefinition, FailureSeverity.Warning, "This is a test FailureDefinition");
failDef.AddResolutionType(FailureResolutionType.DeleteElements, "DeleteElements", typeof(DeleteElements));
failDef.SetDefaultResolutionType(FailureResolutionType.DeleteElements);
return failDef;
}
}
public static void Register()
{
FailureProcessor1 processor = new FailureProcessor1();
RvtApplication.RegisterFailuresProcessor(processor);
}
// A delegate for the caller to define and pass back to the method
public delegate bool ElementHandling(Document doc, ref List<ElementId> ids);
// A helper method for the caller to call
public static void ProcessFailuresInTransaction(Document doc, string transName, ElementHandling action)
{
Transaction trans = new Transaction(doc, transName);
trans.Start();
List<ElementId> ids = new List<ElementId>();
bool success = action(doc, ref ids);
if (!success)
{
FailureMessage failMsg = new FailureMessage(FailureProcessor1_FailureDefinition);
FailureResolution failRsl = DeleteElements.Create(doc, ids);
failMsg.AddResolution(FailureResolutionType.DeleteElements, failRsl);
doc.PostFailure(failMsg);
}
trans.Commit();
}
#region IFailuresProcessor Members
public void Dismiss(Document document)
{
}
public FailureProcessingResult ProcessFailures(FailuresAccessor failuresAccessor)
{
FailureProcessingResult fpResult = FailureProcessingResult.ProceedWithCommit; //NOTE: Change the default result if necessary.
IList<FailureMessageAccessor> msgAccessorList = failuresAccessor.GetFailureMessages();
foreach (FailureMessageAccessor msgAccessor in msgAccessorList)
{
String tranName = failuresAccessor.GetTransactionName();
if (!TransNameFailureResultPairList.Exists(e => e.Key.Equals(tranName)))
{
continue;
}
KeyValuePair<string, FailureProcessingResult> kvp = TransNameFailureResultPairList.First(e => e.Key.Equals(tranName));
if (msgAccessor.GetFailureDefinitionId().Guid.ToString().Equals(FailureProcessor1_FailureDefinition.Guid.ToString()))
{
failuresAccessor.ResolveFailure(msgAccessor); //NOTE: Can be changed to any other resolution actions.
fpResult = kvp.Value;
}
}
return fpResult;
}
#endregion
}
}
Here is the registration code in the external application:
public Result OnStartup(UIControlledApplication uiApp)
{
_cachedUiCtrApp = uiApp;
try
{
RibbonPanel ribbonPanel = CreateRibbonPanel();
//TODO: add you code below.
// Register the FailureProcessor1
FailureProcessor1.Register();
return Result.Succeeded;
}
catch (Exception ex)
{
MessageBox.Show( ex.ToString() );
return Result.Failed;
}
}
Here is the process call and the transaction callback in the external command class:
public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
{
_cachedCmdData = cmdData;
try
{
//TODO: add your code below.
FailureProcessor1.ProcessFailuresInTransaction(CachedDoc, "TestTransaction", CallbackOfTestTransaction);
return Result.Succeeded;
}
catch (Exception ex)
{
msg = ex.ToString();
return Result.Failed;
}
}
private bool CallbackOfTestTransaction(Document doc, ref List<ElementId> ids)
{
//Add code for the transaction TestTransaction
return true; // Or return false to indicate something wrong! Then the failure processor will take action.
}
Give the RevitAddinWizard a try and you will get all these in a few seconds.
Recent Comments