DataNode.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using System.Windows.Shapes;
  11. using Newtonsoft.Json.Linq;
  12. using SAGA.DotNetUtils.DB;
  13. namespace SAGA.Models {
  14. [TableName("DataNode")]
  15. [Serializable]
  16. public class DataNode {
  17. public int Id { get; set; }
  18. /// <summary>
  19. /// 设备revitID
  20. /// </summary>
  21. public string EId { get; set; }
  22. /// <summary>
  23. /// 楼层名称
  24. /// </summary>
  25. public string FloorName { get; set; }
  26. private string name;
  27. /// <summary>
  28. /// 本地名称
  29. /// </summary>
  30. public string EName
  31. {
  32. get => string.IsNullOrEmpty(this.name) ? this.EId : this.name;
  33. set => this.name = value;
  34. }
  35. /// <summary>
  36. /// 本地编码
  37. /// </summary>
  38. public string Sno { get; set; }
  39. /// <summary>
  40. /// 进线号(主电)
  41. /// </summary>
  42. public string InLineNo { get; set; }
  43. /// <summary>
  44. /// 进线号(备电)
  45. /// </summary>
  46. public string InLineNo1 { get; set; }
  47. /// <summary>
  48. /// 设备类型
  49. /// </summary>
  50. public string Type { get; set; }
  51. public DataNode Parent { get; set; }
  52. public List<DataNode> Childrens { get; set; } = new List<DataNode>();
  53. public Point Postition { get; set; }
  54. /// <summary>
  55. /// 用于显示属性窗体
  56. /// </summary>
  57. public JObject Infos { get; set; }
  58. private object data;
  59. public T GetData<T>() {
  60. if (data is T t) {
  61. return t;
  62. }
  63. return default(T);
  64. }
  65. public void SetData(object odata) {
  66. this.data = odata;
  67. }
  68. //public object Data { get; set; }
  69. public DataNode() {
  70. }
  71. public DataNode(string name) {
  72. this.name = name;
  73. this.Childrens = new List<DataNode>();
  74. }
  75. public DataNode(string name, DataNode parent) : this(name) {
  76. this.Parent = parent;
  77. }
  78. public Vertex GetVertex(double y) {
  79. this.Postition = new Point(this.Postition.X, y);
  80. return this.GetVertex();
  81. }
  82. public Vertex GetVertex(Point point) {
  83. this.Postition = point;
  84. return this.GetVertex();
  85. }
  86. public Vertex GetVertex() {
  87. var v = new Vertex(Postition) {
  88. ElementId = this.EId,
  89. RadiusX = 6,
  90. RadiusY = 6
  91. };
  92. this.Childrens.ForEach(t => {
  93. t.Postition = new Point(this.Postition.X + 270, this.Postition.Y);
  94. });
  95. return v;
  96. }
  97. public TextBlock GetText() {
  98. Border border = new Border() {
  99. Width = 140,
  100. Height = 100,
  101. BorderBrush = Brushes.Red,
  102. BorderThickness = new Thickness(1),
  103. Opacity = 0,
  104. CornerRadius = new CornerRadius(2),
  105. Background = Brushes.Azure
  106. };
  107. TextBlock label = new TextBlock {
  108. Text = RemoveNull(this.FloorName+":"+ this.Sno + ":" + this.EName),
  109. Width = 200,
  110. TextWrapping = TextWrapping.Wrap
  111. };
  112. string RemoveNull(string str)
  113. {
  114. return string.Join(":", str.Split(':').Where(t => !string.IsNullOrEmpty(t)));
  115. }
  116. // border.Child = label;
  117. Canvas.SetLeft(label, Postition.X - 70);
  118. Canvas.SetTop(label, Postition.Y - 12);
  119. return label;
  120. }
  121. public List<SgLine> GetLines() {
  122. List<SgLine> lines = new List<SgLine>();
  123. var n = Childrens.Count;
  124. for (int i = 1; i < n + 1; i++) {
  125. var angle = 180d / (n + 1) * i + 90 - 90 * (n % 2 - 1);
  126. SgLine line = new SgLine(Postition, angle);
  127. lines.Add(line);
  128. SgLine line1 = new SgLine(line.EndPoint, new Point(line.EndPoint.X + 40, line.EndPoint.Y));
  129. lines.Add(line1);
  130. Childrens[i - 1].Postition = line1.EndPoint;
  131. }
  132. return lines;
  133. }
  134. }
  135. public class Vertex : Shape, IVertex {
  136. protected override Geometry DefiningGeometry => new EllipseGeometry {
  137. Center = Position,
  138. RadiusX = RadiusX,
  139. RadiusY = RadiusY
  140. };
  141. public Point Position { get; set; }
  142. public string ElementId { get; set; }
  143. public Action<string> ShowAction { set; private get; }
  144. private static double radiusX = 1.6;
  145. private static double radiusY = 1.6;
  146. public static void SetRadius(double x, double y) {
  147. radiusX = x;
  148. radiusY = y;
  149. }
  150. public double RadiusX
  151. {
  152. get => radiusX;
  153. set => radiusX = value;
  154. }
  155. public double RadiusY
  156. {
  157. get => radiusY;
  158. set => radiusY = value;
  159. }
  160. public Vertex(Point center) {
  161. this.Fill = Brushes.Red;
  162. this.Position = center;
  163. this.MouseLeftButtonDown += Vertex_MouseLeftButtonDown;
  164. //this.MouseEnter += Vertex_MouseEnter;
  165. // this.MouseLeave += Vertex_MouseLeave;
  166. }
  167. private void Vertex_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e) {
  168. if (Fill != Brushes.Transparent) {//空心不适用事件
  169. this.Fill = Brushes.Red;
  170. this.RadiusY = RadiusY / 2.0d;
  171. this.RadiusX /= 2.0d;
  172. }
  173. }
  174. private void Vertex_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) {
  175. if (Fill != Brushes.Transparent) {//空心不适用事件
  176. this.Fill = Brushes.Blue;
  177. this.RadiusY = RadiusY * 2;
  178. this.RadiusX *= 2;
  179. }
  180. }
  181. private void Vertex_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
  182. ShowAction?.Invoke(this.ElementId);
  183. }
  184. public static Vertex GetHollow(Point center) {
  185. return new Vertex(center) {
  186. StrokeThickness = 2,
  187. Stroke = Brushes.Red,
  188. Fill = Brushes.Transparent
  189. };
  190. }
  191. }
  192. public interface IVertex {
  193. Point Position { get; }
  194. double RadiusX { get; set; }
  195. double RadiusY { get; set; }
  196. }
  197. public interface ILine {
  198. Point StartPoint { get; set; }
  199. Point EndPoint { get; set; }
  200. }
  201. public class SgLine : Shape, ILine {
  202. protected override Geometry DefiningGeometry => new LineGeometry(StartPoint, EndPoint);
  203. public Point StartPoint { get; set; }
  204. public Point EndPoint { get; set; }
  205. public double Length { get; set; } = 50;
  206. public double Angle { get; set; }
  207. /// <summary>
  208. /// 是否为新建的线
  209. /// </summary>
  210. public bool IsNew { get; set; }
  211. private SgLine() {
  212. StrokeThickness = 1;
  213. Stroke = Brushes.Red;
  214. }
  215. public SgLine(Vertex start, Vertex end) : this() {
  216. this.StartPoint = start.Position;
  217. this.EndPoint = end.Position;
  218. }
  219. public SgLine(Point start, Point end) : this() {
  220. this.StartPoint = start;
  221. this.EndPoint = end;
  222. System.Diagnostics.Debug.WriteLine($"start:{start.ToString()},end:{end}");
  223. }
  224. public SgLine(Point start, double angle) : this() {
  225. this.StartPoint = start;
  226. this.EndPoint = GetEndPoint(angle);
  227. }
  228. Point GetEndPoint(double angle) {
  229. if (angle == 90) {
  230. return new Point(StartPoint.X, StartPoint.Y + Length);
  231. }
  232. else if (angle == 270) {
  233. return new Point(StartPoint.X, StartPoint.Y - Length);
  234. }
  235. else {
  236. return new Point(StartPoint.X + Length, Math.Tan(angle / 180d * Math.PI) * Length + StartPoint.Y);
  237. }
  238. }
  239. }
  240. }