CommonMEP.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Windows.Forms;
  4. using Autodesk.Revit.DB;
  5. namespace SAGA.RevitUtils
  6. {
  7. public class CommonMep
  8. {
  9. /// <summary>
  10. /// 获取与指定Connector相连的Connector(端点相连)
  11. /// </summary>
  12. public static Connector GetConnectConnector(Connector connector)
  13. {
  14. Connector conTarget = null;
  15. if (connector.IsConnected)
  16. {
  17. ConnectorSetIterator conSetIter = connector.AllRefs.ForwardIterator();
  18. while (conSetIter.MoveNext())
  19. {
  20. Connector con = conSetIter.Current as Connector;
  21. if (con != null && con.Owner.UniqueId != connector.Owner.UniqueId && con.ConnectorType != ConnectorType.Logical)
  22. {
  23. conTarget = con;
  24. break;
  25. }
  26. }
  27. }
  28. return conTarget;
  29. }
  30. #region 仅两个Connector
  31. ///// <summary>
  32. ///// 获取相连的所有构件
  33. ///// </summary>
  34. //public static List<Element> GetConnectedElements(Element element, Connector exclude, bool first)
  35. //{
  36. // List<Element> list = new List<Element>();
  37. // ConnectorSet set = null;
  38. // if (element is MEPCurve)
  39. // {
  40. // MEPCurve mep = element as MEPCurve;
  41. // set = mep.ConnectorManager.Connectors;
  42. // }
  43. // else if (element is FamilyInstance)
  44. // {
  45. // FamilyInstance fi = element as FamilyInstance;
  46. // if (fi.MEPModel != null)
  47. // {
  48. // set = fi.MEPModel.ConnectorManager.Connectors;
  49. // }
  50. // }
  51. // if (set == null)
  52. // {
  53. // MessageBox.Show("选择图元不符合要求。");
  54. // return null;
  55. // }
  56. // foreach (Connector con in set)
  57. // {
  58. // if (con.IsConnected)
  59. // {
  60. // //需要排除的Connector
  61. // if (exclude != null && exclude.Origin.IsAlmostEqualTo(con.Origin))
  62. // {
  63. // continue;
  64. // }
  65. // Connector conn = GetConnectConnector(con);
  66. // Element owner = conn.Owner;
  67. // list.Add(owner);
  68. // List<Element> find = GetConnectedElements(owner, conn, false);
  69. // if (find.Count > 0)
  70. // {
  71. // list.AddRange(find);
  72. // }
  73. // }
  74. // }
  75. // if (first)
  76. // {
  77. // list.Add(element);
  78. // }
  79. // return list;
  80. //}
  81. #endregion //仅两个Connector
  82. #region 交叉闭合出错
  83. ///// <summary>
  84. ///// 获取相连的所有构件
  85. ///// </summary>
  86. //public static List<Element> GetConnectedElements(Element element, List<Connector> excludes, bool first)
  87. //{
  88. // if (excludes == null)
  89. // {
  90. // excludes = new List<Connector>();
  91. // }
  92. // List<Element> list = new List<Element>();
  93. // ConnectorSet set = null;
  94. // var curve = element as MEPCurve;
  95. // if (curve != null)
  96. // {
  97. // MEPCurve mep = curve;
  98. // set = mep.ConnectorManager.Connectors;
  99. // }
  100. // else
  101. // {
  102. // FamilyInstance fi = element as FamilyInstance;
  103. // if (fi?.MEPModel != null)
  104. // {
  105. // set = fi.MEPModel.ConnectorManager.Connectors;
  106. // }
  107. // }
  108. // if (set == null)
  109. // {
  110. // MessageBox.Show("选择图元不符合要求。");
  111. // return null;
  112. // }
  113. // foreach (Connector con in set)
  114. // {
  115. // if (con != null && con.IsConnected)
  116. // {
  117. // bool has = excludes.Any(connector => connector != null && connector.Origin.IsAlmostEqualTo(con.Origin));
  118. // if (has)
  119. // {
  120. // continue;
  121. // }
  122. // excludes.Add(con);
  123. // var conn = GetConnectConnector(con);
  124. // var owner = conn.Owner;
  125. // var used = new List<Connector> {conn};
  126. // list.Add(owner);
  127. // var find = GetConnectedElements(owner, used, false);
  128. // if (find.Count > 0)
  129. // {
  130. // list.AddRange(find);
  131. // }
  132. // }
  133. // }
  134. // if (first)
  135. // {
  136. // list.Add(element);
  137. // }
  138. // return list;
  139. //}
  140. #endregion //交叉闭合出错
  141. /// <summary>
  142. /// 获取相连的所有构件
  143. /// </summary>
  144. public static List<Element> GetConnectedElements(Element element, List<Connector> excludes,List<Element> exElements, bool first)
  145. {
  146. if (excludes == null)
  147. {
  148. excludes = new List<Connector>();
  149. }
  150. if (exElements == null)
  151. {
  152. exElements=new List<Element>();
  153. }
  154. List<Element> list = new List<Element>();
  155. if (first)
  156. {
  157. list.Add(element);
  158. exElements.Add(element);
  159. }
  160. ConnectorSet set = null;
  161. var curve = element as MEPCurve;
  162. if (curve != null)
  163. {
  164. MEPCurve mep = curve;
  165. set = mep.ConnectorManager.Connectors;
  166. }
  167. else
  168. {
  169. FamilyInstance fi = element as FamilyInstance;
  170. if (fi?.MEPModel != null)
  171. {
  172. set = fi.MEPModel.ConnectorManager.Connectors;
  173. }
  174. }
  175. if (set == null)
  176. {
  177. MessageBox.Show("选择图元不符合要求。");
  178. return null;
  179. }
  180. foreach (Connector con in set)
  181. {
  182. if (con != null && con.IsConnected)
  183. {
  184. bool has = excludes.Any(connector => connector != null && connector.Origin.IsAlmostEqualTo(con.Origin));
  185. if (has)
  186. {
  187. continue;
  188. }
  189. excludes.Add(con);
  190. var conn = GetConnectConnector(con);
  191. var owner = conn.Owner;
  192. bool exist = exElements.Any(e => e.Id.IntegerValue.Equals(owner.Id.IntegerValue));
  193. if (!exist)
  194. {
  195. exElements.Add(owner);
  196. }
  197. else
  198. {
  199. continue;
  200. }
  201. var used = new List<Connector> { conn };
  202. list.Add(owner);
  203. var find = GetConnectedElements(owner, used, exElements, false);
  204. if (find.Count > 0)
  205. {
  206. list.AddRange(find);
  207. }
  208. }
  209. }
  210. return list;
  211. }
  212. }
  213. }