using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using Yw.WinFrmUI.Q3d; using Yw.WinFrmUI.Q3d; using static System.Math; using static Yw.WinFrmUI.Q3d.ObjectEnum; namespace Yw.WinFrmUI.Q3d { public partial class NetworkViewModel { public void BuildRelation() { //读取坐标 int k1 = 0;//表示管线索引 int k2 = 0;//表示节点索引 Nodes.Sort((a, b) => string.Compare(a.ID, b.ID)); //建立点线关系链表StartNode,先将管线以Node1(节点1的ID)排序,再将Nodes按ID排序,建立两个游标k1、k2,正向一次循环,建立链表关系 //时间复杂度 O(n) Links.Sort((a, b) => string.Compare(a.Node1, b.Node1)); k2 = 0; k1 = 0; while (k1 < Links.Count) { var p = Links[k1]; var J = Nodes[k2]; var k0 = k2; while (J.ID != p.Node1 && k2 < Nodes.Count) { k2++; if (k2 < Nodes.Count) J = Nodes[k2]; } if (k2 == Nodes.Count) { k2 = k0; k1++; p.Visible = false; continue; //throw new Exception($"未找到Link[{p.ID}]的起始节点[{p.Node1}]"); } p.StartNode = J; p.Node1 = J.ID; if (J.MaxDiameter < p.Diameter) J.MaxDiameter = p.Diameter; J.Links.Add(p); k1++; } //建立点线关系链表StartNode,先将管线以Node2(节点1的ID)排序,再将Nodes按ID排序,建立两个游标k1、k2,正向一次循环,建立链表关系 //时间复杂度 O(n) Links.Sort((a, b) => string.Compare(a.Node2, b.Node2)); k2 = 0; k1 = 0; while (k1 < Links.Count) { var p = Links[k1]; var J = Nodes[k2]; var k0 = k2; while (J.ID != p.Node2 && k2 < Nodes.Count) { k2++; if (k2 < Nodes.Count) J = Nodes[k2]; } if (k2 == Nodes.Count) { k2 = k0; k1++; p.Visible = false; continue; //throw new Exception($"未找到Link[{p.ID}]的终止节点[{p.Node2}]"); } p.EndNode = J; p.Node2 = J.ID; if (J.MaxDiameter < p.Diameter) J.MaxDiameter = p.Diameter; J.Links.Add(p); k1++; } if (dict_dataset == null) { dict_dataset = new Dictionary(); } pumps.ForEach(p => { foreach (var kp in p.Datasets) { if (!dict_dataset.ContainsKey(kp.Value.Name)) { dict_dataset.Add(kp.Value.Name, kp.Value); } } }); pumps.ForEach((pump) => { if (dict_dataset.ContainsKey(pump.HeadCurve)) { Dataset ds = dict_dataset[pump.HeadCurve]; if (!pump.Datasets.ContainsKey("流量扬程曲线")) { pump.Datasets.Add("流量扬程曲线", ds); } else { pump.Datasets["流量扬程曲线"] = ds; } ds.pump = pump; } if (!pump.Datasets.ContainsKey("流量扬程曲线")) { pump.Datasets.Add("流量扬程曲线", new Dataset(pump.ID + "_Head", pump)); } if (pump.Datasets["流量扬程曲线"].Data.Count <= 0) { pump.Datasets["流量扬程曲线"].Data.Add(new PointF((float)pump.额定流量, (float)pump.额定扬程)); } pump.HeadCurve = pump.Datasets["流量扬程曲线"].Name; }); //dict_dataset.Clear(); //valveNodes.ForEach(v => Nodes.Remove(v)); ; Hash_ID = new HashSet(); Nodes.ForEach(o => Hash_ID.Add(o.ID)); Links.ForEach(o => Hash_ID.Add(o.ID)); } public bool BuildFromInp(string filePath) { if (filePath == null || !File.Exists(filePath)) return false; List points = new List(); dict_dataset = new Dictionary(); StreamReader sr = new StreamReader(filePath); //try { Nodes = new NodeViewModelList(); Links = new LinkViewModelList(); string line; string section = ""; while ((line = sr.ReadLine()) != null) { if (line.Trim().StartsWith("[")) { section = line.TrimStart('[').TrimEnd(']'); } else { string s = line.Trim('\t').Trim(' '); if (s.Length == 0 || s[0] == ';') continue; Parts parts = new Parts(line); section = section.ToUpper(); IBaseViewModel o = null; switch (section) { case "JUNCTIONS": { NodeViewModel j = null; if (parts[-2] == "Meter") { var m = new MeterViewModel(); //m.DlTemplateID = parts.ToString(-4, ""); j = m; } else if (parts[-2] == "Nozzle") { var n = new NozzleViewModel(); j = n; n.FlowCoefficient = parts.ToFloat(-4, 0); } else j = new JunctionViewModel(); o = j; j.ID = parts.ToString(0, null); j.Z = parts.ToFloat(1, 0); j.Demand = parts.ToFloat(2, 0); j.PatternID = parts.ToString(3, null); if (j.PatternID == "NONE") j.PatternID = null; j.Level = parts.ToInt(-1, 0); Nodes.Add(j); } break; case "RESERVOIRS": { ReservoirViewModel r = new ReservoirViewModel(); o = r; r.ID = parts.ToString(0, null); r.Head = parts.ToFloat(1, 0); r.PatternID = parts.ToString(2, null); r.Level = parts.ToInt(-1, 0); r.Z = parts.ToFloat(-2, 0); Nodes.Add(r); } break; case "TANKS": { TankViewModel tank = new TankViewModel(); o = tank; tank.ID = parts.ToString(0, null); tank.Z = parts.ToFloat(1, 0); tank.InitLevel = parts.ToFloat(2, 0); tank.MinLevel = parts.ToFloat(3, 0); tank.MaxLevel = parts.ToFloat(4, 0); tank.Diameter = parts.ToFloat(5, 0); tank.MinVol = parts.ToFloat(6, 0); tank.VolCurve = parts.ToString(7, null); tank.Overflow = ""; tank.Level = parts.ToInt(-1, 0); Nodes.Add(tank); } break; case "VALVES": { ValveViewModel valve = new ValveViewModel(); o = valve; valve.ID = parts.ToString(0, null); // 取出Node1和Node2中的字母部分,例如“S201326593”被取出为“201326593” valve.Node1 = parts.ToString(1, null); // Regex.Replace(parts[1], "[^0-9]", ""); valve.Node2 = parts.ToString(2, null); // Regex.Replace(parts[2], "[^0-9]", ""); valve.Diameter = parts.ToFloat(3, 0); valve.Type = parts.ToString(4, null); valve.Setting = parts.ToString(5, null); valve.MinorLoss = parts.ToFloat(6, 0); valve.Level = parts.ToInt(-1, 0); Links.Add(valve); } break; case "PUMPS": { PumpViewModel pump = new PumpViewModel(); o = pump; pump.ID = parts.ToString(0, null); // 取出Node1和Node2中的字母部分,例如“S201326593”被取出为“201326593” pump.Node1 = parts.ToString(1, null); // Regex.Replace(parts[1], "[^0-9]", ""); pump.Node2 = parts.ToString(2, null); // Regex.Replace(parts[2], "[^0-9]", ""); int index = 3; string label = null; while ((label = parts.ToString(index, null)) != null) { label = label.ToUpper(); switch (label) { case "HEAD": pump.HeadCurve = parts.ToString(index + 1, "PumpDefault"); break; case "SPEED": pump.当前转速 = parts.ToFloat(index + 1, 0); break; } index += 2; } //pump.Diameter = parts.ToFloat(3, 0); pump.Level = parts.ToInt(-1, 0); Links.Add(pump); } break; case "CURVES": { string ID = parts.ToString(0, ""); if (!dict_dataset.ContainsKey(ID)) { var ds = new Dataset(ID, null); dict_dataset.Add(ID, ds); } dict_dataset[ID]._data.Add(new PointF(parts.ToFloat(1, 0), parts.ToFloat(2, 0))); } break; case "COORDINATES": { string id = parts[0]; float x; float y; if (float.TryParse(parts[1], out x) && float.TryParse(parts[2], out y)) { points.Add(new Coor(id, new PointF(x, y))); } } break; case "STATUS": { LinkViewModel link = Links.Find(l => l.ID == parts.ToString(0, null)); if (link != null) link.Status = StringToStatus(parts.ToString(1, "OPEN")); } break; } if (o != null) { // o.Tags = new TagList(parts.ToString(-3, null)); } } } sr.Close(); if (!dict_dataset.ContainsKey("GPVDefault")) { var data = new Dataset("GPVDefault", null); data.Data = new List() { new PointF(0,0), new PointF(100,0), }; dict_dataset.Add("GPVDefault", data); } if (!dict_dataset.ContainsKey("PumpDefault")) { var data = new Dataset("PumpDefault", null); data.Data = new List() { new PointF(0f , 45.38f), new PointF(83.33333333f ,45.25f), new PointF(111.1111111f ,45.12f), new PointF(138.8888889f ,44.96f), new PointF(166.6666667f ,44.76f), new PointF(194.4444444f ,44.52f), new PointF(222.2222222f ,44.24f), new PointF(250f ,43.92f), new PointF(277.7777778f ,43.56f), new PointF(305.5555556f ,43.17f), new PointF(333.3333333f ,42.73f), new PointF(361.1111111f ,42.25f), new PointF(388.8888889f ,41.74f), new PointF(416.6666667f ,41.18f), new PointF(444.4444444f ,40.58f), new PointF(472.2222222f ,39.95f), new PointF(500f ,39.28f), new PointF(527.7777778f ,38.56f), new PointF(555.5555556f ,37.81f), new PointF(583.3333333f ,37.02f), new PointF(611.1111111f ,36.19f), new PointF(638.8888889f ,35.32f), new PointF(666.6666667f ,34.41f), new PointF(694.4444444f ,33.46f), new PointF(722.2222222f ,32.47f), new PointF(750f ,31.44f), new PointF(777.7777778f ,30.37f), new PointF(805.5555556f ,29.27f), }; dict_dataset.Add("PumpDefault", data); } //读取坐标 int k1 = 0; int k2 = 0; Nodes.Sort((a, b) => string.Compare(a.ID, b.ID)); points.Sort((a, b) => string.Compare(a.ID, b.ID)); k1 = 0; k2 = 0; while (k1 < Nodes.Count) { var J = Nodes[k1]; var coor = points[k2]; while (J.ID != coor.ID && k2 < points.Count) { k2++; if (k2 < points.Count) coor = points[k2]; } if (k2 == points.Count) { throw new Exception($"未找到Node[{J.ID}]的坐标"); } J.X = coor.Position.X; J.Y = coor.Position.Y; k1++; } BuildRelation(); return true; } } private StatusType StringToStatus(string status) { switch (status) { case "CLOESD": case "0": return StatusType.CLOSED; break; case "OPEN": case "1": return StatusType.OPEN; break; case "ACTIVE": return StatusType.ACTIVE; break; default: return StatusType.DEFAULT; break; } } } public class Vector3D { public double X { get; set; } public double Y { get; set; } public double Z { get; set; } public Vector3D(double x, double y, double z) { X = x; Y = y; Z = z; } public static double CalculateAngle(Vector3D v1, Vector3D v2) { // 计算两个向量的点积 double dotProduct = v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z; // 计算两个向量的模长 double v1Magnitude = Math.Sqrt(v1.X * v1.X + v1.Y * v1.Y + v1.Z * v1.Z); double v2Magnitude = Math.Sqrt(v2.X * v2.X + v2.Y * v2.Y + v2.Z * v2.Z); // 计算夹角(弧度) double angleInRadians = Math.Acos(dotProduct / (v1Magnitude * v2Magnitude)); // 将弧度转换为度数 //double angleInDegrees = angleInRadians * (180 / Math.PI); return angleInRadians; } public double Length { get { return Math.Sqrt(X * X + Y * Y + Z * Z); } } } }