lixiaojun
2024-11-14 ae45d123c2ee907fd40f7db86f2aac2b21f976b6
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
using Castle.Core.Internal;
using DevExpress.XtraEditors;
using HStation.Model;
using HStation.Vmo;
using static DevExpress.XtraEditors.XtraInputBox;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
 
namespace HStation.WinFrmUI
{
    /// <summary>
    ///
    /// </summary>
    public class ImportXhsProjectHelper
    {
        private const string _tempFolder = "ImportProjectTemp";//导入项目临时文件夹
 
        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="vm">通用视图Model</param>
        /// <param name="feedBackMsg">信息反馈</param>
        /// <param name="feedBackProgress">进度反馈</param>
        /// <returns></returns>
        public static async Task<bool> Import(ImportXhsProjectViewModel vm, Action<string, Color> feedBackMsg, Action<int, int> feedBackProgress)
        {
            var msg = string.Empty;
 
            #region 第一步 临时文件处理(进度5%)
 
            msg = "开始导入项目...";
            feedBackMsg?.Invoke(msg, Color.Black);
 
            //临时文件夹路径
            var tempFolder = Path.Combine(Yw.Service.ConfigHelper.DataPath, HStation.Settings.XhsParasHelper.Xhs.DataFolder, _tempFolder);
            //如果存在临时文件夹,则删除临时文件夹及子目录与文件
            if (Directory.Exists(tempFolder))
            {
                Directory.Delete(tempFolder, true);
            }
            //创建临时文件夹
            Directory.CreateDirectory(tempFolder);
 
            feedBackProgress?.Invoke(100, 5);
 
            #endregion
 
            #region 第二步 创建项目(进度 10%)
 
            feedBackMsg?.Invoke("正在创建项目...", Color.Black);
 
            var project = new Vmo.XhsProjectExtensionsVmo();
            project.NO = vm.NO;
            project.Name = vm.Name;
            project.Address = vm.Address;
            project.Customer = vm.Customer;
            project.Flags = vm.Flags;
            project.TagName = vm.TagName;
            project.Description = vm.Description;
            project.SiteList = new List<XhsProjectSiteVmo>()
             {
                    new XhsProjectSiteVmo()
                     {
                            Name=vm.Name,
                            Description=vm.Description
                     }
             };
 
            var projectId = await BLLFactory<HStation.BLL.XhsProjectExtensions>.Instance.Insert(project);
            if (projectId < 1)
            {
                feedBackMsg?.Invoke("项目创建失败!!!", Color.Red);
                return false;
            }
 
            feedBackMsg?.Invoke("项目创建成功。。。", Color.Green);
 
            vm.ProjectID = projectId;
 
            feedBackProgress?.Invoke(100, 10);
 
            #endregion
 
            #region 第三步 设置地图位置(进度 20%)
 
            feedBackMsg?.Invoke("正在设置地图位置...", Color.Black);
 
            if (vm.Location != null)
            {
                var mapInfo = new Yw.Vmo.MapInfoVmo();
                mapInfo.ObjectType = HStation.Xhs.DataType.XhsProject;
                mapInfo.ObjectID = projectId;
                mapInfo.ObjectName = project.Name;
                mapInfo.Purpose = Yw.Map.Purpose.Location;
                mapInfo.Kind = Yw.Map.Kind.Gaodei;
                mapInfo.Shape = Yw.Map.Shape.Marker;
                mapInfo.Position = vm.Location.ToJson();
 
                var mapInfoId = await BLLFactory<Yw.BLL.MapInfo>.Instance.Insert(mapInfo);
                if (mapInfoId < 1)
                {
                    feedBackMsg?.Invoke("设置地图位置失败!!!", Color.Red);
                }
                else
                {
                    vm.MapInfoID = mapInfoId;
                    feedBackMsg?.Invoke("设置地图位置成功。。。", Color.Green);
                }
            }
            else
            {
                feedBackMsg?.Invoke("未设置地图位置信息!!!", Color.Red);
            }
 
            feedBackProgress?.Invoke(100, 20);
 
            #endregion
 
            #region 第四步 压缩文件解析 (进度30%)
 
            var zipFilePwd = HStation.Settings.XhsParasHelper.Xhs.File.Password;
            var localZipFile = vm.LocalZipFile;
            if (!vm.IsLocal)
            {
                if (vm.TransferRevitFile != null)
                {
                    feedBackMsg?.Invoke("正在下载模型文件...", Color.Black);
                    localZipFile = await BLLFactory<HStation.BLL.TransferRevitFile>.Instance.Download(vm.TransferRevitFile.StorageHouse, vm.TransferRevitFile.StorageCode);
                    feedBackMsg?.Invoke("模型文件下载完成!!!", Color.Green);
                }
            }
            msg = "正在解析模型文件...";
            feedBackMsg?.Invoke(msg, Color.Black);
            var bol = Yw.FileFolderZipHelper.UnZip(localZipFile, tempFolder, zipFilePwd);
            if (!bol)
            {
                feedBackMsg?.Invoke("模型文件解析失败!!!", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                feedBackProgress?.Invoke(100, 100);
                return true;
            }
 
            var tempFolderInfo = new DirectoryInfo(tempFolder);
            var allFileInfoList = tempFolderInfo.GetFiles();
            if (allFileInfoList == null || allFileInfoList.Count() < 1)
            {
                feedBackMsg?.Invoke("模型文件格式错误!!!", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                feedBackProgress?.Invoke(100, 100);
                return true;
            }
 
            feedBackMsg?.Invoke("模型文件解析成功。。。", Color.Green);
 
            feedBackProgress?.Invoke(100, 30);
 
            #endregion
 
            #region 第五步 解析水力结构文件 (进度40%)
 
            feedBackMsg?.Invoke("正在解析水力结构文件...", Color.Black);
            var structFileInfo = allFileInfoList.Find(x => x.Name == HStation.Revit.ConstParas.StructFileName);
            if (structFileInfo == null)
            {
                feedBackMsg?.Invoke("水力结构文件格式错误!!!", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                feedBackProgress?.Invoke(100, 100);
                return true;
            }
            var structOthersFileInfo = allFileInfoList.Find(x => x.Name == HStation.Revit.ConstParas.StructOthersFileName);
            var structRevitModel = HStation.Service.RevitParseHelper.FromJsonFile
                (structFileInfo.FullName, structOthersFileInfo?.FullName, out bool structFileResult, out List<string> structFileMsgList);
            //解析结果判断
            if (!structFileResult)
            {
                structFileMsgList?.ForEach(x => feedBackMsg?.Invoke(x, Color.Red));
            }
            //Revit属性错误处理
            var hasRevitPropError = false;
            if (structRevitModel != null)
            {
                var allRevitParterList = structRevitModel.GetAllParters();
                foreach (var revitParter in allRevitParterList)
                {
                    if (revitParter.PropStatusList != null && revitParter.PropStatusList.Count > 0)
                    {
                        foreach (var revitParterPropStatus in revitParter.PropStatusList)
                        {
                            if (revitParterPropStatus.PropStatus == HStation.Revit.ePropStatus.Error)
                            {
                                hasRevitPropError = true;
                            }
                            switch (revitParterPropStatus.PropStatus)
                            {
                                case Revit.ePropStatus.Error:
                                    {
                                        feedBackMsg?.Invoke($"构件类型:{revitParter.Catalog},构件编码:{revitParter.Id},构件属性:{revitParterPropStatus.PropName},错误:{revitParterPropStatus.StatusInfo} ", Color.Red);
                                    }
                                    break;
 
                                case Revit.ePropStatus.Lack:
                                    {
                                        //feedBackMsg?.Invoke($"构件编码:{revitParter.Id},构件属性:{revitParterPropStatus.PropName},缺省:{revitParterPropStatus.StatusInfo} ", Color.Gray);
                                    }
                                    break;
 
                                case Revit.ePropStatus.Abnormal:
                                    {
                                        //feedBackMsg?.Invoke($"构件编码:{revitParter.Id},构件属性:{revitParterPropStatus.PropName},异常:{revitParterPropStatus.StatusInfo} ", Color.Orange);
                                    }
                                    break;
 
                                default: break;
                            }
                        }
                    }
                }
            }
            if (hasRevitPropError || !structFileResult)
            {
                feedBackMsg?.Invoke($"水力结构文件解析错误!!!", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                feedBackProgress?.Invoke(100, 100);
                return true;
            }
 
            vm.RevitModel = structRevitModel;
 
            var hydroInfo = HStation.Hydro.TransferHelper.FromRevit(structRevitModel, out msg);
            if (hydroInfo == null)
            {
                feedBackMsg?.Invoke($"水力结构文件解析失败,{msg}!!!", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                feedBackProgress?.Invoke(100, 100);
                return true;
            }
 
            feedBackMsg?.Invoke("正在进行产品匹配...", Color.Black);
            var matchingParas = AssetsMatchingParasHelper.Create(hydroInfo, null);
            if (AssetsMatchingHelper.Matching(matchingParas, out string error))
            {
                if (AssetsMatchingParasHelper.Apply(hydroInfo, matchingParas))
                {
                    feedBackMsg?.Invoke("产品匹配完成。。。", Color.Green);
                }
                else
                {
                    feedBackMsg?.Invoke("产品匹配失败!!!", Color.Red);
                }
            }
            else
            {
                feedBackMsg?.Invoke("产品匹配失败!!!", Color.Red);
            }
            feedBackMsg?.Invoke("正在保存水力结构信息...", Color.Black);
            var hydroId = await BLLFactory<Yw.BLL.HydroModelInfo>.Instance.Save(hydroInfo);
            if (hydroId < 1)
            {
                feedBackMsg?.Invoke("水力结构信息保存失败!!!", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                feedBackProgress?.Invoke(100, 100);
                return true;
            }
            feedBackMsg?.Invoke("水力结构信息保存成功。。。", Color.Green);
 
            feedBackMsg?.Invoke("水力结构文件解析成功。。。", Color.Green);
            feedBackProgress?.Invoke(100, 40);
            vm.HydroID = hydroId;
 
            #endregion
 
            #region 第六步 关联水力结构模型(进度 50%)
 
            var projectSite = await BLLFactory<HStation.BLL.XhsProjectSite>.Instance.GetDefaultByProjectID(projectId);
            if (projectSite == null)
            {
                feedBackMsg?.Invoke("项目站信息错误...", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                feedBackProgress?.Invoke(100, 100);
                return true;
            }
 
            var projectSiteId = projectSite.ID;
            vm.ProjectSiteID = projectSite.ID;
 
            feedBackMsg?.Invoke("正在关联水力结构模型...", Color.Black);
 
            var hydroRelation = new Yw.Vmo.HydroModelRelationVmo();
            hydroRelation.ObjectType = HStation.Xhs.DataType.XhsProjectSite;
            hydroRelation.ObjectID = projectSiteId;
            hydroRelation.ModelID = hydroId;
            hydroRelation.Purpose = HStation.Xhs.Purpose.Simulation;
            hydroRelation.Content = null;
            hydroRelation.Description = null;
 
            var hydroRelationId = await BLLFactory<Yw.BLL.HydroModelRelation>.Instance.Insert(hydroRelation);
            if (hydroRelationId < 1)
            {
                feedBackMsg?.Invoke("关联水力结构模型失败!!!", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                feedBackProgress?.Invoke(100, 100);
                return true;
            }
 
            vm.HydroRelationID = hydroRelationId;
            feedBackMsg?.Invoke("关联水力结构模型成功。。。", Color.Green);
            feedBackProgress?.Invoke(100, 50);
 
            #endregion
 
            #region 第七步 上传Revit模型文件(进度 60%)
 
            feedBackMsg?.Invoke("正在解析Revit模型文件...", Color.Black);
            var rvtFileInfo = allFileInfoList.Find(x => x.Name == HStation.Revit.ConstParas.RvtFileName);
            if (rvtFileInfo == null)
            {
                feedBackMsg?.Invoke("Revit模型文件解析失败!!!", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                feedBackProgress?.Invoke(100, 100);
                return true;
            }
 
            //var bimfaceId = await Yw.WinFrmUI.BimfaceHelper.UploadFile(rvtFileInfo.FullName);//上传
            var bimfaceId = 10000884893369; //测试用例 10000878572231 10000882826621  10000884283372 10000884362886 10000884702464 10000884893369
            if (bimfaceId < 1)
            {
                feedBackMsg?.Invoke("Revit模型文件上传失败!!!", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                feedBackProgress?.Invoke(100, 100);
                return true;
            }
 
            vm.BimfaceId = bimfaceId;
            feedBackMsg?.Invoke("解析Revit模型文件成功。。。", Color.Green);
            feedBackProgress?.Invoke(100, 60);
 
            #endregion
 
            #region 第八步 保存Bimface文件 (进度 65%)
 
            feedBackMsg?.Invoke("正在保存Bimface文件...", Color.Black);
 
            var bimfaceFile = new Yw.Vmo.BimfaceFileVmo();
            bimfaceFile.BimfaceId = bimfaceId.ToString();
            bimfaceFile.Name = vm.Name;
            bimfaceFile.ModelType = (int)Yw.Bimface.eModelType.File;
            bimfaceFile.FileStatus = (int)Yw.Bimface.eFileStatus.UploadSucceed;
            bimfaceFile.FormatType = (int)Yw.Bimface.eFormatType._3D;
            bimfaceFile.FileSuffix = rvtFileInfo.Extension;
            bimfaceFile.FileSize = string.Format("{0}MB", System.Math.Ceiling(rvtFileInfo.Length / 1024.0 / 1024.0));
 
            var bimfaceFileId = await BLLFactory<Yw.BLL.BimfaceFile>.Instance.Insert(bimfaceFile);
            if (bimfaceFileId < 1)
            {
                feedBackMsg?.Invoke("Bimface文件保存失败!!!", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                feedBackProgress?.Invoke(100, 100);
                return true;
            }
 
            vm.BimfaceFileID = bimfaceFileId;
            feedBackMsg?.Invoke("Bimface文件保存成功。。。", Color.Green);
            feedBackProgress?.Invoke(100, 65);
 
            #endregion
 
            #region 第九步 关联Bimface文件(进度 70%)
 
            feedBackMsg?.Invoke("正在关联Bimface文件...", Color.Black);
 
            var bimfaceRelation = new Yw.Vmo.BimfaceFileRelationVmo();
            bimfaceRelation.ObjectType = HStation.Xhs.DataType.XhsProjectSite;
            bimfaceRelation.ObjectID = projectSiteId;
            bimfaceRelation.BimfaceFileID = bimfaceFileId;
            bimfaceRelation.Purpose = HStation.Xhs.Purpose.Simulation;
            bimfaceRelation.Description = vm.Description;
 
            var bimfaceRelationId = await BLLFactory<Yw.BLL.BimfaceFileRelation>.Instance.Insert(bimfaceRelation);
            if (bimfaceRelationId < 1)
            {
                feedBackMsg?.Invoke("Bimface文件关联失败!!!", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                feedBackProgress?.Invoke(100, 100);
                return true;
            }
            vm.BimfaceFileRelationID = bimfaceRelationId;
            feedBackMsg?.Invoke("Bimface文件关联成功。。。", Color.Green);
            feedBackProgress?.Invoke(100, 70);
 
            #endregion
 
            #region 第十步 发起bimface转换(进度 90%)
 
            feedBackMsg?.Invoke("正在进行Bimface模型轻量化...", Color.Black);
            //var bimfaceTranslateStatus = await Yw.WinFrmUI.BimfaceHelper.TranslateRvtFile(bimfaceId);//发起转换
            var bimfaceTranslateStatus = Yw.BIMFace.eTranslateStatus.Processing;//测试代码
            feedBackProgress?.Invoke(100, 60);
            if (bimfaceTranslateStatus != Yw.BIMFace.eTranslateStatus.Success)
            {
                var dlgResult = XtraMessageBox.Show("Bimface模型轻量化需要一定的时间,是否继续等待?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlgResult == DialogResult.Yes)
                {
                    await Yw.WinFrmUI.BimfaceHelper.WaitFileTranslateStatusUntilSuccess(bimfaceId, 3000);
                    vm.BimfaceConverted = true;
                    feedBackMsg?.Invoke("Bimface模型轻量化完成。。。", Color.Green);
                }
                else
                {
                    feedBackMsg?.Invoke("跳过Bimface模型轻量化等待。。。", Color.Black);
                    feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
                    feedBackProgress?.Invoke(100, 100);
                    return true;
                }
            }
            else
            {
                vm.BimfaceConverted = true;
                feedBackMsg?.Invoke("Bimface模型轻量化完成。。。", Color.Green);
            }
 
            feedBackProgress?.Invoke(100, 90);
 
            #endregion
 
            #region 第十一步 更新Bimface文件状态(进度 100%)
 
            feedBackMsg?.Invoke("正在更新Bimface文件状态...", Color.Black);
            bol = await BLLFactory<Yw.BLL.BimfaceFile>.Instance.UpdateFileStatus(bimfaceFileId, (int)Yw.Bimface.eFileStatus.ConvertSucceed);
            if (bol)
            {
                feedBackMsg?.Invoke("Bimface文件状态更新成功。。。", Color.Green);
            }
            else
            {
                feedBackMsg?.Invoke("Bimface文件状态更新失败!!!", Color.Red);
                feedBackMsg?.Invoke("项目导入结束。。。", Color.Black);
                return true;
            }
 
            feedBackProgress?.Invoke(100, 100);
            feedBackMsg?.Invoke("项目导入完成。。。", Color.Green);
            return true;
 
            #endregion
        }
    }
}