qin
2024-06-04 9490957edfc694a0378ee52af47e921b8ac44e7a
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
using dict_py_Inner;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.NetworkInformation;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using static CloudWaterNetwork.Repeater;
using static System.Windows.Forms.LinkLabel;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;
 
namespace CloudWaterNetwork
{
 
    [Serializable]
    public partial class Network
    {
        #region PropertyForm
 
 
        #endregion
 
        public string Name;
 
        public Node StartPoint;
 
        public Node EndPoint;
      
        /// <summary>
        /// 核心承载字段
        /// </summary>
        public List<Node> Nodes=new List<Node>();
 
        /// <summary>
        /// 核心承载字段
        /// </summary>
        public List<Link> Links = new List<Link>();
 
 
        public List<MapObject> MapObjects
        {
            get
            {
                List<MapObject> objects = new List<MapObject>();
                objects.AddRange(Nodes);
                objects.AddRange(Links);
                return objects;
            }
        }
        public List<Junction> junctions
        {
            get
            {
                return Nodes.FindAll(n=>n is Junction).Select(n=>n as Junction).ToList();
            }
        }
        public List<Reservoir> reservoirs
        {
            get
            {
                return Nodes.FindAll(n => n is Reservoir).Select(n => n as Reservoir).ToList();
            }
        }
        public List<Tank> tanks
        {
            get
            {
                return Nodes.FindAll(n => n is Tank).Select(n => n as Tank).ToList();
            }
        }
        public List<Meter> meters
        {
            get
            {
                return Nodes.FindAll(n => n is Meter).Select(n => n as Meter).ToList();
            }
        }
 
 
        public List<Pipe> pipes
        {
            get
            {
                return Links.FindAll(n => n is Pipe).Select(n => n as Pipe).ToList();
            }
        }
 
        public List<Valve> valves
        {
            get
            {
                return Links.FindAll(n => n is Valve).Select(n => n as Valve).ToList();
            }
        }
        public List<Repeater> repeaters
        {
            get
            {
                return Links.FindAll(n => n is Repeater).Select(n => n as Repeater).ToList();
            }
        }
 
        public List<Pump> pumps
        {
            get
            {
                return Links.FindAll(n => n is Pump).Select(n => n as Pump).ToList();
            }
        }
 
 
        public List<Area> areas = new List<Area>();
 
        
        public Junction AddJunction(PointF position,float z=0)
        {
            Junction j = new Junction();
            int i = 0;
            string ID = $"{Default.GetPreString(j)}{i}";
            while (Nodes.Find(p0 => p0.ID == ID) != null)
            {
                i++;
                ID = $"{Default.GetPreString(j)}{i}";
            }
            j.ID = ID;
            j.Elev = 0;
            j.Demand = 0;
            j.Pattern = "";
            j.Elev = z;
            j.X = position.X;
            j.Y = position.Y;
            Nodes.Add(j);
            return j;
        }
        public Meter AddMeter(PointF position)
        {
            Meter j = new Meter();
            int i = 0;
            string ID = $"{Default.GetPreString(j)}{i}";
            while (Nodes.Find(p0 => p0.ID == ID) != null)
            {
                i++;
                ID = $"{Default.GetPreString(j)}{i}";
            }
            j.ID = ID;
            j.Elev = 0;
            j.Demand = 0;
            j.Pattern = "";
 
            j.X = position.X;
            j.Y = position.Y;
            Nodes.Add(j);
            meters.Add(j);
            return j;
        }
        public Reservoir AddReservoir(PointF position)
        {
            Reservoir j = new Reservoir();
            int i = 0;
            string ID = $"{Default.GetPreString(j)}{i}";
            while (Nodes.Find(p0 => p0.ID == ID) != null)
            {
                i++;
                ID = $"{Default.GetPreString(j)}{i}";
            }
            j.ID = ID;
            j.Elev = 0;
            j.Demand = 0;
            j.Pattern = "";
            j.Head = 100;
            j.X = position.X;
            j.Y = position.Y;
            Nodes.Add(j);
            reservoirs.Add(j);
            return j;
        }
        public Tank AddTank(PointF position)
        {
            Tank j = new Tank();
            int i = 0;
            string ID = $"{Default.GetPreString(j)}{i}";
            while (Nodes.Find(p0 => p0.ID == ID) != null)
            {
                i++;
                ID = $"{Default.GetPreString(j)}{i}";
            }
            j.ID = ID;
            j.Elev = 0;
            j.Demand = 0;
            j.Pattern = "";
 
            j.X = position.X;
            j.Y = position.Y;
            Nodes.Add(j);
            tanks.Add(j);
            return j;
        }
        public Pipe AddPipe(Node node1, Node node2)
        {
            Pipe pipe = new Pipe();
            int i = 0;
            string ID = $"{Default.GetPreString(pipe)}{i}";
            while (Links.Find(p0 => p0.ID == ID) != null)
            {
                i++;
                ID = $"{Default.GetPreString(pipe)}{i}";
            }
            pipe.ID = ID;
            pipe.Diameter = 100;
            pipe.MinorLoss = 0;
            pipe.Roughness = 100;
            pipe.Status = "1";
            pipe.StartNode = node1;
            pipe.EndNode = node2;
 
 
 
            Links.Add(pipe);
            return pipe;
        }
        public Valve AddValve(Node node1, Node node2)
        {
            Valve valve = new Valve();
            int i = 0;
            string ID = $"{Default.GetPreString(valve)}{i}";
            while (Links.Find(p0 => p0.ID == ID) != null)
            {
                i++;
                ID = $"{Default.GetPreString(valve)}{i}";
            }
            valve.ID = ID;
 
            valve.Diameter = 100;
            valve.Status = "1";
            valve.StartNode = node1;
            valve.EndNode = node2;
 
            Links.Add(valve);
            valves.Add(valve);
            return valve;
        }
        public Repeater AddRepeater(Node node1, Node node2)
        {
            Repeater repeater = new Repeater();
            int i = 0;
            string ID = $"{Default.GetPreString(repeater)}{i}";
            while (Links.Find(p0 => p0.ID == ID) != null)
            {
                i++;
                ID = $"{Default.GetPreString(repeater)}{i}";
            }
            repeater.ID = ID;
 
 
            repeater.StartNode = node1;
            repeater.EndNode = node2;
 
            Links.Add(repeater);
            repeaters.Add(repeater);
            return repeater;
        }
 
        public void ChangePoint(Node source,Node newPoint)
        {
            Links.ForEach(l => 
            {
                if (l.StartNode == source)
                {
                    l.StartNode = newPoint;
                }
                else if (l.EndNode == source)
                {
                    l.EndNode = newPoint;
                }
                
            });
            source.Visible = false;
            //Nodes.Remove(source);
        }
        public string GetValidID(MapObject obj)
        {
            string DefaultString = Default.GetPreString(obj); 
            int i = 0;
            string ID = obj.ID;
            List<MapObject> objs;
            while ((objs = MapObjects.FindAll(p0 => p0.ID == ID)).Count>1 && objs[0]!=obj)
            {               
                ID = $"{DefaultString}{i}";
                i++;
            }
            return ID;
        }
 
 
        public Network CreateNew(PointF3D basepoint)
        {
            Network net = this.DeepCopy<Network>();
            var BasePos = StartPoint.ToPointF3D();
            
            net.Nodes.ForEach(n => 
            {
                n.X += basepoint.X- BasePos.X;
                n.Y += basepoint.Y - BasePos.Y;
                n.Elev += basepoint.Z - BasePos.Z;
            });
 
            return net;
        }
        
        public void Add(Network network)
        {
 
            if (Nodes.Count == 0)
            {
                Nodes.AddRange(network.Nodes);
                Links.AddRange(network.Links);
                StartPoint=network.StartPoint;
                EndPoint=network.EndPoint;
            }
            else
            {
             
                ChangePoint(EndPoint, network.StartPoint);
                Nodes.AddRange(network.Nodes);
                Links.AddRange(network.Links);
                //MapObjects.ForEach(n => n.ID = GetValidID(n));
                EndPoint = network.EndPoint;
            }         
        }
        public void Rename()
        {
            MapObjects.ForEach(o => o.ID = $"{Name}_{o.ID}");
        }
        public void Add(Repeater repeater)
        {
            ChangePoint(repeater.StartNode, repeater.StartNode_inner);
            MoveDownNetwork(repeater);
            ChangePoint(repeater.EndNode, repeater.EndNode_inner);
 
            Nodes.AddRange(repeater.netList.Nodes);
            Links.AddRange(repeater.netList.Links);
        }
        public void LoadRepeaters(int MaxLevel,dict param, Dictionary<TemplateType, bool> viewModel,bool ViewMode=true)
        {
            
            //repeaters.ForEach(r => 
            //{
            //    r.Load();
            //    Add(r);
            //});
            repeaters.ForEach(r =>
            {
                
                if ( !r.Load(MaxLevel,param, viewModel, ViewMode))
                {
                    r.Status = Repeater.RepeatStatus.收起;
                    r.Visible= true;
                }
                        
                else
                {
                    Add(r);
                    r.Visible = false;
                }
                //if (r.Status == Repeater.RepeatStatus.显示)
                //{
                    
                        
                //}
 
            });
        }
 
        public void MoveDownNetwork(Repeater repeater)
        {
            visited = new Dictionary<Node, bool>();
            visited.Add(repeater.StartNode,true);
            var p1 = repeater.EndNode_inner;
            var p2 = repeater.EndNode;
            PointF3D p = new PointF3D(p1.X-p2.X,p1.Y-p2.Y,p1.Elev-p2.Elev);
            BFS(this, repeater.EndNode,new Vector3(p.X,p.Y,p.Z));
            
        }
 
 
        // 定义 visited 字典记录已访问过的节点和待访问的节点队列
        Dictionary<Node, bool> visited;//= new Dictionary<Node, bool>();
 
        public void BFS(Network net, Node startNode,Vector3 vector)
        {
            
            Queue<Node> queue = new Queue<Node>();
 
            // 标记起始节点为已访问,并加入队列
            visited[startNode] = true;
            queue.Enqueue(startNode);
 
            // 不断从队列中取出节点进行访问,直到队列为空
            while (queue.Count > 0)
            {
                // 取出队首节点并输出
                Node node = queue.Dequeue();
                node.Move(vector);
                
 
                // 遍历当前节点的所有未访问邻居节点,并标记为已访问
                foreach (Link link in net.Links)
                {
                    if (link.StartNode == node && !visited.ContainsKey(link.EndNode))
                    {
                        visited[link.EndNode] = true;
                        queue.Enqueue(link.EndNode);
                        
                    }
                    else if (link.EndNode == node && !visited.ContainsKey(link.StartNode))
                    {
                        visited[link.StartNode] = true;
                        queue.Enqueue(link.StartNode);
                    }
                }
            }
        }
 
        public List<Node> GetNode(string node)
        {
            return  Nodes.FindAll(n => n.ID == node);
        }
    }
    
 
 
 
 
}