SelectionExtension.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:SelectionExtension
  3. * 作者:xulisong
  4. * 创建时间: 2019/5/23 11:31:45
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Autodesk.Revit.DB;
  13. using Autodesk.Revit.UI.Selection;
  14. namespace FWindSoft.Revit
  15. {
  16. /// <summary>
  17. /// 选择项相关扩展
  18. /// </summary>
  19. public static class SelectionExtension
  20. {
  21. static SelectionExtension()
  22. {
  23. AllSnapTypes = GetAllSnapTypes();
  24. }
  25. /// <summary>
  26. /// 全部可选信息
  27. /// </summary>
  28. public static ObjectSnapTypes AllSnapTypes { get; private set; }
  29. #region 私有方法
  30. /// <summary>
  31. /// 获取全局选择配置
  32. /// </summary>
  33. /// <returns></returns>
  34. private static ObjectSnapTypes GetAllSnapTypes()
  35. {
  36. ObjectSnapTypes snapSettings =
  37. ObjectSnapTypes.Centers | ObjectSnapTypes.Endpoints |
  38. ObjectSnapTypes.Intersections | ObjectSnapTypes.Midpoints |
  39. ObjectSnapTypes.Nearest | ObjectSnapTypes.WorkPlaneGrid |
  40. ObjectSnapTypes.Perpendicular | ObjectSnapTypes.Points |
  41. ObjectSnapTypes.Quadrants | ObjectSnapTypes.Tangents;
  42. return snapSettings;
  43. }
  44. #endregion
  45. #region 选取点信息
  46. /// <summary>
  47. /// 选择点信息
  48. /// </summary>
  49. /// <param name="selection"></param>
  50. /// <param name="snapSettings">选择项</param>
  51. /// <param name="strMessage">提示消息</param>
  52. /// <returns></returns>
  53. public static XYZ PickPointT(this Selection selection, ObjectSnapTypes snapSettings, string strMessage)
  54. {
  55. try
  56. {
  57. return selection.PickPoint(snapSettings, strMessage);
  58. }
  59. catch (OperationCanceledException)
  60. {
  61. return null;
  62. }
  63. }
  64. /// <summary>
  65. /// 选取点
  66. /// </summary>
  67. /// <param name="selection"></param>
  68. /// <param name="strMessage"></param>
  69. /// <returns></returns>
  70. public static XYZ PickPointT(this Selection selection, string strMessage)
  71. {
  72. return PickPointT(selection, AllSnapTypes, strMessage);
  73. }
  74. #endregion
  75. #region 选取reference
  76. /// <summary>
  77. /// 选取指定的reference
  78. /// </summary>
  79. /// <param name="selection"></param>
  80. /// <param name="strMessage"></param>
  81. /// <param name="filter"></param>
  82. /// <param name="objType"></param>
  83. /// <returns></returns>
  84. public static Reference PickReferenceT(this Selection selection, string strMessage,
  85. ISelectionFilter filter, ObjectType objType)
  86. {
  87. strMessage = strMessage ?? string.Empty;
  88. Reference reference = null;
  89. try
  90. {
  91. if (filter == null)
  92. {
  93. reference = selection.PickObject(objType, strMessage);
  94. }
  95. else
  96. {
  97. reference = selection.PickObject(objType, filter, strMessage);
  98. }
  99. }
  100. #region 异常处理
  101. catch (OperationCanceledException)
  102. {
  103. return null;
  104. }
  105. catch (InvalidOperationException)
  106. {
  107. return null;
  108. }
  109. #endregion
  110. return reference;
  111. }
  112. public static IList<Reference> PickReferencesT(this Selection selection, string strMessage,
  113. ISelectionFilter filter, ObjectType objType)
  114. {
  115. strMessage = strMessage ?? string.Empty;
  116. IList<Reference> references = null;
  117. try
  118. {
  119. if (filter == null)
  120. {
  121. references = selection.PickObjects(objType, strMessage);
  122. }
  123. else
  124. {
  125. references = selection.PickObjects(objType, filter, strMessage);
  126. }
  127. }
  128. #region 异常处理
  129. catch (OperationCanceledException)
  130. {
  131. return null;
  132. }
  133. #endregion
  134. return references;
  135. }
  136. /// <summary>
  137. /// 获取选中元素elementId
  138. /// </summary>
  139. /// <param name="selection"></param>
  140. /// <param name="strMessage"></param>
  141. /// <param name="filter"></param>
  142. /// <returns></returns>
  143. public static ElementId PickElementId(this Selection selection, string strMessage, ISelectionFilter filter)
  144. {
  145. ElementId selectedElementId = null;
  146. strMessage = strMessage ?? string.Empty;
  147. try
  148. {
  149. Reference reference = null;
  150. reference = selection.PickReferenceT(strMessage, filter, ObjectType.Element);
  151. if (reference != null)
  152. {
  153. selectedElementId = reference.ElementId;
  154. }
  155. }
  156. catch (OperationCanceledException)
  157. {
  158. return null;
  159. }
  160. return selectedElementId;
  161. }
  162. #endregion
  163. #region 框选
  164. /// <summary>
  165. /// 框选相关元素信息
  166. /// </summary>
  167. /// <param name="selection"></param>
  168. /// <param name="strMessage"></param>
  169. /// <param name="filter">过滤器</param>
  170. /// <returns></returns>
  171. public static List<Element> PickElementsByRectangleT(this Selection selection, string strMessage, ISelectionFilter filter)
  172. {
  173. strMessage = strMessage ?? string.Empty;
  174. try
  175. {
  176. var listSelected = filter != null
  177. ? selection.PickElementsByRectangle(filter, strMessage).ToList()
  178. : selection.PickElementsByRectangle(strMessage).ToList();
  179. if (listSelected.Count == 0)
  180. {
  181. return PickElementsByRectangleT(selection,strMessage, filter);
  182. }
  183. return listSelected;
  184. }
  185. #region 异常处理
  186. catch (OperationCanceledException)
  187. {
  188. return null;
  189. }
  190. catch (InvalidOperationException)
  191. {
  192. return null;
  193. }
  194. #endregion
  195. }
  196. #endregion
  197. }
  198. }