ConnectorExtension.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using Autodesk.Revit.DB;
  2. using FWindSoft.SystemExtensions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace FWindSoft.Revit
  9. {
  10. public static class ConnectorExtension
  11. {
  12. static ConnectorExtension()
  13. {
  14. InitMepComponment();
  15. }
  16. #region 缓存设置
  17. private static List<int> m_MepComponment;
  18. private static void InitMepComponment()
  19. {
  20. m_MepComponment= new List<int>() { (int)BuiltInCategory.OST_PipeCurves, (int)BuiltInCategory.OST_PipeFitting, (int)BuiltInCategory.OST_DuctCurves, (int)BuiltInCategory.OST_DuctFitting };
  21. }
  22. #endregion
  23. #region 节点获取相关
  24. /// <summary>
  25. /// 获取ConnectorSet
  26. /// </summary>
  27. /// <param name="element"></param>
  28. /// <returns></returns>
  29. private static ConnectorSet GetConnectorSet(this Element element)
  30. {
  31. if (element != null)
  32. {
  33. FamilyInstance instance = element as FamilyInstance;
  34. if (instance!=null && instance.MEPModel?.ConnectorManager != null)
  35. {
  36. return instance.MEPModel.ConnectorManager.Connectors;
  37. }
  38. MEPSystem system = element as MEPSystem;
  39. if (system != null)
  40. {
  41. return system.ConnectorManager.Connectors;
  42. }
  43. MEPCurve curve = element as MEPCurve;
  44. if (curve!=null)
  45. {
  46. return curve.ConnectorManager.Connectors;
  47. }
  48. }
  49. return null;
  50. }
  51. /// <summary>
  52. /// 获取EndConnector
  53. /// </summary>
  54. /// <param name="connectorSet"></param>
  55. /// <returns></returns>
  56. public static List<Connector> ToList(this ConnectorSet connectorSet)
  57. {
  58. List<Connector> result = new List<Connector>();
  59. try
  60. {
  61. if (connectorSet != null)
  62. {
  63. ConnectorSetIterator iterator = connectorSet.ForwardIterator();
  64. while (iterator.MoveNext())
  65. {
  66. Connector current = iterator.Current as Connector;
  67. if (current.IsPhysical())
  68. {
  69. result.Add(current);
  70. }
  71. }
  72. }
  73. return result;
  74. }
  75. catch
  76. {
  77. }
  78. return result;
  79. }
  80. /// <summary>
  81. /// 获取第一个满足条件的Connector,条件可以为空
  82. /// </summary>
  83. /// <param name="element"></param>
  84. /// <param name="predicate"></param>
  85. /// <returns></returns>
  86. public static Connector GetFirstConnector(this Element element,Predicate<Connector> predicate)
  87. {
  88. var set = element.GetConnectorSet();
  89. Connector result = null;
  90. try
  91. {
  92. if (set != null)
  93. {
  94. ConnectorSetIterator iterator = set.ForwardIterator();
  95. while (iterator.MoveNext())
  96. {
  97. Connector current = iterator.Current as Connector;
  98. if (current.IsPhysical()&& (predicate==null|| predicate(current)))
  99. {
  100. return current;
  101. }
  102. }
  103. }
  104. return result;
  105. }
  106. catch
  107. {
  108. }
  109. return result;
  110. }
  111. /// <summary>
  112. /// 获取指定类型的Connector
  113. /// </summary>
  114. /// <param name="element"></param>
  115. /// <param name="domain"></param>
  116. /// <returns></returns>
  117. public static List<Connector> GetConnectors(this Element element, Domain domain)
  118. {
  119. return element.GetConnectorSet().ToList().Where(c=>c.Domain==domain).ToList();
  120. }
  121. /// <summary>
  122. /// 获取指定类型的所有可用Connector
  123. /// </summary>
  124. /// <param name="element"></param>
  125. /// <returns></returns>
  126. public static List<Connector> GetAllConnectors(this Element element)
  127. {
  128. return element.GetConnectorSet().ToList();
  129. }
  130. #endregion
  131. #region 逻辑相关处理
  132. /// <summary>
  133. /// 标志connector是否是可被检索的
  134. /// </summary>
  135. /// <param name="connector"></param>
  136. /// <returns></returns>
  137. public static bool Searchable(this Connector connector)
  138. {
  139. return connector.IsPhysical() &&connector.IsConnected && connector.AllRefs != null;
  140. }
  141. #endregion
  142. /// <summary>
  143. /// 是否是物理节点
  144. /// </summary>
  145. /// <param name="connector"></param>
  146. /// <returns></returns>
  147. public static bool IsPhysical(this Connector connector)
  148. {
  149. return ConnectorType.Physical.HasFlag(connector.ConnectorType);
  150. }
  151. /// <summary>
  152. /// 获取相连的第一个Connector
  153. /// </summary>
  154. /// <param name="connector"></param>
  155. /// <returns></returns>
  156. public static Connector GetJoinConnector(this Connector connector)
  157. {
  158. return connector.GetJoinConnectors().FirstOrDefault();
  159. }
  160. /// <summary>
  161. /// 获取相连的Connector;
  162. /// </summary>
  163. /// <param name="connector"></param>
  164. /// <returns></returns>
  165. public static List<Connector> GetJoinConnectors(this Connector connector)
  166. {
  167. return GetJoinConnectors(connector, false);
  168. }
  169. /// <summary>
  170. /// 获取相连的Connector;
  171. /// </summary>
  172. /// <param name="connector"></param>
  173. /// <returns></returns>
  174. public static List<Connector> GetJoinConnectors(this Connector connector,bool containInputOwner)
  175. {
  176. List<Connector> list = new List<Connector>();
  177. if (connector.AllRefs != null)
  178. {
  179. var cons = connector.AllRefs.ToList();
  180. if(!containInputOwner)
  181. {
  182. return cons.Where(t =>t.Owner.Id!=(connector.Owner.Id)).ToList();
  183. }
  184. }
  185. return list;
  186. }
  187. /// <summary>
  188. ///根据点获取Connector
  189. /// </summary>
  190. /// <param name="element"></param>
  191. /// <param name="origin"></param>
  192. /// <returns></returns>
  193. public static Connector GetConnectorByOrigin(this Element element, XYZ origin)
  194. {
  195. var connectors = element.GetAllConnectors();
  196. return connectors.FirstOrDefault(c => c.ConnectorType == ConnectorType.End && c.Origin.IsEqual(origin));
  197. }
  198. /// <summary>
  199. /// 根据方向获取Connector
  200. /// </summary>
  201. /// <param name="element"></param>
  202. /// <param name="direction"></param>
  203. /// <returns></returns>
  204. public static Connector GetConnectorByDirection(this Element element, XYZ direction)
  205. {
  206. var connectors = element.GetAllConnectors();
  207. return connectors.FirstOrDefault(c => c.ConnectorType == ConnectorType.End && c.CoordinateSystem.BasisZ.IsSameDirection(direction));
  208. }
  209. /// <summary>
  210. /// 获取与给定方向,夹角最小的点
  211. /// </summary>
  212. /// <param name="element"></param>
  213. /// <param name="direction"></param>
  214. /// <returns></returns>
  215. public static Connector GetConnectorByMinAngle(this Element element, XYZ direction)
  216. {
  217. var connectors = element.GetAllConnectors();
  218. Connector useConnector = null;
  219. double angle = double.MinValue;
  220. foreach (Connector connector in connectors)
  221. {
  222. var tempDirection = connector.CoordinateSystem.BasisZ;
  223. var tempAngle = direction.DotProduct(tempDirection);
  224. //点乘值越大,角度越小
  225. if (tempAngle.More(angle))
  226. {
  227. useConnector = connector;
  228. angle = tempAngle;
  229. }
  230. }
  231. return useConnector;
  232. }
  233. /// <summary>
  234. /// 获取给点附近的Connector
  235. /// </summary>
  236. /// <param name="element"></param>
  237. /// <param name="position"></param>
  238. /// <returns></returns>
  239. public static Connector GetNearConnector(this Element element, XYZ position)
  240. {
  241. var connectors = element.GetAllConnectors();
  242. Connector resultConnector = null;
  243. double minDistance = double.MaxValue;
  244. foreach (var connector in connectors)
  245. {
  246. if (connector.ConnectorType != ConnectorType.End)
  247. {
  248. //只计算端点
  249. continue;
  250. }
  251. var tempLength = connector.Origin.DistanceTo(position);
  252. if (tempLength < minDistance)
  253. {
  254. resultConnector = connector;
  255. minDistance = tempLength;
  256. }
  257. }
  258. return resultConnector;
  259. }
  260. }
  261. }