duheng
2025-03-28 dfe7e1653f8309e23e4c314cd58ac4ff7ce49dbc
WinFrmUI/Yw.WinFrmUI.Hydro.Q3d.Core/MapView/NetworkViewModel2Inp.cs
@@ -1,7 +1,4 @@
using Yw.WinFrmUI.Q3d;
using Yw.WinFrmUI.Q3d;
using Newtonsoft.Json;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
@@ -12,15 +9,17 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Yw.WinFrmUI.Q3d.ObjectEnum;
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()
        {
            //读取坐标
@@ -89,7 +88,7 @@
                k1++;
            }
            if (dict_dataset == null)
            {
                dict_dataset = new Dictionary<string, Dataset>();
@@ -139,8 +138,291 @@
            Links.ForEach(o => Hash_ID.Add(o.ID));
        }
        public bool BuildFromInp(string filePath)
        {
            if (filePath == null || !File.Exists(filePath)) return false;
            List<Coor> points = new List<Coor>();
            dict_dataset = new Dictionary<string, Dataset>();
            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<PointF>()
                    {
                        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<PointF>()
                    {
                        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;
            }
        }
    }