123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718 |
- /* ==============================================================================
- * 功能描述:DrawEnd
- * 创 建 者:SAGACLOUD
- * 创建日期:2017/9/20 9:46:12
- * ==============================================================================*/
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Linq;
- using System.Runtime.Caching;
- using System.Text.RegularExpressions;
- using System.Windows;
- using System.Windows.Media.Media3D;
- using Autodesk.Revit.DB;
- using Autodesk.Revit.DB.Plumbing;
- using SAGA.Models;
- using SAGA.RevitUtils;
- using SAGA.RevitUtils.Extends;
- using SAGA.RevitUtils.MEP;
- using WPoint = System.Windows.Point;
- namespace SAGA.GplotRelationComputerManage.PumpEnd
- {
- /// <summary>
- /// DrawEnd
- /// </summary>
- public class DrawEnd
- {
- /// <summary>
- /// 空间数据
- /// </summary>
- private static SpaceData spaceData;
- private static SpaceData SpaceDatas
- {
- get
- {
- if (spaceData == null)
- {
- spaceData = DrawDataServer.GetSpaceData(SpaceRelatedEnum.All);
- }
- return spaceData;
- }
- }
- //从查找目录中移除同一系统中被找到标记立管和特殊阀门
- public static Action<ElementId> RemoveFindedElement;
- //所有终点
- public static List<SgElement> EndElements = new List<SgElement>();
- //所有线
- public static List<List<WPoint>> EndLines = new List<List<WPoint>>();
- //所有已遍历Element
- private static List<int> AllPipeSystemElement = new List<int>();
- /// <summary>
- /// 绘制风管末端平面
- /// </summary>
- /// <param name="elementTreeNode"></param>
- public static void DrawDuctPlane(ElementTreeNode elementTreeNode)
- {
- ClearDrawData();
- DrawTree(elementTreeNode, new List<XYZ>());
- }
- /// <summary>
- /// 绘制水管末端平面
- /// </summary>
- /// <param name="elementTreeNode"></param>
- public static void DrawPipePlane(ElementTreeNode elementTreeNode)
- {
- ClearDrawData();
- DrawPipePlane(elementTreeNode, new List<XYZ>());
- }
- /// <summary>
- /// 清空数据
- /// </summary>
- private static void ClearDrawData()
- {
- EndElements.Clear();
- EndLines.Clear();
- }
- /// <summary>
- /// 计算管道拓扑结构
- /// </summary>
- /// <param name="etn"></param>
- /// <param name="domain"></param>
- /// <returns></returns>
- public static SgElementTreeNode GetPipeSystemDataByValve(ElementTreeNode etn, Domain domain = Domain.DomainPiping)
- {
- ClearDrawData();
- AllPipeSystemElement.Clear();
- //计算
- if (etn.Nodes.Count > 0) //查找特殊阀门
- {
- //将特殊阀门放入已查找序列,他是从连接件开始
- AllPipeSystemElement.Add(etn.Current.Id.IntegerValue);
- etn.ElementTypeName = AcType.Valve;
- GetPipeSystem(etn.Nodes[0], domain);
- }
- else
- {
- etn.ElementTypeName = AcType.MarkStandPipe;
- GetPipeSystem(etn, domain);
- }
- //取值
- GetDrawData(etn);
- return ConvertToEtn(etn);
- }
- /// <summary>
- /// 将管道拓扑关系转换成可存储的数据结构
- /// </summary>
- /// <param name="ent"></param>
- /// <returns></returns>
- static SgElementTreeNode ConvertToEtn(ElementTreeNode ent)
- {
- var ele = ent.Current;
- SgElementTreeNode setn = new SgElementTreeNode();
- AcWaterData acWater = new AcWaterData
- {
- Id = ent.Current.Id.IntegerValue + "",
- Type = ent.ElementTypeName
- };
- if (ent.ElementTypeName == AcType.MarkStandPipe && ele is MEPCurve m)
- {
- acWater.UpPoint3D = Convert3DToWin(m.GetVerticalTopPoint());
- acWater.DownPoint3D = Convert3DToWin(m.GetVerticalBottomPoint());
- }
- if (IsEnd(ele)) acWater.Type = AcType.Terminal;
- setn.Current = acWater;
- setn.PipeSystemTypeName = ent.PipeSystemTypeName;
- setn.StartLocation = ent.StartLocation;
- foreach (var node in ent.Nodes)
- {
- setn.AddChild(ConvertToEtn(node));
- }
- return setn;
- }
- /// <summary>
- /// 转换3D数据
- /// </summary>
- /// <param name="xyz"></param>
- /// <returns></returns>
- static Point3D Convert3DToWin(XYZ xyz)
- {
- return new Point3D(xyz.X, xyz.Y, xyz.Z);
- }
- /// <summary>
- /// 画水管的平面图
- /// </summary>
- /// <param name="elementTreeNode"></param>
- /// <param name="xyzs"></param>
- private static void DrawPipePlane(ElementTreeNode elementTreeNode,
- List<XYZ> xyzs)
- {
- Element element = elementTreeNode.Current;
- var nodes = elementTreeNode.Nodes;
- if (nodes.Count == 1)
- {
- AddXyzs(xyzs, elementTreeNode);
- foreach (var node in nodes)
- {
- DrawPipePlane(node, xyzs);
- }
- }
- else
- {
- if (IsEnd(element))
- {
- AddXyzs(xyzs, elementTreeNode);
- DrawLine(xyzs);
- XYZ origin = xyzs.LastOrDefault();
- DrawPoint(origin, element);
- // EndPoints.Add(new WPoint(origin.X, origin.Y));
- }
- else
- {
- AddXyzs(xyzs, elementTreeNode);
- DrawLine(xyzs);
- }
- foreach (var node in nodes)
- {
- if (IsUseFullNode(node))
- {
- xyzs = new List<XYZ>();
- DrawPipePlane(node, xyzs);
- }
- }
- }
- }
- /// <summary>
- /// 画出图
- /// </summary>
- /// <param name="elementTreeNode"></param>
- /// <param name="xyzs"></param>
- private static void DrawTree(ElementTreeNode elementTreeNode, List<XYZ> xyzs)
- {
- Element element = elementTreeNode.Current;
- var nodes = elementTreeNode.Nodes;
- if (nodes.Count == 1)
- {
- AddXyzs(xyzs, elementTreeNode);
- foreach (var node in nodes)
- {
- DrawTree(node, xyzs);
- }
- }
- else
- {
- if (IsEnd(element))
- {
- AddLastPoint(xyzs, elementTreeNode);
- DrawLine(xyzs);
- XYZ origin = xyzs.LastOrDefault();
- DrawPoint(origin, element);
- }
- else
- {
- //AddXyzs(xyzs, element, parentElement);
- AddTeePoint(xyzs, elementTreeNode);
- DrawLine(xyzs);
- }
- foreach (var node in nodes)
- {
- if (IsUseFullNode(node))
- {
- xyzs = new List<XYZ>();
- DrawTree(node, xyzs);
- }
- }
- }
- }
- /// <summary>
- /// 是否为有用的节点
- /// </summary>
- /// <param name="elementTreeNode"></param>
- /// <returns></returns>
- private static bool IsUseFullNode(ElementTreeNode elementTreeNode)
- {
- var leaves = elementTreeNode.GetLeaves();
- return leaves.Any(t => IsEnd(t.Current));
- }
- /// <summary>
- /// 是否为末端
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- private static bool IsEnd(Element element)
- {
- BuiltInCategory category = element.GetCategory();
- return (category == BuiltInCategory.OST_MechanicalEquipment ||
- category == BuiltInCategory.OST_DuctTerminal);
- }
- /// <summary>
- /// 添加三通(或者管替代三通)的定位点
- /// 如果为不包含末端的分支,舍弃掉
- /// </summary>
- /// <param name="xyzs"></param>
- /// <param name="elementTreeNode"></param>
- private static void AddTeePoint(List<XYZ> xyzs, ElementTreeNode elementTreeNode)
- {
- Element element = elementTreeNode.Current;
- Element parentElement = null;
- if (elementTreeNode.Parent != null)
- parentElement = elementTreeNode.Parent.Current;
- Curve curve = element.GetLocationCurve();
- if (curve != null)
- {
- Line line = curve as Line;
- if (line != null)
- {
- #region Start
- if (parentElement != null)
- {
- AddXyzs(xyzs, elementTreeNode);
- }
- #endregion
- foreach (var node in elementTreeNode.Nodes)
- {
- if (IsUseFullNode(node))
- {
- XYZ tempPoint = node.Current.GetLocationPoint();
- XYZ project = line.GetProjectPt(tempPoint);
- xyzs.Add(project);
- }
- }
- }
- }
- else
- {
- AddXyzs(xyzs, elementTreeNode);
- }
- }
- /// <summary>
- /// 如果最后一个管为立管,将其旋转为水平
- /// </summary>
- private static void AddLastPoint(List<XYZ> xyzs, ElementTreeNode elementTreeNode)
- {
- Element element = elementTreeNode.Current;
- XYZ origin = element.GetLocationPoint();
- try
- {
- Line parentLine = elementTreeNode.Parent.Current.GetLocationCurve() as Line;
- XYZ vector = parentLine.Direction;
- if (vector.IsParallel(XYZ.BasisZ))
- {
- var tempNode = elementTreeNode;
- while (true)
- {
- tempNode = tempNode.Parent;
- if (tempNode == null) break;
- Line line = tempNode.Current.GetLocationCurve() as Line;
- if (line != null)
- {
- if (!line.Direction.IsParallel(XYZ.BasisZ))
- {
- XYZ project = line.GetProjectPt(origin);
- XYZ farXyz = line.GetFarnessPoint(project);
- XYZ farVector = project - farXyz;
- XYZ horVector = farVector.VectorRotate(Math.PI / 2);
- double offset = project.Z - origin.Z;
- XYZ endXyz = project.OffsetPoint(horVector, offset);
- //xyzs.Clear();
- xyzs.Add(project);
- xyzs.Add(endXyz);
- break;
- }
- }
- }
- }
- else
- {
- xyzs.Add(origin);
- }
- }
- catch
- {
- xyzs.Add(origin);
- }
- }
- /// <summary>
- /// 向集合中添加点
- /// </summary>
- /// <param name="xyzs"></param>
- /// <param name="elementTreeNode"></param>
- private static void AddXyzs(List<XYZ> xyzs, ElementTreeNode elementTreeNode)
- {
- Element element = elementTreeNode.Current;
- XYZ elementXyz = element.GetLocationPoint();
- if (elementXyz != null)
- {
- try
- {
- ElementTreeNode parentNode = elementTreeNode.Parent;
- XYZ parentXyz = parentNode.Current.GetLocationPoint();
- if (parentXyz != null)
- {
- xyzs.Add(parentXyz);
- xyzs.Add(elementXyz);
- }
- else
- {
- Line parentLine = parentNode.Current.GetLocationCurve() as Line;
- XYZ project = parentLine.GetProjectPt(elementXyz);
- xyzs.Add(project);
- }
- }
- catch (Exception e)
- {
- xyzs.Add(elementXyz);
- }
- }
- else
- {
- Curve elementCurve = element.GetLocationCurve();
- if (elementCurve != null)
- {
- Line elementLine = element.GetLocationCurve() as Line;
- try
- {
- XYZ parentXyz = elementTreeNode.Parent.Current.GetLocationPoint();
- XYZ parentProject = elementLine.GetProjectPt(parentXyz);
- xyzs.Add(parentProject);
- foreach (ElementTreeNode node in elementTreeNode.Nodes)
- {
- if (IsUseFullNode(node))
- {
- XYZ childXyz = node.Current.GetLocationPoint();
- XYZ childProject = elementLine.GetProjectPt(childXyz);
- xyzs.Add(childProject);
- }
- }
- }
- catch (Exception e)
- {
- xyzs.Add(elementCurve.StartPoint());
- xyzs.Add(elementCurve.EndPoint());
- }
- }
- }
- }
- /// <summary>
- /// 画线
- /// </summary>
- /// <param name="xyzs"></param>
- private static void DrawLine(IList<XYZ> xyzs)
- {
- int count = xyzs.Count;
- for (int index = 0; index < count - 1; index++)
- {
- var start = xyzs[index].NewZ();
- var end = xyzs[index + 1].NewZ();
- if (!start.IsEqual(end))
- {
- // Line line = start.NewLine(end);
- // line.NewDetailCurve(view);
- EndLines.Add(new List<WPoint>()
- {
- new WPoint(start.X,start.Y), new WPoint(end.X,end.Y)
- });
- }
- }
- }
- /// <summary>
- /// 画节点
- /// </summary>
- /// <param name="center"></param>
- /// <param name="element"></param>
- private static void DrawPoint(XYZ center, Element element)
- {
- //此处构件坐标为管道末端,非构件端点
- EndElements.Add(new SgElement(element.Id.ToString(), element.Name)
- {
- Point = new WPoint(center.X, center.Y),
- FamilyName = element.GetFamily()?.Name,
- ElementType = element.GetFamilySymbol()?.Name
- });
- }
- /// <summary>
- /// 记录所有管道系统路径以及终点,此方法只适合没有回路的情况
- /// </summary>
- /// <param name="etn"></param>
- public static void GetPipeSystem(ElementTreeNode etn, Domain domain = Domain.DomainPiping)
- {
- var currentElement = etn.Current;
- if (!AllPipeSystemElement.Contains(currentElement.Id.IntegerValue))
- AllPipeSystemElement.Add(currentElement.Id.IntegerValue);
- else return;
- ////如果是横管记录坐标
- //if (currentElement is Pipe pipe && !pipe.IsVerticalPipe())
- //{
- // DrawLine(pipe.GetLocationCurve().Tessellate());
- // //如果管道一端没有连接东西,求此管道与空间的轮廓相交,终端用空间表示,找不到空间,直接标记室外
- //}
- //else if (IsEnd(currentElement)) //如果是水管设备记录设备
- //{
- // DrawPoint(currentElement.GetLocationPoint(), currentElement);
- //}
- foreach (Connector connector in currentElement.GetConnectors(domain))
- {
- if (connector.IsConnected)
- {
- //物理类型才会有origin
- foreach (Connector connector2 in connector.GetReferenceConnectors().Where(t => (t.ConnectorType & ConnectorType.Physical) == t.ConnectorType))
- {
- var refElement = connector2.Owner;
- if (refElement.Id.IsEqual(currentElement.Id)
- && refElement.Id.IsEqual(etn.Parent.Current.Id)) continue;
- if (AllPipeSystemElement.Contains(refElement.Id.IntegerValue)) continue;
- var theEtn = new ElementTreeNode()
- {
- Current = refElement,
- Parent = etn,
- StartLocation = connector2.Origin
- };
- if (refElement is MEPCurve p)//判断此管道系统是否和上一级保持一致
- {
- var systemType = p is Pipe ? p.GetParameterString(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM) :
- p.GetParameterString(BuiltInParameter.RBS_DUCT_SYSTEM_TYPE_PARAM);//系统类型
- if (systemType != etn.PipeSystemTypeName)
- {
- continue;
- }
- }
- theEtn.PipeSystemTypeName = etn.PipeSystemTypeName;
- //检测当前设备是否为阀门,如果是阀门也跳过
- if (refElement is FamilyInstance fi)
- {
- if (Regex.IsMatch(fi.GetFamily()?.Name ?? "", "流向指示符|风向箭头"))
- {
- etn.AddChild(theEtn);
- theEtn.ElementTypeName = AcType.Valve;
- RemoveFindedElement?.Invoke(fi.Id);
- continue;
- }
- }
- //找到标记立管也停止
- if (refElement is MEPCurve pipe && pipe.IsVerticalPipe()
- && pipe.GetParameterString(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS) == "start")
- {
- etn.AddChild(theEtn);
- theEtn.ElementTypeName = AcType.MarkStandPipe;
- RemoveFindedElement?.Invoke(pipe.Id);
- continue;
- }
- GetPipeSystem(theEtn, domain);
- etn.AddChild(theEtn);
- }
- }
- }
- }
- public static void GetDrawData(ElementTreeNode etn)
- {
- var currentElement = etn.Current;
- //如果是横管记录坐标
- if (currentElement is MEPCurve pipe && !pipe.IsVerticalPipe())
- {
- DrawLine(pipe.GetLocationCurve().Tessellate());
- //连线
- DrawLine(GetChildLine(etn, pipe.GetLocationCurve()));
- //如果管道一端没有连接东西,求此管道与空间的轮廓相交,终端用空间表示,找不到空间,直接标记室外
- var connector = currentElement.GetConnectors(Domain.DomainPiping).FirstOrDefault(t => !t.IsConnected);
- if (connector != null)//找到一端没有被连接的管道
- {
- bool isFind = false;
- var origin = connector.Origin;
- //获取上一级的连接件所在的空间
- if (etn.Parent != null && etn.Parent.Current is FamilyInstance fi && fi.Space != null)
- {
- var level = fi.Space.Level;
- //根据标高查找
- //找到本层的空间
- var levelSpaces = SpaceDatas?.FloorDatas.FirstOrDefault(t => t.LevelName == level.Name.Replace("-saga", ""));
- //空间轮廓与管线做相交判断
- if (levelSpaces != null)
- {
- foreach (var sgSpace in levelSpaces.EndPoints)
- {
- #region 点在区域内判断
- GraphicsPath myGraphicsPath = new GraphicsPath();
- Region myRegion = new Region();
- myGraphicsPath.Reset();
- sgSpace.BoundarySegment.ForEach(p =>
- {
- myGraphicsPath.AddCurve(p.Select(t => new System.Drawing.PointF((float)t.X, (float)t.Y)).ToArray());
- });
- // myGraphicsPath.AddPolygon(points);//points);
- myRegion.MakeEmpty();
- myRegion.Union(myGraphicsPath);
- //返回判断点是否在多边形里
- bool myPoint = myRegion.IsVisible(new PointF((float)origin.X, (float)origin.Y));
- //重新定位空间的位置,选用管道口的位置
- sgSpace.Point = new System.Windows.Point(origin.X, origin.Y);
- #endregion
- if (myPoint)
- {
- EndElements.Add(sgSpace);
- isFind = true;
- break;
- }
- }
- }
- //if (!isFind)
- //{
- // //如果找到则记录,找不到标记为户外
- // EndElements.Add(new SgElement("", "室外")
- // {
- // Point = new WPoint(origin.X, origin.Y),
- // FamilyName = "室外",
- // ElementType = "室外"
- // });
- //}
- }
- }
- }
- else if (IsEnd(currentElement)) //如果是水管设备记录设备
- {
- //设备连接管道,防止设备太大连接不上
- // DrawLine(GetChildLine(etn));现在取的是连接connector的origin,所以不需要再加了
- DrawPoint(etn.StartLocation, currentElement);
- }
- foreach (var etnNode in etn.Nodes)
- {
- //如果以流量指示符开始,父节点为空
- if (etnNode.Parent != null && etnNode.Current is FamilyInstance fi && Regex.IsMatch(fi.GetFamily()?.Name ?? "", "流向指示符|风向箭头"))
- {
- continue;
- }
- GetDrawData(etnNode);
- }
- }
- /// <summary>
- /// 当前设备与上一设备的连线
- /// </summary>
- /// <param name="etn"></param>
- /// <param name="pipeCurve"></param>
- /// <returns></returns>
- static List<XYZ> GetChildLine(ElementTreeNode etn, Curve pipeCurve)
- {
- if (etn.Parent == null) return new List<XYZ>();
- etn = etn.Parent;
- List<XYZ> linePoints = new List<XYZ>();
- Line line = null;
- var currentElement = etn.Current;
- //如果是横管记录坐标
- if (currentElement is MEPCurve pipe && !pipe.IsVerticalPipe())
- {
- var cpipe = pipe.GetLocationCurve();
- var list1 = new List<XYZ>() { pipeCurve.StartPoint(), pipeCurve.EndPoint() };
- var list2 = new List<XYZ>() { cpipe.StartPoint(), cpipe.EndPoint() };
- var length = double.MaxValue;
- var nearPoint = cpipe.StartPoint();
- var otherPoint = pipeCurve.StartPoint();
- foreach (var xyz in list1)
- {
- foreach (var xyz1 in list2)
- {
- var l = xyz.IsEqual(xyz1) ? 0 : xyz.NewLine(xyz1).Length;
- if (l < length)
- {
- length = l;
- nearPoint = xyz1;
- otherPoint = xyz;
- }
- }
- }
- linePoints.Add(otherPoint);
- linePoints.Add(nearPoint);
- return linePoints;
- }
- else if (IsEnd(currentElement)) //如果是水管设备记录设备
- {
- var p1 = pipeCurve.StartPoint();
- var p2 = pipeCurve.EndPoint();
- var p3 = etn.StartLocation;
- linePoints.Add(p3);
- linePoints.Add((p3 - p1).GetLength() < (p3 - p2).GetLength() ? p1 : p2);
- return linePoints;
- }
- linePoints = GetChildLine(etn, pipeCurve);
- return linePoints;
- }
- static List<XYZ> GetChildLine(ElementTreeNode etn)
- {
- if (etn.Parent == null) return new List<XYZ>();
- var parent = etn.Parent;
- List<XYZ> linePoints = new List<XYZ>();
- Line line = null;
- var currentElement = etn.Current;
- var parentElement = parent.Current;
- //如果是横管记录坐标
- if (parentElement is MEPCurve pipe && !pipe.IsVerticalPipe())
- {
- var pipeCurve = pipe.GetLocationCurve();
- var p1 = pipeCurve.StartPoint();
- var p2 = pipeCurve.EndPoint();
- var p3 = currentElement.GetLocationPoint();
- linePoints.Add(p3);
- linePoints.Add((p3 - p1).GetLength() < (p3 - p2).GetLength() ? p1 : p2);
- return linePoints;
- }
- linePoints = GetChildLine(parent);
- return linePoints;
- }
- }
- }
|