123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /* ==============================================================================
- * 功能描述:DocumentExtension
- * 创 建 者:Garrett
- * 创建日期:2019/6/27 11:11:17
- * ==============================================================================*/
- using Autodesk.Revit.DB;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ExportStart
- {
- public static class DocumentExtension
- {
- public static void Invoke(this Document doc, Action<Transaction> action, string name = "default")
- {
- using (var tr = new Transaction(doc, name))
- {
- tr.Start();
- action(tr);
- var status = tr.GetStatus();
- switch (status)
- {
- case TransactionStatus.Started:
- tr.Commit();
- return;
- case TransactionStatus.Committed:
- case TransactionStatus.RolledBack:
- return;
- case TransactionStatus.Error:
- tr.RollBack();
- return;
- default:
- return;
- }
- }
- }
- public static TResult Invoke<TResult>(this Document doc, Func<Transaction, TResult> func, string name = "default")
- {
- using (var tr = new Transaction(doc, name))
- {
- tr.Start();
- 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 InvokeSub(this Document doc, Action<SubTransaction> action)
- {
- using (var tr = new SubTransaction(doc))
- {
- tr.Start();
- action(tr);
- var status = tr.GetStatus();
- switch (status)
- {
- case TransactionStatus.Started:
- tr.Commit();
- return;
- case TransactionStatus.Committed:
- case TransactionStatus.RolledBack:
- break;
- case TransactionStatus.Error:
- tr.RollBack();
- return;
- default:
- return;
- }
- }
- }
- public static TResult InvokeSub<TResult>(this Document doc, Func<SubTransaction, TResult> func)
- {
- using (var tr = new SubTransaction(doc))
- {
- tr.Start();
- 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 InvokeGroup(this Document doc, Action<TransactionGroup> action, string name = "default")
- {
- using (var tr = new TransactionGroup(doc, name))
- {
- tr.Start();
- action(tr);
- var status = tr.GetStatus();
- switch (status)
- {
- case TransactionStatus.Started:
- tr.Commit();
- return;
- case TransactionStatus.Committed:
- case TransactionStatus.RolledBack:
- break;
- case TransactionStatus.Error:
- tr.RollBack();
- return;
- default:
- return;
- }
- }
- }
- public static TResult InvokeGroup<TResult>(this Document doc, Func<TransactionGroup, TResult> func, string name = "default")
- {
- using (var tr = new TransactionGroup(doc, name))
- {
- tr.Start();
- 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 FilteredElementCollector QueryByType<T>(this Document doc) where T : Element
- {
- return new FilteredElementCollector(doc).OfClass(typeof(T));
- }
- }
- }
|