qinjie
2023-12-02 c2fa168aff53d879f72412c94baa7be4998dae5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static CloudWaterNetwork.Repeater;
using System.Windows.Forms;
using System.Diagnostics.Eventing.Reader;
using System.Text.RegularExpressions;
 
namespace CloudWaterNetwork
{
    partial class Network
    {
      
        
        
        public void saveInpFile(string filePath,string userCoorString=null,bool 原版输出=false)
        {
            bool isReplace = false;
            if (File.Exists(filePath)) { isReplace = true; }
            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(filePath);
            }
            
            
            Dictionary<string, string> dictExchange = new Dictionary<string, string>() {
                {"{junctions}","{0}" },
                {"{reservoirs}","{1}" },
                {"{tanks}","{2}" },
                {"{pipes}","{3}" },
                {"{valves}","{4}" },
                {"{repeaters}","{5}" },
                {"{coor}","{6}" },
 
 
 
            };
            dictExchange.ToList().ForEach(m => tempString = tempString.Replace(m.Key, m.Value));
 
            StringBuilder junctionStringBuilder = new StringBuilder();
            //junctionStringBuilder.AppendLine("[JUNCTIONS]");
            junctionStringBuilder.AppendLine(";ID                  Elev            Demand          Pattern         Type");
 
            Nodes.ForEach(o =>
            {
                if (!o.Visible) return;
                if (o is Junction j)
                    junctionStringBuilder.AppendLine(j.ToString() + $"{j.Level}");
                if (o is Meter m)
                    junctionStringBuilder.AppendLine(m.ToString() + $"{o.Level}");
            });
            string junctionString = junctionStringBuilder.ToString();
 
            StringBuilder reservoirStringBuilder = new StringBuilder();
            //reservoirStringBuilder.AppendLine("[RESERVOIRS]");
            reservoirStringBuilder.AppendLine(";ID                  Head            Pattern ");
 
            reservoirs.ForEach(o =>
            {
                if (!o.Visible) return;
                reservoirStringBuilder.AppendLine(o.ToString() + $"{o.Level}");
            });
            string reserverString = reservoirStringBuilder.ToString();
 
            StringBuilder tankStringBuilder = new StringBuilder();
            //tankStringBuilder.AppendLine("[TANKS]");
            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("[PIPES]");
            pipeStringBuilder.AppendLine(";ID                  Node1               Node2               Length          Diameter        Roughness       MinorLoss       Status");
 
            Links.ForEach(o =>
            {
                if (!o.Visible) return;
                if (o is Pipe p)
                    pipeStringBuilder.AppendLine(p.ToString() + $"{p.Level}");
            });
            string pipeString = pipeStringBuilder.ToString();
 
            StringBuilder valveStringBuilder = new StringBuilder();
            //valveStringBuilder.AppendLine("[VALVES]");
            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();
 
            bool hasRepeater = false;
            StringBuilder repeaterStringBuilder = new StringBuilder();
            //repeaterStringBuilder.AppendLine("[REPEATERS]");
            repeaterStringBuilder.AppendLine(";ID                  TEMP_ID                  REPEAT_TIMES");
 
            repeaters.ForEach(o =>
            {
                if (!o.Visible) return;
                repeaterStringBuilder.AppendLine(o.ToString() + $"{o.Level}");
                hasRepeater = true;
 
            });
            if (!hasRepeater) repeaterStringBuilder = new StringBuilder();
            string repeaterString = repeaterStringBuilder.ToString();
 
            StringBuilder coorStringBuilder = new StringBuilder();
            if (userCoorString==null)
            {
                
                coorStringBuilder.AppendLine("[COORDINATES]");
                coorStringBuilder.AppendLine(";Node                X-Coord               Y-Coord");
                Nodes.ForEach(o => coorStringBuilder.AppendLine(o.ToCoorString()));
            }
            else
            {
                coorStringBuilder.Append(userCoorString);
            }
            
            // 继续添加坐标信息到StringBuilder中
 
            string coorString = coorStringBuilder.ToString();
            string output = "";
            if (!isReplace) 
            { 
                output = string.Format(tempString, junctionString, reserverString, tankString, pipeString, valveString, repeaterString, coorString);
            }
            else
            {
                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);
                if (!string.IsNullOrEmpty( repeaterString))
                    output = replaceContent(output, "REPEATERS", repeaterString);
                output = replaceContent(output, "COORDINATES", coorString);
            }
 
            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);
 
            File.Copy(filePath, backupFilePath, true);
            File.WriteAllText(filePath, output);
            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;
        }
    }
}