ConnectorPatternUtils.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Autodesk.Revit.DB;
  2. using FWindSoft.SystemExtensions;
  3. using FWindSoft.Tools;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace FWindSoft.Revit.Mep
  10. {
  11. /// <summary>
  12. /// 连接点模式判断相关函数
  13. /// </summary>
  14. public class ConnectorPatternUtils
  15. {
  16. /// <summary>
  17. /// 判定连接点连接模式
  18. /// </summary>
  19. /// <param name="connector1"></param>
  20. /// <param name="connector2"></param>
  21. /// <param name="connector3"></param>
  22. /// <param name="connectors">返回调整顺序后的连接点;T形顺序为前两个平行</param>
  23. /// <returns>-1:未识别;0:普通三通;1:角三通</returns>
  24. public static int JudgeTeePattern(Connector connector1, Connector connector2, Connector connector3, out List<Connector> connectors)
  25. {
  26. int flag = -1;
  27. connectors = new List<Connector>();
  28. //将互相垂直的放在第1,3位置。第二个位置尽量放平行相关的
  29. List<Connector> inputConnectors = new List<Connector>() { connector1, connector2, connector3 };
  30. var tuple = PermutationUtil.PermutationTraversal(inputConnectors, 3, (useConnectors) => {
  31. List<Connector> reConnectors = new List<Connector>();
  32. var innerFlag = -1;
  33. var dir1 = useConnectors[0].CoordinateSystem.BasisZ;
  34. var dir2 = useConnectors[1].CoordinateSystem.BasisZ;
  35. var dir3 = useConnectors[2].CoordinateSystem.BasisZ;
  36. if (dir1.IsVertical(dir2))
  37. {
  38. if (dir1.IsVertical(dir3) && dir2.IsVertical(dir3))
  39. {
  40. reConnectors.AddRange(new Connector[] { useConnectors[0], useConnectors[1], useConnectors[2] });
  41. innerFlag = 1;
  42. }
  43. else if (dir3.IsVertical(dir2))
  44. {
  45. //存在共面的连接管,放到主位置上
  46. reConnectors.AddRange(new Connector[] { useConnectors[0], useConnectors[2], useConnectors[1] });
  47. innerFlag = 0;
  48. }
  49. }
  50. else if (dir1.IsParallel(dir2))
  51. {
  52. //存在平行
  53. reConnectors.AddRange(new Connector[] { useConnectors[0], useConnectors[1], useConnectors[2] });
  54. innerFlag = 0;
  55. }
  56. if (innerFlag != -1)
  57. {
  58. return new Tuple<int, List<Connector>>(innerFlag, reConnectors);
  59. }
  60. return null;
  61. });
  62. if (tuple != null)
  63. {
  64. flag = tuple.Item1;
  65. connectors = tuple.Item2;
  66. }
  67. return flag;
  68. }
  69. /// <summary>
  70. /// 整理成三通创建需要的Connector模式(第一个Connector和第三个Connector连接方向不能平行)
  71. /// </summary>
  72. /// <param name="connector1"></param>
  73. /// <param name="connector2"></param>
  74. /// <param name="connector3"></param>
  75. /// <returns></returns>
  76. public static List<Connector> ArrangeTeeConnectors(Connector connector1, Connector connector2, Connector connector3)
  77. {
  78. var flag = JudgeTeePattern(connector1, connector2, connector3, out List<Connector> newConnectors);
  79. if (flag == -1)
  80. {
  81. newConnectors.Clear();
  82. #region 处理非正交情况
  83. var baseDirection = -connector1.CoordinateSystem.BasisZ;
  84. var dotValue2 = Math.Abs((-connector2.CoordinateSystem.BasisZ).DotProduct(baseDirection));
  85. var dotValue3 = Math.Abs((-connector3.CoordinateSystem.BasisZ).DotProduct(baseDirection));
  86. if (dotValue2.IsEqual(dotValue3))
  87. {
  88. //基准变成垂直的
  89. newConnectors.Add(connector2);
  90. newConnectors.Add(connector3);
  91. newConnectors.Add(connector1);
  92. }
  93. else if (dotValue2.Less(dotValue3))
  94. {
  95. //绝对值小,接近垂直。将垂直的放在第三个位置
  96. newConnectors.Add(connector1);
  97. newConnectors.Add(connector3);
  98. newConnectors.Add(connector2);
  99. }
  100. else
  101. {
  102. newConnectors.Add(connector1);
  103. newConnectors.Add(connector2);
  104. newConnectors.Add(connector3);
  105. }
  106. #region 过时
  107. //if (!connector1.CoordinateSystem.BasisZ.IsParallel(connector2.CoordinateSystem.BasisZ))
  108. //{
  109. // newConnectors.Add(connector1);
  110. // newConnectors.Add(connector3);
  111. // newConnectors.Add(connector2);
  112. //}
  113. //else if (!connector1.CoordinateSystem.BasisZ.IsParallel(connector3.CoordinateSystem.BasisZ))
  114. //{
  115. // newConnectors.Add(connector1);
  116. // newConnectors.Add(connector2);
  117. // newConnectors.Add(connector3);
  118. //}
  119. //else if (!connector2.CoordinateSystem.BasisZ.IsParallel(connector3.CoordinateSystem.BasisZ))
  120. //{
  121. // newConnectors.Add(connector2);
  122. // newConnectors.Add(connector1);
  123. // newConnectors.Add(connector3);
  124. //}
  125. #endregion
  126. #endregion
  127. }
  128. return newConnectors;
  129. }
  130. }
  131. }