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;
|
}
|
}
|
}
|