The following wizard pages will guide you to set options and create the failure preprocessor:
The whole FailurePreprocessor1 class has been appended bellow:
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 RevitAddinCSProject
{
public class FailurePreprocessor1 : IFailuresPreprocessor
{
private List<FailureDefinitionId> FailureDefinitionIdList
{
get
{
List<FailureDefinitionId> list = new List<FailureDefinitionId>();
list.Add(BuiltInFailures.OverlapFailures.AreaBoundaryLinesOverlap);
list.Add(BuiltInFailures.OverlapFailures.CurvesOverlap);
list.Add(BuiltInFailures.OverlapFailures.DuplicateInstances);
list.Add(BuiltInFailures.OverlapFailures.DuplicatePoints);
list.Add(BuiltInFailures.OverlapFailures.DuplicateRebar);
list.Add(BuiltInFailures.OverlapFailures.LevelsOverlap);
list.Add(BuiltInFailures.OverlapFailures.RoomSeparationLinesOverlap);
list.Add(BuiltInFailures.OverlapFailures.SpaceSeparationLinesOverlap);
list.Add(BuiltInFailures.OverlapFailures.WallAreaBoundaryOverlap);
list.Add(BuiltInFailures.OverlapFailures.WallRoomSeparationOverlap);
list.Add(BuiltInFailures.OverlapFailures.WallsOverlap);
list.Add(BuiltInFailures.OverlapFailures.WallSpaceSeparationOverlap);
return list;
}
}
private List<FailureSeverity> FailureSeverityList
{
get
{
List<FailureSeverity> list = new List<FailureSeverity>();
list.Add(FailureSeverity.Warning);
list.Add(FailureSeverity.Error);
return list;
}
}
private List<KeyValuePair<string, FailureProcessingResult>> TransNameFailureResultPairList
{
get
{
List<KeyValuePair<string, FailureProcessingResult>> list = new List<KeyValuePair<string, FailureProcessingResult>>();
list.Add(new KeyValuePair<string, FailureProcessingResult>("TestTransaction1", FailureProcessingResult.ProceedWithCommit));
list.Add(new KeyValuePair<string, FailureProcessingResult>("TestTransaction2", FailureProcessingResult.ProceedWithCommit));
return list;
}
}
// A delegate for the caller to define and pass back to the method
public delegate void ActionCallback(Document doc);
// A helper method for the caller to call
public static void PreprocessFailuresInTransaction(Document doc, string transName, ActionCallback action)
{
Transaction trans = new Transaction(doc, transName);
FailureHandlingOptions opts = trans.GetFailureHandlingOptions();
opts.SetFailuresPreprocessor(new FailurePreprocessor1());
trans.SetFailureHandlingOptions(opts);
trans.Start();
action(doc);
trans.Commit();
}
#region IFailuresPreprocessor Members
public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
{
FailureProcessingResult fpResult = FailureProcessingResult.Continue;
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 (FailureDefinitionIdList.Exists(e=>e.Guid.ToString().Equals(msgAccessor.GetFailureDefinitionId().Guid.ToString())) &&
FailureSeverityList.Contains(msgAccessor.GetSeverity()))
{
failuresAccessor.DeleteWarning(msgAccessor);
fpResult = kvp.Value;
}
}
return fpResult;
}
#endregion
}
}
The delegate callback methods and a sample external command class have also been appended:
namespace RevitAddinCSProject
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class ExtCmd : IExternalCommand
{
#region Cached Variables
private static ExternalCommandData _cachedCmdData;
public static UIApplication CachedUiApp
{
get
{
return _cachedCmdData.Application;
}
}
public static RvtApplication CachedApp
{
get
{
return CachedUiApp.Application;
}
}
public static RvtDocument CachedDoc
{
get
{
return CachedUiApp.ActiveUIDocument.Document;
}
}
#endregion
#region IExternalCommand Members
public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
{
_cachedCmdData = cmdData;
try
{
//TODO: add your code below.
FailurePreprocessor1.PreprocessFailuresInTransaction(CachedDoc, "TestTransaction1", CallbackOfTestTransaction1);
FailurePreprocessor1.PreprocessFailuresInTransaction(CachedDoc, "TestTransaction2", CallbackOfTestTransaction2);
return Result.Succeeded;
}
catch (Exception ex)
{
msg = ex.ToString();
return Result.Failed;
}
}
#endregion
private void CallbackOfTestTransaction1(Document doc)
{
//Add code for the transaction TestTransaction1
}
private void CallbackOfTestTransaction2(Document doc)
{
//Add code for the transaction TestTransaction2
}
}
}
Give the RevitAddinWizard a try and you will get all these in a few seconds.
Links to some related articles:
Use RevitAddinWizard to Create IUpdater Derivatives of Revit API
Implement IFailuresPreprocessor of Revit API
Use RevitAddinWizard to Implement IFailuresPreprocessor of Revit API
Implement An IFailuresProcessor of Revit API
Use RevitAddinWizard to Implement IFailuresProcessor of Revit API
Command Availability And Revit Flavors/Categories of Revit API
Use RevitAddinWizard to Implement IExternalCommandavailability of Revit API
Implement ISelectionFilter of Revit API
Use RevitAddinWizard to Implement ISelectionFilter of Revit API
Recent Comments