duheng
2024-10-16 083c4e47c7a59025f3348bd093693c6b3febb014
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
using HStation.Model;
 
namespace HStation.Service
{
    /// <summary>
    /// 修正辅助类
    /// </summary>
    public static class RevitCorrectHelper
    {
        /// <summary>
        /// 修正
        /// </summary>
        public static bool Correct(this Model.RevitModel rhs, out string msg)
        {
            if (rhs == null)
            {
                msg = "数据为空";
                return false;
            }
            if (!Zero(rhs, out msg))
            {
                return false;
            }
            if (!First(rhs, out msg))
            {
                return false;
            }
            if (!Exchanger(rhs, out msg))
            {
                return false;
            }
            if (!Second(rhs, out msg))
            {
                return false;
            }
            if (!Three(rhs, out msg))
            {
                return false;
            }
            return true;
        }
 
        //前提:验证合法性
        private static bool Zero(Model.RevitModel rhs, out string msg)
        {
            msg = string.Empty;
            var allWaterSourceList = rhs.GetAllSources();
            if (allWaterSourceList == null || allWaterSourceList.Count < 1)
            {
                msg = "无水源";
                return false;
            }
            var allJunctionList = rhs.GetAllJunctions();
            if (allJunctionList == null || allJunctionList.Count < 1)
            {
                msg = "无连接节点";
                return false;
            }
            return true;
        }
 
        //第一步:检查集合初始化
        private static bool First(Model.RevitModel rhs, out string msg)
        {
            msg = string.Empty;
            if (rhs.Reservoirs == null)
            {
                rhs.Reservoirs = new List<Model.RevitReservoir>();
            }
            if (rhs.Tanks == null)
            {
                rhs.Tanks = new List<Model.RevitTank>();
            }
            if (rhs.Waterboxs == null)
            {
                rhs.Waterboxs = new List<Model.RevitWaterbox>();
            }
            if (rhs.Junctions == null)
            {
                rhs.Waterboxs = new List<Model.RevitWaterbox>();
            }
            if (rhs.Elbows == null)
            {
                rhs.Elbows = new List<Model.RevitElbow>();
            }
            if (rhs.Threelinks == null)
            {
                rhs.Threelinks = new List<Model.RevitThreelink>();
            }
            if (rhs.Fourlinks == null)
            {
                rhs.Fourlinks = new List<Model.RevitFourlink>();
            }
            if (rhs.Meters == null)
            {
                rhs.Meters = new List<RevitMeter>();
            }
            if (rhs.Flowmeters == null)
            {
                rhs.Flowmeters = new List<Model.RevitFlowmeter>();
            }
            if (rhs.Pressmeters == null)
            {
                rhs.Pressmeters = new List<Model.RevitPressmeter>();
            }
            if (rhs.Nozzles == null)
            {
                rhs.Nozzles = new List<Model.RevitNozzle>();
            }
            if (rhs.Hydrants == null)
            {
                rhs.Hydrants = new List<Model.RevitHydrant>();
            }
            if (rhs.Bluntheads == null)
            {
                rhs.Bluntheads = new List<Model.RevitBlunthead>();
            }
            if (rhs.Pipes == null)
            {
                rhs.Pipes = new List<Model.RevitPipe>();
            }
            if (rhs.Translations == null)
            {
                rhs.Translations = new List<Model.RevitTranslation>();
            }
            if (rhs.Exchangers == null)
            {
                rhs.Exchangers = new List<RevitExchanger>();
            }
            if (rhs.Pumps == null)
            {
                rhs.Pumps = new List<Model.RevitPump>();
            }
            if (rhs.Valves == null)
            {
                rhs.Valves = new List<Model.RevitValve>();
            }
            return true;
        }
 
        //单独修复换热器
        private static bool Exchanger(Model.RevitModel rhs, out string msg)
        {
            msg = string.Empty;
            if (rhs.Exchangers != null && rhs.Exchangers.Count > 0)
            {
                var allParterList = rhs.GetAllParters();
                foreach (var exchanger in rhs.Exchangers)
                {
                    if (exchanger.ConnectList != null && exchanger.ConnectList.Count > 0)
                    {
                        foreach (var connect in exchanger.ConnectList.ToList())
                        {
                            var connectParter = allParterList.Find(x => x.Id == connect.Id);
                            if (connectParter == null)
                            {
                                exchanger.ConnectList.Remove(connect);
                            }
                            else
                            {
                                if (connectParter is RevitBlunthead blunthead)
                                {
                                    var decorator = new Model.RevitDecorator();
                                    decorator.Id = blunthead.Id;
                                    decorator.Code = blunthead.Code;
                                    decorator.Name = blunthead.Name;
                                    decorator.Category = RevitJsonCatalog.Blunthead;
                                    decorator.Decoration = null;
                                    decorator.Description = blunthead.Description;
                                    rhs.Decorators.Add(decorator);
 
                                    rhs.Bluntheads.Remove(blunthead);
                                    allParterList.Remove(connectParter);
                                }
                            }
                        }
 
                        var startConnectList = exchanger.ConnectList.GetStartConnects();
                        if (startConnectList == null || startConnectList.Count < 1)
                        {
                            msg = $"换热器:{exchanger.Id} 上游连接组件不存在";
                            return false;
                        }
                        var startJunction = new Model.RevitJunction();
                        startJunction.Id = Yw.Untity.UniqueHelper.CreateFromFirst("junction", allParterList.Select(x => x.Code).ToList());
                        startJunction.Code = startJunction.Id;
                        startJunction.Name = UniqueHelper.CreateFromFirst("连接节点", allParterList.Select(x => x.Name).Distinct().ToList());
                        startJunction.Flags = null;
                        startJunction.ModelType = null;
                        startJunction.Description = "换热器修正时,自动添加";
                        startJunction.Quality = exchanger.StartQuality;
                        startJunction.Position = startConnectList.GetCenterPosition();
                        startJunction.Elev = exchanger.StartElev;
                        startJunction.Demand = null;
                        startJunction.DemandPattern = null;
                        rhs.Junctions.Add(startJunction);
                        exchanger.StartCode = startJunction.Code;
                        foreach (var startConnect in startConnectList)
                        {
                            var startConnectParter = allParterList.Find(x => x.Id == startConnect.Id);
                            if (startConnectParter is RevitLink revitLink)
                            {
                                if (revitLink.StartCode == exchanger.Id)
                                {
                                    revitLink.StartCode = startJunction.Id;
                                }
                                else if (revitLink.EndCode == exchanger.Id)
                                {
                                    revitLink.EndCode = startJunction.Id;
                                }
                            }
                        }
                        allParterList.Add(startJunction);
 
                        var endConnectList = exchanger.ConnectList.GetEndConnects();
                        if (endConnectList == null || endConnectList.Count < 1)
                        {
                            msg = $"换热器:{exchanger.Id} 下游连接组件不存在";
                            return false;
                        }
                        var endJunction = new Model.RevitJunction();
                        endJunction.Id = Yw.Untity.UniqueHelper.CreateFromFirst("junction", allParterList.Select(x => x.Code).ToList());
                        endJunction.Code = endJunction.Id;
                        endJunction.Name = UniqueHelper.CreateFromFirst("连接节点", allParterList.Select(x => x.Name).Distinct().ToList());
                        endJunction.Flags = null;
                        endJunction.ModelType = null;
                        endJunction.Description = "换热器修正时,自动添加";
                        endJunction.Quality = exchanger.EndQuality;
                        endJunction.Position = endConnectList.GetCenterPosition();
                        endJunction.Elev = exchanger.EndElev;
                        endJunction.Demand = null;
                        endJunction.DemandPattern = null;
                        rhs.Junctions.Add(endJunction);
                        exchanger.EndCode = endJunction.Code;
                        foreach (var endConnect in endConnectList)
                        {
                            var endConnectParter = allParterList.Find(x => x.Id == endConnect.Id);
                            if (endConnectParter is RevitLink revitLink)
                            {
                                if (revitLink.StartCode == exchanger.Id)
                                {
                                    revitLink.StartCode = endJunction.Id;
                                }
                                else if (revitLink.EndCode == exchanger.Id)
                                {
                                    revitLink.EndCode = endJunction.Id;
                                }
                            }
                        }
                        allParterList.Add(endJunction);
                    }
                }
            }
            return true;
        }
 
        //第二步:检查上下游编码
        private static bool Second(Model.RevitModel rhs, out string msg)
        {
            msg = string.Empty;
            var allParterList = rhs.GetAllParters();
            var allLinks = rhs.GetAllLinks();
            foreach (var link in allLinks)
            {
                var startLinkParter = allParterList.Find(x => x.Id == link.StartCode);
                if (startLinkParter == null)
                {
                    msg = $"管段: {link.Id} 上游节点错误";
                    return false;
                }
                link.StartCode = startLinkParter.Code;
                var endLinkParter = allParterList.Find(x => x.Id == link.EndCode);
                if (endLinkParter == null)
                {
                    msg = $"管段: {link.Id} 下游节点错误";
                    return false;
                }
                link.EndCode = endLinkParter.Code;
            }
            return true;
        }
 
        //第三步:按照水力结构进行修正
        private static bool Three(Model.RevitModel rhs, out string msg)
        {
            msg = string.Empty;
            var allParterList = rhs.GetAllParters();
            var allLinks = rhs.GetAllLinks();
            foreach (var link in allLinks)
            {
                var startLinkParter = allParterList.Find(x => x.Code == link.StartCode);
                if (startLinkParter is RevitLink startLink)
                {
                    var junction = new Model.RevitJunction();
                    junction.Id = Yw.Untity.UniqueHelper.CreateFromFirst("junction", allParterList.Select(x => x.Code).ToList());
                    junction.Code = junction.Id;
                    junction.Name = UniqueHelper.CreateFromFirst("连接节点", allParterList.Select(x => x.Name).Distinct().ToList());
                    junction.Flags = null;
                    junction.ModelType = null;
                    junction.Description = "水力修正时,自动添加";
                    junction.Quality = link.StartQuality;
                    junction.Position = link.StartPosition;
                    junction.Elev = link.StartElev;
                    junction.Demand = null;
                    junction.DemandPattern = null;
                    rhs.Junctions.Add(junction);
                    link.StartCode = junction.Code;
 
                    if (startLink.StartCode == link.Code)
                    {
                        startLink.StartCode = junction.Code;
                    }
                    else if (startLink.EndCode == link.Code)
                    {
                        startLink.EndCode = junction.Code;
                    }
                    allParterList.Add(junction);
                }
 
                var endLinkParter = allParterList.Find(x => x.Code == link.EndCode);
                if (endLinkParter is RevitLink endLink)
                {
                    var junction = new Model.RevitJunction();
                    junction.Id = Yw.Untity.UniqueHelper.CreateFromFirst("junction", allParterList.Select(x => x.Code).ToList());
                    junction.Code = junction.Id;
                    junction.Name = UniqueHelper.CreateFromFirst("连接节点", allParterList.Select(x => x.Name).Distinct().ToList());
                    junction.Flags = null;
                    junction.ModelType = null;
                    junction.Description = "水力修正时,自动添加";
                    junction.Quality = link.EndQuality;
                    junction.Position = link.EndPosition;
                    junction.Elev = link.EndElev;
                    junction.Demand = null;
                    junction.DemandPattern = null;
                    rhs.Junctions.Add(junction);
                    link.EndCode = junction.Code;
                    if (endLink.StartCode == link.Code)
                    {
                        endLink.StartCode = junction.Code;
                    }
                    else if (endLink.EndCode == link.Code)
                    {
                        endLink.EndCode = junction.Code;
                    }
                    allParterList.Add(junction);
                }
            }
            return true;
        }
 
    }
}