ConnectorUtils.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using Autodesk.Revit.DB;
  2. using FWindSoft.Data;
  3. using FWindSoft.SystemExtensions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using HorizontalAlign = FWindSoft.Data.HorizontalAlign;
  10. namespace FWindSoft.Revit.Utils
  11. {
  12. public static class ConnectorUtils
  13. {
  14. /// <summary>
  15. ///根据点获取Connector
  16. /// </summary>
  17. /// <param name="connectors"></param>
  18. /// <param name="origin"></param>
  19. /// <returns></returns>
  20. public static Connector GetConnectorByOrigin(List<Connector> connectors, XYZ origin)
  21. {
  22. return connectors.FirstOrDefault(c => c.ConnectorType == ConnectorType.End && c.Origin.IsEqual(origin));
  23. }
  24. /// <summary>
  25. /// 根据方向获取Connector
  26. /// </summary>
  27. /// <param name="connectors"></param>
  28. /// <param name="direction"></param>
  29. /// <returns></returns>
  30. public static Connector GetConnectorByDirection(List<Connector> connectors, XYZ direction)
  31. {
  32. return connectors.FirstOrDefault(c => c.ConnectorType == ConnectorType.End && c.CoordinateSystem.BasisZ.IsSameDirection(direction));
  33. }
  34. /// <summary>
  35. /// 获取与给定方向,夹角最小的点
  36. /// </summary>
  37. /// <param name="connectors"></param>
  38. /// <param name="direction"></param>
  39. /// <returns></returns>
  40. public static Connector GetConnectorByMinAngle(List<Connector> connectors, XYZ direction)
  41. {
  42. return GetConnectorByMinAngle(connectors, direction, double.MinValue);
  43. }
  44. /// <summary>
  45. /// 获取与给定方向,夹角最小的点
  46. /// </summary>
  47. /// <param name="connectors"></param>
  48. /// <param name="direction"></param>
  49. /// <param name="minAngleCosValue">夹角下限的Cos值,如果direction不是单位向量,该值未direction.length*夹角下限cos值</param>
  50. /// <returns></returns>
  51. public static Connector GetConnectorByMinAngle(List<Connector> connectors, XYZ direction, double minAngleCosValue)
  52. {
  53. Connector useConnector = null;
  54. double angle = minAngleCosValue;// double.MinValue;
  55. foreach (Connector connector in connectors)
  56. {
  57. var tempDirection = connector.CoordinateSystem.BasisZ;
  58. var tempAngle = direction.DotProduct(tempDirection);
  59. //点乘值越大,角度越小
  60. if (tempAngle.More(angle))
  61. {
  62. useConnector = connector;
  63. angle = tempAngle;
  64. }
  65. }
  66. return useConnector;
  67. }
  68. /// <summary>
  69. /// 获取给点附近的Connector
  70. /// </summary>
  71. /// <param name="connectors"></param>
  72. /// <param name="position"></param>
  73. /// <returns></returns>
  74. public static Connector GetNearConnector(List<Connector> connectors, XYZ position)
  75. {
  76. Connector resultConnector = null;
  77. double minDistance = double.MaxValue;
  78. foreach (var connector in connectors)
  79. {
  80. if (connector.ConnectorType != ConnectorType.End)
  81. {
  82. //只计算端点
  83. continue;
  84. }
  85. var tempLength = connector.Origin.DistanceTo(position);
  86. if (tempLength < minDistance)
  87. {
  88. resultConnector = connector;
  89. minDistance = tempLength;
  90. }
  91. }
  92. return resultConnector;
  93. }
  94. /// <summary>
  95. /// 计算两个风管的矩形对齐方式
  96. /// </summary>
  97. /// <param name="baseConnector"></param>
  98. /// <param name="otherConnector"></param>
  99. /// <returns></returns>
  100. public static RectangleAlignment CalcAlignment(Connector baseConnector, Connector otherConnector)
  101. {
  102. #region 确定Connector基准数据
  103. var firstConnector = baseConnector;
  104. var secondConnector = otherConnector;
  105. //如果两个Connector中出现圆形和非圆形的组合,则让非圆形Connector充当基准点
  106. if (firstConnector.Shape == ConnectorProfileType.Round && secondConnector.Shape != ConnectorProfileType.Round)
  107. {
  108. firstConnector = otherConnector;
  109. secondConnector = baseConnector;
  110. }
  111. #endregion
  112. #region 判断规则
  113. /*
  114. 1、Y平行,判断上下对齐方式
  115. 2、X平行,判断左右对齐方式
  116. 不平行的情况下,取居中设定
  117. Connector中上为-Y,左为-X
  118. 如果是圆形管道的话,要自定义上下,左右的方向;
  119. 圆形判定:中心点位置相同(连线与延伸方向平行),则为中心对齐。
  120. 两个中心在投影到平面内,做一个方向为左右对齐方向;
  121. 特殊如果,中心延长线与世界坐标Z轴垂直,则取Z轴为上下对齐方式
  122. */
  123. #endregion
  124. if (firstConnector.Shape == ConnectorProfileType.Round && secondConnector.Shape == ConnectorProfileType.Round)
  125. {
  126. //都是圆形特殊处理,圆形形式上而言只有中心对齐和边对齐
  127. #region 圆形连接件判定
  128. if (firstConnector.CoordinateSystem.BasisZ.IsParallel(secondConnector.CoordinateSystem.BasisZ))
  129. {
  130. var secondProject = secondConnector.Origin.GetPlaneProject(firstConnector.CoordinateSystem.BasisZ, firstConnector.Origin);
  131. var linkVector = secondProject - firstConnector.Origin;
  132. //简化处理
  133. #region 圆形构件对齐处理,中心对齐,或者左中对齐
  134. if (linkVector.IsParallel(secondConnector.CoordinateSystem.BasisZ))
  135. {
  136. return RectangleAlignment.CenterMiddle;
  137. }
  138. else
  139. {
  140. if (firstConnector.Radius.IsEqual(secondConnector.Radius + linkVector.GetLength()))
  141. {
  142. return RectangleAlignment.LeftMiddle;
  143. }
  144. }
  145. return RectangleAlignment.CenterMiddle;
  146. #endregion
  147. }
  148. else
  149. {
  150. //返回边对齐形式,边对齐是左右对齐。
  151. var baseDirection = firstConnector.CoordinateSystem.BasisZ.CrossProduct(secondConnector.CoordinateSystem.BasisZ);
  152. var secondProject = secondConnector.Origin.GetLineProject(baseDirection, firstConnector.Origin);
  153. var linkVector = secondProject - firstConnector.Origin;
  154. if (linkVector.GetLength().IsEqual(0))
  155. {
  156. return RectangleAlignment.CenterMiddle;
  157. }
  158. else if (firstConnector.Radius.IsEqual(secondConnector.Radius + linkVector.GetLength()))
  159. {
  160. return RectangleAlignment.LeftMiddle;
  161. }
  162. return RectangleAlignment.CenterMiddle;
  163. }
  164. #endregion
  165. }
  166. else
  167. {
  168. bool useUpDown = false, useLeftRight = false;
  169. XYZ upDownDirection = null, leftRightDirection = null;
  170. #region 方形连接件判定
  171. if (firstConnector.CoordinateSystem.BasisY.IsParallel(secondConnector.CoordinateSystem.BasisY))
  172. {
  173. useUpDown = true;
  174. upDownDirection = firstConnector.CoordinateSystem.BasisY.Negate();
  175. }
  176. if (firstConnector.CoordinateSystem.BasisX.IsParallel(secondConnector.CoordinateSystem.BasisX))
  177. {
  178. useLeftRight = true;
  179. leftRightDirection = firstConnector.CoordinateSystem.BasisX.Negate();
  180. }
  181. if (!useUpDown && !useLeftRight)
  182. {
  183. //默认返回正中对齐样式
  184. return RectangleAlignment.CenterMiddle;
  185. }
  186. #region 判断上下对齐
  187. VerticalAlign verticalAlignment = VerticalAlign.Middle;
  188. if (useUpDown)
  189. {
  190. var baseDirection = upDownDirection.Normalize();
  191. var secondProject = secondConnector.Origin.GetLineProject(baseDirection, firstConnector.Origin);
  192. var linkVector = secondProject - firstConnector.Origin;
  193. do
  194. {
  195. var secondDotValue = linkVector.DotProduct(baseDirection);
  196. if (secondDotValue.IsEqual(0))
  197. {
  198. verticalAlignment = VerticalAlign.Middle;
  199. break;
  200. }
  201. var firstLength = firstConnector.Height / 2;
  202. var secondLength = secondConnector.Shape == ConnectorProfileType.Round ? secondConnector.Radius : (secondConnector.Height / 2);
  203. if (firstLength.IsEqual(secondLength + secondDotValue))
  204. {
  205. verticalAlignment = VerticalAlign.Top;
  206. break;
  207. }
  208. if ((-firstLength).IsEqual(-secondLength + secondDotValue))
  209. {
  210. verticalAlignment = VerticalAlign.Bottom;
  211. break;
  212. }
  213. } while (false);
  214. }
  215. #endregion
  216. #region 判断左对齐
  217. HorizontalAlign horizontalAlignment = HorizontalAlign.Center;
  218. if (useLeftRight)
  219. {
  220. var baseDirection = leftRightDirection.Normalize();
  221. var secondProject = secondConnector.Origin.GetLineProject(baseDirection, firstConnector.Origin);
  222. var linkVector = secondProject - firstConnector.Origin;
  223. do
  224. {
  225. var secondDotValue = linkVector.DotProduct(baseDirection);
  226. if (secondDotValue.IsEqual(0))
  227. {
  228. horizontalAlignment = HorizontalAlign.Center;
  229. break;
  230. }
  231. var firstLength = firstConnector.Width / 2;
  232. var secondLength = secondConnector.Shape == ConnectorProfileType.Round ? secondConnector.Radius : (secondConnector.Width / 2);
  233. if (firstLength.IsEqual(secondLength + secondDotValue))
  234. {
  235. horizontalAlignment = HorizontalAlign.Left;
  236. break;
  237. }
  238. if ((-firstLength).IsEqual(-secondLength + secondDotValue))
  239. {
  240. horizontalAlignment = HorizontalAlign.Right;
  241. break;
  242. }
  243. } while (false);
  244. }
  245. #endregion
  246. #endregion
  247. return (RectangleAlignment)((int)horizontalAlignment | (int)verticalAlignment);
  248. }
  249. return RectangleAlignment.CenterMiddle;
  250. }
  251. }
  252. }