using Castle.Core.Internal;
namespace HStation.WinFrmUI
{
///
///
///
public class ImportXhsProjectHelper
{
private const string _tempFolder = "ImportProjectTemp";//导入项目临时文件夹
///
/// 导入
///
/// 通用视图Model
/// 信息反馈
/// 进度反馈
///
public static async Task Import(ImportXhsProjectViewModel vm, Action feedBackMsg, Action 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%)
//判断项目数据合法性
if (vm.Project == null)
{
feedBackMsg?.Invoke("项目数据初始化错误!!!", Color.Red);
return false;
}
if (vm.Project.ID < 1)
{
feedBackMsg?.Invoke("正在创建项目...", Color.Black);
vm.Project.ID = await BLLFactory.Instance.Insert(vm.Project);
if (vm.Project.ID < 1)
{
feedBackMsg?.Invoke("项目创建失败!!!", Color.Red);
return false;
}
vm.Project = await BLLFactory.Instance.GetByID(vm.Project.ID);
feedBackMsg?.Invoke("项目创建成功。。。", Color.Green);
}
else
{
feedBackMsg?.Invoke("正在更新项目...", Color.Black);
if (!await BLLFactory.Instance.Update(vm.Project))
{
feedBackMsg?.Invoke("项目更新失败!!!", Color.Red);
return false;
}
vm.Project = await BLLFactory.Instance.GetByID(vm.Project.ID);
feedBackMsg?.Invoke("项目更新成功。。。", Color.Green);
}
feedBackProgress?.Invoke(100, 10);
#endregion
#region 第三步 设置地图位置(进度 20%)
//已有地图信息才需要进行设置
if (vm.MapInfo != null)
{
feedBackMsg?.Invoke("正在设置位置信息...", Color.Black);
vm.MapInfo.ObjectID = vm.Project.ID;
vm.MapInfo.ObjectName = vm.Project.Name;
if (vm.MapInfo.ID < 1)
{
vm.MapInfo.ID = await BLLFactory.Instance.Insert(vm.MapInfo);
if (vm.MapInfo.ID < 1)
{
feedBackMsg?.Invoke("位置信息设置失败!!!", Color.Red);
}
else
{
feedBackMsg?.Invoke("位置信息设置成功。。。", Color.Green);
}
}
else
{
if (!await BLLFactory.Instance.Update(vm.MapInfo))
{
feedBackMsg?.Invoke("位置信息设置失败!!!", Color.Red);
}
else
{
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.Instance.Download(vm.TransferRevitFile.StorageHouse, vm.TransferRevitFile.StorageCode);
feedBackMsg?.Invoke("模型文件下载完成!!!", Color.Green);
}
}
feedBackMsg?.Invoke("正在解析模型文件...", 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);
vm.RevitModel = HStation.Service.RevitParseHelper.FromJsonFile
(structFileInfo.FullName, structOthersFileInfo?.FullName, out bool structFileResult, out List structFileMsgList);
//解析结果判断
if (!structFileResult)
{
structFileMsgList?.ForEach(x => feedBackMsg?.Invoke(x, Color.Red));
}
//Revit属性错误处理
var hasRevitPropError = false;
if (vm.RevitModel != null)
{
var allRevitParterList = vm.RevitModel.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;
default: break;
}
}
}
}
}
if (hasRevitPropError || !structFileResult)
{
feedBackMsg?.Invoke($"水力结构文件解析错误!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
feedBackProgress?.Invoke(100, 100);
return true;
}
vm.HydroInfo = HStation.Hydro.TransferHelper.FromRevit
(
vm.RevitModel,
out List allPropStatusDbList,
out List allScenePropValueList,
out List allDecoratorList,
out msg
);
if (vm.HydroInfo == null)
{
feedBackMsg?.Invoke($"水力结构文件解析失败,{msg}!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
feedBackProgress?.Invoke(100, 100);
return true;
}
var propStatusHelper = new HydroPropStatusHelper(vm.HydroInfo, allPropStatusDbList);
feedBackMsg?.Invoke("正在进行产品匹配...", Color.Black);
var matchingParas = Yw.WinFrmUI.HydroMatchingHelper.Create(vm.HydroInfo);
if (AssetsMatchingHelper.Matching(matchingParas, out string error))
{
if (Yw.WinFrmUI.HydroMatchingHelper.Apply(vm.HydroInfo, matchingParas))
{
feedBackMsg?.Invoke("产品匹配完成。。。", Color.Green);
}
else
{
feedBackMsg?.Invoke("产品匹配失败!!!", Color.Red);
}
}
else
{
feedBackMsg?.Invoke("产品匹配失败!!!", Color.Red);
}
feedBackMsg?.Invoke("正在进行管网参数修复...", Color.Black);
vm.HydroInfo.RepairParas(propStatusHelper, out msg);
if (string.IsNullOrEmpty(msg))
{
feedBackMsg?.Invoke("管网参数修复完成。。。", Color.Green);
}
else
{
feedBackMsg?.Invoke(msg, Color.Red);
}
feedBackMsg?.Invoke("正在保存水力结构信息...", Color.Black);
vm.HydroInfo.ID = await BLLFactory.Instance.Save(vm.HydroInfo);
if (vm.HydroInfo.ID < 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.HydroInfo = await BLLFactory.Instance.GetByID(vm.HydroInfo.ID);
//属性状态
if (allPropStatusDbList != null && allPropStatusDbList.Count > 0)
{
await BLLFactory.Instance.Save(vm.HydroInfo.ID, allPropStatusDbList);
}
//场景属性
if (allScenePropValueList != null && allScenePropValueList.Count > 0)
{
await BLLFactory.Instance.Save(vm.HydroInfo.ID, HStation.Revit.ConstParas.Scene, allScenePropValueList);
}
//装饰件
if (allDecoratorList != null && allDecoratorList.Count > 0)
{
await BLLFactory.Instance.Save(vm.HydroInfo.ID, HStation.Revit.ConstParas.Scene, allDecoratorList);
}
#endregion
#region 第六步 关联水力结构模型(进度 50%)
feedBackMsg?.Invoke("正在关联水力结构模型...", Color.Black);
vm.HydroRelation = new Yw.Vmo.HydroModelRelationVmo();
vm.HydroRelation.ObjectType = HStation.Xhs.DataType.XhsProjectSite;
vm.HydroRelation.ObjectID = vm.Project.SiteList.First().ID;
vm.HydroRelation.ModelID = vm.HydroInfo.ID;
vm.HydroRelation.Purpose = Yw.Hydro.Purpose.Simulation;
vm.HydroRelation.Content = null;
vm.HydroRelation.Description = "导入自动生成";
vm.HydroRelation.ID = await BLLFactory.Instance.Insert(vm.HydroRelation);
if (vm.HydroRelation.ID < 1)
{
feedBackMsg?.Invoke("关联水力结构模型失败!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
feedBackProgress?.Invoke(100, 100);
return true;
}
vm.HydroRelation = await BLLFactory.Instance.GetByID(vm.HydroRelation.ID);
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;//测试用例 制氮循环:10000884893369 10000896755736 10000899666805
if (bimfaceId < 1)
{
feedBackMsg?.Invoke("Revit模型文件上传失败!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
feedBackProgress?.Invoke(100, 100);
return true;
}
feedBackMsg?.Invoke("解析Revit模型文件成功。。。", Color.Green);
feedBackProgress?.Invoke(100, 60);
#endregion
#region 第八步 保存Bimface文件 (进度 65%)
feedBackMsg?.Invoke("正在保存Bimface文件...", Color.Black);
vm.BimfaceFile = new Yw.Vmo.BimfaceFileVmo();
vm.BimfaceFile.BimfaceId = bimfaceId.ToString();
vm.BimfaceFile.Name = vm.Project.Name;
vm.BimfaceFile.ModelType = (int)Yw.Bimface.eModelType.File;
vm.BimfaceFile.FileStatus = (int)Yw.Bimface.eFileStatus.UploadSucceed;
vm.BimfaceFile.FormatType = (int)Yw.Bimface.eFormatType._3D;
vm.BimfaceFile.FileSuffix = rvtFileInfo.Extension;
vm.BimfaceFile.FileSize = string.Format("{0}MB", System.Math.Ceiling(rvtFileInfo.Length / 1024.0 / 1024.0));
vm.BimfaceFile.ID = await BLLFactory.Instance.Insert(vm.BimfaceFile);
if (vm.BimfaceFile.ID < 1)
{
feedBackMsg?.Invoke("Bimface文件保存失败!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
feedBackProgress?.Invoke(100, 100);
return true;
}
vm.BimfaceFile = await BLLFactory.Instance.GetByID(vm.BimfaceFile.ID);
feedBackMsg?.Invoke("Bimface文件保存成功。。。", Color.Green);
feedBackProgress?.Invoke(100, 65);
#endregion
#region 第九步 关联Bimface文件(进度 70%)
feedBackMsg?.Invoke("正在关联Bimface文件...", Color.Black);
vm.BimfaceRelation = new Yw.Vmo.BimfaceFileRelationVmo();
vm.BimfaceRelation.ObjectType = HStation.Xhs.DataType.XhsProjectSite;
vm.BimfaceRelation.ObjectID = vm.Project.SiteList.First().ID;
vm.BimfaceRelation.BimfaceFileID = vm.BimfaceFile.ID;
vm.BimfaceRelation.Purpose = Yw.Bimface.Purpose.Simulation;
vm.BimfaceRelation.Description = "导入自动生成";
vm.BimfaceRelation.ID = await BLLFactory.Instance.Insert(vm.BimfaceRelation);
if (vm.BimfaceRelation.ID < 1)
{
feedBackMsg?.Invoke("Bimface文件关联失败!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
feedBackProgress?.Invoke(100, 100);
return true;
}
vm.BimfaceRelation = await BLLFactory.Instance.GetByID(vm.BimfaceRelation.ID);
feedBackMsg?.Invoke("Bimface文件关联成功。。。", Color.Green);
feedBackProgress?.Invoke(100, 70);
#endregion
#region 第十步 发起模型转换(进度 90%)
feedBackProgress?.Invoke(100, 60);
feedBackMsg?.Invoke("正在进行模型轻量化...", Color.Black);
var bimfaceTranslateStatus = await Yw.WinFrmUI.BimfaceHelper.TranslateRvtFile(bimfaceId);//发起转换
//var bimfaceTranslateStatus = Yw.BIMFace.eTranslateStatus.Processing;//测试代码
if (bimfaceTranslateStatus != Yw.BIMFace.eTranslateStatus.Success)
{
await Yw.WinFrmUI.BimfaceHelper.WaitFileTranslateStatusUntilSuccess(bimfaceId, 3000);
}
feedBackMsg?.Invoke("模型轻量化完成。。。", Color.Green);
feedBackProgress?.Invoke(100, 90);
#endregion
#region 第十一步 更新模型文件状态(进度 100%)
feedBackMsg?.Invoke("正在更新模型文件状态...", Color.Black);
bol = await BLLFactory.Instance.UpdateFileStatus(vm.BimfaceFile.ID, (int)Yw.Bimface.eFileStatus.ConvertSucceed);
if (bol)
{
vm.BimfaceFile.FileStatus = (int)Yw.Bimface.eFileStatus.ConvertSucceed;
feedBackMsg?.Invoke("模型文件状态更新成功。。。", Color.Green);
}
else
{
feedBackMsg?.Invoke("模型文件状态更新失败!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
return true;
}
feedBackProgress?.Invoke(100, 100);
feedBackMsg?.Invoke("项目导入完成。。。", Color.Green);
return true;
#endregion
}
}
}