using IStation.Epanet.Network.IO.Input;
|
using IStation.Epanet.Network.IO.Output;
|
using EpanetNetwork = IStation.Epanet.Network.Network;
|
|
namespace IStation.Epanet
|
{
|
public class NetworkHelper
|
{
|
public static string Open(string netFile, out EpanetNetwork eapNet)
|
{
|
eapNet = null;
|
string fileExtension = (Path.GetExtension(netFile) ?? string.Empty).ToLowerInvariant();
|
|
if (fileExtension != ".net" &&
|
fileExtension != ".inp" &&
|
fileExtension != ".xml" &&
|
fileExtension != ".gz")
|
return $"无法识别{fileExtension}";
|
|
InputParser inpParser;
|
switch (fileExtension.ToLowerInvariant())
|
{
|
case ".inp":
|
inpParser = new InpParser();
|
break;
|
|
case ".net":
|
inpParser = new NetParser();
|
break;
|
|
case ".xml":
|
inpParser = new XmlParser(false);
|
break;
|
|
case ".gz":
|
inpParser = new XmlParser(true);
|
break;
|
default:
|
return $"Not supported file type:{fileExtension}";
|
}
|
|
EpanetNetwork net;
|
|
try
|
{
|
var network = new EpanetNetwork();
|
net = inpParser.Parse(network, netFile);
|
}
|
catch (EnException ex)
|
{
|
return $"{ex.Message}\nCheck epanet.log for detailed error description";
|
}
|
catch (Exception ex)
|
{
|
return $"{ex.Message}\nUnable to parse network configuration file";
|
}
|
eapNet = net;
|
return string.Empty;
|
}
|
|
public static void Save(EpanetNetwork eapNet, string filePath)
|
{
|
if (eapNet == null)
|
return;
|
|
OutputComposer composer;
|
string fileName = Path.GetFullPath(filePath);
|
string extension = Path.GetExtension(filePath);
|
|
switch (extension)
|
{
|
case ".inp":
|
composer = new InpComposer(eapNet);
|
break;
|
|
default:
|
extension = ".inp";
|
composer = new InpComposer(eapNet);
|
break;
|
}
|
|
fileName = Path.ChangeExtension(fileName, extension);
|
|
try
|
{
|
composer.Compose(fileName);
|
}
|
catch (EnException ex)
|
{
|
throw new Exception(ex.Message + "\nCheck epanet.log for detailed error description");
|
}
|
catch (Exception ex)
|
{
|
throw new Exception(ex.Message + "\nCheck epanet.log for detailed error description");
|
}
|
|
}
|
|
}
|
}
|