using Castle.Core.Internal;
using DevExpress.XtraEditors;
using HStation.Vmo;
using static DevExpress.XtraEditors.XtraInputBox;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
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 第一步 临时文件处理(进度5%)
#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()
{
new XhsProjectSiteVmo()
{
Name=vm.Name,
Description=vm.Description
}
};
var projectId = await BLLFactory.Instance.Insert(project);
if (projectId < 1)
{
feedBackMsg?.Invoke("项目创建失败!!!", Color.Red);
return false;
}
feedBackMsg?.Invoke("项目创建成功。。。", Color.Green);
vm.ProjectID = projectId;
feedBackProgress?.Invoke(100, 10);
#endregion 第二步 创建项目(进度 10%)
#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.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 第三步 设置地图位置(进度 20%)
#region 第四步 压缩文件解析 (进度25%)
msg = "正在解析模型文件...";
feedBackMsg?.Invoke(msg, Color.Black);
var zipFilePwd = HStation.Settings.XhsParasHelper.Xhs.File.Password;
var bol = Yw.FileFolderZipHelper.UnZip(vm.ZipFile, 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, 25);
#endregion 第四步 压缩文件解析 (进度25%)
#region 第五步 解析水力结构文件 (进度30%)
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 msg);
if (structRevitModel == null)
{
feedBackMsg?.Invoke($"{msg}!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
feedBackProgress?.Invoke(100, 100);
return true;
}
//Revit 属性错误处理
var hasRevitError = false;
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)
{
hasRevitError = true;
}
switch (revitParterPropStatus.PropStatus)
{
case Revit.ePropStatus.Error:
{
feedBackMsg?.Invoke($"构件编码:{revitParter.Code},构件属性:{revitParterPropStatus.PropName},错误:{revitParterPropStatus.StatusInfo} ", Color.Red);
}
break;
case Revit.ePropStatus.Lack:
{
feedBackMsg?.Invoke($"构件编码:{revitParter.Code},构件属性:{revitParterPropStatus.PropName},缺省:{revitParterPropStatus.StatusInfo} ", Color.Gray);
}
break;
case Revit.ePropStatus.Abnormal:
{
feedBackMsg?.Invoke($"构件编码:{revitParter.Code},构件属性:{revitParterPropStatus.PropName},异常:{revitParterPropStatus.StatusInfo} ", Color.Orange);
}
break;
}
}
}
}
if (hasRevitError)
{
feedBackMsg?.Invoke($"水力结构文件属性错误!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
feedBackProgress?.Invoke(100, 100);
return true;
}
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;
}
var hydroId = await BLLFactory.Instance.Save(hydroInfo);
if (hydroId < 1)
{
feedBackMsg?.Invoke("水力结构信息保存失败!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
feedBackProgress?.Invoke(100, 100);
return true;
}
feedBackMsg?.Invoke("正在进行产品匹配...", Color.Black);
hydroInfo = await BLLFactory.Instance.GetByID(hydroId);
var matchingParas = AssetsMatchingParasHelper.Create(hydroInfo);
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);
}
hydroId = await BLLFactory.Instance.Save(hydroInfo);
feedBackMsg?.Invoke("水力结构文件解析成功。。。", Color.Green);
feedBackProgress?.Invoke(100, 30);
vm.HydroID = hydroId;
#endregion 第五步 解析水力结构文件 (进度30%)
#region 第六步 关联水力结构模型(进度 40%)
var projectSite = await BLLFactory.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.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, 40);
#endregion 第六步 关联水力结构模型(进度 40%)
#region 第七步 上传Revit模型文件(进度 45%)
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 = 10000878572231; //测试用例
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, 45);
#endregion 第七步 上传Revit模型文件(进度 45%)
#region 第八步 保存Bimface文件 (进度 50%)
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.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, 50);
#endregion 第八步 保存Bimface文件 (进度 50%)
#region 第九步 关联Bimface文件(进度 55%)
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.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, 55);
#endregion 第九步 关联Bimface文件(进度 55%)
#region 第十步 发起bimface转换(进度 80%)
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, 80);
#endregion 第十步 发起bimface转换(进度 80%)
#region 第十一步 更新Bimface文件状态(进度 100%)
feedBackMsg?.Invoke("正在更新Bimface文件状态...", Color.Black);
bol = await BLLFactory.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 第十一步 更新Bimface文件状态(进度 100%)
}
}
}