cloudflight
2023-12-20 ac3e88e5dc69c74c80045a86abe38caae81d0101
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
using dict_py_Inner;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace CloudWaterNetwork
{
    [Serializable]
    public class Repeater : Link
    {
 
        [Category("1、基本信息")]
        [DisplayName("对象前缀名称")]
        [Browsable(true)]
        /// <summary>
        /// 模板ID
        /// </summary>
        public string NetworkPreName { get; set; }
 
        [Category("1、基本信息")]
        [DisplayName("显示名称")]
        [Browsable(true)]
        /// <summary>
        /// 模板ID
        /// </summary>
        public string NetworkShowName { get; set; }
 
 
 
 
        [Category("1、基本信息")]
        [DisplayName("前缀最小序号")]
        [Browsable(true)]
        /// <summary>
        /// 模板ID
        /// </summary>
        public int NetworkPreStartNum { get; set; } = 1;
 
 
        [Category("1、基本信息")]
        [DisplayName("前缀正序")]
        [Browsable(true)]
        /// <summary>
        /// 模板ID
        /// </summary>
        public bool NetworkIsAscNum { get; set; } = true;
 
 
 
        [Category("2、参数")]
        [DisplayName("模板ID")]
        [Browsable(true)]
        [Editor(typeof(TemplatePropertyEditor), typeof(UITypeEditor))]
 
        /// <summary>
        /// 模板ID
        /// </summary>
        public string TemplateID { get; set; }
 
 
        [Category("2、参数")]
        [DisplayName("模板名称")]
        [Browsable(true)]
        public string TempName => Global.Templates.GetTemplate(TemplateID)?.名称;
 
        public Template template = null;
 
        [NonSerialized]
        public Network netList = null;
 
     
 
        public Node StartNode_inner;
 
        public Node EndNode_inner;
 
 
        public bool isLoadded = false;
 
 
 
        [Category("2、参数")]
        [DisplayName("重复次数")]
        [Browsable(true)]
        /// <summary>
        /// 重复次数
        /// </summary>
        public int RepeatTimes { get; set; }
 
 
 
 
        [Category("2、参数")]
        [DisplayName("海森威廉系数")]
        [Browsable(false)]
        public float Roughness { get; set; }
        [Category("2、参数")]
        [DisplayName("局部阻力系数")]
        [Browsable(false)]
        public float MinorLoss { get; set; }
        [Category("1、基本信息")]
        [DisplayName("长度")]
        [Browsable(false)]
        public float Length { get; set; }
 
        [Category("1、基本信息")]
        [DisplayName("口径")]
        [Browsable(false)]
        public float Diameter { get; set; }
 
 
        [Category("2、参数")]
        [DisplayName("显示状态")]
        [Browsable(true)]
        public RepeatStatus Status { get; set; }
 
 
        public bool Load(int MaxLevel, dict param, Dictionary<TemplateType, bool> viewModel,bool viewMode =true)
        {
 
            if (template == null)
            {
                template = Global.Templates.GetTemplate(TemplateID);
                if (template == null)
                {
                    return false;
                }
                template.loadInpFile();
                template.network.MapObjects.ForEach(o => 
                { 
                    if (o.Level > MaxLevel)
                        o.Visible = false; 
                    else 
                        o.Visible = true; 
                });
                Network net = template.network;
                net.StartPoint = net.GetNode(template.Node1)[0];
                net.EndPoint = net.GetNode(template.Node2)[0];
 
            }
 
 
            if (!viewMode)
            {
                Status = RepeatStatus.收起;
            }
            else if (viewModel != null)
            {
                if(viewModel[template.Type])
                    Status = RepeatStatus.显示;
                else
                    Status = RepeatStatus.收起;
            }
            else
            {
                Status = RepeatStatus.显示;
            }
            
                
            if (param.Contains("层数") && param["层数"]>0 && template.Type==TemplateType.楼层模板)
            {
                RepeatTimes =(int) param["层数"];
            }
            
            if (Status==RepeatStatus.收起)
            {
                return false;
            }
            PointF3D basepoint = StartNode.ToPointF3D();
            netList = new Network();
            for (int i = 0; i < RepeatTimes; i++)
            {
                Network net = template.network.CreateNew(basepoint);
                int index = GetIndex(i);
 
                net.Name = NetworkPreName + (index==0?"":index.ToString("00"));
                net.StartPoint = net.GetNode(template.Node1)[0];
                net.EndPoint = net.GetNode(template.Node2)[0];
                if (template.Type==TemplateType.楼层模板)
                    ChangeNetbyParam(net, param);
                if (i == 0) StartNode_inner = net.StartPoint;
                EndNode_inner = net.EndPoint;
                net.LoadRepeaters(MaxLevel, param, viewModel);
                net.Rename();
                netList.Add(net);
                basepoint = netList.EndPoint.ToPointF3D();
            }
            return true;
        }
        void ChangeNetbyParam(Network net,dict param)
        {
            if (param.Contains("层高") && param["层高"] > 0 && net.StartPoint!=null && net.EndPoint!=null)
            {
                var currentHeight = Math.Abs(net.StartPoint.Elev - net.EndPoint.Elev);
                var k = (float)param["层高"] / currentHeight;
                net.Nodes.ForEach(n=>n.Elev=(n.Elev-net.StartPoint.Elev) * k + net.StartPoint.Elev);
            }
 
            if (param.Contains("入户管长") && param["入户管长"] > 0 &&  net.StartPoint != null && net.EndPoint != null)
            {
                Dictionary<Node, float> Distance = new Dictionary<Node, float>();
                CalcDistance(net, net.StartPoint, Distance);
                float sumDistance = 0;
                net.meters.ForEach(m => sumDistance += Distance[m]);
                float avgDistance = sumDistance / net.meters.Count;
                var k= (float)param["入户管长"] / avgDistance;
                net.Nodes.ForEach(n =>
                {
                    n.X = (n.X - net.StartPoint.X) * k + net.StartPoint.X;
                    n.Y = (n.Y - net.StartPoint.Y) * k + net.StartPoint.Y;
                });
            }
            if (param.Contains("户数") && template.Type == TemplateType.楼层模板)
            {
                var MaxLevel =(int) param["户数"];
                net.MapObjects.ForEach(o =>
                {
                    if (o.Level > MaxLevel)
                        o.Visible = false;
                    else
                        o.Visible = true;
                });
            }
 
 
        }
 
 
 
        public static void CalcDistance(Network net, Node startNode, Dictionary<Node, float> Distance)
        {
            // 定义 visited 字典记录已访问过的节点和待访问的节点队列
            
            Queue<Node> queue = new Queue<Node>();
 
            // 标记起始节点为已访问,并加入队列
            Distance[startNode] = 0;
            queue.Enqueue(startNode);
 
            // 不断从队列中取出节点进行访问,直到队列为空
            while (queue.Count > 0)
            {
                // 取出队首节点并输出
                Node node = queue.Dequeue();
                //Console.WriteLine(node.Name);
 
                // 遍历当前节点的所有未访问邻居节点,并标记为已访问
                foreach (Link link in net.Links)
                {
                    if (link.StartNode == node )
                    {
                        var node1 = link.EndNode;
                        var dis = Distance[node] + (float)Math.Sqrt(Math.Pow(node.X - node1.X, 2) + Math.Pow(node.Y - node1.Y, 2));
                        if (!Distance.ContainsKey(node1) || Distance[node1] > dis)
                        Distance[link.EndNode] = dis;
                        queue.Enqueue(link.EndNode);
                    }
                    else if (link.EndNode == node && !Distance.ContainsKey(link.StartNode))
                    {
                        var node1 = link.StartNode;
                        var dis = Distance[node] + (float)Math.Sqrt(Math.Pow(node.X - node1.X, 2) + Math.Pow(node.Y - node1.Y, 2));
                        if (!Distance.ContainsKey(node1) || Distance[node1] > dis)
                            Distance[link.StartNode] = dis;
                        queue.Enqueue(link.StartNode);
                    }
                }
            }
        }
 
        public int GetIndex(int i)
        {
            int index = 0;
            if (NetworkIsAscNum)
            {
                index = NetworkPreStartNum + i;
            }
            else
            {
                index = RepeatTimes - 1 - i + NetworkPreStartNum;
            }
 
            return index;
        }
 
 
 
        // 重写ToString()方法以便将阀门属性转换为字符串
        public override string ToString()
        {
            return $"{ID}\t{Node1}\t{Node2}\t{TemplateID}\t{RepeatTimes}\t{Status}\t{NetworkPreName}\t{NetworkPreStartNum}\t{NetworkIsAscNum}\t{NetworkShowName}\t";
        }
        public enum RepeatStatus
        {
            收起,
            显示
        }
    }
 
}