using Autodesk.Revit.DB; using ShrlAlgoToolkit.RevitCore.Assists; using System.Runtime.CompilerServices; namespace ShrlAlgoToolkit.RevitCore.Extensions { public static class TransactionExtensions { /// /// 事务 /// /// /// /// /// /// /// public static T Invoke(this Document doc, Func func, [CallerMemberName] string name = "外部命令", bool isBlockWarning = false) { using var tr = new Transaction(doc, name); tr.Start(); if (isBlockWarning) { var failureHandlingOptions = tr.GetFailureHandlingOptions(); failureHandlingOptions.SetFailuresPreprocessor(new FailuresPreProcessor()); tr.SetFailureHandlingOptions(failureHandlingOptions); } var result = func(tr); var status = tr.GetStatus(); switch (status) { case TransactionStatus.Started: tr.Commit(); return result; case TransactionStatus.Committed: case TransactionStatus.RolledBack: return result; case TransactionStatus.Error: tr.RollBack(); return result; default: return result; } } /// /// 事务 /// /// /// /// /// 阻止警告 public static void Invoke(this Document doc, Action action, [CallerMemberName] string name = "外部命令", bool isBlockWarning = false) { if (doc == null) { throw new ArgumentException("参数为空", nameof(doc)); } using var tr = new Transaction(doc, name); tr.Start(); try { if (isBlockWarning) { var failureHandlingOptions = tr.GetFailureHandlingOptions(); failureHandlingOptions.SetFailuresPreprocessor(new FailuresPreProcessor()); tr.SetFailureHandlingOptions(failureHandlingOptions); } action?.Invoke(tr); } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { } //catch (Exception ex) //{ // Debug.WriteLine($"异常:{ex.Message}"); // MessageBox.Show(ex.Message); // //Log.Logger.Error(ex, $"{ex.Source}"); //} var status = tr.GetStatus(); //根据事务的当前状态判断是否提交 switch (status) { case TransactionStatus.Started: tr.Commit(); return; case TransactionStatus.Error: tr.RollBack(); return; default: return; } } /// /// 事务组 /// /// /// /// /// /// public static T InvokeGroup(this Document doc, Func func, [CallerMemberName] string name = "外部命令组") { if (doc == null) { throw new ArgumentException(null, nameof(doc)); } using var tg = new TransactionGroup(doc, name); tg.Start(); var result = func(tg); var status = tg.GetStatus(); switch (status) { case TransactionStatus.Started: tg.Assimilate(); return result; case TransactionStatus.Error: tg.RollBack(); return result; default: return result; } } /// /// 事务组 /// /// /// /// public static void InvokeGroup(this Document doc, Action action, [CallerMemberName] string name = "外部命令") { if (doc == null) { throw new ArgumentException(nameof(doc)); } using var tg = new TransactionGroup(doc, name); tg.Start(); action(tg); var status = tg.GetStatus(); switch (status) { case TransactionStatus.Started: tg.Assimilate(); return; case TransactionStatus.Error: tg.RollBack(); return; default: return; } } /// /// 子事务 /// /// /// /// /// public static T InvokeSub(this Document doc, Func func) { if (doc == null) { throw new ArgumentException(nameof(doc)); } using SubTransaction st = new(doc); st.Start(); var result = func(st); var status = st.GetStatus(); switch (status) { case TransactionStatus.Started: st.Commit(); return result; case TransactionStatus.Error: st.RollBack(); return result; default: return result; } } public static void InvokeSub(this Document doc, Action action) { if (doc == null) { throw new ArgumentException(nameof(doc)); } using var st = new SubTransaction(doc); st.Start(); action(st); var status = st.GetStatus(); switch (status) { case TransactionStatus.Started: st.Commit(); return; case TransactionStatus.Error: st.RollBack(); return; default: return; } } /// /// 事务错误处理 /// /// /// public static void SetFailureProcess(this Transaction ts, IFailuresPreprocessor failuresPreprocessor) { var options = ts.GetFailureHandlingOptions(); options.SetFailuresPreprocessor(failuresPreprocessor); ts.SetFailureHandlingOptions(options); } } }