using Autodesk.Revit.DB; using FWindSoft.SystemExtensions; using FWindSoft.Tools; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FWindSoft.Revit.Mep { /// /// 连接点模式判断相关函数 /// public class ConnectorPatternUtils { /// /// 判定连接点连接模式 /// /// /// /// /// 返回调整顺序后的连接点;T形顺序为前两个平行 /// -1:未识别;0:普通三通;1:角三通 public static int JudgeTeePattern(Connector connector1, Connector connector2, Connector connector3, out List connectors) { int flag = -1; connectors = new List(); //将互相垂直的放在第1,3位置。第二个位置尽量放平行相关的 List inputConnectors = new List() { connector1, connector2, connector3 }; var tuple = PermutationUtil.PermutationTraversal(inputConnectors, 3, (useConnectors) => { List reConnectors = new List(); var innerFlag = -1; var dir1 = useConnectors[0].CoordinateSystem.BasisZ; var dir2 = useConnectors[1].CoordinateSystem.BasisZ; var dir3 = useConnectors[2].CoordinateSystem.BasisZ; if (dir1.IsVertical(dir2)) { if (dir1.IsVertical(dir3) && dir2.IsVertical(dir3)) { reConnectors.AddRange(new Connector[] { useConnectors[0], useConnectors[1], useConnectors[2] }); innerFlag = 1; } else if (dir3.IsVertical(dir2)) { //存在共面的连接管,放到主位置上 reConnectors.AddRange(new Connector[] { useConnectors[0], useConnectors[2], useConnectors[1] }); innerFlag = 0; } } else if (dir1.IsParallel(dir2)) { //存在平行 reConnectors.AddRange(new Connector[] { useConnectors[0], useConnectors[1], useConnectors[2] }); innerFlag = 0; } if (innerFlag != -1) { return new Tuple>(innerFlag, reConnectors); } return null; }); if (tuple != null) { flag = tuple.Item1; connectors = tuple.Item2; } return flag; } /// /// 整理成三通创建需要的Connector模式(第一个Connector和第三个Connector连接方向不能平行) /// /// /// /// /// public static List ArrangeTeeConnectors(Connector connector1, Connector connector2, Connector connector3) { var flag = JudgeTeePattern(connector1, connector2, connector3, out List newConnectors); if (flag == -1) { newConnectors.Clear(); #region 处理非正交情况 var baseDirection = -connector1.CoordinateSystem.BasisZ; var dotValue2 = Math.Abs((-connector2.CoordinateSystem.BasisZ).DotProduct(baseDirection)); var dotValue3 = Math.Abs((-connector3.CoordinateSystem.BasisZ).DotProduct(baseDirection)); if (dotValue2.IsEqual(dotValue3)) { //基准变成垂直的 newConnectors.Add(connector2); newConnectors.Add(connector3); newConnectors.Add(connector1); } else if (dotValue2.Less(dotValue3)) { //绝对值小,接近垂直。将垂直的放在第三个位置 newConnectors.Add(connector1); newConnectors.Add(connector3); newConnectors.Add(connector2); } else { newConnectors.Add(connector1); newConnectors.Add(connector2); newConnectors.Add(connector3); } #region 过时 //if (!connector1.CoordinateSystem.BasisZ.IsParallel(connector2.CoordinateSystem.BasisZ)) //{ // newConnectors.Add(connector1); // newConnectors.Add(connector3); // newConnectors.Add(connector2); //} //else if (!connector1.CoordinateSystem.BasisZ.IsParallel(connector3.CoordinateSystem.BasisZ)) //{ // newConnectors.Add(connector1); // newConnectors.Add(connector2); // newConnectors.Add(connector3); //} //else if (!connector2.CoordinateSystem.BasisZ.IsParallel(connector3.CoordinateSystem.BasisZ)) //{ // newConnectors.Add(connector2); // newConnectors.Add(connector1); // newConnectors.Add(connector3); //} #endregion #endregion } return newConnectors; } } }