lixiaojun
2024-08-13 9bd12d8745d7a886f088b07408526c504384444d
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
using HStation.Model;
using Microsoft.VisualBasic;
 
namespace HStation.Service
{
    /// <summary>
    /// 修正辅助类
    /// </summary>
    public static class RevitCorrectHelper
    {
        /// <summary>
        /// 修正(无法修正会抛出异常)
        /// </summary>
        /// <param name="rhs">RevitModel</param>
        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 (!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.GetAllWaterSources();
            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.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.Pumps == null)
            {
                rhs.Pumps = new List<Model.RevitPump>();
            }
            if (rhs.Valves == null)
            {
                rhs.Valves = new List<Model.RevitValve>();
            }
            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 allLinks = rhs.GetAllLinks();
            foreach (var link in allLinks)
            {
                var allParterList = rhs.GetAllParters();
                var startLinkParter = allParterList.Find(x => x.Code == link.StartCode);
                if (startLinkParter is IRevitLink)
                {
                    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;
                    (startLinkParter as IRevitLink).EndCode = junction.Code;
                }
 
                allParterList = rhs.GetAllParters();
                var endLinkParter = allParterList.Find(x => x.Code == link.EndCode);
                if (endLinkParter is IRevitLink)
                {
                    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;
                    (endLinkParter as IRevitLink).StartCode = junction.Code;
                }
            }
            return true;
        }
 
    }
}