DuctExtension.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using Autodesk.Revit.DB;
  2. using Autodesk.Revit.DB.Mechanical;
  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 DuctExtension
  11. {
  12. /// <summary>
  13. /// 获取风管系统类型(非枚举)
  14. /// </summary>
  15. /// <param name="duct">风管</param>
  16. /// <returns></returns>
  17. public static MechanicalSystemType GetMechanicalSystemType(this Duct duct)
  18. {
  19. var id = duct.GetParameterElementId(BuiltInParameter.RBS_DUCT_SYSTEM_TYPE_PARAM);
  20. return duct.Document.GetElement(id) as MechanicalSystemType;
  21. }
  22. /// <summary>
  23. /// 获取风管系统分类
  24. /// </summary>
  25. /// <param name="duct"></param>
  26. /// <returns></returns>
  27. public static DuctSystemType GetDuctSystemType(this Duct duct)
  28. {
  29. var ductSystem = duct.MEPSystem as MechanicalSystem;
  30. if (ductSystem != null)
  31. return ductSystem.SystemType;
  32. return DuctSystemType.UndefinedSystemType;
  33. }
  34. /// <summary>
  35. /// 获取风管的朝向
  36. /// </summary>
  37. /// <param name="duct"></param>
  38. /// <returns></returns>
  39. public static XYZ GetCurveNormal(this Duct duct)
  40. {
  41. //通过connector basizY的方向去确定朝向
  42. Connector connector = duct.GetFirstConnector(c=>c.ConnectorType==ConnectorType.End);
  43. return connector.CoordinateSystem.BasisY.Negate();
  44. }
  45. /// <summary>
  46. /// 设置风管的朝向
  47. /// </summary>
  48. /// <param name="duct"></param>
  49. /// <param name="normal"></param>
  50. public static void SetCurveNormal(this Duct duct, XYZ normal)
  51. {
  52. XYZ sorce = duct.GetCurveNormal();
  53. Line axis = duct.GetLocationLine();
  54. double angle = sorce.AngleOnPlaneTo(normal, axis.Direction.Normalize());
  55. duct.Rotate(axis, angle);
  56. }
  57. /// <summary>
  58. /// 获取风管的最长尺寸
  59. /// </summary>
  60. /// <param name="duct"></param>
  61. /// <returns></returns>
  62. public static double GetLongestSize(this Duct duct)
  63. {
  64. double tempDia = duct.GetParameterDouble(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM).MmFromFt();
  65. double tempWidth = duct.GetParameterDouble(BuiltInParameter.RBS_CURVE_WIDTH_PARAM).MmFromFt();
  66. double tempHeight = duct.GetParameterDouble(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM).MmFromFt();
  67. double length = tempHeight > tempWidth ? tempHeight : tempWidth;
  68. length = tempDia > length ? tempDia : length;
  69. return length;
  70. }
  71. /// <summary>
  72. /// 判断给定的风管集合的垂直对齐方式
  73. /// </summary>
  74. /// <param name="ducts">风管集合</param>
  75. /// <returns>返回垂直对齐的标识(-1,底对齐;0,一般处理;1,顶对齐。)</returns>
  76. public static int GetVerAlignType(this List<Duct> ducts)
  77. {
  78. int flag = 0;
  79. //List<Connector> connectors = ducts.Select(d => d.GetConnectors()[0]).ToList();
  80. //List<Tuple<XYZ, XYZ, double>> listTuple = new List<Tuple<XYZ, XYZ, double>>();
  81. //foreach (var connector in connectors)
  82. //{
  83. // double length = connector.Shape == ConnectorProfileType.Round ? connector.Radius * 2 : connector.Height;
  84. // listTuple.Add(new Tuple<XYZ, XYZ, double>(connector.Origin, connector.CoordinateSystem.BasisY, length));
  85. //}
  86. //if (listTuple.Count == 0)
  87. // return flag;
  88. //Tuple<XYZ, XYZ, double> baseTuple = listTuple[0];
  89. //if (listTuple.Any(t => !t.Item2.IsParallel(baseTuple.Item2)))
  90. // return 0;
  91. //Line baseLine = baseTuple.Item1.NewLine(baseTuple.Item1.OffsetPoint(baseTuple.Item2, 1000));
  92. //XYZ baseOrigion = baseLine.GetProjectPt(baseTuple.Item1, true);
  93. //if (listTuple.All(t => baseLine.GetProjectPt(t.Item1, true).IsEqual(baseOrigion, 0.001)))
  94. //{
  95. // return 0;
  96. //}
  97. //XYZ bottom = baseTuple.Item1.OffsetPoint(baseTuple.Item2, baseTuple.Item3 / 2);
  98. //XYZ bottomProject = baseLine.GetProjectPt(bottom, true);
  99. //if (
  100. // listTuple.All(
  101. // t =>
  102. // baseLine.GetProjectPt(t.Item1.OffsetPoint(t.Item2, t.Item3 / 2), true)
  103. // .IsEqual(bottomProject, 0.001)))
  104. //{
  105. // return -1;
  106. //}
  107. //XYZ top = baseTuple.Item1.OffsetPoint(baseTuple.Item2.Negate(), baseTuple.Item3 / 2);
  108. //XYZ topProject = baseLine.GetProjectPt(top, true);
  109. //if (
  110. // listTuple.All(
  111. // t =>
  112. // baseLine.GetProjectPt(t.Item1.OffsetPoint(t.Item2.Negate(), t.Item3 / 2), true)
  113. // .IsEqual(topProject, 0.001)))
  114. //{
  115. // return 1;
  116. //}
  117. return flag;
  118. }
  119. /// <summary>
  120. /// 获取风管实例截面类型
  121. /// </summary>
  122. /// <param name="duct">指定风管</param>
  123. /// <returns>风管截面形状</returns>
  124. public static DuctShape GetDuctShape(this Duct duct)
  125. {
  126. DuctShape shape = DuctShape.Rectangular;
  127. var connector = duct.GetFirstConnector(c=>c.ConnectorType==ConnectorType.End);
  128. switch (connector.Shape)
  129. {
  130. case ConnectorProfileType.Oval:
  131. {
  132. shape = DuctShape.Oval;
  133. break;
  134. }
  135. case ConnectorProfileType.Round:
  136. {
  137. shape = DuctShape.Round;
  138. break;
  139. }
  140. case ConnectorProfileType.Rectangular:
  141. {
  142. shape = DuctShape.Rectangular;
  143. break;
  144. }
  145. }
  146. return shape;
  147. }
  148. /// <summary>
  149. /// 获取垂直风管角度
  150. /// </summary>
  151. /// <param name="duct"></param>
  152. /// <returns>如果风管垂直返回正确角度,如果风管竖直,返回π/2</returns>
  153. public static double GetVerAngle(this Duct duct)
  154. {
  155. if (duct == null)
  156. return 0;
  157. double defalut = Math.PI / 2;
  158. var line = duct.GetLocationLine();
  159. XYZ start = line.StartPoint();
  160. XYZ end = line.EndPoint();
  161. if (!start.IsEqual2(end))
  162. {
  163. return defalut;
  164. }
  165. XYZ normal = duct.GetCurveNormal();
  166. //不封装,是为了保证使用的严谨性,时刻记住是三维
  167. return XYZ.BasisX.AngleOnPlaneTo(normal, XYZ.BasisZ);
  168. }
  169. /// <summary>
  170. /// 根据界面类型获取所需要的尺寸
  171. /// </summary>
  172. /// <param name="doc"></param>
  173. /// <param name="shape"></param>
  174. /// <returns></returns>
  175. public static List<double> GetDuctSizes(this Document doc, DuctShape shape)
  176. {
  177. List<double> sizes = new List<double>();
  178. DuctSizeSettings setting = DuctSizeSettings.GetDuctSizeSettings(doc);
  179. if (setting != null)
  180. {
  181. sizes = setting[shape].Select(size => size.NominalDiameter.MmFromFt()).ToList();
  182. }
  183. return sizes;
  184. }
  185. /// <summary>
  186. /// 获取项目中设置的空气密度
  187. /// </summary>
  188. /// <param name="doc"></param>
  189. /// <returns></returns>
  190. public static double GetAirDensity(this Document doc)
  191. {
  192. double airDensity = 1;
  193. List<double> sizes = new List<double>();
  194. DuctSettings setting = DuctSettings.GetDuctSettings(doc);
  195. if (setting != null)
  196. {
  197. airDensity = setting.AirDensity;
  198. }
  199. //单位转换的问题
  200. return 0;//airDensity.FromApi(DisplayUnitType.DUT_KILOGRAMS_PER_CUBIC_METER);
  201. }
  202. /// <summary>
  203. /// 获取制定风管类型的截面类型
  204. /// </summary>
  205. /// <param name="ductType">风管类型</param>
  206. /// <returns></returns>
  207. public static DuctShape GetDuctShape(this DuctType ductType)
  208. {
  209. string strShape = ductType.GetParameterString(BuiltInParameter.ALL_MODEL_FAMILY_NAME);
  210. do
  211. {
  212. if ("矩形风管".Equals(strShape))
  213. {
  214. return DuctShape.Rectangular;
  215. }
  216. if ("圆形风管".Equals(strShape))
  217. {
  218. return DuctShape.Round;
  219. }
  220. if ("椭圆形风管".Equals(strShape))
  221. {
  222. return DuctShape.Oval;
  223. }
  224. } while (false);
  225. throw new Exception("获取风管类型截面出错");
  226. }
  227. /// <summary>
  228. /// 获取项目中所有风管类型
  229. /// </summary>
  230. /// <param name="doc"></param>
  231. /// <returns></returns>
  232. public static List<DuctType> GetDuctTypes(this Document doc)
  233. {
  234. List<DuctType> list = doc.GetElements<DuctType>();
  235. return list;
  236. }
  237. /// <summary>
  238. /// 获取项目中所有风管类型
  239. /// </summary>
  240. /// <param name="doc"></param>
  241. /// <param name="shape"></param>
  242. /// <returns></returns>
  243. public static List<DuctType> GetDuctTypes(this Document doc, DuctShape shape)
  244. {
  245. List<DuctType> list = doc.GetDuctTypes();
  246. return list.Where(type => type.GetDuctShape() == shape).ToList();
  247. }
  248. /// <summary>
  249. /// 获取所有的软管类型
  250. /// </summary>
  251. /// <param name="doc"></param>
  252. /// <returns></returns>
  253. public static List<FlexDuctType> GetFlexDuctTypes(this Document doc)
  254. {
  255. List<FlexDuctType> list = doc.GetElements<FlexDuctType>();
  256. return list;
  257. }
  258. }
  259. }