DocumentExtension.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* ==============================================================================
  2. * 功能描述:DocumentExtension
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/6/27 11:11:17
  5. * ==============================================================================*/
  6. using Autodesk.Revit.DB;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace ExportStart
  13. {
  14. public static class DocumentExtension
  15. {
  16. public static void Invoke(this Document doc, Action<Transaction> action, string name = "default")
  17. {
  18. using (var tr = new Transaction(doc, name))
  19. {
  20. tr.Start();
  21. action(tr);
  22. var status = tr.GetStatus();
  23. switch (status)
  24. {
  25. case TransactionStatus.Started:
  26. tr.Commit();
  27. return;
  28. case TransactionStatus.Committed:
  29. case TransactionStatus.RolledBack:
  30. return;
  31. case TransactionStatus.Error:
  32. tr.RollBack();
  33. return;
  34. default:
  35. return;
  36. }
  37. }
  38. }
  39. public static TResult Invoke<TResult>(this Document doc, Func<Transaction, TResult> func, string name = "default")
  40. {
  41. using (var tr = new Transaction(doc, name))
  42. {
  43. tr.Start();
  44. var result = func(tr);
  45. var status = tr.GetStatus();
  46. switch (status)
  47. {
  48. case TransactionStatus.Started:
  49. tr.Commit();
  50. return result;
  51. case TransactionStatus.Committed:
  52. case TransactionStatus.RolledBack:
  53. return result;
  54. case TransactionStatus.Error:
  55. tr.RollBack();
  56. return result;
  57. default:
  58. return result;
  59. }
  60. }
  61. }
  62. public static void InvokeSub(this Document doc, Action<SubTransaction> action)
  63. {
  64. using (var tr = new SubTransaction(doc))
  65. {
  66. tr.Start();
  67. action(tr);
  68. var status = tr.GetStatus();
  69. switch (status)
  70. {
  71. case TransactionStatus.Started:
  72. tr.Commit();
  73. return;
  74. case TransactionStatus.Committed:
  75. case TransactionStatus.RolledBack:
  76. break;
  77. case TransactionStatus.Error:
  78. tr.RollBack();
  79. return;
  80. default:
  81. return;
  82. }
  83. }
  84. }
  85. public static TResult InvokeSub<TResult>(this Document doc, Func<SubTransaction, TResult> func)
  86. {
  87. using (var tr = new SubTransaction(doc))
  88. {
  89. tr.Start();
  90. var result = func(tr);
  91. var status = tr.GetStatus();
  92. switch (status)
  93. {
  94. case TransactionStatus.Started:
  95. tr.Commit();
  96. return result;
  97. case TransactionStatus.Committed:
  98. case TransactionStatus.RolledBack:
  99. return result;
  100. case TransactionStatus.Error:
  101. tr.RollBack();
  102. return result;
  103. default:
  104. return result;
  105. }
  106. }
  107. }
  108. public static void InvokeGroup(this Document doc, Action<TransactionGroup> action, string name = "default")
  109. {
  110. using (var tr = new TransactionGroup(doc, name))
  111. {
  112. tr.Start();
  113. action(tr);
  114. var status = tr.GetStatus();
  115. switch (status)
  116. {
  117. case TransactionStatus.Started:
  118. tr.Commit();
  119. return;
  120. case TransactionStatus.Committed:
  121. case TransactionStatus.RolledBack:
  122. break;
  123. case TransactionStatus.Error:
  124. tr.RollBack();
  125. return;
  126. default:
  127. return;
  128. }
  129. }
  130. }
  131. public static TResult InvokeGroup<TResult>(this Document doc, Func<TransactionGroup, TResult> func, string name = "default")
  132. {
  133. using (var tr = new TransactionGroup(doc, name))
  134. {
  135. tr.Start();
  136. var result = func(tr);
  137. var status = tr.GetStatus();
  138. switch (status)
  139. {
  140. case TransactionStatus.Started:
  141. tr.Commit();
  142. return result;
  143. case TransactionStatus.Committed:
  144. case TransactionStatus.RolledBack:
  145. return result;
  146. case TransactionStatus.Error:
  147. tr.RollBack();
  148. return result;
  149. default:
  150. return result;
  151. }
  152. }
  153. }
  154. public static FilteredElementCollector QueryByType<T>(this Document doc) where T : Element
  155. {
  156. return new FilteredElementCollector(doc).OfClass(typeof(T));
  157. }
  158. }
  159. }