using DevExpress.XtraEditors;
using static DevExpress.XtraEditors.XtraInputBox;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
namespace HStation.WinFrmUI
{
///
///
///
public class ImportXhsProjectFileHelper
{
private const string _tempFolder = "ImportProjectTemp";//导入项目临时文件夹
///
/// 导入
///
/// 通用视图Model
/// 信息反馈
/// 进度反馈
///
public static async Task Import(ImportXhsProjectFileViewModel vm, Action feedBackMsg, Action feedBackProgress)
{
var msg = string.Empty;
#region 第一步 临时文件处理(进度5%)
//临时文件夹路径
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 第四步 压缩文件解析 (进度25%)
msg = "正在解析模型文件...";
feedBackMsg?.Invoke(msg, Color.Black);
var bol = Yw.FileFolderZipHelper.UnZip(vm.ZipFile, tempFolder, null);
if (!bol)
{
feedBackMsg?.Invoke("模型文件解析失败!!!", Color.Red);
return false;
}
var tempFolderInfo = new DirectoryInfo(tempFolder);
var allFileInfoList = tempFolderInfo.GetFiles();
if (allFileInfoList == null || allFileInfoList.Count() < 1)
{
feedBackMsg?.Invoke("模型文件格式错误!!!", Color.Red);
return false;
}
feedBackMsg?.Invoke("模型文件解析成功。。。", Color.Green);
feedBackProgress?.Invoke(100, 25);
#endregion 第四步 压缩文件解析 (进度25%)
#region 第五步 解析水力结构文件 (进度30%)
feedBackMsg?.Invoke("正在解析水力结构文件...", Color.Black);
var jsonFileInfo = allFileInfoList.Where(x => x.Extension == ".json").FirstOrDefault();
if (jsonFileInfo == null)
{
feedBackMsg?.Invoke("水力结构文件格式错误!!!", Color.Red);
return false;
}
var revitModel = HStation.Service.RevitParseHelper.FromJsonFile(jsonFileInfo.FullName, out msg);
if (revitModel == null)
{
feedBackMsg?.Invoke($"{msg}!!!", Color.Red);
return false;
}
var hydroInfo = HStation.Hydro.TransferHelper.FromRevit(revitModel, out msg);
if (hydroInfo == null)
{
feedBackMsg?.Invoke($"水力结构文件解析失败,{msg}!!!", Color.Red);
return false;
}
var bllHydroInfo = new Yw.BLL.HydroModelInfo();
var hydroId = await bllHydroInfo.Save(hydroInfo);
if (hydroId < 1)
{
feedBackMsg?.Invoke("水力结构信息保存失败!!!", Color.Red);
return false;
}
feedBackMsg?.Invoke("水力结构文件解析成功。。。", Color.Green);
feedBackProgress?.Invoke(100, 30);
vm.HydroID = hydroId;
#endregion 第五步 解析水力结构文件 (进度30%)
#region 第六步 关联水力结构模型(进度 40%)
var projectSite = await BLLFactory.Instance.GetDefaultByProjectID((long)vm.ProjectID);
if (projectSite == null)
{
feedBackMsg?.Invoke("项目站信息错误...", Color.Red);
return false;
}
var projectSiteId = projectSite.ID;
vm.ProjectSiteID = projectSite.ID;
feedBackMsg?.Invoke("正在关联水力结构模型...", Color.Black);
var hydroRelation = new Yw.Dto.AddHydroModelRelationInput();
hydroRelation.ObjectType = HStation.Xhs.DataType.XhsProjectSite;
hydroRelation.ObjectID = projectSiteId;
hydroRelation.ModelID = hydroId;
hydroRelation.Purpose = Yw.Hydro.Purpose.Simulation;
hydroRelation.Content = null;
hydroRelation.Description = null;
var bllHydroRelation = new Yw.BLL.HydroModelRelation();
var hydroRelationId = await bllHydroRelation.Insert(hydroRelation);
if (hydroRelationId < 1)
{
feedBackMsg?.Invoke("关联水力结构模型失败!!!", Color.Red);
return false;
}
vm.HydroRelationID = hydroRelationId;
feedBackMsg?.Invoke("关联水力结构模型成功。。。", Color.Green);
feedBackProgress?.Invoke(100, 40);
#endregion 第六步 关联水力结构模型(进度 40%)
#region 第七步 上传Revit模型文件(进度 45%)
feedBackMsg?.Invoke("正在解析Revit模型文件...", Color.Black);
var rvtFileInfo = allFileInfoList.Where(x => x.Extension == ".rvt").FirstOrDefault();
if (rvtFileInfo == null)
{
feedBackMsg?.Invoke("Revit模型文件解析失败!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
return true;
}
//var bimfaceId = await Yw.WinFrmUI.BimfaceHelper.UploadFile(rvtFileInfo.FullName);//上传
var bimfaceId = 10000872887098; //测试用例
if (bimfaceId < 1)
{
feedBackMsg?.Invoke("Revit模型文件上传失败!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
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 bllBimfaceFile = new Yw.BLL.BimfaceFile();
var bimfaceFileId = await bllBimfaceFile.Insert(bimfaceFile);
if (bimfaceFileId < 1)
{
feedBackMsg?.Invoke("Bimface文件保存失败!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
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 = Yw.Bimface.Purpose.Simulation;
bimfaceRelation.Description = vm.Description;
var bllBimfaceRelation = new Yw.BLL.BimfaceFileRelation();
var bimfaceRelationId = await bllBimfaceRelation.Insert(bimfaceRelation);
if (bimfaceRelationId < 1)
{
feedBackMsg?.Invoke("Bimface文件关联失败!!!", Color.Red);
feedBackMsg?.Invoke("项目导入结束。。。", Color.Blue);
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);
}
}
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 bllBimfaceFile.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);
#endregion 第十一步 更新Bimface文件状态(进度 100%)
#region 弃用 第十二步 项目发布(进度 100%)
/* feedBackMsg?.Invoke("正在发布项目...");
var bllXhsProject = new BLL.XhsProject();
bol = await bllXhsProject.Publish(projectId);
if (!bol)
{
feedBackMsg?.Invoke("项目发布失败...");
}
else
{
vm.IsPublished = true;
feedBackMsg?.Invoke("项目发布成功...");
}
feedBackProgress?.Invoke(100, 100);
feedBackMsg?.Invoke("项目导入结束。。。");*/
return true;
#endregion 弃用 第十二步 项目发布(进度 100%)
}
}
}