/* ==============================================================================
* 功能描述:VerticalPipeUtil
* 创 建 者:Garrett
* 创建日期:2019/11/5 15:35:20
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using FWindSoft.Revit;
using FWindSoft.Wpf;
using NPOI.SS.UserModel;
using Saga.PlugIn.ModelCheck;
using SAGA.DotNetUtils;
using SAGA.DotNetUtils.Extend;
using SAGA.DotNetUtils.Others;
using SAGA.RevitUtils.MEP;
namespace Saga.PlugIn.VerticalPipeCheck
{
///
/// VerticalPipeUtil
///
class VerticalPipeUtil
{
///
/// 执行立管检查
///
///
///
///
///
public static Tuple Execute(string upPath,string downPath,string savePath)
{
try
{
var upPipes = GetVerticalMEPCurves(upPath,true);
var downPipes = GetVerticalMEPCurves(downPath, false);
Action, List, string, bool> check = (pipes1, pipes2, path, isup) =>
{
string fileName = path.GetFileName();
foreach (VerticalPipe pipe in pipes1)
{
var pipes = pipes2.Where(t => t.OpenXyz.IsAlmostEqualTo(pipe.OpenXyz));
if (pipes.Any())
{
pipe.TargetFileName = fileName;
pipe.TargetPath = path;
pipe.IsRight = true;
string tip = isup ? "下" : "上";
pipe.RMessage = $"在{tip}层相应位置到了对应的立管。Id:{pipes.FirstOrDefault().MepCurve.Id}";
}
else
{
pipe.TargetFileName = fileName;
pipe.TargetPath = path;
pipe.IsRight = false;
string tip = isup ? "下" : "上";
pipe.RMessage = $"在{tip}层相应位置找不到对应的立管,请检查模型";
}
}
};
check(upPipes, downPipes, downPath, true);
check(downPipes, upPipes, upPath, false);
List results=new List(upPipes);
results.AddRange(downPipes);
results.Sort(new CommonComparer((x, y) => {return x.IsRight.CompareTo(y.IsRight); }));
#region Save
//重置workbook准备保存结果
DCRExport.ClearWorkbook(DCRExport.VerticalPipePath);
//保存
ExportResult(results);
DCRExport.Save(savePath, DCRExport.GetWorkbook());
return new Tuple(upPipes.Count,downPipes.Count);
#endregion
}
catch (Exception e)
{
MessageShowBase.Show(e);
}
return null;
}
///
/// 获取所有的立管
///
///
///
///
public static List GetVerticalMEPCurves(string path,bool isup)
{
var doc=RevitCore.App.OpenDocumentFile(path);
string fileName = path.GetFileName();
var mepcurves = doc.GetElements();
List verticalPipes=new List();
foreach (MEPCurve mepCurve in mepcurves)
{
bool result = false;
var curve = mepCurve.GetLocationCurve();
if (curve is Line line)
{
if (line.Direction.IsParallel(XYZ.BasisZ))
{
var start = line.StartPoint();
var end = line.EndPoint();
XYZ point = null;
if (isup)
{
point = start.Z.IsThan(end.Z) ? end : start;
}
else
{
point = start.Z.IsThan(end.Z) ? start : end;
}
Connector connector = mepCurve.GetNearConnector(point);
if (connector!=null&&!connector.IsConnected)
{
VerticalPipe verticalPipe=new VerticalPipe();
verticalPipe.MepCurve = mepCurve;
verticalPipe.OpenXyz = point;
verticalPipe.Path = path;
verticalPipe.FileName = fileName;
verticalPipes.Add(verticalPipe);
}
}
}
}
return verticalPipes;
}
///
/// 检查结果汇总
///
///
///
private static void ExportResult(List list)
{
try
{
IWorkbook book = DCRExport.GetWorkbook();
string sheetName = "立管对齐检查";
ISheet sheet = book.GetSheet(sheetName);
#region 添加数据
int index = 3;
//添加 共检查XXX条数据,未通过检查的如下 提示
IRow rowTip = sheet.CreateRow(index - 1);
//rowTip.AddCell(0, $"总检查{list.Count}条数据,未通过检查的如下", DataCheckNPOIStyle.Title);
foreach (var result in list)
{
index++;
IRow rowN = sheet.CreateRow(index);
DataCheckNPOIStyle style = result.IsRight ? DataCheckNPOIStyle.Content : DataCheckNPOIStyle.Error;
int j = -1;
rowN.AddCell(++j, result.FileName, style);
rowN.AddCell(++j, result.Path, style);
rowN.AddCell(++j, result.MepCurve.Id.ToString(), style);
rowN.AddCell(++j, result.MepCurve.GetSystemTypeName(), style);
rowN.AddCell(++j, result.TargetFileName, style);
rowN.AddCell(++j, result.TargetPath, style);
string rowN4 = result.IsRight ? "通过" : "不通过";
rowN.AddCell(++j, rowN4, style);
rowN.AddCell(++j, result.RMessage, style);
}
#endregion
}
catch (Exception e)
{
MessageShowBase.Show(e);
}
}
}
class VerticalPipe
{
public MEPCurve MepCurve { get; set; }
public XYZ OpenXyz { get; set; }
public string Path { get; set; }
public string FileName { get; set; }
public string TargetPath { get; set; }
public string TargetFileName { get; set; }
///
/// 是否通过较验
///
public bool IsRight { get; set; }
///
/// 提示信息
///
public string RMessage { get; set; }
}
}