Shuxia Ning
2024-07-17 fd681339c81201ed6fb3303647ecab89e3e6c0c1
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
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");
            }
 
        }
 
    }
}