123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- /*-------------------------------------------------------------------------
- * 功能描述:SystemParseManager
- * 作者:xulisong
- * 创建时间: 2019/1/10 14:34:23
- * 版本号:v1.0
- * -------------------------------------------------------------------------*/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Media.Media3D;
- using Autodesk.Revit.DB;
- using Autodesk.Revit.DB.Mechanical;
- using Autodesk.Revit.DB.Plumbing;
- using SAGA.Models;
- using SAGA.Models.Graphs;
- using SAGA.RevitUtils;
- using SAGA.RevitUtils.Extends;
- using SAGA.RevitUtils.MEP;
- namespace SAGA.GplotRelationComputerManage
- {
- /// <summary>
- /// 系统解析管理
- /// </summary>
- public class SystemParseManager
- {
- #region 解析管路系统数据
- #region 通用基础方法
- /// <summary>
- /// 创建树节点
- /// </summary>
- /// <param name="startElement"></param>
- /// <param name="startConnector"></param>
- /// <param name="predicateEnd"></param>
- /// <returns></returns>
- public static ElementTreeNode CreateTreeNode(Element startElement, Connector startConnector,
- Predicate<Element> predicateEnd)
- {
- ElementTreeNode node = new ElementTreeNode();
- node.Current = startElement;
- List<ElementTreeNode> reference = new List<ElementTreeNode>();
- reference.Add(node);
- for (int i = 0; i < reference.Count; i++)
- {
- var baseNode = reference[i];
- List<Connector> connectors;
- if (i == 0 && startConnector != null)
- {
- connectors = new List<Connector> { startConnector };
- }
- else
- {
- connectors = baseNode.Current.GetAllConnectors().ToList();
- }
- foreach (var connector in connectors)
- {
- if (connector.Searchable())
- {
- var refConnectors = connector.GetReferenceConnectors()
- .Where(c => ConnectorType.Physical.HasFlag(c.ConnectorType)).ToList();
- foreach (var refConnector in refConnectors)
- {
- var refElement = refConnector.Owner;
- ElementTreeNode refNode = new ElementTreeNode();
- refNode.Current = refElement;
- refNode.StartLocation = refConnector.Origin;
- baseNode.Nodes.Add(refNode);
- if (predicateEnd != null && predicateEnd(refElement))
- {
- continue;
- }
- if (reference.All(e => e.Current.Id.IntegerValue != refElement.Id.IntegerValue))
- {
- reference.Add(refNode);
- }
- }
- }
- }
- }
- return node;
- }
- #endregion
- /// <summary>
- /// 构件系统连接节点
- /// </summary>
- /// <param name="etn">开始元素</param>
- /// <param name="domain"></param>
- /// <param name="endConditon">遍历中断条件,遇到指定条件的管道则停止遍历</param>
- /// <returns>树形节点中包好的Id信息</returns>
- public static List<int> BuildSystemNode(ElementTreeNode etn, Domain domain, Func<MEPCurve, bool> endConditon)
- {
- List<int> useIds = new List<int>();
- etn.GetAllNodes().ForEach(n =>
- {
- if (!n.IsLeaf&&n.Current != null)
- {
- useIds.Add(n.Current.Id.IntegerValue);
- }
- });
- var leaves = etn.GetLeaves();
- foreach (var elementTreeNode in leaves)
- {
- BuildSystemNode(elementTreeNode, domain, useIds, endConditon);
- }
- return useIds;
- }
- private static void BuildSystemNode(ElementTreeNode etn, Domain domain, List<int> useIds, Func<MEPCurve, bool> endConditon)
- {
- var useElement = etn.Current;
- #region 预判退出
- if (useElement == null)
- {
- return;
- }
- if (useIds.Any(id => id == useElement.Id.IntegerValue))
- {
- return;
- }
- #endregion
- useIds.Add(useElement.Id.IntegerValue);
- var connectors = useElement.GetConnectors(domain);
- foreach (var baseConnector in connectors)
- {
- if (!baseConnector.Searchable())
- continue;
- var refConnectors = baseConnector.GetReferenceConnectors()
- .Where(c => ConnectorType.Physical.HasFlag(c.ConnectorType)).ToList();
- foreach (var refConnector in refConnectors)
- {
- var refElement = refConnector.Owner;
- if (useIds.Any(id => id == refElement.Id.IntegerValue))
- {
- continue;
- }
- //初始化当前节点,未必一定加入集合,为了下面代码书写方便,先进性初始化
- ElementTreeNode currentNode = new ElementTreeNode()
- {
- Current = refElement,
- StartLocation = refConnector.Origin
- };
- if (refElement is MEPCurve mepCurve)
- {
- //优先判定系统,再进行立管相关逻辑的处理;如果系统不兼容则直接退出遍历
- //var systemName=mepCurve.GetSystemTypeName();
- if (endConditon != null && endConditon(mepCurve))
- {
- continue;
- }
- if (SystemCalcUtil.IsStart(mepCurve))
- {
- currentNode.ElementTypeName = AcType.MarkStandPipe;
- etn.AddChild(currentNode);
- useIds.Add(refElement.Id.IntegerValue);
- continue;
- }
- }
- else if (refElement is FamilyInstance fi)
- {
- //非管线,遇到结束标志,加入当前子节点,但不在进行递归;
- if (SystemCalcUtil.IsStart(fi))
- {
- currentNode.ElementTypeName = AcType.Valve;
- etn.AddChild(currentNode);
- useIds.Add(refElement.Id.IntegerValue);
- continue;
- }
- }
- //useIds的控制:跳出时,直接加入UseIds。继续遍历的,在遍历函数中加入
- etn.AddChild(currentNode);
- BuildSystemNode(currentNode, domain, useIds, endConditon);
- }
- }
- }
- #endregion
- #region 创建绘图数据相关
- /// <summary>
- /// 构件绘图数据
- /// </summary>
- /// <param name="etn"></param>
- /// <returns></returns>
- public static SystemDrawItems CreateDrawing(ElementTreeNode etn)
- {
- #region 描述
- /*
- *读取几何信息
- * 1、管道,管件直接按connector连线获取。
- * 2、设备,可能出现多个connector,确定有效的连线;
- * 最终:如果是管道,通过本身的connector生成线。如果是familyInstance通过定位点连接,父节点和子节点
- * 3、具体名字相关内容,后续操作中添加
- */
- #endregion
- List<SystemCurveItem> curveItems = new List<SystemCurveItem>();
- List<SystemPointItem> pointItems = new List<SystemPointItem>();
- Queue<ElementTreeNode> nodeQueue = new Queue<ElementTreeNode>() ;
- nodeQueue.Enqueue(etn);
- while (nodeQueue.Any())
- {
- #region 控制验证
- var currentNode = nodeQueue.Dequeue();
- if (currentNode == null)
- continue;
- var element = currentNode.Current;
- if (element == null)
- continue;
- #endregion
- if (element is MEPCurve mepCurve)
- {
- #region 联通线处理
- Curve curve = mepCurve.GetCurve();
- var xyzs = curve.Tessellate().Select(xyz => xyz.XYZToPoint());
- SystemCurveItem curveItem = new SystemCurveItem();
- curveItem.Points.AddRange(xyzs.ToList());
- curveItem.RefId = element.Id.IntegerValue;
- curveItems.Add(curveItem);
- #endregion
- #region 点信息处理
- if (SystemCalcUtil.IsStart(mepCurve))
- {
- SystemPointItem point = new SystemPointItem();
- point.Point = mepCurve.GetCurve().StartPoint().XYZToPoint();
- point.RefId = mepCurve.Id.IntegerValue;
- point.IsVirtual = true;
- point.EquipmentType = PointItemType.StandPipe.ToString();
- pointItems.Add(point);
- }
- #endregion
- }
- else if (element is FamilyInstance fiEq)
- {
- #region 获取引用点
- List<XYZ> usePoints = new List<XYZ>();
- if (currentNode.StartLocation != null)
- {
- usePoints.Add(currentNode.StartLocation);
- }
- foreach (ElementTreeNode childNode in currentNode.Nodes)
- {
- var usePoint = childNode.StartLocation;
- if (usePoint != null)
- {
- usePoints.Add(usePoint);
- }
- }
- #endregion
- var location = element.GetBoxCenter().XYZToPoint();
- #region 根据定位点并创建线条
- foreach (var usePoint in usePoints)
- {
- SystemCurveItem curveItem = new SystemCurveItem();
- curveItem.Points.Add(location);
- curveItem.Points.Add(usePoint.XYZToPoint());
- curveItem.RefId = element.Id.IntegerValue;
- curveItems.Add(curveItem);
- }
- #endregion
- #region 判断是否是设备
- //先初始化不一定加入
- SystemPointItem point = new SystemPointItem();
- point.Point = location;
- point.RefId = fiEq.Id.IntegerValue;
- point.BimId = element.GetBimId();
- if (MBIInfoUtil.IsEquipment(fiEq))
- {
- point.Name = string.Empty;
- point.EquipmentType = PointItemType.Equipment.ToString();
- pointItems.Add(point);
- }
- else if (SystemCalcUtil.IsStart(fiEq))
- {
- point.Name = string.Empty;
- point.EquipmentType = PointItemType.StartValve.ToString(); ;
- point.IsVirtual = true;
- pointItems.Add(point);
- }
- #endregion
- }
- else if (element is Space)
- {
- SystemPointItem point = new SystemPointItem();
- point.Point = element.GetLocationPoint().XYZToPoint();
- point.RefId = element.Id.IntegerValue;
- point.EquipmentType = PointItemType.Space.ToString();
- point.IsVirtual = true;
- pointItems.Add(point);
- }
- currentNode.Nodes.ForEach(e => nodeQueue.Enqueue(e));
- }
- SystemDrawItems drawItems = new SystemDrawItems();
- drawItems.Curves = curveItems;
- drawItems.Points = pointItems;
- return drawItems;
- }
- #endregion
- #region 创建关系数据相关
- public static List<BinaryRelationItem> CreateRelation(ElementTreeNode etn)
- {
- List<BinaryRelationItem> result = new List<BinaryRelationItem>();
- #region 描述
- /*
- * 1、将树形结构初步处理,修剪成存留设备之间的关系;
- * 2、处理成二维关系
- */
- #endregion
- var equipmentNodes= ConvertEquipmentNodes(etn);
- var loopNodes = new List<EquipmentNode>(equipmentNodes);
- for (int i = 0; i < loopNodes.Count; i++)
- {
- var currentNode = loopNodes[i];
- foreach (var childNode in currentNode.Nodes)
- {
- BinaryRelationItem relation = new BinaryRelationItem();
- relation.From = currentNode.CloneCurrentNode();
- relation.To = childNode.CloneCurrentNode();
- result.Add(relation);
- loopNodes.Add(childNode);
- }
- }
- return result;
- }
- /// <summary>
- /// 将elment树转换成设备节点类
- /// </summary>
- /// <param name="etn"></param>
- /// <returns></returns>
- private static List<EquipmentNode> ConvertEquipmentNodes(ElementTreeNode etn)
- {
- List<EquipmentNode> nodes = new List<EquipmentNode>();
- List<ElementTreeNode> loopNodes = new List<ElementTreeNode>(){ etn };
- for (int i = 0; i < loopNodes.Count; i++)
- {
- var currentNode = loopNodes[i];
- if (TryGetNode(currentNode.Current, out EquipmentNode outNode))
- {
- foreach (var currentNodeNode in currentNode.Nodes)
- {
- outNode.AddChildren(ConvertEquipmentNodes(currentNodeNode));
- }
- nodes.Add(outNode);
- }
- else
- {
- loopNodes.AddRange(currentNode.Nodes);
- }
- }
- return nodes;
- }
- /// <summary>
- /// 获取element对应节点
- /// </summary>
- /// <param name="element">输入element</param>
- /// <param name="outNode">成功获取时,node真实值</param>
- /// <returns>成功获取</returns>
- private static bool TryGetNode(Element element,out EquipmentNode outNode)
- {
- bool flag = false;
- EquipmentNode node = new EquipmentNode();
-
- if (element is Space space)
- {
- node.Category = PointItemType.Space.ToString();
- }
- else if (element is Pipe&& SystemCalcUtil.IsStart(element))
- {
- node.Category = PointItemType.StandPipe.ToString();
- }
- else if (element is FamilyInstance)
- {
- if (MBIInfoUtil.IsEquipment(element))
- {
- node.Category = PointItemType.Equipment.ToString();
- }
- else if (SystemCalcUtil.IsStart(element))
- {
- node.Category = PointItemType.StartValve.ToString();
- }
- }
- flag = !string.IsNullOrEmpty(node.Category);
- outNode = flag ? node : null;
- #region 确定返回之后进行后初始化处理
- if (outNode != null)
- {
- outNode.RevitId = element.Id.IntegerValue;
- outNode.BimId = element.GetBimId();
- }
- #endregion
- return flag;
- }
- #endregion
- #region 创建立面绘图和关系数据
- /// <summary>
- /// 计算立面数据
- /// </summary>
- public static VerticalResult ComputerVerticalData(List<VerticalPipe> pipeDatas, List<LevelData> levelDatas)
- {
- List<VerticalDrawRecord> drawRecords = new List<VerticalDrawRecord>();
- List<VerticalRelationRecord> relationRecords = new List<VerticalRelationRecord>();
- #region 标高绘图数据整理
- List<VFloor> floors = new List<VFloor>();
- for (int i = 0; i < levelDatas.Count; i++)
- {
- var tempFloor = new VFloor();
- tempFloor.Name = levelDatas[i].Name;
- if (tempFloor.Name == "F1")
- {
- tempFloor.ValueDescription = "G";
- }
- tempFloor.Index = i;
- tempFloor.FloorId = i.ToString();
- tempFloor.LinkId = levelDatas[i].Id;
- floors.Add(tempFloor);
- }
- #endregion
- #region 立管或相当于立管的空间处理
- /*
- * 按系统分组,[一个空间会不会对应多个系统]
- */
- var comparer = new VpComparer();
-
- int groupId = 0;
- //按系统分组
- var goupSystems = pipeDatas.GroupBy(p => p.PipeSytemType);
- foreach (var groupSystem in goupSystems)
- {
- //按坐标分组
- foreach (var groupPosition in groupSystem.GroupBy(p=>p.DownPoint3D, comparer))
- {
- var currentGroupdId = groupId + 1;
- SetRelationItem set = new SetRelationItem();
- #region 处理绘图数据
- foreach (var groupFloor in groupPosition.GroupBy(p => p.LevelName))
- {
- var verData = groupFloor.FirstOrDefault();
- string floorId = floors.FirstOrDefault(c => c.Name == groupFloor.Key)?.FloorId;
- #region 生成存储核心数据
- VPipe pipe = new VPipe();
- pipe.LinkId = verData.Id;
- pipe.Name = string.Join(",", groupFloor.Select(v => v.Display));//verData.Display;
- pipe.Tip = verData.DisplayTip;
- pipe.PipeSystemType = verData.PipeSytemType;
- pipe.FloorId = floorId;
- #endregion
- VerticalDrawRecord drawRecord = new VerticalDrawRecord();
- drawRecord.GroupId = currentGroupdId.ToString();
- drawRecord.RefPipe = pipe;
- drawRecord.SystemName = groupSystem.Key;
- drawRecords.Add(drawRecord);
- set.AddRange(groupFloor.Select(v => string.Format("{0}:{1}", floorId, v.Id)));
- }
- #endregion
- #region 处理关系数据
- VerticalRelationRecord relationRecord = new VerticalRelationRecord();
- relationRecord.SystemName = groupSystem.Key;
- relationRecord.RelationItems = set;
- relationRecords.Add(relationRecord);
- #endregion
- }
- }
- #endregion
- VerticalResult result = new VerticalResult();
- result.DrawData = new VerticalDrawData() { LevelDatas = floors, DrawRecords = drawRecords };
- result.RelationData = relationRecords;
- return result;
- }
- #endregion
- }
- /// <summary>
- /// 点位置比较相关
- /// </summary>
- public class VpComparer : IEqualityComparer<Point3D>
- {
- /// <summary>
- /// 立管左右错开小于2Cm
- ///
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- /// <returns></returns>
- public bool Equals(Point3D x, Point3D y)
- {//数据为英寸
- var diff = 2 * 10d / 304.8;
- return Math.Abs(x.X - y.X) <= diff && Math.Abs(x.Y - y.Y) <= diff;
- }
- public int GetHashCode(Point3D obj)
- {
- return 0;
- }
- }
- }
|