123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Forms;
- using Autodesk.Revit.DB;
- namespace SAGA.RevitUtils
- {
- public class CommonMep
- {
- /// <summary>
- /// 获取与指定Connector相连的Connector(端点相连)
- /// </summary>
- public static Connector GetConnectConnector(Connector connector)
- {
- Connector conTarget = null;
- if (connector.IsConnected)
- {
- ConnectorSetIterator conSetIter = connector.AllRefs.ForwardIterator();
- while (conSetIter.MoveNext())
- {
- Connector con = conSetIter.Current as Connector;
- if (con != null && con.Owner.UniqueId != connector.Owner.UniqueId && con.ConnectorType != ConnectorType.Logical)
- {
- conTarget = con;
- break;
- }
- }
- }
- return conTarget;
- }
- #region 仅两个Connector
- ///// <summary>
- ///// 获取相连的所有构件
- ///// </summary>
- //public static List<Element> GetConnectedElements(Element element, Connector exclude, bool first)
- //{
- // List<Element> list = new List<Element>();
- // ConnectorSet set = null;
- // if (element is MEPCurve)
- // {
- // MEPCurve mep = element as MEPCurve;
- // set = mep.ConnectorManager.Connectors;
- // }
- // else if (element is FamilyInstance)
- // {
- // FamilyInstance fi = element as FamilyInstance;
- // if (fi.MEPModel != null)
- // {
- // set = fi.MEPModel.ConnectorManager.Connectors;
- // }
- // }
- // if (set == null)
- // {
- // MessageBox.Show("选择图元不符合要求。");
- // return null;
- // }
- // foreach (Connector con in set)
- // {
- // if (con.IsConnected)
- // {
- // //需要排除的Connector
- // if (exclude != null && exclude.Origin.IsAlmostEqualTo(con.Origin))
- // {
- // continue;
- // }
- // Connector conn = GetConnectConnector(con);
- // Element owner = conn.Owner;
- // list.Add(owner);
- // List<Element> find = GetConnectedElements(owner, conn, false);
- // if (find.Count > 0)
- // {
- // list.AddRange(find);
- // }
- // }
- // }
- // if (first)
- // {
- // list.Add(element);
- // }
- // return list;
- //}
- #endregion //仅两个Connector
- #region 交叉闭合出错
- ///// <summary>
- ///// 获取相连的所有构件
- ///// </summary>
- //public static List<Element> GetConnectedElements(Element element, List<Connector> excludes, bool first)
- //{
- // if (excludes == null)
- // {
- // excludes = new List<Connector>();
- // }
- // List<Element> list = new List<Element>();
- // ConnectorSet set = null;
- // var curve = element as MEPCurve;
- // if (curve != null)
- // {
- // MEPCurve mep = curve;
- // set = mep.ConnectorManager.Connectors;
- // }
- // else
- // {
- // FamilyInstance fi = element as FamilyInstance;
- // if (fi?.MEPModel != null)
- // {
- // set = fi.MEPModel.ConnectorManager.Connectors;
- // }
- // }
- // if (set == null)
- // {
- // MessageBox.Show("选择图元不符合要求。");
- // return null;
- // }
- // foreach (Connector con in set)
- // {
- // if (con != null && con.IsConnected)
- // {
- // bool has = excludes.Any(connector => connector != null && connector.Origin.IsAlmostEqualTo(con.Origin));
- // if (has)
- // {
- // continue;
- // }
- // excludes.Add(con);
- // var conn = GetConnectConnector(con);
- // var owner = conn.Owner;
- // var used = new List<Connector> {conn};
- // list.Add(owner);
- // var find = GetConnectedElements(owner, used, false);
- // if (find.Count > 0)
- // {
- // list.AddRange(find);
- // }
- // }
- // }
- // if (first)
- // {
- // list.Add(element);
- // }
- // return list;
- //}
- #endregion //交叉闭合出错
- /// <summary>
- /// 获取相连的所有构件
- /// </summary>
- public static List<Element> GetConnectedElements(Element element, List<Connector> excludes,List<Element> exElements, bool first)
- {
- if (excludes == null)
- {
- excludes = new List<Connector>();
- }
- if (exElements == null)
- {
- exElements=new List<Element>();
- }
- List<Element> list = new List<Element>();
- if (first)
- {
- list.Add(element);
- exElements.Add(element);
- }
- ConnectorSet set = null;
- var curve = element as MEPCurve;
- if (curve != null)
- {
- MEPCurve mep = curve;
- set = mep.ConnectorManager.Connectors;
- }
- else
- {
- FamilyInstance fi = element as FamilyInstance;
- if (fi?.MEPModel != null)
- {
- set = fi.MEPModel.ConnectorManager.Connectors;
- }
- }
- if (set == null)
- {
- MessageBox.Show("选择图元不符合要求。");
- return null;
- }
- foreach (Connector con in set)
- {
- if (con != null && con.IsConnected)
- {
- bool has = excludes.Any(connector => connector != null && connector.Origin.IsAlmostEqualTo(con.Origin));
- if (has)
- {
- continue;
- }
- excludes.Add(con);
- var conn = GetConnectConnector(con);
- var owner = conn.Owner;
- bool exist = exElements.Any(e => e.Id.IntegerValue.Equals(owner.Id.IntegerValue));
- if (!exist)
- {
- exElements.Add(owner);
- }
- else
- {
- continue;
- }
- var used = new List<Connector> { conn };
- list.Add(owner);
- var find = GetConnectedElements(owner, used, exElements, false);
- if (find.Count > 0)
- {
- list.AddRange(find);
- }
- }
- }
- return list;
- }
- }
- }
|