ComputerRelationFromPhysicalWorld.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:ComputerRelationFromPhysicalWorld
  3. * 作者:李勇
  4. * 创建时间: 2018/11/1 9:43:35
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Autodesk.Revit.DB;
  13. using Newtonsoft.Json.Linq;
  14. using SAGA.MBI.DataArrange;
  15. using SAGA.MBI.RequestData;
  16. using SAGA.Models;
  17. namespace SAGA.GplotRelationComputerManage
  18. {
  19. public abstract class ComputerRelationFromPhysicalWorld
  20. {
  21. private Dictionary<string, string> floorNameAndId;
  22. /// <summary>
  23. /// 计算服务端配电
  24. /// </summary>
  25. public List<DataNode> ComputerFromServer()
  26. {
  27. var str = new[] { "TDHS", "TDLS", "TDTF", "TDLSDW" };
  28. List<JObject> datas = CommonConvert.QueryObjectInfoByTypes(str);
  29. var result = new List<DataNode>();
  30. floorNameAndId = DalProjectTree.GetFloorNameAndId();
  31. var allDataNodes = ConvertToDataNode(datas);
  32. DealServerDatas(allDataNodes);
  33. //独立树形:有子节点,父节点为空
  34. var incomingCabinet = allDataNodes.Where(t => t.Childrens.Count != 0 && t.Parent == null).ToList();
  35. //孤立节点:无父无子
  36. var orphanNodes = allDataNodes.Where(t => t.Childrens.Count == 0 && t.Parent == null).ToList();
  37. DrawDataServer.SaveAsFile<DataNode>(new DataNode("root") { Childrens = incomingCabinet }, "LUDistribution");
  38. result.Add(new DataNode() { Childrens = incomingCabinet });
  39. result.Add(new DataNode() { Childrens = orphanNodes });
  40. return result;
  41. }
  42. private List<DataNode> DealDatas(List<DataNode> familyInstances, out List<DataNode> incomingCabinet)
  43. {
  44. var allDataNodes = familyInstances;
  45. //无信息节点的孤儿节点
  46. var orphanNodes = allDataNodes.Where(t => t.Sno == "" || t.Type == "" || t.EName == "").ToList();
  47. //正常数据
  48. var dataNodes = allDataNodes.Where(t => t.Sno != "" && t.Type != "" && t.EName != "").ToList();
  49. //找出所有出线柜(outgoingCubicle)
  50. //找出所有进线柜
  51. incomingCabinet = dataNodes.Where(t => t.Type == "进线柜" || t.Type == "母联柜").ToList();
  52. //总数据里面移除所有进线柜,并移除进线柜中非根节点的进线柜(进线柜的进线号为变压器)
  53. for (var i = 0; i < incomingCabinet.Count; i++)
  54. {
  55. var node = incomingCabinet[i];
  56. if ((dataNodes.Any(t => t.Sno == node.InLineNo && t.Type == "低压变压器") || node.Type == "母联柜" ||
  57. node.InLineNo == "") && node.EName != "")
  58. {
  59. dataNodes.Remove(node);
  60. }
  61. else
  62. {
  63. incomingCabinet.Remove(node);
  64. i--;
  65. }
  66. }
  67. //incomingCabinet.ForEach(d => );
  68. //List<DataNode> incomingCabinetNode =
  69. incomingCabinet.ForEach(d =>
  70. {
  71. if (d.Type != "母联柜")
  72. {
  73. GetRelationNode(d, dataNodes);
  74. }
  75. RemoveNode(d, dataNodes); //剩余的节点中去除已成数列的节点
  76. });
  77. //剩余节点中找到进线号不存在或者为空的节点,作为孤立点的子节点
  78. var oroot = dataNodes.Where(t => t.InLineNo == "" || dataNodes.All(o => o.Sno != t.InLineNo)).ToList();
  79. oroot.ForEach(d => { GetRelationNode(d, dataNodes); });
  80. orphanNodes.AddRange(oroot);
  81. return orphanNodes;
  82. }
  83. private void DealServerDatas(List<DataNode> familyInstances)
  84. {
  85. for (int i = 0; i < familyInstances.Count; i++)
  86. {
  87. var dn = familyInstances[i];
  88. GetRelationNodes(dn, familyInstances);
  89. }
  90. }
  91. public List<DataNode> ConvertToDataNode(List<JObject> datas)
  92. {
  93. var allDataNodes = datas.Select(f =>
  94. {
  95. var node = new DataNode(GetValue(f, "EquipLocalName"))//设备本地名称,
  96. {
  97. EId = GetValue(f, "EquipID"),
  98. Sno = GetValue(f, "EquipLocalID"),//设备本地编码
  99. InLineNo = GetValue(f, "ctm-MainPower"), //进线号,主电
  100. InLineNo1 = GetValue(f, "ctm-StandbyPower"), //进线号,主电
  101. FloorName = GetFloorNameByBimId(GetValue(f, "BIMID")), //+ //+ f.GetParameterString("所属柜号"),
  102. Infos = f,
  103. Type = (f["category"] + "").Remove(0, 2)//设备类型编码
  104. };
  105. return node;
  106. });
  107. return allDataNodes.ToList();
  108. }
  109. string GetFloorNameByBimId(string bimId)
  110. {
  111. if (string.IsNullOrEmpty(bimId)) return "";
  112. var id = bimId.Split(':')[0];
  113. if (floorNameAndId.ContainsKey(id))
  114. return floorNameAndId[id];
  115. return id;
  116. }
  117. public string GetValue(JObject equip, string propertyName)
  118. {
  119. string result = "";
  120. if (equip != null)
  121. {
  122. result = equip["infos"][propertyName] + "";
  123. //本地名称为空显示设备名称
  124. if (propertyName == "EquipLocalName" && result == "")
  125. {
  126. result = equip["infos"]["EquipName"] + "";
  127. }
  128. }
  129. return result;
  130. }
  131. void GetRelationNode(DataNode dn, List<DataNode> datas)
  132. {
  133. var nodes = datas.Where(t => t.InLineNo == dn.Sno).ToList();
  134. if (nodes.Count > 0)
  135. {
  136. dn.Childrens.AddRange(nodes);
  137. datas.Remove(dn);
  138. foreach (var dataNode in nodes)
  139. {
  140. GetRelationNode(dataNode, datas);
  141. }
  142. }
  143. }
  144. void GetRelationNodes(DataNode dn, List<DataNode> datas)
  145. {
  146. var nodes = datas.Where(t => t.InLineNo == dn.Sno && dn.Sno != "").ToList();
  147. if (nodes.Count > 0 && dn.Childrens.Count == 0)
  148. {
  149. dn.Childrens.AddRange(nodes);
  150. foreach (var dataNode in nodes)
  151. {
  152. dataNode.Parent = dn;
  153. if (dataNode.Childrens.Count > 0) continue;//已查找过子节点
  154. GetRelationNodes(dataNode, datas);
  155. }
  156. }
  157. }
  158. void RemoveNode(DataNode dn, List<DataNode> datas)
  159. {
  160. datas.Remove(dn);
  161. if (dn.Childrens.Count > 0)
  162. {
  163. foreach (var dataNode in dn.Childrens)
  164. {
  165. RemoveNode(dataNode, datas);
  166. }
  167. }
  168. }
  169. }
  170. }