| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Threading;
- using SAGA.MBI.Common;
- using SAGA.Models;
- using SAGA.RevitUtils.Windows;
- namespace DrawData
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class WinControlRelation : WinBase
- {
- public WinControlRelation()
- {
- InitializeComponent();
- this.Loaded += MainWindow_Loaded;
- }
- public ObservableCollection<DataNode> InDataNodes;
- private bool m_isReadData = true;
- private List<DataNode> dataNodes;
- private List<DataNode> orphanNodes;
- public WinControlRelation(List<DataNode> dataNodes, List<DataNode> orphanNodes) : this()
- {
- this.dataNodes = dataNodes;
- this.orphanNodes = orphanNodes;
- m_isReadData = false;
- }
- private Canvas Canvas => ucShowElement.Canvas;
- public UcShowElement UcShow => ucShowElement;
- public void ReLoad(List<DataNode> dataNodes, List<DataNode> orphanNodes)
- {
- this.dataNodes = dataNodes;
- this.orphanNodes = orphanNodes;
- MainWindow_Loaded(null, null);
- }
- private void MainWindow_Loaded(object sender, RoutedEventArgs e)
- {
- if (m_isReadData)
- {
- var dataNodes = DbHelper.FindOneByFile<DataNode>();
- DataNode root = new DataNode("对象受控关系") { Sno = "root", FloorName = "1" };
- dataNodes.Childrens.ForEach(d =>
- {
- //if (d.Type != "母联柜")
- {
- root.Childrens.Add(new DataNode(d.EName)
- {
- EId = d.EId,
- Sno = d.Sno,
- Type = d.Type,
- InLineNo = d.InLineNo,
- FloorName = d.FloorName
- });
- }
- });
- this.InDataNodes = new ObservableCollection<DataNode>(dataNodes.Childrens);
- this.rootNode.ItemsSource = new ObservableCollection<DataNode> { root };
- }
- else
- {
- DataNode root = new DataNode("对象受控关系") { Sno = "root", FloorName = "1" };
- var orphan = new DataNode("孤立节点") { Sno = "orphan", FloorName = "2" };
- //配电箱
- var orphanTdls = new DataNode("控制箱") { Sno = "TDLS", FloorName = "1" };
- //设备
- var orphanEq = new DataNode("设备") { Sno = "EQ", FloorName = "2" };
- orphan.Childrens.Add(orphanTdls);
- orphan.Childrens.Add(orphanEq);
- this.InDataNodes = new ObservableCollection<DataNode>(dataNodes);
- dataNodes.ForEach(d =>
- {
- root.Childrens.Add(new DataNode(d.EName)
- {
- EId = d.EId,
- Sno = d.Sno,
- Type = d.Type,
- InLineNo = d.InLineNo,
- FloorName = d.FloorName
- });
- });
- orphanNodes.ForEach(d =>
- {
- InDataNodes.Add(d);
- if (d.Type == "BACP" || d.Type == "BANC")
- {
- orphan.Childrens[0].Childrens.Add(new DataNode(d.EName)
- {
- EId = d.EId,
- Sno = d.Sno,
- Type = d.Type,
- InLineNo = d.InLineNo,
- FloorName = d.FloorName
- });
- }
- else
- {
- orphan.Childrens[1].Childrens.Add(new DataNode(d.EName)
- {
- EId = d.EId,
- Sno = d.Sno,
- Type = d.Type,
- InLineNo = d.InLineNo,
- FloorName = d.FloorName
- });
- }
- });
- root.Childrens = root.Childrens.OrderBy(t => t.FloorName).ToList();
- orphan.Childrens = orphan.Childrens.OrderBy(t => t.FloorName).ToList();
- orphan.Childrens[0].Childrens = orphan.Childrens[0].Childrens.OrderBy(t => t.FloorName).ToList();
- orphan.Childrens[1].Childrens = orphan.Childrens[1].Childrens.OrderBy(t => t.FloorName).ToList();
- this.rootNode.ItemsSource = new ObservableCollection<DataNode> { root, orphan };
- DrawRootNode(root);
- }
- }
- List<UIElement> m_rootElements = new List<UIElement>();
- void DrawRootNode(DataNode dn)
- {
- this.Canvas.Children.Clear();
- // var inData = dn.Childrens.Where(t => t.Type == "进线柜").OrderBy(t => t.Sno).ToList();
- var inData = dn.Childrens;
- var muData = dn.Childrens.Where(t => t.Type == "母联柜").ToList();
- var n = inData.Count;
- var perHeight = this.Canvas.ActualHeight / (n + 2d);
- var left = 100;
- for (int i = 1; i < n + 1; i++)
- {
- var node = inData[i - 1];
- //判断是否连接母联柜
- var regex = new Regex($"{node.Sno},|,{node.Sno}");
- var mu = muData.FirstOrDefault(d => regex.IsMatch(d.InLineNo));
- node.Postition = new Point(left, perHeight * i);
- //画当前点
- m_rootElements.Add(GetVertex(node));
- var text = node.GetText();
- text.Text = node.EName + "-" + node.Type;
- m_rootElements.Add(text);
- if (i == n) return;
- if (mu != null)
- {
- mu.Postition = new Point(left, perHeight * i + perHeight / 2.0d);
- //画母联柜
- m_rootElements.Add(GetVertex(mu));
- var muText = mu.GetText();
- muText.Text = mu.EName + "-" + mu.Type;
- m_rootElements.Add(muText);
- //画母联柜上下连线
- m_rootElements.Add(new SgLine(node.Postition, mu.Postition));
- m_rootElements.Add(new SgLine(mu.Postition, new Point(left, perHeight * (i + 1))));
- //移除母联柜
- muData.Remove(mu);
- }
- else
- {
- m_rootElements.Add(new SgLine(node.Postition, new Point(left, perHeight * (i + 1))));
- }
- }
- this.m_rootElements.ForEach(t => this.Canvas.Children.Add(t));
- }
- private int CurrentNodeCount = 0;
- private int level = 0;
- private double PerHeight;
- void Draw(DataNode dn)
- {
- CountNode(dn);
- PerHeight = Math.Max(this.Canvas.ActualHeight / (CurrentNodeCount + 2), 30d);
- //this.viewer.Height = PerHeight * (CurrentNodeCount + 2);
- this.Canvas.Height = PerHeight * (CurrentNodeCount + 2);
- CurrentNodeCount = 0;
- dn.Postition = new Point(100, 0);
- DrawData(dn);
- }
- /// <summary>
- /// 计算节点总数
- /// </summary>
- /// <param name="dn"></param>
- void CountNode(DataNode dn)
- {
- System.Diagnostics.Debug.WriteLine(dn.EName + ":" + dn.Type + ":" + dn.Sno);
- if (dn.Childrens.Count > 0)
- {
- foreach (var node in dn.Childrens)
- {
- if (node.Childrens.Count == 0)
- {
- CurrentNodeCount += 1;
- }
- else
- {
- CountNode(node);
- }
- }
- }
- }
- /// <summary>
- /// 将数据画呈现到窗体
- /// </summary>
- /// <param name="dn"></param>
- private void DrawData(DataNode dn)
- {
- CountNode(dn);
- double y;
- if (dn.Childrens.Count > 0)
- {
- y = ((CurrentNodeCount) / 2.0d + 1 + level) * PerHeight;
- }
- else
- {
- y = (CurrentNodeCount + 1 + level) * PerHeight;
- }
- var vertex = GetVertex(dn, y);
- this.Canvas.Children.Add(dn.GetText());
- this.Canvas.Children.Add(vertex);
- if (dn.Parent != null)//连接父节点
- {
- var turningPoint = new Point(dn.Postition.X - 100, dn.Postition.Y);
- this.Canvas.Children.Add(new SgLine(dn.Parent.Postition, turningPoint));
- var turningLine = new SgLine(turningPoint, dn.Postition);
- //备电使用虚线
- if (!string.IsNullOrEmpty(dn.InLineNo1))
- {
- ConvertToDotLine(turningLine);
- }
- this.Canvas.Children.Add(turningLine);
- }
- // dn.GetLines().ForEach((d) => m_canvas.Children.Add(d));
- CurrentNodeCount = 0;
- if (dn.Childrens.Count == 0) level += 1;
- string Sno(DataNode d)
- {
- var no = Regex.Match(d.Sno, "\\d+$").Value;
- return string.IsNullOrEmpty(no) ? "0" : no;
- }
- foreach (var d in dn.Childrens.OrderBy(t => t.FloorName).ThenBy(t => int.Parse(Sno(t))).ToList())
- {
- d.Parent = dn;
- DrawData(d);
- }
- }
- SgLine ConvertToDotLine(SgLine l)
- {
- l.StrokeThickness = 2;
- l.StrokeDashArray = new DoubleCollection() { 2, 3 };
- l.StrokeDashCap = PenLineCap.Triangle;
- l.StrokeEndLineCap = PenLineCap.Square;
- l.StrokeStartLineCap = PenLineCap.Round;
- return l;
- }
- Vertex GetVertex(DataNode dn, double y = double.MaxValue)
- {
- var v = y == double.MaxValue ? dn.GetVertex() : dn.GetVertex(y);
- v.ShowAction = (eid) =>
- {
- //显示属性窗体
- RevitModelFileUpload.ShowSpaceProperty(dn.EId);
- // var ShowProperty = this.scrollViewer.Content;
- //if (ShowProperty != null)
- //{
- // var mbi = ShowProperty.GetType();
- // MethodInfo method = mbi.GetMethod("ShowData");
- // method.Invoke(ShowProperty, new object[] { eid });
- //}
- };
- return v;
- }
- private delegate void NoArgDelegate();
- private void RootNode_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
- {
- Dispatcher.BeginInvoke(DispatcherPriority.Background,
- (NoArgDelegate)delegate
- {
- if (this.rootNode.SelectedItem is DataNode currNode)
- {
- this.Canvas.Children.Clear();
- if (currNode.Sno == "root")
- {
- // this.m_rootElements.ForEach(t => this.Canvas.Children.Add(t));
- }
- else
- {
- // var data = InDataNodes.FirstOrDefault(d => d.Sno == currNode.Sno);//本地编码可能为空,改用服务器ID
- var data = InDataNodes.FirstOrDefault(d => d.EId == currNode.EId);
- if (data != null)
- {
- level = 0;
- Draw(data);
- //显示属性窗体
- RevitModelFileUpload.ShowSpaceProperty(data.EId);
- }
- }
- }
- });
- }
- }
- }
|