SystemParseManager.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. //逻辑变更,如果存在,不继续遍历。但要完成本次操作;移动到最后
  131. //if (useIds.Any(id => id == refElement.Id.IntegerValue))
  132. //{
  133. // continue;
  134. //}
  135. //初始化当前节点,未必一定加入集合,为了下面代码书写方便,先进性初始化
  136. ElementTreeNode currentNode = new ElementTreeNode()
  137. {
  138. Current = refElement,
  139. StartLocation = refConnector.Origin
  140. };
  141. if (refElement is MEPCurve mepCurve)
  142. {
  143. //优先判定系统,再进行立管相关逻辑的处理;如果系统不兼容则直接退出遍历
  144. //var systemName=mepCurve.GetSystemTypeName();
  145. if (endConditon != null && endConditon(mepCurve))
  146. {
  147. continue;
  148. }
  149. if (SystemCalcUtil.IsStart(mepCurve))
  150. {
  151. currentNode.ElementTypeName = AcType.MarkStandPipe;
  152. etn.AddChild(currentNode);
  153. useIds.Add(refElement.Id.IntegerValue);
  154. continue;
  155. }
  156. }
  157. else if (refElement is FamilyInstance fi)
  158. {
  159. //非管线,遇到结束标志,加入当前子节点,但不在进行递归;
  160. if (SystemCalcUtil.IsStart(fi))
  161. {
  162. currentNode.ElementTypeName = AcType.Valve;
  163. etn.AddChild(currentNode);
  164. useIds.Add(refElement.Id.IntegerValue);
  165. continue;
  166. }
  167. }
  168. //useIds的控制:跳出时,直接加入UseIds。继续遍历的,在遍历函数中加入
  169. etn.AddChild(currentNode);
  170. if (useIds.Any(id => id == refElement.Id.IntegerValue))
  171. {
  172. continue;
  173. }
  174. BuildSystemNode(currentNode, domain, useIds, endConditon);
  175. }
  176. }
  177. }
  178. #endregion
  179. #region 创建绘图数据相关
  180. /// <summary>
  181. /// 构件绘图数据
  182. /// </summary>
  183. /// <param name="etn"></param>
  184. /// <returns></returns>
  185. public static SystemDrawItems CreateDrawing(ElementTreeNode etn)
  186. {
  187. #region 描述
  188. /*
  189. *读取几何信息
  190. * 1、管道,管件直接按connector连线获取。
  191. * 2、设备,可能出现多个connector,确定有效的连线;
  192. * 最终:如果是管道,通过本身的connector生成线。如果是familyInstance通过定位点连接,父节点和子节点
  193. * 3、具体名字相关内容,后续操作中添加
  194. */
  195. #endregion
  196. List<SystemCurveItem> curveItems = new List<SystemCurveItem>();
  197. List<SystemPointItem> pointItems = new List<SystemPointItem>();
  198. Queue<ElementTreeNode> nodeQueue = new Queue<ElementTreeNode>() ;
  199. nodeQueue.Enqueue(etn);
  200. while (nodeQueue.Any())
  201. {
  202. #region 控制验证
  203. var currentNode = nodeQueue.Dequeue();
  204. if (currentNode == null)
  205. continue;
  206. var element = currentNode.Current;
  207. if (element == null)
  208. continue;
  209. #endregion
  210. if (element is MEPCurve mepCurve)
  211. {
  212. #region 联通线处理
  213. Curve curve = mepCurve.GetCurve();
  214. var xyzs = curve.Tessellate().Select(xyz => xyz.XYZToPoint());
  215. SystemCurveItem curveItem = new SystemCurveItem();
  216. curveItem.Points.AddRange(xyzs.ToList());
  217. curveItem.RefId = element.Id.IntegerValue;
  218. curveItems.Add(curveItem);
  219. #endregion
  220. #region 点信息处理
  221. if (SystemCalcUtil.IsStart(mepCurve))
  222. {
  223. SystemPointItem point = new SystemPointItem();
  224. point.Point = mepCurve.GetCurve().StartPoint().XYZToPoint();
  225. point.RefId = mepCurve.Id.IntegerValue;
  226. point.IsVirtual = true;
  227. point.EquipmentType = PointItemType.StandPipe.ToString();
  228. point.Name = AppSetting.StartFlag;
  229. pointItems.Add(point);
  230. }
  231. #endregion
  232. }
  233. else if (element is FamilyInstance fiEq)
  234. {
  235. #region 获取引用点
  236. List<XYZ> usePoints = new List<XYZ>();
  237. if (currentNode.StartLocation != null)
  238. {
  239. usePoints.Add(currentNode.StartLocation);
  240. }
  241. foreach (ElementTreeNode childNode in currentNode.Nodes)
  242. {
  243. var usePoint = childNode.StartLocation;
  244. if (usePoint != null)
  245. {
  246. usePoints.Add(usePoint);
  247. }
  248. }
  249. #endregion
  250. var location = element.GetBoxCenter().XYZToPoint();
  251. #region 根据定位点并创建线条
  252. foreach (var usePoint in usePoints)
  253. {
  254. SystemCurveItem curveItem = new SystemCurveItem();
  255. curveItem.Points.Add(location);
  256. curveItem.Points.Add(usePoint.XYZToPoint());
  257. curveItem.RefId = element.Id.IntegerValue;
  258. curveItems.Add(curveItem);
  259. }
  260. #endregion
  261. #region 判断是否是设备
  262. //先初始化不一定加入
  263. SystemPointItem point = new SystemPointItem();
  264. point.Point = location;
  265. point.RefId = fiEq.Id.IntegerValue;
  266. point.BimId = element.GetBimId();
  267. if (MBIInfoUtil.IsEquipment(fiEq))
  268. {
  269. point.Name = string.Empty;
  270. point.EquipmentType = PointItemType.Equipment.ToString();
  271. var equipExtend = currentNode.GetTag<NodeExtendData>();
  272. if (equipExtend != null)
  273. {
  274. point.Name = equipExtend.Name;
  275. point.EquipmentType = equipExtend.Category;
  276. }
  277. pointItems.Add(point);
  278. }
  279. else if (SystemCalcUtil.IsStart(fiEq))
  280. {
  281. point.Name = string.Empty;
  282. point.EquipmentType = PointItemType.StartValve.ToString(); ;
  283. point.IsVirtual = true;
  284. point.Name=AppSetting.StartFlag;
  285. pointItems.Add(point);
  286. }
  287. #endregion
  288. }
  289. else if (element is Space)
  290. {
  291. SystemPointItem point = new SystemPointItem();
  292. point.Point = element.GetLocationPoint().XYZToPoint();
  293. point.RefId = element.Id.IntegerValue;
  294. point.EquipmentType = PointItemType.Space.ToString();
  295. point.IsVirtual = true;
  296. pointItems.Add(point);
  297. }
  298. currentNode.Nodes.ForEach(e => nodeQueue.Enqueue(e));
  299. }
  300. SystemDrawItems drawItems = new SystemDrawItems();
  301. drawItems.Curves = curveItems;
  302. drawItems.Points = pointItems;
  303. return drawItems;
  304. }
  305. #endregion
  306. #region 创建关系数据相关
  307. public static List<BinaryRelationItem> CreateRelation(ElementTreeNode etn)
  308. {
  309. List<BinaryRelationItem> result = new List<BinaryRelationItem>();
  310. #region 描述
  311. /*
  312. * 1、将树形结构初步处理,修剪成存留设备之间的关系;
  313. * 2、处理成二维关系
  314. */
  315. #endregion
  316. var equipmentNodes= ConvertEquipmentNodes(etn);
  317. var loopNodes = new List<EquipmentNode>(equipmentNodes);
  318. for (int i = 0; i < loopNodes.Count; i++)
  319. {
  320. var currentNode = loopNodes[i];
  321. foreach (var childNode in currentNode.Nodes)
  322. {
  323. BinaryRelationItem relation = new BinaryRelationItem();
  324. relation.From = currentNode.CloneCurrentNode();
  325. relation.To = childNode.CloneCurrentNode();
  326. result.Add(relation);
  327. loopNodes.Add(childNode);
  328. }
  329. }
  330. return result;
  331. }
  332. /// <summary>
  333. /// 将elment树转换成设备节点类
  334. /// </summary>
  335. /// <param name="etn"></param>
  336. /// <returns></returns>
  337. private static List<EquipmentNode> ConvertEquipmentNodes(ElementTreeNode etn)
  338. {
  339. List<EquipmentNode> nodes = new List<EquipmentNode>();
  340. List<ElementTreeNode> loopNodes = new List<ElementTreeNode>(){ etn };
  341. for (int i = 0; i < loopNodes.Count; i++)
  342. {
  343. var currentNode = loopNodes[i];
  344. if (TryGetNode(currentNode.Current, out EquipmentNode outNode))
  345. {
  346. foreach (var currentNodeNode in currentNode.Nodes)
  347. {
  348. outNode.AddChildren(ConvertEquipmentNodes(currentNodeNode));
  349. }
  350. nodes.Add(outNode);
  351. }
  352. else
  353. {
  354. loopNodes.AddRange(currentNode.Nodes);
  355. }
  356. }
  357. return nodes;
  358. }
  359. /// <summary>
  360. /// 获取element对应节点
  361. /// </summary>
  362. /// <param name="element">输入element</param>
  363. /// <param name="outNode">成功获取时,node真实值</param>
  364. /// <returns>成功获取</returns>
  365. private static bool TryGetNode(Element element,out EquipmentNode outNode)
  366. {
  367. bool flag = false;
  368. EquipmentNode node = new EquipmentNode();
  369. if (element is Space space)
  370. {
  371. node.Category = PointItemType.Space.ToString();
  372. }
  373. else if (element is Pipe&& SystemCalcUtil.IsStart(element))
  374. {
  375. node.Category = PointItemType.StandPipe.ToString();
  376. }
  377. else if (element is FamilyInstance)
  378. {
  379. if (MBIInfoUtil.IsEquipment(element))
  380. {
  381. node.Category = PointItemType.Equipment.ToString();
  382. }
  383. else if (SystemCalcUtil.IsStart(element))
  384. {
  385. node.Category = PointItemType.StartValve.ToString();
  386. }
  387. }
  388. flag = !string.IsNullOrEmpty(node.Category);
  389. outNode = flag ? node : null;
  390. #region 确定返回之后进行后初始化处理
  391. if (outNode != null)
  392. {
  393. outNode.RevitId = element.Id.IntegerValue;
  394. outNode.BimId = element.GetBimId();
  395. }
  396. #endregion
  397. return flag;
  398. }
  399. #endregion
  400. #region 创建立面绘图和关系数据
  401. /// <summary>
  402. /// 计算立面数据
  403. /// </summary>
  404. public static VerticalResult ComputerVerticalData(List<VerticalPipe> pipeDatas, List<LevelData> levelDatas)
  405. {
  406. List<VerticalDrawRecord> drawRecords = new List<VerticalDrawRecord>();
  407. List<VerticalRelationRecord> relationRecords = new List<VerticalRelationRecord>();
  408. #region 标高绘图数据整理
  409. List<VFloor> floors = new List<VFloor>();
  410. for (int i = 0; i < levelDatas.Count; i++)
  411. {
  412. var tempFloor = new VFloor();
  413. tempFloor.Name = levelDatas[i].Name;
  414. if (tempFloor.Name == "F1")
  415. {
  416. tempFloor.ValueDescription = "G";
  417. }
  418. tempFloor.Index = i;
  419. tempFloor.FloorId = i.ToString();
  420. tempFloor.LinkId = levelDatas[i].Id;
  421. floors.Add(tempFloor);
  422. }
  423. #endregion
  424. #region 立管或相当于立管的空间处理
  425. /*
  426. * 按系统分组,[一个空间会不会对应多个系统]
  427. */
  428. var comparer = new VpComparer();
  429. int groupId = 0;
  430. //按系统分组
  431. var goupSystems = pipeDatas.GroupBy(p => p.PipeSytemType);
  432. foreach (var groupSystem in goupSystems)
  433. {
  434. //按坐标分组
  435. foreach (var groupPosition in groupSystem.GroupBy(p=>p.DownPoint3D, comparer))
  436. {
  437. var currentGroupdId = ++groupId ;
  438. SetRelationItem set = new SetRelationItem();
  439. #region 处理绘图数据
  440. foreach (var groupFloor in groupPosition.GroupBy(p => p.LevelName))
  441. {
  442. var verData = groupFloor.FirstOrDefault();
  443. string floorId = floors.FirstOrDefault(c => c.Name == groupFloor.Key)?.FloorId;
  444. #region 生成存储核心数据
  445. VPipe pipe = new VPipe();
  446. pipe.LinkId = verData.Id;
  447. pipe.Name = string.Join(",", groupFloor.Select(v => v.Display));//verData.Display;
  448. pipe.Tip = verData.DisplayTip;
  449. pipe.PipeSystemType = verData.PipeSytemType;
  450. pipe.FloorId = floorId;
  451. #endregion
  452. VerticalDrawRecord drawRecord = new VerticalDrawRecord();
  453. drawRecord.GroupId = currentGroupdId.ToString();
  454. drawRecord.RefPipe = pipe;
  455. drawRecord.SystemName = groupSystem.Key;
  456. drawRecords.Add(drawRecord);
  457. set.AddRange(groupFloor.Select(v => string.Format("{0}:{1}", floorId, v.Id)));
  458. }
  459. #endregion
  460. #region 处理关系数据
  461. VerticalRelationRecord relationRecord = new VerticalRelationRecord();
  462. relationRecord.SystemName = groupSystem.Key;
  463. relationRecord.RelationItems = set;
  464. relationRecords.Add(relationRecord);
  465. #endregion
  466. }
  467. }
  468. #endregion
  469. VerticalResult result = new VerticalResult();
  470. result.DrawData = new VerticalDrawData() { LevelDatas = floors, DrawRecords = drawRecords };
  471. result.RelationData = relationRecords;
  472. return result;
  473. }
  474. #endregion
  475. }
  476. /// <summary>
  477. /// 点位置比较相关
  478. /// </summary>
  479. public class VpComparer : IEqualityComparer<Point3D>
  480. {
  481. /// <summary>
  482. /// 立管左右错开小于2Cm
  483. ///
  484. /// </summary>
  485. /// <param name="x"></param>
  486. /// <param name="y"></param>
  487. /// <returns></returns>
  488. public bool Equals(Point3D x, Point3D y)
  489. {//数据为英寸
  490. var diff = 2 * 10d / 304.8;
  491. return Math.Abs(x.X - y.X) <= diff && Math.Abs(x.Y - y.Y) <= diff;
  492. }
  493. public int GetHashCode(Point3D obj)
  494. {
  495. return 0;
  496. }
  497. }
  498. }