lixiaojun
2024-12-10 aabff7f27021a23a433d50a74daba4f3888f82d5
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Yw.WinFrmUI.Q3d;
using Yw.EPAnet;
using System.IO;
using Newtonsoft.Json;
 
namespace Hydro.ClientTool
{
    public partial class Form3 : Form
    {
        public NetworkViewModel _viewnet { get; set; }
 
        public Network _network { get; set; }
        public Dictionary<string, Node> dictNodes { get; set; }
        public Dictionary<string, Link> dictLinks { get; set; }
        public Form3()
        {
            InitializeComponent();
        }
 
        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //打开文件对话框,选择inp类型的文件
            var ofd = new OpenFileDialog();
            ofd.Filter = "json文件|*.json|inp文件|*.inp";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
 
                //string inptxt = File.ReadAllText(ofd.FileName);
                //如果是inp文件,调用InpInteropHelper.FromInpString方法,将inp文件转换为Network对象
                //如果是json文件,直接调用JsonConvert.DeserializeObject方法,将json文件转换为Network对象
                if (ofd.FileName.EndsWith(".inp"))
                    //_network = InpInteropHelper.FromInpString(ofd.FileName);
                    _network = InpInteropHelper.FromInpString(ofd.FileName);
                else
                {
                    string inptxt = File.ReadAllText(ofd.FileName);
                    _network = JsonConvert.DeserializeObject<Network>(inptxt);
                    //_network.GetAllNodes().ForEach(n =>
                    //{
                    //    n.Links = new List<Link>();
                    //});
                    dictNodes = _network.GetAllNodes().ToDictionary(n => n.Id);
                    _network.GetAllLinks().ForEach(l =>
                    {
                        l.StartNode = dictNodes[l.StartNode.Id];
                        l.EndNode = dictNodes[l.EndNode.Id];
 
                    });
                    dictLinks = _network.GetAllLinks().ToDictionary(l => l.Id);
 
                    _network.GetAllLinks().ForEach(l =>
                    {
                        if (l.StartNode.Links == null) l.StartNode.Links = new List<Link>();
                        if (l.EndNode.Links == null) l.EndNode.Links = new List<Link>();
                        l.StartNode.Links.Add(l);
                        l.EndNode.Links.Add(l);
                    });
 
 
 
                }
 
 
                _viewnet = ConvertNetworkToNetworkViewModel(_network);
                map.SetData(_viewnet);
                map.SelectedObjectsChanged += (oo, ee) =>
                {
                    if (map.selectedObjs.Count > 1)
                    {
                        propertyGrid1.SelectedObjects = map.selectedObjs.ToArray();
                    }
                    else if (map.selectedObjs.Count == 1)
                    {
                        propertyGrid1.SelectedObject = map.selectedObjs[0];
                    }
                    else
                    {
                        propertyGrid1.SelectedObject = null;
                    }
                };
            }
 
 
 
        }
        public NetworkViewModel ConvertNetworkToNetworkViewModel(Network network)
        {
            NetworkViewModel net = new NetworkViewModel();
            network.GetAllLinks().ForEach(l =>
            {
                if (l is Pipe)
                {
                    Pipe pipe = l as Pipe;
                    net.Links.Add(new PipeViewModel()
                    {
                        ID = pipe.Id,
                        Node1 = pipe.StartNode.Id,
                        Node2 = pipe.EndNode.Id,
                        Length = (float)pipe.Length,
                        Diameter = (float)pipe.Diameter,
                        Roughness = (float)pipe.Roughness,
                        Status = pipe.LinkStatus == null ? ObjectEnum.StatusType.DEFAULT : (ObjectEnum.StatusType)Enum.Parse(typeof(ObjectEnum.StatusType), pipe.LinkStatus.ToUpper()),
                    });
                }
                else if (l is Pump)
                {
                    Pump pump = l as Pump;
                    net.Links.Add(new PumpViewModel()
                    {
                        ID = pump.Id,
                        Node1 = pump.StartNode.Id,
                        Node2 = pump.EndNode.Id,
                        HeadCurve = pump.CurveQH,
                        Status = pump.LinkStatus == null ? ObjectEnum.StatusType.DEFAULT : (ObjectEnum.StatusType)Enum.Parse(typeof(ObjectEnum.StatusType), pump.LinkStatus.ToUpper()),
                    });
                }
                else if (l is Valve)
                {
                    Valve valve = l as Valve;
                    net.Links.Add(new ValveViewModel()
                    {
                        ID = valve.Id,
                        Node1 = valve.StartNode.Id,
                        Node2 = valve.EndNode.Id,
                        Diameter = (float)valve.Diameter,
                        Setting = valve.ValveSetting,
                        Status = valve.LinkStatus == null ? ObjectEnum.StatusType.DEFAULT : (ObjectEnum.StatusType)Enum.Parse(typeof(ObjectEnum.StatusType), valve.LinkStatus.ToUpper()),
                    });
                }
                else if (l is Exchanger)
                {
                    Exchanger valve = l as Exchanger;
                    net.Links.Add(new HeatExchanger()
                    {
                        ID = valve.Id,
                        Node1 = valve.StartNode.Id,
                        Node2 = valve.EndNode.Id,
                        Diameter = (float)valve.Diameter,
                        Setting = valve.CurveQL,
                        Status = (ObjectEnum.StatusType)Enum.Parse(typeof(ObjectEnum.StatusType), valve.LinkStatus.ToUpper()),
                    });
                }
                else if (l is Compressor)
                {
                    Compressor valve = l as Compressor;
                    net.Links.Add(new AirCompressor()
                    {
                        ID = valve.Id,
                        Node1 = valve.StartNode.Id,
                        Node2 = valve.EndNode.Id,
                        Diameter = (float)valve.Diameter,
                        Setting = valve.CurveQL,
                        Status = (ObjectEnum.StatusType)Enum.Parse(typeof(ObjectEnum.StatusType), valve.LinkStatus.ToUpper()),
                    });
                }
 
            });
 
            network.GetAllNodes().ForEach(n =>
            {
                if (n is Junction)
                {
                    var junction = (Junction)n;
                    net.Nodes.Add(new JunctionViewModel()
                    {
                        ID = junction.Id,
                        X = (float)junction.Position.X,
                        Y = (float)junction.Position.Y,
                        Z = (float)junction.Elev,
                        Demand = (float)junction.Demand,
                        PatternID = junction.DemandPattern,
                    });
                }
                else if (n is Tank)
                {
                    var tank = (Tank)n;
                    net.Nodes.Add(new TankViewModel()
                    {
                        ID = tank.Id,
                        X = (float)tank.Position.X,
                        Y = (float)tank.Position.Y,
                        Z = (float)tank.PoolElev,
                        InitLevel = (float)tank.InitLevel,
                        MinLevel = (float)tank.MinLevel,
                        MaxLevel = (float)tank.MaxLevel,
                        Diameter = (float)tank.Diameter,
                    });
                }
                else if (n is Reservoir)
                {
                    var reservoir = (Reservoir)n;
                    net.Nodes.Add(new ReservoirViewModel()
                    {
                        ID = reservoir.Id,
                        X = (float)reservoir.Position.X,
                        Y = (float)reservoir.Position.Y,
                        Z = (float)(reservoir.PoolElev ?? 0),
                        Head = (float)reservoir.Head,
                    });
                }
                else if (n is Emitter)
                {
                    var emitter = (Emitter)n;
                    net.Nodes.Add(new NozzleViewModel()
                    {
                        ID = emitter.Id,
                        X = (float)emitter.Position.X,
                        Y = (float)emitter.Position.Y,
                        Z = (float)emitter.Elev,
                        FlowCoefficient = (float)emitter.Coefficient,
                    });
                }
                else if (n is Meter)
                {
                    var meter = (Meter)n;
                    net.Nodes.Add(new MeterViewModel()
                    {
                        ID = meter.Id,
                        X = (float)meter.Position.X,
                        Y = (float)meter.Position.Y,
                        Z = (float)meter.Elev,
                        Demand = (float)meter.Demand,
                        PatternID = meter.DemandPattern,
                    });
                }
            });
            net.BuildRelation();
            return net;
        }
 
        private void Form3_Load(object sender, EventArgs e)
        {
 
        }
 
 
 
        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
 
 
        private void 简单计算ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CalcuResult result = _network.Calcu(CalcuMode.Simple);
            //将计算结果建立node字典和link字典
            Dictionary<string, CalcuNode> dictNodes = result.NodeList.ToDictionary(n => n.Id);
            Dictionary<string, CalcuLink> dictLinks = result.LinkList.ToDictionary(l => l.Id);
            //将计算结果显示在_net中
            _viewnet.Links.ForEach(l =>
            {
                if (dictLinks.ContainsKey(l.ID))
                {
                    l.EN_FLOW = (float)dictLinks[l.ID].Flow;
                    l.EN_VELOCITY = (float)dictLinks[l.ID].Velocity;
                    l.EN_HEADLOSS = (float)dictLinks[l.ID].Headloss;
 
                }
            });
            _viewnet.Nodes.ForEach(n =>
            {
                if (dictNodes.ContainsKey(n.ID))
                {
                    n.EN_HEAD = (float)dictNodes[n.ID].Head;
                    n.EN_PRESSURE = (float)dictNodes[n.ID].Press;
                    n.EN_DEMAND = (float)dictNodes[n.ID].Demand;
 
                }
            });
 
 
 
 
 
        }
 
        private void 局部损失计算ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var result = _network.Calcu(CalcuMode.MinorLoss);
        }
 
        private void 下游路径分析ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_network == null)
            {
                MessageBox.Show("请先打开文件");
                return;
            }
            if (map.selectedObjs.Count > 0 && map.selectedObjs[0] is NodeViewModel node)
            {
                var calcResult = _network.Calcu(CalcuMode.MinorLoss);
                var Snode = _network.GetAllNodes().FirstOrDefault(n => n.Id == node.ID);
                var links = _network.AnalyzeDownstreamPath(Snode, calcResult);
                if (links == null || links.Count <= 0) return;
                map.SetSelectObj(links.Select(l => l.Id).ToList());
 
            }
 
 
        }
        private void 沿程纵断面分析ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_network == null)
            {
                MessageBox.Show("请先打开文件");
                return;
            }
            if (map.selectedObjs.Count > 0 && map.selectedObjs[0] is NodeViewModel node)
            {
                var calcResult = _network.Calcu(CalcuMode.MinorLoss);
                var Snode = _network.GetAllNodes().FirstOrDefault(n => n.Id == node.ID);
                var links = _network.AnalyzeDownstreamPath(Snode, calcResult);
                if (links == null || links.Count <= 0) return;
 
                map.SetSelectObj(links.Select(l => l.Id).ToList());
                var result = _network.GetChartNodeByPathLinks(links, calcResult);
 
            }
 
        }
 
        private void iNPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //打开文件对话框,将 _network.ToInpString();导出
            var sfd = new SaveFileDialog();
            sfd.Filter = "inp文件|*.inp";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllText(sfd.FileName, _network.ToInpString());
            }
        }
    }
}