Building Coder 链接:Failure API Take Two
Revit 二次开发论坛链接:翻译 Building Coder - 失败处理API(下)
public class RegisterFailureEvent : IExternalCommand { public Result Execute( ExternalCommandData commandData, ref string messages, ElementSet elements ) { UIApplication app = commandData.Application; Document doc = app.ActiveUIDocument.Document; app.Application.FailuresProcessing += new EventHandler<FailuresProcessingEventArgs>( OnFailuresProcessing ); return Result.Succeeded; } /// <summary> /// 事件处理器 /// </summary> private void OnFailuresProcessing( object sender, FailuresProcessingEventArgs e ) { FailuresAccessor failuresAccessor = e.GetFailuresAccessor(); String transactionName = failuresAccessor.GetTransactionName(); IList<FailureMessageAccessor> fmas = failuresAccessor.GetFailureMessages(); if( fmas.Count == 0 ) { // 允许失败处理周期进入下一个步骤 e.SetProcessingResult( FailureProcessingResult.Continue ); return; } // 如果手动删除一个元素,事务名称为 'Delete Selection' if( transactionName.Equals( "Delete Selection" ) ) { foreach( FailureMessageAccessor fma in fmas ) { // 模拟手动单击"Remove Link"按钮 failuresAccessor.ResolveFailure( fma ); // 模拟手动单击"Ok"按钮 //failuresAccessor.DeleteWarning(fma); } e.SetProcessingResult( FailureProcessingResult.ProceedWithCommit ); return; } e.SetProcessingResult( FailureProcessingResult.Continue ); } }
[TransactionAttribute( TransactionMode.Manual )] [RegenerationAttribute( RegenerationOption.Manual )] public class FailureHandle : IExternalCommand { public Result Execute( ExternalCommandData commandData, ref string messages, ElementSet elements ) { UIApplication app = commandData.Application; Document doc = app.ActiveUIDocument.Document; Transaction trans = new Transaction( doc, "DeleteLinkedModel" ); trans.Start(); FailureHandlingOptions options = trans.GetFailureHandlingOptions(); MyPreProcessor preproccessor = new MyPreProcessor(); options.SetFailuresPreprocessor( preproccessor ); trans.SetFailureHandlingOptions( options ); Selection sel = app.ActiveUIDocument.Selection; Reference ref1 = sel.PickObject( ObjectType.Element, "Please pick a linked model instance" ); Element elem = ref1.Element; doc.Delete( elem ); trans.Commit(); return Result.Succeeded; } } /// <summary> /// 定义失败预处理器 /// </summary> public class MyPreProcessor : IFailuresPreprocessor { FailureProcessingResult IFailuresPreprocessor.PreprocessFailures( FailuresAccessor failuresAccessor ) { String transactionName = failuresAccessor.GetTransactionName(); IList<FailureMessageAccessor> fmas = failuresAccessor.GetFailureMessages(); if( fmas.Count == 0 ) { return FailureProcessingResult.Continue; } // 这里我们假设已经知道了事务的名称 if( transactionName.Equals( "DeleteLinkedModel" ) ) { foreach( FailureMessageAccessor fma in fmas ) { // 模拟手动单击"Remove Link"按钮 failuresAccessor.ResolveFailure( fma ); // 模拟手动单击"OK"按钮 //failuresAccessor.DeleteWarning( fma ); } return FailureProcessingResult.ProceedWithCommit; } return FailureProcessingResult.Continue; } }