123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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
- {
- /// <summary>
- /// 连接点模式判断相关函数
- /// </summary>
- public class ConnectorPatternUtils
- {
- /// <summary>
- /// 判定连接点连接模式
- /// </summary>
- /// <param name="connector1"></param>
- /// <param name="connector2"></param>
- /// <param name="connector3"></param>
- /// <param name="connectors">返回调整顺序后的连接点;T形顺序为前两个平行</param>
- /// <returns>-1:未识别;0:普通三通;1:角三通</returns>
- public static int JudgeTeePattern(Connector connector1, Connector connector2, Connector connector3, out List<Connector> connectors)
- {
- int flag = -1;
- connectors = new List<Connector>();
- //将互相垂直的放在第1,3位置。第二个位置尽量放平行相关的
- List<Connector> inputConnectors = new List<Connector>() { connector1, connector2, connector3 };
- var tuple = PermutationUtil.PermutationTraversal(inputConnectors, 3, (useConnectors) => {
- List<Connector> reConnectors = new List<Connector>();
- 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<int, List<Connector>>(innerFlag, reConnectors);
- }
- return null;
- });
- if (tuple != null)
- {
- flag = tuple.Item1;
- connectors = tuple.Item2;
- }
- return flag;
- }
- /// <summary>
- /// 整理成三通创建需要的Connector模式(第一个Connector和第三个Connector连接方向不能平行)
- /// </summary>
- /// <param name="connector1"></param>
- /// <param name="connector2"></param>
- /// <param name="connector3"></param>
- /// <returns></returns>
- public static List<Connector> ArrangeTeeConnectors(Connector connector1, Connector connector2, Connector connector3)
- {
- var flag = JudgeTeePattern(connector1, connector2, connector3, out List<Connector> 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;
- }
- }
- }
|