123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- using Autodesk.Revit.DB;
- using FWindSoft.SystemExtensions;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace FWindSoft.Revit
- {
- public static class ConnectorExtension
- {
- static ConnectorExtension()
- {
- InitMepComponment();
- }
- #region 缓存设置
- private static List<int> m_MepComponment;
- private static void InitMepComponment()
- {
- m_MepComponment= new List<int>() { (int)BuiltInCategory.OST_PipeCurves, (int)BuiltInCategory.OST_PipeFitting, (int)BuiltInCategory.OST_DuctCurves, (int)BuiltInCategory.OST_DuctFitting };
- }
- #endregion
- #region 节点获取相关
- /// <summary>
- /// 获取ConnectorSet
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- private static ConnectorSet GetConnectorSet(this Element element)
- {
- if (element != null)
- {
- FamilyInstance instance = element as FamilyInstance;
- if (instance!=null && instance.MEPModel?.ConnectorManager != null)
- {
- return instance.MEPModel.ConnectorManager.Connectors;
- }
- MEPSystem system = element as MEPSystem;
- if (system != null)
- {
- return system.ConnectorManager.Connectors;
- }
- MEPCurve curve = element as MEPCurve;
- if (curve!=null)
- {
- return curve.ConnectorManager.Connectors;
- }
- }
- return null;
- }
- /// <summary>
- /// 获取EndConnector
- /// </summary>
- /// <param name="connectorSet"></param>
- /// <returns></returns>
- public static List<Connector> ToList(this ConnectorSet connectorSet)
- {
- List<Connector> result = new List<Connector>();
- try
- {
- if (connectorSet != null)
- {
- ConnectorSetIterator iterator = connectorSet.ForwardIterator();
- while (iterator.MoveNext())
- {
- Connector current = iterator.Current as Connector;
- if (current.IsPhysical())
- {
- result.Add(current);
- }
- }
- }
- return result;
- }
- catch
- {
- }
- return result;
- }
- /// <summary>
- /// 获取第一个满足条件的Connector,条件可以为空
- /// </summary>
- /// <param name="element"></param>
- /// <param name="predicate"></param>
- /// <returns></returns>
- public static Connector GetFirstConnector(this Element element,Predicate<Connector> predicate)
- {
- var set = element.GetConnectorSet();
- Connector result = null;
- try
- {
- if (set != null)
- {
- ConnectorSetIterator iterator = set.ForwardIterator();
- while (iterator.MoveNext())
- {
- Connector current = iterator.Current as Connector;
- if (current.IsPhysical()&& (predicate==null|| predicate(current)))
- {
- return current;
- }
- }
- }
- return result;
- }
- catch
- {
- }
- return result;
- }
- /// <summary>
- /// 获取指定类型的Connector
- /// </summary>
- /// <param name="element"></param>
- /// <param name="domain"></param>
- /// <returns></returns>
- public static List<Connector> GetConnectors(this Element element, Domain domain)
- {
- return element.GetConnectorSet().ToList().Where(c=>c.Domain==domain).ToList();
- }
- /// <summary>
- /// 获取指定类型的所有可用Connector
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- public static List<Connector> GetAllConnectors(this Element element)
- {
- return element.GetConnectorSet().ToList();
- }
- #endregion
- #region 逻辑相关处理
- /// <summary>
- /// 标志connector是否是可被检索的
- /// </summary>
- /// <param name="connector"></param>
- /// <returns></returns>
- public static bool Searchable(this Connector connector)
- {
- return connector.IsPhysical() &&connector.IsConnected && connector.AllRefs != null;
- }
- #endregion
- /// <summary>
- /// 是否是物理节点
- /// </summary>
- /// <param name="connector"></param>
- /// <returns></returns>
- public static bool IsPhysical(this Connector connector)
- {
- return ConnectorType.Physical.HasFlag(connector.ConnectorType);
- }
- /// <summary>
- /// 获取相连的第一个Connector
- /// </summary>
- /// <param name="connector"></param>
- /// <returns></returns>
- public static Connector GetJoinConnector(this Connector connector)
- {
- return connector.GetJoinConnectors().FirstOrDefault();
- }
- /// <summary>
- /// 获取相连的Connector;
- /// </summary>
- /// <param name="connector"></param>
- /// <returns></returns>
- public static List<Connector> GetJoinConnectors(this Connector connector)
- {
- return GetJoinConnectors(connector, false);
- }
- /// <summary>
- /// 获取相连的Connector;
- /// </summary>
- /// <param name="connector"></param>
- /// <returns></returns>
- public static List<Connector> GetJoinConnectors(this Connector connector,bool containInputOwner)
- {
- List<Connector> list = new List<Connector>();
- if (connector.AllRefs != null)
- {
- var cons = connector.AllRefs.ToList();
- if(!containInputOwner)
- {
- return cons.Where(t =>t.Owner.Id!=(connector.Owner.Id)).ToList();
- }
- }
- return list;
- }
- /// <summary>
- ///根据点获取Connector
- /// </summary>
- /// <param name="element"></param>
- /// <param name="origin"></param>
- /// <returns></returns>
- public static Connector GetConnectorByOrigin(this Element element, XYZ origin)
- {
- var connectors = element.GetAllConnectors();
- return connectors.FirstOrDefault(c => c.ConnectorType == ConnectorType.End && c.Origin.IsEqual(origin));
- }
- /// <summary>
- /// 根据方向获取Connector
- /// </summary>
- /// <param name="element"></param>
- /// <param name="direction"></param>
- /// <returns></returns>
- public static Connector GetConnectorByDirection(this Element element, XYZ direction)
- {
- var connectors = element.GetAllConnectors();
- return connectors.FirstOrDefault(c => c.ConnectorType == ConnectorType.End && c.CoordinateSystem.BasisZ.IsSameDirection(direction));
- }
- /// <summary>
- /// 获取与给定方向,夹角最小的点
- /// </summary>
- /// <param name="element"></param>
- /// <param name="direction"></param>
- /// <returns></returns>
- public static Connector GetConnectorByMinAngle(this Element element, XYZ direction)
- {
- var connectors = element.GetAllConnectors();
- Connector useConnector = null;
- double angle = double.MinValue;
- foreach (Connector connector in connectors)
- {
- var tempDirection = connector.CoordinateSystem.BasisZ;
- var tempAngle = direction.DotProduct(tempDirection);
- //点乘值越大,角度越小
- if (tempAngle.More(angle))
- {
- useConnector = connector;
- angle = tempAngle;
- }
- }
- return useConnector;
- }
- /// <summary>
- /// 获取给点附近的Connector
- /// </summary>
- /// <param name="element"></param>
- /// <param name="position"></param>
- /// <returns></returns>
- public static Connector GetNearConnector(this Element element, XYZ position)
- {
- var connectors = element.GetAllConnectors();
- Connector resultConnector = null;
- double minDistance = double.MaxValue;
- foreach (var connector in connectors)
- {
- if (connector.ConnectorType != ConnectorType.End)
- {
- //只计算端点
- continue;
- }
- var tempLength = connector.Origin.DistanceTo(position);
- if (tempLength < minDistance)
- {
- resultConnector = connector;
- minDistance = tempLength;
- }
- }
- return resultConnector;
- }
- }
- }
|