PipeExtension.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using Autodesk.Revit.DB;
  2. using Autodesk.Revit.DB.Mechanical;
  3. using Autodesk.Revit.DB.Plumbing;
  4. using FWindSoft.SystemExtensions;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace FWindSoft.Revit
  11. {
  12. public static class PipeExtension
  13. {
  14. /// <summary>
  15. /// 获取较较大直径的管道
  16. /// </summary>
  17. /// <param name="first"></param>
  18. /// <param name="second"></param>
  19. /// <returns></returns>
  20. public static Pipe GetLargerDiameter(this Pipe first, Pipe second)
  21. {
  22. return first.Diameter.MoreEqual(second.Diameter)
  23. ? first
  24. : second;
  25. }
  26. /// <summary>
  27. /// 获取较较小直径的管道
  28. /// </summary>
  29. /// <param name="first"></param>
  30. /// <param name="second"></param>
  31. /// <returns></returns>
  32. public static Pipe GetSmallerDiameter(this Pipe first, Pipe second)
  33. {
  34. return first.Diameter.LessEqual(second.Diameter)
  35. ? first
  36. : second;
  37. }
  38. /// <summary>
  39. /// 获取管道系统分类
  40. /// </summary>
  41. /// <param name="pipe"></param>
  42. /// <returns></returns>
  43. public static PipeSystemType GetPipeSystemType(this Pipe pipe)
  44. {
  45. var pipingSystem = pipe.MEPSystem as PipingSystem;
  46. if (pipingSystem != null)
  47. return pipingSystem.SystemType;
  48. return PipeSystemType.UndefinedSystemType;
  49. }
  50. /// <summary>
  51. ///
  52. /// </summary>
  53. /// <param name="pipe"></param>
  54. /// <returns></returns>Horizontal
  55. public static PipingSystemType GetPipingSystemType(this Pipe pipe)
  56. {
  57. var elementId = pipe.GetParameterElementId(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM);
  58. return pipe.Document.GetElement(elementId) as PipingSystemType;
  59. }
  60. public static List<XYZ> GetPoints(this Pipe pipe)
  61. {
  62. return new List<XYZ> { pipe.GetLocationLine().StartPoint(), pipe.GetLocationLine().EndPoint() };
  63. }
  64. /// <summary>
  65. /// 获取指定管道的管道系统
  66. /// </summary>
  67. /// <param name="pipe"></param>
  68. /// <returns></returns>
  69. public static PipingSystemType GePipingSystemType(this Pipe pipe)
  70. {
  71. return pipe.Document.GetElement(pipe.MEPSystem.GetTypeId()) as PipingSystemType;
  72. }
  73. /// <summary>
  74. /// 获取横向管道系统,处于同一直线的一系列管道
  75. /// </summary>
  76. /// <param name="pipe">指定管道</param>
  77. /// <returns></returns>
  78. public static List<Pipe> GetSystemPipe(this Pipe pipe)
  79. {
  80. List<Pipe> pipes = new List<Pipe>();
  81. do
  82. {
  83. //if (pipe == null)
  84. //{
  85. // break;
  86. //}
  87. //Line baseLine = pipe.GetLocationLine();
  88. //pipes.Add(pipe);
  89. //for (int i = 0; i < pipes.Count; i++)
  90. //{
  91. // Pipe tempPipe = pipes[i];
  92. // List<Element> verPipes = tempPipe.GetFirstSameTypeElements();
  93. // foreach (var element in verPipes)
  94. // {
  95. // Pipe tempVerPipe = element as Pipe;
  96. // if (tempVerPipe != null && tempVerPipe.GetCurveExt().IsParallel(baseLine, 0.001) &&
  97. // pipes.All(e => e.Id.IntegerValue != tempVerPipe.Id.IntegerValue))
  98. // {
  99. // pipes.Add(tempVerPipe);
  100. // }
  101. // }
  102. //}
  103. } while (false);
  104. return pipes;
  105. }
  106. /// <summary>
  107. /// 判断管道系统是否为坡度管
  108. /// </summary>
  109. /// <param name="pipes"></param>
  110. /// <returns></returns>
  111. public static bool IsSlopPipe(this List<Pipe> pipes)
  112. {
  113. if (pipes == null || pipes.Count == 0)
  114. return false;
  115. double baseZ = pipes[0].GetPoints()[0].Z;
  116. foreach (var pipe in pipes)
  117. {
  118. List<XYZ> ends = pipe.GetPoints();
  119. if (ends.Any(xyz => !xyz.Z.IsEqual(baseZ)))
  120. {
  121. return false;
  122. }
  123. }
  124. return true;
  125. }
  126. /// <summary>
  127. /// 设置管道直径
  128. /// </summary>
  129. /// <param name="pipe"></param>
  130. /// <param name="dia">直径</param>
  131. public static void SetDiameter(this Pipe pipe, double dia)
  132. {
  133. pipe.SetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM, dia); //直径
  134. }
  135. /// <summary>
  136. /// 获取管道的管材
  137. /// </summary>
  138. /// <param name="pipe"></param>
  139. public static string GetMaterial(this Pipe pipe)
  140. {
  141. string material = pipe.GetParameterString(BuiltInParameter.RBS_PIPE_MATERIAL_PARAM);
  142. return material;
  143. }
  144. /// <summary>
  145. /// 是否是相同的系统
  146. /// </summary>
  147. /// <param name="pipe"></param>
  148. /// <param name="element"></param>
  149. /// <returns></returns>
  150. public static bool IsSameSystem(this Pipe pipe, Element element)
  151. {
  152. var elementId = pipe.GetParameterElementId(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM);
  153. if (elementId == null)
  154. return false;
  155. var refElementId = element.GetParameterElementId(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM);
  156. if (refElementId == null)
  157. return false;
  158. if (elementId.IntegerValue == -1 || refElementId.IntegerValue == -1)
  159. return false;
  160. return elementId == refElementId;
  161. }
  162. /// <summary>
  163. /// 获取坡度设置
  164. /// </summary>
  165. /// <param name="doc"></param>
  166. /// <returns></returns>
  167. public static List<double> GetSlopes(this Document doc)
  168. {
  169. List<double> slopes = new List<double>();
  170. PipeSettings setting = PipeSettings.GetPipeSettings(doc);
  171. if (setting != null)
  172. {
  173. slopes = setting.GetPipeSlopes().ToList();
  174. }
  175. return slopes;
  176. }
  177. /// <summary>
  178. /// 获取项目中所有管道类型
  179. /// </summary>
  180. /// <param name="doc"></param>
  181. /// <returns></returns>
  182. public static List<PipeType> GetPipeTypes(this Document doc)
  183. {
  184. List<PipeType> list = doc.GetElements<PipeType>();
  185. return list;
  186. }
  187. /// <summary>
  188. /// 获取管道类型的 管段 信息
  189. /// </summary>
  190. /// <param name="pipeType">管道类型</param>
  191. /// <param name="doc">所在项目</param>
  192. /// <returns></returns>
  193. public static PipeSegment GetPipeSegment(this PipeType pipeType, Document doc)
  194. {
  195. PipeSegment pipeSegment = null;
  196. RoutingPreferenceRule rule =
  197. pipeType.RoutingPreferenceManager.GetRule(RoutingPreferenceRuleGroupType.Segments, 0);
  198. if (rule == null)
  199. return pipeSegment;
  200. pipeSegment = doc.GetElement(rule.MEPPartId) as PipeSegment;
  201. return pipeSegment;
  202. }
  203. /// <summary>
  204. /// 增加管道的管段信息
  205. /// </summary>
  206. /// <param name="pipeType">管道类型</param>
  207. /// <param name="segmentId">管段Id</param>
  208. /// <returns></returns>
  209. public static int AddPipeSegment(this PipeType pipeType, ElementId segmentId)
  210. {
  211. return pipeType.AddRoutingRule(RoutingPreferenceRuleGroupType.Segments, segmentId, 0);
  212. }
  213. /// <summary>
  214. /// 移除管段类型
  215. /// </summary>
  216. /// <param name="pipeType"></param>
  217. /// <param name="index"></param>
  218. /// <returns></returns>
  219. public static int RemovePipeSegment(this PipeType pipeType, int index)
  220. {
  221. return pipeType.RemoveRoutingRule(RoutingPreferenceRuleGroupType.Segments, index);
  222. }
  223. /// <summary>
  224. /// 获取管道类型的可用直径信息
  225. /// </summary>
  226. /// <param name="pipeType">管道类型</param>
  227. /// <param name="doc">所在项目</param>
  228. /// <returns></returns>
  229. public static List<double> GetAvailableDias(this PipeType pipeType, Document doc)
  230. {
  231. PipeSegment pipeSegment = pipeType.GetPipeSegment(doc);
  232. List<double> dias = new List<double>();
  233. if (pipeSegment != null)
  234. {
  235. dias = pipeSegment.GetSizes().Select(mepSize => mepSize.NominalDiameter.MmFromFt()).ToList();
  236. }
  237. return dias;
  238. }
  239. #region 管道类型规则操作基础
  240. /// <summary>
  241. /// 增加管道优先规则
  242. /// </summary>
  243. /// <param name="pipeType">管道类型</param>
  244. /// <param name="groupType">规则分组</param>
  245. /// <param name="elementId">规则使用的element</param>
  246. /// <param name="index">规则在分组中的位置</param>
  247. /// <returns></returns>
  248. public static int AddRoutingRule(this PipeType pipeType, RoutingPreferenceRuleGroupType groupType,
  249. ElementId elementId, int index = 0)
  250. {
  251. RoutingPreferenceManager manager = pipeType.RoutingPreferenceManager;
  252. RoutingPreferenceRule rule = new RoutingPreferenceRule(elementId, "");
  253. rule.AddCriterion(PrimarySizeCriterion.All());
  254. manager.AddRule(groupType, rule, 0);
  255. return index;
  256. }
  257. /// <summary>
  258. /// 移除管道规则分组中制定位置的规则
  259. /// </summary>
  260. /// <param name="pipeType">管道类型</param>
  261. /// <param name="groupType">规则分组</param>
  262. /// <param name="index">规则在该分组中的位置</param>
  263. /// <returns></returns>
  264. public static int RemoveRoutingRule(this PipeType pipeType, RoutingPreferenceRuleGroupType groupType,
  265. int index = 0)
  266. {
  267. RoutingPreferenceManager manager = pipeType.RoutingPreferenceManager;
  268. manager.RemoveRule(groupType, index);
  269. return index;
  270. }
  271. /// <summary>
  272. /// 获取管道优先规则分组中指定位置的规则
  273. /// </summary>
  274. /// <param name="pipeType"></param>
  275. /// <param name="groupType"></param>
  276. /// <param name="index"></param>
  277. /// <returns></returns>
  278. public static RoutingPreferenceRule GetRoutingRule(this PipeType pipeType,
  279. RoutingPreferenceRuleGroupType groupType, int index = 0)
  280. {
  281. RoutingPreferenceRule rule = pipeType.RoutingPreferenceManager.GetRule(groupType, index);
  282. return rule;
  283. }
  284. public static ElementId GetRoutingRule(this PipeType pipeType, RoutingPreferenceRuleGroupType groupType,
  285. double nd)
  286. {
  287. RoutingConditions conditions = new RoutingConditions(RoutingPreferenceErrorLevel.None);
  288. conditions.AppendCondition(new RoutingCondition(nd));
  289. ElementId id = pipeType.RoutingPreferenceManager.GetMEPPartId(groupType,
  290. conditions);
  291. return id;
  292. }
  293. #endregion
  294. #region 动态修改连接件辅助使用方法
  295. /// <summary>
  296. /// 在三通规则分组第一个位置添加制定类型三通
  297. /// </summary>
  298. /// <param name="pipeType"></param>
  299. /// <param name="id"></param>
  300. /// <returns></returns>
  301. public static int AddFirstTee(this PipeType pipeType, ElementId id)
  302. {
  303. return pipeType.AddRoutingRule(RoutingPreferenceRuleGroupType.Junctions, id, 0);
  304. }
  305. /// <summary>
  306. /// 移除三通规则分组第一个位置的三通
  307. /// </summary>
  308. /// <param name="pipeType"></param>
  309. public static void RemoveFirstTee(this PipeType pipeType)
  310. {
  311. pipeType.RemoveRoutingRule(RoutingPreferenceRuleGroupType.Junctions);
  312. }
  313. /// <summary>
  314. /// 获取第一个三通
  315. /// </summary>
  316. /// <param name="pipeType"></param>
  317. /// <returns></returns>
  318. public static FamilySymbol GetFirstTee(this PipeType pipeType)
  319. {
  320. Document doc = pipeType.Document;
  321. FamilySymbol fitting = null;
  322. RoutingPreferenceRule rule = pipeType.GetRoutingRule(RoutingPreferenceRuleGroupType.Junctions);
  323. if (rule == null)
  324. return fitting;
  325. fitting = doc.GetElement(rule.MEPPartId) as FamilySymbol;
  326. return fitting;
  327. }
  328. /// <summary>
  329. /// 通过直径获取指定规则的三通,如果指定直径不存在对应三通,则默认去第一个设定的三通
  330. /// </summary>
  331. /// <param name="pipeType">管道类型</param>
  332. /// <param name="nd">直径</param>
  333. /// <returns></returns>
  334. public static FamilySymbol GetFirstTeeByNd(this PipeType pipeType, double nd)
  335. {
  336. Document doc = pipeType.Document;
  337. FamilySymbol fitting = null;
  338. ElementId elementId = pipeType.GetRoutingRule(RoutingPreferenceRuleGroupType.Junctions, nd);
  339. if (elementId == null)
  340. {
  341. RoutingPreferenceRule rule = pipeType.GetRoutingRule(RoutingPreferenceRuleGroupType.Junctions);
  342. if (rule == null)
  343. return fitting;
  344. elementId = rule.MEPPartId;
  345. }
  346. fitting = doc.GetElement(elementId) as FamilySymbol;
  347. return fitting;
  348. }
  349. #endregion
  350. }
  351. }