SystemParseManager.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:SystemParseManager
  3. * 作者:xulisong
  4. * 创建时间: 2019/1/10 14:34:23
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Windows.Media.Media3D;
  11. using Autodesk.Revit.DB;
  12. using Autodesk.Revit.DB.Mechanical;
  13. using Autodesk.Revit.DB.Plumbing;
  14. using SAGA.Models;
  15. using SAGA.Models.Graphs;
  16. using SAGA.RevitUtils;
  17. using SAGA.RevitUtils.Extends;
  18. using SAGA.RevitUtils.MEP;
  19. namespace SAGA.GplotRelationComputerManage
  20. {
  21. /// <summary>
  22. /// 系统解析管理
  23. /// </summary>
  24. public class SystemParseManager
  25. {
  26. #region 解析管路系统数据
  27. #region 通用基础方法
  28. /// <summary>
  29. /// 创建树节点
  30. /// </summary>
  31. /// <param name="startElement"></param>
  32. /// <param name="startConnector"></param>
  33. /// <param name="predicateEnd"></param>
  34. /// <returns></returns>
  35. public static ElementTreeNode CreateTreeNode(Element startElement, Connector startConnector,
  36. Predicate<Element> predicateEnd)
  37. {
  38. ElementTreeNode node = new ElementTreeNode();
  39. node.Current = startElement;
  40. List<ElementTreeNode> reference = new List<ElementTreeNode>();
  41. reference.Add(node);
  42. for (int i = 0; i < reference.Count; i++)
  43. {
  44. var baseNode = reference[i];
  45. List<Connector> connectors;
  46. if (i == 0 && startConnector != null)
  47. {
  48. connectors = new List<Connector> { startConnector };
  49. }
  50. else
  51. {
  52. connectors = baseNode.Current.GetAllConnectors().ToList();
  53. }
  54. foreach (var connector in connectors)
  55. {
  56. if (connector.Searchable())
  57. {
  58. var refConnectors = connector.GetReferenceConnectors()
  59. .Where(c => ConnectorType.Physical.HasFlag(c.ConnectorType)).ToList();
  60. foreach (var refConnector in refConnectors)
  61. {
  62. var refElement = refConnector.Owner;
  63. ElementTreeNode refNode = new ElementTreeNode();
  64. refNode.Current = refElement;
  65. refNode.StartLocation = refConnector.Origin;
  66. baseNode.Nodes.Add(refNode);
  67. if (predicateEnd != null && predicateEnd(refElement))
  68. {
  69. continue;
  70. }
  71. if (reference.All(e => e.Current.Id.IntegerValue != refElement.Id.IntegerValue))
  72. {
  73. reference.Add(refNode);
  74. }
  75. }
  76. }
  77. }
  78. }
  79. return node;
  80. }
  81. #endregion
  82. /// <summary>
  83. /// 构件系统连接节点
  84. /// </summary>
  85. /// <param name="etn">开始元素</param>
  86. /// <param name="domain"></param>
  87. /// <param name="endConditon">遍历中断条件,遇到指定条件的管道则停止遍历</param>
  88. /// <returns>树形节点中包好的Id信息</returns>
  89. public static List<int> BuildSystemNode(ElementTreeNode etn, Domain domain, Func<MEPCurve, bool> endConditon)
  90. {
  91. List<int> useIds = new List<int>();
  92. etn.GetAllNodes().ForEach(n =>
  93. {
  94. if (!n.IsLeaf&&n.Current != null)
  95. {
  96. useIds.Add(n.Current.Id.IntegerValue);
  97. }
  98. });
  99. var leaves = etn.GetLeaves();
  100. foreach (var elementTreeNode in leaves)
  101. {
  102. BuildSystemNode(elementTreeNode, domain, useIds, endConditon);
  103. }
  104. return useIds;
  105. }
  106. private static void BuildSystemNode(ElementTreeNode etn, Domain domain, List<int> useIds, Func<MEPCurve, bool> endConditon)
  107. {
  108. var useElement = etn.Current;
  109. #region 预判退出
  110. if (useElement == null)
  111. {
  112. return;
  113. }
  114. if (useIds.Any(id => id == useElement.Id.IntegerValue))
  115. {
  116. return;
  117. }
  118. #endregion
  119. useIds.Add(useElement.Id.IntegerValue);
  120. var connectors = useElement.GetConnectors(domain);
  121. foreach (var baseConnector in connectors)
  122. {
  123. if (!baseConnector.Searchable())
  124. continue;
  125. var refConnectors = baseConnector.GetReferenceConnectors()
  126. .Where(c => ConnectorType.Physical.HasFlag(c.ConnectorType)).ToList();
  127. foreach (var refConnector in refConnectors)
  128. {
  129. var refElement = refConnector.Owner;
  130. if (useIds.Any(id => id == refElement.Id.IntegerValue))
  131. {
  132. continue;
  133. }
  134. //初始化当前节点,未必一定加入集合,为了下面代码书写方便,先进性初始化
  135. ElementTreeNode currentNode = new ElementTreeNode()
  136. {
  137. Current = refElement,
  138. StartLocation = refConnector.Origin
  139. };
  140. if (refElement is MEPCurve mepCurve)
  141. {
  142. //优先判定系统,再进行立管相关逻辑的处理;如果系统不兼容则直接退出遍历
  143. //var systemName=mepCurve.GetSystemTypeName();
  144. if (endConditon != null && endConditon(mepCurve))
  145. {
  146. continue;
  147. }
  148. if (SystemCalcUtil.IsStart(mepCurve))
  149. {
  150. currentNode.ElementTypeName = AcType.MarkStandPipe;
  151. etn.AddChild(currentNode);
  152. useIds.Add(refElement.Id.IntegerValue);
  153. continue;
  154. }
  155. }
  156. else if (refElement is FamilyInstance fi)
  157. {
  158. //非管线,遇到结束标志,加入当前子节点,但不在进行递归;
  159. if (SystemCalcUtil.IsStart(fi))
  160. {
  161. currentNode.ElementTypeName = AcType.Valve;
  162. etn.AddChild(currentNode);
  163. useIds.Add(refElement.Id.IntegerValue);
  164. continue;
  165. }
  166. }
  167. //useIds的控制:跳出时,直接加入UseIds。继续遍历的,在遍历函数中加入
  168. etn.AddChild(currentNode);
  169. BuildSystemNode(currentNode, domain, useIds, endConditon);
  170. }
  171. }
  172. }
  173. #endregion
  174. #region 创建绘图数据相关
  175. /// <summary>
  176. /// 构件绘图数据
  177. /// </summary>
  178. /// <param name="etn"></param>
  179. /// <returns></returns>
  180. public static SystemDrawItems CreateDrawing(ElementTreeNode etn)
  181. {
  182. #region 描述
  183. /*
  184. *读取几何信息
  185. * 1、管道,管件直接按connector连线获取。
  186. * 2、设备,可能出现多个connector,确定有效的连线;
  187. * 最终:如果是管道,通过本身的connector生成线。如果是familyInstance通过定位点连接,父节点和子节点
  188. * 3、具体名字相关内容,后续操作中添加
  189. */
  190. #endregion
  191. List<SystemCurveItem> curveItems = new List<SystemCurveItem>();
  192. List<SystemPointItem> pointItems = new List<SystemPointItem>();
  193. Queue<ElementTreeNode> nodeQueue = new Queue<ElementTreeNode>() ;
  194. nodeQueue.Enqueue(etn);
  195. while (nodeQueue.Any())
  196. {
  197. #region 控制验证
  198. var currentNode = nodeQueue.Dequeue();
  199. if (currentNode == null)
  200. continue;
  201. var element = currentNode.Current;
  202. if (element == null)
  203. continue;
  204. #endregion
  205. if (element is MEPCurve mepCurve)
  206. {
  207. #region 联通线处理
  208. Curve curve = mepCurve.GetCurve();
  209. var xyzs = curve.Tessellate().Select(xyz => xyz.XYZToPoint());
  210. SystemCurveItem curveItem = new SystemCurveItem();
  211. curveItem.Points.AddRange(xyzs.ToList());
  212. curveItem.RefId = element.Id.IntegerValue;
  213. curveItems.Add(curveItem);
  214. #endregion
  215. #region 点信息处理
  216. if (SystemCalcUtil.IsStart(mepCurve))
  217. {
  218. SystemPointItem point = new SystemPointItem();
  219. point.Point = mepCurve.GetCurve().StartPoint().XYZToPoint();
  220. point.RefId = mepCurve.Id.IntegerValue;
  221. point.IsVirtual = true;
  222. point.EquipmentType = PointItemType.StandPipe.ToString();
  223. pointItems.Add(point);
  224. }
  225. #endregion
  226. }
  227. else if (element is FamilyInstance fiEq)
  228. {
  229. #region 获取引用点
  230. List<XYZ> usePoints = new List<XYZ>();
  231. if (currentNode.StartLocation != null)
  232. {
  233. usePoints.Add(currentNode.StartLocation);
  234. }
  235. foreach (ElementTreeNode childNode in currentNode.Nodes)
  236. {
  237. var usePoint = childNode.StartLocation;
  238. if (usePoint != null)
  239. {
  240. usePoints.Add(usePoint);
  241. }
  242. }
  243. #endregion
  244. var location = element.GetBoxCenter().XYZToPoint();
  245. #region 根据定位点并创建线条
  246. foreach (var usePoint in usePoints)
  247. {
  248. SystemCurveItem curveItem = new SystemCurveItem();
  249. curveItem.Points.Add(location);
  250. curveItem.Points.Add(usePoint.XYZToPoint());
  251. curveItem.RefId = element.Id.IntegerValue;
  252. curveItems.Add(curveItem);
  253. }
  254. #endregion
  255. #region 判断是否是设备
  256. //先初始化不一定加入
  257. SystemPointItem point = new SystemPointItem();
  258. point.Point = location;
  259. point.RefId = fiEq.Id.IntegerValue;
  260. point.BimId = element.GetBimId();
  261. if (MBIInfoUtil.IsEquipment(fiEq))
  262. {
  263. point.Name = string.Empty;
  264. point.EquipmentType = PointItemType.Equipment.ToString();
  265. pointItems.Add(point);
  266. }
  267. else if (SystemCalcUtil.IsStart(fiEq))
  268. {
  269. point.Name = string.Empty;
  270. point.EquipmentType = PointItemType.StartValve.ToString(); ;
  271. point.IsVirtual = true;
  272. pointItems.Add(point);
  273. }
  274. #endregion
  275. }
  276. else if (element is Space)
  277. {
  278. SystemPointItem point = new SystemPointItem();
  279. point.Point = element.GetLocationPoint().XYZToPoint();
  280. point.RefId = element.Id.IntegerValue;
  281. point.EquipmentType = PointItemType.Space.ToString();
  282. point.IsVirtual = true;
  283. pointItems.Add(point);
  284. }
  285. currentNode.Nodes.ForEach(e => nodeQueue.Enqueue(e));
  286. }
  287. SystemDrawItems drawItems = new SystemDrawItems();
  288. drawItems.Curves = curveItems;
  289. drawItems.Points = pointItems;
  290. return drawItems;
  291. }
  292. #endregion
  293. #region 创建关系数据相关
  294. public static List<BinaryRelationItem> CreateRelation(ElementTreeNode etn)
  295. {
  296. List<BinaryRelationItem> result = new List<BinaryRelationItem>();
  297. #region 描述
  298. /*
  299. * 1、将树形结构初步处理,修剪成存留设备之间的关系;
  300. * 2、处理成二维关系
  301. */
  302. #endregion
  303. var equipmentNodes= ConvertEquipmentNodes(etn);
  304. var loopNodes = new List<EquipmentNode>(equipmentNodes);
  305. for (int i = 0; i < loopNodes.Count; i++)
  306. {
  307. var currentNode = loopNodes[i];
  308. foreach (var childNode in currentNode.Nodes)
  309. {
  310. BinaryRelationItem relation = new BinaryRelationItem();
  311. relation.From = currentNode.CloneCurrentNode();
  312. relation.To = childNode.CloneCurrentNode();
  313. result.Add(relation);
  314. loopNodes.Add(childNode);
  315. }
  316. }
  317. return result;
  318. }
  319. /// <summary>
  320. /// 将elment树转换成设备节点类
  321. /// </summary>
  322. /// <param name="etn"></param>
  323. /// <returns></returns>
  324. private static List<EquipmentNode> ConvertEquipmentNodes(ElementTreeNode etn)
  325. {
  326. List<EquipmentNode> nodes = new List<EquipmentNode>();
  327. List<ElementTreeNode> loopNodes = new List<ElementTreeNode>(){ etn };
  328. for (int i = 0; i < loopNodes.Count; i++)
  329. {
  330. var currentNode = loopNodes[i];
  331. if (TryGetNode(currentNode.Current, out EquipmentNode outNode))
  332. {
  333. foreach (var currentNodeNode in currentNode.Nodes)
  334. {
  335. outNode.AddChildren(ConvertEquipmentNodes(currentNodeNode));
  336. }
  337. nodes.Add(outNode);
  338. }
  339. else
  340. {
  341. loopNodes.AddRange(currentNode.Nodes);
  342. }
  343. }
  344. return nodes;
  345. }
  346. /// <summary>
  347. /// 获取element对应节点
  348. /// </summary>
  349. /// <param name="element">输入element</param>
  350. /// <param name="outNode">成功获取时,node真实值</param>
  351. /// <returns>成功获取</returns>
  352. private static bool TryGetNode(Element element,out EquipmentNode outNode)
  353. {
  354. bool flag = false;
  355. EquipmentNode node = new EquipmentNode();
  356. if (element is Space space)
  357. {
  358. node.Category = PointItemType.Space.ToString();
  359. }
  360. else if (element is Pipe&& SystemCalcUtil.IsStart(element))
  361. {
  362. node.Category = PointItemType.StandPipe.ToString();
  363. }
  364. else if (element is FamilyInstance)
  365. {
  366. if (MBIInfoUtil.IsEquipment(element))
  367. {
  368. node.Category = PointItemType.Equipment.ToString();
  369. }
  370. else if (SystemCalcUtil.IsStart(element))
  371. {
  372. node.Category = PointItemType.StartValve.ToString();
  373. }
  374. }
  375. flag = !string.IsNullOrEmpty(node.Category);
  376. outNode = flag ? node : null;
  377. #region 确定返回之后进行后初始化处理
  378. if (outNode != null)
  379. {
  380. outNode.RevitId = element.Id.IntegerValue;
  381. outNode.BimId = element.GetBimId();
  382. }
  383. #endregion
  384. return flag;
  385. }
  386. #endregion
  387. #region 创建立面绘图和关系数据
  388. /// <summary>
  389. /// 计算立面数据
  390. /// </summary>
  391. public static VerticalResult ComputerVerticalData(List<VerticalPipe> pipeDatas, List<LevelData> levelDatas)
  392. {
  393. List<VerticalDrawRecord> drawRecords = new List<VerticalDrawRecord>();
  394. List<VerticalRelationRecord> relationRecords = new List<VerticalRelationRecord>();
  395. #region 标高绘图数据整理
  396. List<VFloor> floors = new List<VFloor>();
  397. for (int i = 0; i < levelDatas.Count; i++)
  398. {
  399. var tempFloor = new VFloor();
  400. tempFloor.Name = levelDatas[i].Name;
  401. if (tempFloor.Name == "F1")
  402. {
  403. tempFloor.ValueDescription = "G";
  404. }
  405. tempFloor.Index = i;
  406. tempFloor.FloorId = i.ToString();
  407. tempFloor.LinkId = levelDatas[i].Id;
  408. floors.Add(tempFloor);
  409. }
  410. #endregion
  411. #region 立管或相当于立管的空间处理
  412. /*
  413. * 按系统分组,[一个空间会不会对应多个系统]
  414. */
  415. var comparer = new VpComparer();
  416. int groupId = 0;
  417. //按系统分组
  418. var goupSystems = pipeDatas.GroupBy(p => p.PipeSytemType);
  419. foreach (var groupSystem in goupSystems)
  420. {
  421. //按坐标分组
  422. foreach (var groupPosition in groupSystem.GroupBy(p=>p.DownPoint3D, comparer))
  423. {
  424. var currentGroupdId = groupId + 1;
  425. SetRelationItem set = new SetRelationItem();
  426. #region 处理绘图数据
  427. foreach (var groupFloor in groupPosition.GroupBy(p => p.LevelName))
  428. {
  429. var verData = groupFloor.FirstOrDefault();
  430. string floorId = floors.FirstOrDefault(c => c.Name == groupFloor.Key)?.FloorId;
  431. #region 生成存储核心数据
  432. VPipe pipe = new VPipe();
  433. pipe.LinkId = verData.Id;
  434. pipe.Name = string.Join(",", groupFloor.Select(v => v.Display));//verData.Display;
  435. pipe.Tip = verData.DisplayTip;
  436. pipe.PipeSystemType = verData.PipeSytemType;
  437. pipe.FloorId = floorId;
  438. #endregion
  439. VerticalDrawRecord drawRecord = new VerticalDrawRecord();
  440. drawRecord.GroupId = currentGroupdId.ToString();
  441. drawRecord.RefPipe = pipe;
  442. drawRecord.SystemName = groupSystem.Key;
  443. drawRecords.Add(drawRecord);
  444. set.AddRange(groupFloor.Select(v => string.Format("{0}:{1}", floorId, v.Id)));
  445. }
  446. #endregion
  447. #region 处理关系数据
  448. VerticalRelationRecord relationRecord = new VerticalRelationRecord();
  449. relationRecord.SystemName = groupSystem.Key;
  450. relationRecord.RelationItems = set;
  451. relationRecords.Add(relationRecord);
  452. #endregion
  453. }
  454. }
  455. #endregion
  456. VerticalResult result = new VerticalResult();
  457. result.DrawData = new VerticalDrawData() { LevelDatas = floors, DrawRecords = drawRecords };
  458. result.RelationData = relationRecords;
  459. return result;
  460. }
  461. #endregion
  462. }
  463. /// <summary>
  464. /// 点位置比较相关
  465. /// </summary>
  466. public class VpComparer : IEqualityComparer<Point3D>
  467. {
  468. /// <summary>
  469. /// 立管左右错开小于2Cm
  470. ///
  471. /// </summary>
  472. /// <param name="x"></param>
  473. /// <param name="y"></param>
  474. /// <returns></returns>
  475. public bool Equals(Point3D x, Point3D y)
  476. {//数据为英寸
  477. var diff = 2 * 10d / 304.8;
  478. return Math.Abs(x.X - y.X) <= diff && Math.Abs(x.Y - y.Y) <= diff;
  479. }
  480. public int GetHashCode(Point3D obj)
  481. {
  482. return 0;
  483. }
  484. }
  485. }