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.MapView/MapViewNetWork2Inp.cs |  260 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 255 insertions(+), 5 deletions(-)

diff --git a/Hydro.MapView/MapViewNetWork2Inp.cs b/Hydro.MapView/MapViewNetWork2Inp.cs
index 50b1322..7491423 100644
--- a/Hydro.MapView/MapViewNetWork2Inp.cs
+++ b/Hydro.MapView/MapViewNetWork2Inp.cs
@@ -1,4 +1,5 @@
 锘縰sing Hydro.Core.Model;
+using Hydro.MapView.Common;
 using System;
 using System.Collections.Generic;
 using System.Drawing;
@@ -7,6 +8,7 @@
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading.Tasks;
+using System.Windows.Forms;
 using static Hydro.Core.ObjectEnum;
 using static Hydro.MapView.RepeaterViewModel;
 
@@ -14,6 +16,7 @@
 {
     partial class MapViewNetWork
     {
+
         public bool BuildFromInp(string filePath, bool use_old=false)
         {
             this.use_old = use_old;
@@ -27,8 +30,8 @@
             StreamReader sr = new StreamReader(filePath);
             //try
             {
-                Nodes = new List<NodeViewModel>();
-                Links = new List<LinkViewModel>();
+                Nodes = new NodeViewModelList();
+                Links = new LinkViewModelList();
 
 
 
@@ -590,12 +593,12 @@
             StreamReader sr = new StreamReader(filePath);
             //try
             {
-                Nodes = new List<NodeViewModel>();
+                Nodes = new NodeViewModelList();
                 //reservoirs = new List<Reservoir>();
                 //tanks = new List<Tank>();
                 //meters = new List<Meter>();
 
-                Links = new List<LinkViewModel>();
+                Links =new LinkViewModelList();
                 //valves = new List<Valve>();
                 //repeaters = new List<Repeater>();
 
@@ -834,7 +837,7 @@
                     while (J.ID != p.Node2 && k1 < Nodes.Count)
                     {
                         k1++;
-                        if (k1 < Nodes.Count) J = Nodes[k1];
+                        if (k1 < Nodes.Count) J =Nodes[k1];
                     }
                     if (k1 == Nodes.Count)
                     {
@@ -854,5 +857,252 @@
 
         }
 
+
+
+        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(o0 =>
+            {
+                var o = o0;
+                if (!o.Visible) return;
+                if (o is JunctionViewModel j)
+                    junctionStringBuilder.AppendLine(j.ToString() + $"{j.Level}\tJunction");
+                else if (o is MeterViewModel m)
+                    junctionStringBuilder.AppendLine(m.ToString() + $"{o.Level}\tMeter");
+                else if (o is NozzleViewModel no)
+                    junctionStringBuilder.AppendLine(no.ToString() + $"{o.Level}\tNozzle\t{no.FlowCoefficient}");
+            });
+            string junctionString = junctionStringBuilder.ToString();
+
+            StringBuilder reservoirStringBuilder = new StringBuilder();
+
+            reservoirStringBuilder.AppendLine(";ID              	Head        	Pattern ");
+
+            reservoirs.ForEach(o =>
+            {
+                if (!o.Visible) return;
+                reservoirStringBuilder.AppendLine(o.ToString() + $"{o.Level}\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() + $"{o.Level}");
+            });
+            string tankString = tankStringBuilder.ToString();
+
+            StringBuilder pipeStringBuilder = new StringBuilder();
+
+            pipeStringBuilder.AppendLine(";ID              	Node1           	Node2           	Length      	Diameter    	Roughness   	MinorLoss   	Status");
+
+            Links.ForEach(o0 =>
+            {
+                var o = (LinkViewModel)o0;
+                if (!o.Visible) return;
+                if (o is PipeViewModel p)
+                    pipeStringBuilder.AppendLine(p.ToString() + $"{p.Level}");
+                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() + $"{o.Level}");
+            });
+            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() + $"{o.Level}");
+            });
+            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($"淇濆瓨鎴愬姛!");
+        }
+
+        private string replaceContent(string text, string content, string replaceString)
+        {
+
+
+            string str = replaceString;
+
+            string replacedText = ReplaceCoordinatesSection(text, content, str);
+
+            return replacedText;
+            //Console.WriteLine(replacedText);
+        }
+
+        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[";
+                }
+                else
+                {
+                    return $"{match.Groups[1].Value}\n{str}\n[";
+                }
+            }, RegexOptions.Singleline);
+
+            return replacedText;
+        }
+
     }
 }

--
Gitblit v1.9.3