WinControlRelation.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using System.Windows.Threading;
  10. using SAGA.MBI.Common;
  11. using SAGA.Models;
  12. using SAGA.RevitUtils.Windows;
  13. namespace DrawData
  14. {
  15. /// <summary>
  16. /// MainWindow.xaml 的交互逻辑
  17. /// </summary>
  18. public partial class WinControlRelation : WinBase
  19. {
  20. public WinControlRelation()
  21. {
  22. InitializeComponent();
  23. this.Loaded += MainWindow_Loaded;
  24. }
  25. public ObservableCollection<DataNode> InDataNodes;
  26. private bool m_isReadData = true;
  27. private List<DataNode> dataNodes;
  28. private List<DataNode> orphanNodes;
  29. public WinControlRelation(List<DataNode> dataNodes, List<DataNode> orphanNodes) : this()
  30. {
  31. this.dataNodes = dataNodes;
  32. this.orphanNodes = orphanNodes;
  33. m_isReadData = false;
  34. }
  35. private Canvas Canvas => ucShowElement.Canvas;
  36. public UcShowElement UcShow => ucShowElement;
  37. public void ReLoad(List<DataNode> dataNodes, List<DataNode> orphanNodes)
  38. {
  39. this.dataNodes = dataNodes;
  40. this.orphanNodes = orphanNodes;
  41. MainWindow_Loaded(null, null);
  42. }
  43. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  44. {
  45. if (m_isReadData)
  46. {
  47. var dataNodes = DbHelper.FindOneByFile<DataNode>();
  48. DataNode root = new DataNode("对象受控关系") { Sno = "root", FloorName = "1" };
  49. dataNodes.Childrens.ForEach(d =>
  50. {
  51. //if (d.Type != "母联柜")
  52. {
  53. root.Childrens.Add(new DataNode(d.EName)
  54. {
  55. EId = d.EId,
  56. Sno = d.Sno,
  57. Type = d.Type,
  58. InLineNo = d.InLineNo,
  59. FloorName = d.FloorName
  60. });
  61. }
  62. });
  63. this.InDataNodes = new ObservableCollection<DataNode>(dataNodes.Childrens);
  64. this.rootNode.ItemsSource = new ObservableCollection<DataNode> { root };
  65. }
  66. else
  67. {
  68. DataNode root = new DataNode("对象受控关系") { Sno = "root", FloorName = "1" };
  69. var orphan = new DataNode("孤立节点") { Sno = "orphan", FloorName = "2" };
  70. //配电箱
  71. var orphanTdls = new DataNode("控制箱") { Sno = "TDLS", FloorName = "1" };
  72. //设备
  73. var orphanEq = new DataNode("设备") { Sno = "EQ", FloorName = "2" };
  74. orphan.Childrens.Add(orphanTdls);
  75. orphan.Childrens.Add(orphanEq);
  76. this.InDataNodes = new ObservableCollection<DataNode>(dataNodes);
  77. dataNodes.ForEach(d =>
  78. {
  79. root.Childrens.Add(new DataNode(d.EName)
  80. {
  81. EId = d.EId,
  82. Sno = d.Sno,
  83. Type = d.Type,
  84. InLineNo = d.InLineNo,
  85. FloorName = d.FloorName
  86. });
  87. });
  88. orphanNodes.ForEach(d =>
  89. {
  90. InDataNodes.Add(d);
  91. if (d.Type == "BACP" || d.Type == "BANC")
  92. {
  93. orphan.Childrens[0].Childrens.Add(new DataNode(d.EName)
  94. {
  95. EId = d.EId,
  96. Sno = d.Sno,
  97. Type = d.Type,
  98. InLineNo = d.InLineNo,
  99. FloorName = d.FloorName
  100. });
  101. }
  102. else
  103. {
  104. orphan.Childrens[1].Childrens.Add(new DataNode(d.EName)
  105. {
  106. EId = d.EId,
  107. Sno = d.Sno,
  108. Type = d.Type,
  109. InLineNo = d.InLineNo,
  110. FloorName = d.FloorName
  111. });
  112. }
  113. });
  114. root.Childrens = root.Childrens.OrderBy(t => t.FloorName).ToList();
  115. orphan.Childrens = orphan.Childrens.OrderBy(t => t.FloorName).ToList();
  116. orphan.Childrens[0].Childrens = orphan.Childrens[0].Childrens.OrderBy(t => t.FloorName).ToList();
  117. orphan.Childrens[1].Childrens = orphan.Childrens[1].Childrens.OrderBy(t => t.FloorName).ToList();
  118. this.rootNode.ItemsSource = new ObservableCollection<DataNode> { root, orphan };
  119. DrawRootNode(root);
  120. }
  121. }
  122. List<UIElement> m_rootElements = new List<UIElement>();
  123. void DrawRootNode(DataNode dn)
  124. {
  125. this.Canvas.Children.Clear();
  126. // var inData = dn.Childrens.Where(t => t.Type == "进线柜").OrderBy(t => t.Sno).ToList();
  127. var inData = dn.Childrens;
  128. var muData = dn.Childrens.Where(t => t.Type == "母联柜").ToList();
  129. var n = inData.Count;
  130. var perHeight = this.Canvas.ActualHeight / (n + 2d);
  131. var left = 100;
  132. for (int i = 1; i < n + 1; i++)
  133. {
  134. var node = inData[i - 1];
  135. //判断是否连接母联柜
  136. var regex = new Regex($"{node.Sno},|,{node.Sno}");
  137. var mu = muData.FirstOrDefault(d => regex.IsMatch(d.InLineNo));
  138. node.Postition = new Point(left, perHeight * i);
  139. //画当前点
  140. m_rootElements.Add(GetVertex(node));
  141. var text = node.GetText();
  142. text.Text = node.EName + "-" + node.Type;
  143. m_rootElements.Add(text);
  144. if (i == n) return;
  145. if (mu != null)
  146. {
  147. mu.Postition = new Point(left, perHeight * i + perHeight / 2.0d);
  148. //画母联柜
  149. m_rootElements.Add(GetVertex(mu));
  150. var muText = mu.GetText();
  151. muText.Text = mu.EName + "-" + mu.Type;
  152. m_rootElements.Add(muText);
  153. //画母联柜上下连线
  154. m_rootElements.Add(new SgLine(node.Postition, mu.Postition));
  155. m_rootElements.Add(new SgLine(mu.Postition, new Point(left, perHeight * (i + 1))));
  156. //移除母联柜
  157. muData.Remove(mu);
  158. }
  159. else
  160. {
  161. m_rootElements.Add(new SgLine(node.Postition, new Point(left, perHeight * (i + 1))));
  162. }
  163. }
  164. this.m_rootElements.ForEach(t => this.Canvas.Children.Add(t));
  165. }
  166. private int CurrentNodeCount = 0;
  167. private int level = 0;
  168. private double PerHeight;
  169. void Draw(DataNode dn)
  170. {
  171. CountNode(dn);
  172. PerHeight = Math.Max(this.Canvas.ActualHeight / (CurrentNodeCount + 2), 30d);
  173. //this.viewer.Height = PerHeight * (CurrentNodeCount + 2);
  174. this.Canvas.Height = PerHeight * (CurrentNodeCount + 2);
  175. CurrentNodeCount = 0;
  176. dn.Postition = new Point(100, 0);
  177. DrawData(dn);
  178. }
  179. /// <summary>
  180. /// 计算节点总数
  181. /// </summary>
  182. /// <param name="dn"></param>
  183. void CountNode(DataNode dn)
  184. {
  185. System.Diagnostics.Debug.WriteLine(dn.EName + ":" + dn.Type + ":" + dn.Sno);
  186. if (dn.Childrens.Count > 0)
  187. {
  188. foreach (var node in dn.Childrens)
  189. {
  190. if (node.Childrens.Count == 0)
  191. {
  192. CurrentNodeCount += 1;
  193. }
  194. else
  195. {
  196. CountNode(node);
  197. }
  198. }
  199. }
  200. }
  201. /// <summary>
  202. /// 将数据画呈现到窗体
  203. /// </summary>
  204. /// <param name="dn"></param>
  205. private void DrawData(DataNode dn)
  206. {
  207. CountNode(dn);
  208. double y;
  209. if (dn.Childrens.Count > 0)
  210. {
  211. y = ((CurrentNodeCount) / 2.0d + 1 + level) * PerHeight;
  212. }
  213. else
  214. {
  215. y = (CurrentNodeCount + 1 + level) * PerHeight;
  216. }
  217. var vertex = GetVertex(dn, y);
  218. this.Canvas.Children.Add(dn.GetText());
  219. this.Canvas.Children.Add(vertex);
  220. if (dn.Parent != null)//连接父节点
  221. {
  222. var turningPoint = new Point(dn.Postition.X - 100, dn.Postition.Y);
  223. this.Canvas.Children.Add(new SgLine(dn.Parent.Postition, turningPoint));
  224. var turningLine = new SgLine(turningPoint, dn.Postition);
  225. //备电使用虚线
  226. if (!string.IsNullOrEmpty(dn.InLineNo1))
  227. {
  228. ConvertToDotLine(turningLine);
  229. }
  230. this.Canvas.Children.Add(turningLine);
  231. }
  232. // dn.GetLines().ForEach((d) => m_canvas.Children.Add(d));
  233. CurrentNodeCount = 0;
  234. if (dn.Childrens.Count == 0) level += 1;
  235. string Sno(DataNode d)
  236. {
  237. var no = Regex.Match(d.Sno, "\\d+$").Value;
  238. return string.IsNullOrEmpty(no) ? "0" : no;
  239. }
  240. foreach (var d in dn.Childrens.OrderBy(t => t.FloorName).ThenBy(t => int.Parse(Sno(t))).ToList())
  241. {
  242. d.Parent = dn;
  243. DrawData(d);
  244. }
  245. }
  246. SgLine ConvertToDotLine(SgLine l)
  247. {
  248. l.StrokeThickness = 2;
  249. l.StrokeDashArray = new DoubleCollection() { 2, 3 };
  250. l.StrokeDashCap = PenLineCap.Triangle;
  251. l.StrokeEndLineCap = PenLineCap.Square;
  252. l.StrokeStartLineCap = PenLineCap.Round;
  253. return l;
  254. }
  255. Vertex GetVertex(DataNode dn, double y = double.MaxValue)
  256. {
  257. var v = y == double.MaxValue ? dn.GetVertex() : dn.GetVertex(y);
  258. v.ShowAction = (eid) =>
  259. {
  260. //显示属性窗体
  261. RevitModelFileUpload.ShowSpaceProperty(dn.EId);
  262. // var ShowProperty = this.scrollViewer.Content;
  263. //if (ShowProperty != null)
  264. //{
  265. // var mbi = ShowProperty.GetType();
  266. // MethodInfo method = mbi.GetMethod("ShowData");
  267. // method.Invoke(ShowProperty, new object[] { eid });
  268. //}
  269. };
  270. return v;
  271. }
  272. private delegate void NoArgDelegate();
  273. private void RootNode_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  274. {
  275. Dispatcher.BeginInvoke(DispatcherPriority.Background,
  276. (NoArgDelegate)delegate
  277. {
  278. if (this.rootNode.SelectedItem is DataNode currNode)
  279. {
  280. this.Canvas.Children.Clear();
  281. if (currNode.Sno == "root")
  282. {
  283. // this.m_rootElements.ForEach(t => this.Canvas.Children.Add(t));
  284. }
  285. else
  286. {
  287. // var data = InDataNodes.FirstOrDefault(d => d.Sno == currNode.Sno);//本地编码可能为空,改用服务器ID
  288. var data = InDataNodes.FirstOrDefault(d => d.EId == currNode.EId);
  289. if (data != null)
  290. {
  291. level = 0;
  292. Draw(data);
  293. //显示属性窗体
  294. RevitModelFileUpload.ShowSpaceProperty(data.EId);
  295. }
  296. }
  297. }
  298. });
  299. }
  300. }
  301. }