|
@@ -0,0 +1,108 @@
|
|
|
+/*-------------------------------------------------------------------------
|
|
|
+ * 功能描述:SystemGraphUtil
|
|
|
+ * 作者:xulisong
|
|
|
+ * 创建时间: 2019/2/14 10:28:28
|
|
|
+ * 版本号:v1.0
|
|
|
+ * -------------------------------------------------------------------------*/
|
|
|
+
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using Autodesk.Revit.DB;
|
|
|
+using SAGA.RevitUtils;
|
|
|
+using SAGA.RevitUtils.Data.Graph;
|
|
|
+using SAGA.RevitUtils.MEP;
|
|
|
+
|
|
|
+namespace SAGA.GplotRelationComputerManage
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// 系统图使用单元
|
|
|
+ /// </summary>
|
|
|
+ public static class SystemGraphUtil
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// 根据起点创建系统图
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="startElement"></param>
|
|
|
+ /// <param name="ignoredConnector">忽略的Connector</param>
|
|
|
+ /// <param name="breakCondition">当connector数量不满足,却又想识别成节点的元素控制</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static SystemGraph CreateGraph(Element startElement,Predicate<Connector> ignoredConnector, Predicate<Element> breakCondition)
|
|
|
+ {
|
|
|
+ SystemGraph graph = new SystemGraph();
|
|
|
+ Queue<JoinElement> joinElements = new Queue<JoinElement>();
|
|
|
+ joinElements.Enqueue(CreateJoinElement(startElement, ignoredConnector, new List<int>()));
|
|
|
+ while (joinElements.Any())
|
|
|
+ {
|
|
|
+ var current = joinElements.Peek();
|
|
|
+ var joinItem = current.MoveNext();
|
|
|
+ if (joinItem == null)
|
|
|
+ {
|
|
|
+ joinElements.Dequeue();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ var pathElements = joinItem.BaseElement.GetPathElements(joinItem.BaseConnector, breakCondition);
|
|
|
+ if (pathElements.Count < 2)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ #region 添加边
|
|
|
+ var start = pathElements[0];
|
|
|
+ var end = pathElements[pathElements.Count - 1];
|
|
|
+ var startVertex = graph.AddVertex(new SystemData(start));
|
|
|
+ var endVertex = graph.AddVertex(new SystemData(end));
|
|
|
+ var edge = graph.AddEdge(new SystemEdge(new SystemData(pathElements.GetRange(1, pathElements.Count - 2))));
|
|
|
+ edge.StartVertex = startVertex.Id;
|
|
|
+ edge.EndVertex = endVertex.Id;
|
|
|
+ edge = graph.AddEdge(edge);
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region 延伸遍历项
|
|
|
+ var existJoinElement = joinElements.FirstOrDefault(j => j.Element.Id == end.Id);
|
|
|
+ if (existJoinElement == null)
|
|
|
+ {
|
|
|
+ existJoinElement = CreateJoinElement(startElement, ignoredConnector, new List<int>(){ });
|
|
|
+ joinElements.Enqueue(existJoinElement);
|
|
|
+ }
|
|
|
+ existJoinElement.UsedRefIds.Add(pathElements[pathElements.Count - 2].Id.IntegerValue);
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return graph;
|
|
|
+ }
|
|
|
+ #region 辅助类构建
|
|
|
+ public static JoinElement CreateJoinElement(Element element, Predicate<Connector> ignoredConnector, List<int> usedIds)
|
|
|
+ {
|
|
|
+ var joinItems = GetJoinItems(element, ignoredConnector, usedIds);
|
|
|
+ JoinElement joinElement = new JoinElement(element, joinItems);
|
|
|
+ return joinElement;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<JoinItem> GetJoinItems(Element element, Predicate<Connector> ignoredConnector, List<int> usedIds)
|
|
|
+ {
|
|
|
+ List<JoinItem> joinItems = new List<JoinItem>();
|
|
|
+ var connectors = element.GetAllConnectors();
|
|
|
+ foreach (var connector in connectors)
|
|
|
+ {
|
|
|
+ if (ignoredConnector(connector))
|
|
|
+ continue;
|
|
|
+ if (connector.IsConnected)
|
|
|
+ {
|
|
|
+ foreach (Connector refConnector in connector.AllRefs)
|
|
|
+ {
|
|
|
+ if (refConnector.IsLogical() || refConnector.Owner.Id == element.Id || usedIds.Any(id => refConnector.Owner.Id.IntegerValue == id))
|
|
|
+ continue;
|
|
|
+ if (ignoredConnector(refConnector))
|
|
|
+ continue;
|
|
|
+ joinItems.Add(new JoinItem(connector, refConnector));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return joinItems;
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
+}
|