From 5fa6947054206e2e781eadd4effdcdf52eda28c4 Mon Sep 17 00:00:00 2001
From: cloudflight <cloudflight@126.com>
Date: 星期二, 26 十二月 2023 11:02:28 +0800
Subject: [PATCH] 2023年12月26日

---
 Hydro.Inp/NetWork.cs |  414 +++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 329 insertions(+), 85 deletions(-)

diff --git a/Hydro.Inp/NetWork.cs b/Hydro.Inp/NetWork.cs
index 544ea59..18fe610 100644
--- a/Hydro.Inp/NetWork.cs
+++ b/Hydro.Inp/NetWork.cs
@@ -1,7 +1,8 @@
 锘縰sing Hydro.Core.Model;
-using Hydro.HydraulicModel;
+//using Hydro.HydraulicModel;
 using System;
 using System.Collections.Generic;
+using System.Data;
 using System.Drawing;
 using System.IO;
 using System.IO.Ports;
@@ -16,6 +17,7 @@
 
 namespace Hydro.Inp
 {
+    [Serializable]
     public class NetWork //: INetWork
     {
         public NetWork()
@@ -174,20 +176,20 @@
             nozzles.ForEach(nozzle => { AddNozzle(nozzle); });
         }
 
-        private void CheckNodesExist(NodeModel node)
+        private void CheckNodesExist(NodeCalcModel node)
         {
             if (Nodes.Any(d => d.ID == node.ID))
                 throw new Exception("宸插瓨鍦ㄩ噸澶嶇殑瀵硅薄");
         }
 
-        private void CheckLinksExist(LinkModel link)
+        private void CheckLinksExist(LinkCalcModel link)
         {
             if (Links.Any(d => d.ID == link.ID))
                 throw new Exception("宸插瓨鍦ㄩ噸澶嶇殑瀵硅薄");
         }
 
-        public List<NodeModel> Nodes { get; set; } = new List<NodeModel>();
-        public List<LinkModel> Links { get; set; } = new List<LinkModel>();
+        public List<NodeCalcModel> Nodes { get; set; } = new List<NodeCalcModel>();
+        public List<LinkCalcModel> Links { get; set; } = new List<LinkCalcModel>();
 
         /// <summary>
         /// 鏍规嵁INP鏂囦欢鐢熸垚
@@ -405,86 +407,94 @@
                 }
                 sr.Close();
 
-                //璇诲彇鍧愭爣
-                Junctions.ForEach(o =>
-                {
-                    var p = points.Where(d => d.ID == o.ID).FirstOrDefault();
-                    if (p != null)
-                    {
-                        o.X = p.Position.X;
-                        o.Y = p.Position.Y;
-                    }
-                });
+                #region 鍧愭爣鍖归厤鏂规硶
+                #endregion
 
-                Reservos.ForEach(o =>
-                {
-                    var p = points.Where(d => d.ID == o.ID).FirstOrDefault();
-                    if (p != null)
-                    {
-                        o.X = p.Position.X;
-                        o.Y = p.Position.Y;
-                    }
-                });
+                #region 鍒犻櫎
+                //[Cloudflight淇敼]2023-11-27    浜岄噸寰幆鍖归厤鍧愭爣閫熷害澶參
+                ////璇诲彇鍧愭爣
+                //Junctions.ForEach(o =>
+                //{
+                //    var p = points.Where(d => d.ID == o.ID).FirstOrDefault();
+                //    if (p != null)
+                //    {
+                //        o.X = p.Position.X;
+                //        o.Y = p.Position.Y;
+                //    }
+                //});
 
-                Tanks.ForEach(o =>
-                {
-                    var p = points.Where(d => d.ID == o.ID).FirstOrDefault();
-                    if (p != null)
-                    {
-                        o.X = p.Position.X;
-                        o.Y = p.Position.Y;
-                    }
-                });
+                //Reservos.ForEach(o =>
+                //{
+                //    var p = points.Where(d => d.ID == o.ID).FirstOrDefault();
+                //    if (p != null)
+                //    {
+                //        o.X = p.Position.X;
+                //        o.Y = p.Position.Y;
+                //    }
+                //});
 
-                Meters.ForEach(o =>
-                {
-                    var p = points.Where(d => d.ID == o.ID).FirstOrDefault();
-                    if (p != null)
-                    {
-                        o.X = p.Position.X;
-                        o.Y = p.Position.Y;
-                    }
-                });
+                //Tanks.ForEach(o =>
+                //{
+                //    var p = points.Where(d => d.ID == o.ID).FirstOrDefault();
+                //    if (p != null)
+                //    {
+                //        o.X = p.Position.X;
+                //        o.Y = p.Position.Y;
+                //    }
+                //});
 
-                Nozzles.ForEach(o =>
-                {
-                    var p = points.Where(d => d.ID == o.ID).FirstOrDefault();
-                    if (p != null)
-                    {
-                        o.X = p.Position.X;
-                        o.Y = p.Position.Y;
-                    }
-                });
+                //Meters.ForEach(o =>
+                //{
+                //    var p = points.Where(d => d.ID == o.ID).FirstOrDefault();
+                //    if (p != null)
+                //    {
+                //        o.X = p.Position.X;
+                //        o.Y = p.Position.Y;
+                //    }
+                //});
 
+                //Nozzles.ForEach(o =>
+                //{
+                //    var p = points.Where(d => d.ID == o.ID).FirstOrDefault();
+                //    if (p != null)
+                //    {
+                //        o.X = p.Position.X;
+                //        o.Y = p.Position.Y;
+                //    }
+                //});
+                #endregion
+
+                #region 浼樺寲鏂规
                 int k1 = 0;
                 int k2 = 0;
-                #region delete
 
-                //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];
+                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];
 
-                //    }
-                //    if (k2 == points.Count)
-                //    {
-                //        throw new Exception($"鏈壘鍒癗ode[{J.ID}]鐨勫潗鏍�");
-                //    }
-                //    J.X = coor.Position.X;
-                //    J.Y = coor.Position.Y;
+                    while (J.ID != coor.ID && k2 < points.Count)
+                    {
+                        k2++;
+                        if (k2 < points.Count) coor = points[k2];
 
-                //    k1++;
-                //}
+                    }
+                    if (k2 == points.Count)
+                    {
+                        throw new Exception($"鏈壘鍒癗ode[{J.ID}]鐨勫潗鏍�");
+                    }
+                    J.X = coor.Position.X;
+                    J.Y = coor.Position.Y;
+
+                    k1++;
+                }
                 #endregion
+
 
 
                 //寤虹珛鐐圭嚎鍏崇郴閾捐〃StartNode锛屽厛灏嗙绾夸互Node1锛堣妭鐐�1鐨処D锛夋帓搴忥紝鍐嶅皢Nodes鎸塈D鎺掑簭锛屽缓绔嬩袱涓父鏍噆1銆乲2锛屾鍚戜竴娆″惊鐜紝寤虹珛閾捐〃鍏崇郴
@@ -534,7 +544,6 @@
                     //if (J.MaxDiameter < p.Diameter) J.MaxDiameter = p.Diameter;
                     //J.Links.Add(p);
                     k2++;
-
                 }
                 return true;
             }
@@ -546,7 +555,7 @@
         /// <param name="InpPath">Inp鏂囦欢璺緞</param>
         /// <param name="TemplateInpFullPath">Inp妯℃澘鏂囦欢缁濆璺緞锛屽鏋滀负绌哄氨鐢ㄧ郴缁熼粯璁ゆā鏉挎枃浠�</param>
         /// <exception cref="Exception"></exception>
-        public virtual void BuildToInp(string InpPath, string TemplateInpFullPath = "")
+        public virtual void BuildToInp(string InpPath, string TemplateInpFullPath = "", string sourcePath = null, bool isReplace = false)
         {
 
             string tempString = "";
@@ -557,10 +566,17 @@
             }
             if (!File.Exists(tempPath))
             {
-                throw new Exception("妯℃澘鏂囦欢涓嶅瓨鍦�");
+                throw new Exception("妯℃澘鏂囦欢涓嶅瓨鍦�," + tempPath);
             }
+            
             if (!File.Exists(InpPath))
+            {
+                FileInfo fi=new FileInfo(InpPath);
+                if (!Directory.Exists(fi.DirectoryName))
+                Directory.CreateDirectory(fi.DirectoryName);
                 File.Create(InpPath).Close();
+
+            }
             tempString = File.ReadAllText(tempPath);
             StringBuilder statusStringBuilder = new StringBuilder();
             statusStringBuilder.AppendLine(";ID              \tStatus/Setting\r\n");
@@ -596,7 +612,7 @@
             });
             Meters.ForEach(m =>
             {
-                junctionStringBuilder.AppendLine(m.ToString() + $"0\tJunction");
+                junctionStringBuilder.AppendLine(m.ToString() + $"0\tMeters");
                 coorStringBuilder.AppendLine(m.ToCoorString());
             });
             Nozzles.ForEach(no =>
@@ -625,7 +641,7 @@
 
             Tanks.ForEach(o =>
             {
-                tankStringBuilder.AppendLine(o.ToString());// + $"{o.Level}");
+                tankStringBuilder.AppendLine(o.ToString());// + $"0");
                 coorStringBuilder.AppendLine(o.ToCoorString());
             });
             string tankString = tankStringBuilder.ToString();
@@ -648,7 +664,7 @@
 
             Valves.ForEach(o =>
             {
-                valveStringBuilder.AppendLine(o.ToString());// + $"{o.Level}");
+                valveStringBuilder.AppendLine(o.ToString());// + $"0");
                 statusStringBuilder.Append(o.ToStatusString());
             });
             string valveString = valveStringBuilder.ToString();
@@ -659,7 +675,7 @@
 
             Pumps.ForEach(o =>
             {
-                pumpStringBuilder.AppendLine(o.ToString());// + $"{o.Level}");
+                pumpStringBuilder.AppendLine(o.ToString());// + $"0");
                 statusStringBuilder.Append(o.ToStatusString());
             });
             string pumpString = pumpStringBuilder.ToString();
@@ -729,19 +745,233 @@
             //Global.ClearFileReadOnly(InpPath);
             File.WriteAllText(InpPath, output);
         }
+        //public void BuildToInp(string filePath, string userCoorString = null, string sourcePath = null, bool isReplace = false)
+        //{
 
+        //    if (sourcePath == null) sourcePath = filePath;
+
+        //    string tempString = "";
+        //    if (!isReplace)
+        //    {
+        //        var tempPath = Path.Combine(Directory.GetCurrentDirectory(), @"template\inp\瀵煎嚭妯℃澘.inp");
+        //        if (!File.Exists(tempPath))
+        //        {
+        //            //MessageBox.Show($"妯℃澘鏂囦欢涓嶅瓨鍦╗{tempPath}]");
+        //            return;
+        //        }
+        //        tempString = File.ReadAllText(tempPath);
+        //    }
+        //    else
+        //    {
+        //        tempString = File.ReadAllText(sourcePath);
+        //    }
+
+
+        //    Dictionary<string, string> dictExchange = new Dictionary<string, string>() {
+        //        {"{junctions}","{0}" },
+        //        {"{reservoirs}","{1}" },
+        //        {"{tanks}","{2}" },
+        //        {"{pipes}","{3}" },
+        //        {"{valves}","{4}" },
+        //        {"{pumps}","{5}" },
+
+        //        {"{coor}","{6}" },
+        //        {"{curve}","{7}" },
+        //    };
+        //    dictExchange.ToList().ForEach(m => tempString = tempString.Replace(m.Key, m.Value));
+
+        //    StringBuilder junctionStringBuilder = new StringBuilder();
+
+        //    junctionStringBuilder.AppendLine(";ID              	Elev        	Demand      	Pattern         Type");
+
+        //    Nodes.ForEach(o =>
+        //    {
+        //        //if (!o.Visible) return;
+        //        if (o is JunctionModel j)
+        //            junctionStringBuilder.AppendLine(j.ToString() + $"0\tJunction");
+        //        else if (o is MeterModel m)
+        //            junctionStringBuilder.AppendLine(m.ToString() + $"0\tMeter");
+        //        else if (o is NozzleModel no)
+        //            junctionStringBuilder.AppendLine(no.ToString() + $"0\tNozzle\t{no.FlowCoefficient}");
+        //    });
+        //    string junctionString = junctionStringBuilder.ToString();
+
+        //    StringBuilder reservoirStringBuilder = new StringBuilder();
+
+        //    reservoirStringBuilder.AppendLine(";ID              	Head        	Pattern ");
+
+        //    Reservos.ForEach(o =>
+        //    {
+        //        //if (!o.Visible) return;
+        //        reservoirStringBuilder.AppendLine(o.ToString() + $"0\t{o.Elev}");
+        //    });
+        //    string reserverString = reservoirStringBuilder.ToString();
+
+        //    StringBuilder tankStringBuilder = new StringBuilder();
+
+        //    tankStringBuilder.AppendLine(";ID              	Elevation   	InitLevel   	MinLevel    	MaxLevel    	Diameter    	MinVol      	VolCurve        	Overflow");
+
+        //    Tanks.ForEach(o =>
+        //    {
+        //        //if (!o.Visible) return;
+        //        tankStringBuilder.AppendLine(o.ToString() + $"0");
+        //    });
+        //    string tankString = tankStringBuilder.ToString();
+
+        //    StringBuilder pipeStringBuilder = new StringBuilder();
+
+        //    pipeStringBuilder.AppendLine(";ID              	Node1           	Node2           	Length      	Diameter    	Roughness   	MinorLoss   	Status");
+
+        //    Links.ForEach(o =>
+        //    {
+        //        //if (!o.Visible) return;
+        //        if (o is PipeModel p)
+        //            pipeStringBuilder.AppendLine(p.ToString() + $"0");
+        //        //else if (o is RepeaterViewModel r)
+        //        //    pipeStringBuilder.AppendLine(r.ToString());
+        //    });
+        //    string pipeString = pipeStringBuilder.ToString();
+
+        //    StringBuilder valveStringBuilder = new StringBuilder();
+
+        //    valveStringBuilder.AppendLine(";ID              	Node1           	Node2           	Diameter    	Type	Setting     	MinorLoss  ");
+
+        //    Valves.ForEach(o =>
+        //    {
+        //        //if (!o.Visible) return;
+        //        valveStringBuilder.AppendLine(o.ToString() + $"0");
+        //    });
+        //    string valveString = valveStringBuilder.ToString();
+
+        //    StringBuilder pumpStringBuilder = new StringBuilder();
+
+        //    pumpStringBuilder.AppendLine(";ID              	Node1           	Node2           	Diameter    	Type	Setting     	MinorLoss  ");
+
+        //    Pumps.ForEach(o =>
+        //    {
+        //        //if (!o.Visible) return;
+        //        pumpStringBuilder.AppendLine(o.ToString() + $"0");
+        //    });
+        //    string pumpString = pumpStringBuilder.ToString();
+
+
+        //    StringBuilder curveStringBuilder = new StringBuilder();
+
+
+
+        //    //pumps.ForEach(o =>
+        //    //{
+        //    //    if (!o.Visible || !o.Datasets.ContainsKey("娴侀噺鎵▼鏇茬嚎")) return;
+        //    //    curveStringBuilder.AppendLine(o.Datasets["娴侀噺鎵▼鏇茬嚎"].ToString());
+        //    //});
+        //    if (dict_dataset != null)
+        //        foreach (var kp in dict_dataset)
+        //        {
+        //            curveStringBuilder.AppendLine(kp.Value.ToString());
+        //        }
+        //    string curveString = curveStringBuilder.ToString();
+
+
+        //    StringBuilder coorStringBuilder = new StringBuilder();
+        //    if (userCoorString == null)
+        //    {
+
+        //        coorStringBuilder.AppendLine(";Node            	X-Coord           	Y-Coord");
+        //        Nodes.ForEach(o => coorStringBuilder.AppendLine(o.ToCoorString()));
+        //    }
+        //    else
+        //    {
+        //        coorStringBuilder.Append(userCoorString);
+        //    }
+
+
+
+        //    string coorString = coorStringBuilder.ToString();
+        //    string output = "";
+
+
+
+
+
+        //    StringBuilder emitterStringBuilder = new StringBuilder();
+        //    emitterStringBuilder.AppendLine(";Junction        \tCoefficient");
+        //    Nodes.ForEach(o => emitterStringBuilder.Append(o.ToEmitterString()));
+
+
+
+        //    string emitterString = emitterStringBuilder.ToString();
+
+
+
+
+
+        //    StringBuilder statusStringBuilder = new StringBuilder();
+        //    statusStringBuilder.AppendLine(";ID              \tStatus/Setting\r\n");
+        //    Links.ForEach(o => statusStringBuilder.Append(o.ToStatusString()));
+
+
+
+        //    string statusString = statusStringBuilder.ToString();
+
+
+
+        //    output = tempString;
+
+        //    output = ReplaceContent(output, "JUNCTIONS", junctionString);
+        //    output = ReplaceContent(output, "RESERVOIRS", reserverString);
+        //    output = ReplaceContent(output, "TANKS", tankString);
+        //    output = ReplaceContent(output, "PIPES", pipeString);
+        //    output = ReplaceContent(output, "VALVES", valveString);
+        //    output = ReplaceContent(output, "PUMPS", pumpString);
+        //    output = ReplaceContent(output, "CURVES", curveString);
+
+
+        //    output = ReplaceContent(output, "COORDINATES", coorString);
+        //    output = ReplaceContent(output, "EMITTERS", emitterString);
+        //    output = ReplaceContent(output, "STATUS", statusString);
+        //    string backupFolderPath = Path.Combine(Path.GetDirectoryName(filePath), "bk");
+        //    if (!Directory.Exists(backupFolderPath))
+        //    {
+        //        Directory.CreateDirectory(backupFolderPath);
+        //    }
+
+        //    string backupFileName = $"{Path.GetFileNameWithoutExtension(filePath)}_{DateTime.Now:yyyyMMddHHmmss}{Path.GetExtension(filePath)}";
+        //    string backupFilePath = Path.Combine(backupFolderPath, backupFileName);
+        //    if (File.Exists(filePath)) File.Copy(filePath, backupFilePath, true);
+
+
+        //    // 妫�鏌ユ枃浠舵槸鍚﹀瓨鍦�
+        //    try
+        //    {
+        //        Global.ClearFileReadOnly(filePath);
+
+        //        File.WriteAllText(filePath, output);
+        //    }
+        //    catch (Exception ex)
+        //    {
+        //        MessageBox.Show(ex.Message);
+        //    }
+
+        //    //MessageBox.Show($"淇濆瓨鎴愬姛!");
+        //}
         /// <summary>
         /// 鏍规嵁Inp鏂囦欢璁$畻
         /// </summary>
         /// <returns></returns>
         public virtual List<TimePoint> Calc(string InpPath)
         {
-            Epanet epanet = new Epanet();
+            HydraulicModel.HydraulicCore epanet = new HydraulicModel.HydraulicCore();
             var result = new List<TimePoint>();
-            var err = epanet.open(InpPath, "d:\\5.log", "");
+            var err = epanet.open(InpPath, "", "");
+
             if (err != 0)
             {
-                throw new Exception($"璁$畻澶辫触锛歿err}");
+                throw new Exception($"鎵撳紑璁$畻鏂囦欢澶辫触锛歿err}");
+            }
+            var errCal = epanet.solveH();
+            if (errCal != 0)
+            {
+                throw new Exception($"璁$畻澶辫触锛歿errCal}");
             }
             int nodeCount = 0, linkCount = 0;
             epanet.getcount((int)CountType.Node, ref nodeCount);
@@ -792,7 +1022,8 @@
         /// <returns></returns>
         public virtual List<TimePoint> Calc()
         {
-            var inpPath = Path.Combine(Directory.GetCurrentDirectory(), @"data\inp\calc.inp");
+
+            var inpPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"data\inp\calc.inp");
             BuildToInp(inpPath);
             return Calc(inpPath);
         }
@@ -810,17 +1041,24 @@
 
         private string ReplaceContent(string text, string content, string replaceString)
         {
+
+
             string str = replaceString;
+
             string replacedText = ReplaceCoordinatesSection(text, content, str);
+
             return replacedText;
+            //Console.WriteLine(replacedText);
         }
 
-        private string ReplaceCoordinatesSection(string text, string content, string str)
+        public static string ReplaceCoordinatesSection(string text, string content, string str)
         {
             string pattern = $@"(\[{content}\]).*?(\[|$)";
+
             string replacedText = Regex.Replace(text, pattern, match =>
             {
                 string section = match.Groups[2].Value.Trim();
+
                 if (!string.IsNullOrEmpty(section))
                 {
                     return $"{match.Groups[1].Value}\n{str}\n[";
@@ -830,7 +1068,13 @@
                     return $"{match.Groups[1].Value}\n{str}\n[";
                 }
             }, RegexOptions.Singleline);
+
             return replacedText;
         }
+
     }
+
+ 
+
+
 }

--
Gitblit v1.9.3