using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FWindSoft.Revit
{
public static class DuctExtension
{
///
/// 获取风管系统类型(非枚举)
///
/// 风管
///
public static MechanicalSystemType GetMechanicalSystemType(this Duct duct)
{
var id = duct.GetParameterElementId(BuiltInParameter.RBS_DUCT_SYSTEM_TYPE_PARAM);
return duct.Document.GetElement(id) as MechanicalSystemType;
}
///
/// 获取风管系统分类
///
///
///
public static DuctSystemType GetDuctSystemType(this Duct duct)
{
var ductSystem = duct.MEPSystem as MechanicalSystem;
if (ductSystem != null)
return ductSystem.SystemType;
return DuctSystemType.UndefinedSystemType;
}
///
/// 获取风管的朝向
///
///
///
public static XYZ GetCurveNormal(this Duct duct)
{
//通过connector basizY的方向去确定朝向
Connector connector = duct.GetFirstConnector(c=>c.ConnectorType==ConnectorType.End);
return connector.CoordinateSystem.BasisY.Negate();
}
///
/// 设置风管的朝向
///
///
///
public static void SetCurveNormal(this Duct duct, XYZ normal)
{
XYZ sorce = duct.GetCurveNormal();
Line axis = duct.GetLocationLine();
double angle = sorce.AngleOnPlaneTo(normal, axis.Direction.Normalize());
duct.Rotate(axis, angle);
}
///
/// 获取风管的最长尺寸
///
///
///
public static double GetLongestSize(this Duct duct)
{
double tempDia = duct.GetParameterDouble(BuiltInParameter.RBS_CURVE_DIAMETER_PARAM).MmFromFt();
double tempWidth = duct.GetParameterDouble(BuiltInParameter.RBS_CURVE_WIDTH_PARAM).MmFromFt();
double tempHeight = duct.GetParameterDouble(BuiltInParameter.RBS_CURVE_HEIGHT_PARAM).MmFromFt();
double length = tempHeight > tempWidth ? tempHeight : tempWidth;
length = tempDia > length ? tempDia : length;
return length;
}
///
/// 判断给定的风管集合的垂直对齐方式
///
/// 风管集合
/// 返回垂直对齐的标识(-1,底对齐;0,一般处理;1,顶对齐。)
public static int GetVerAlignType(this List ducts)
{
int flag = 0;
//List connectors = ducts.Select(d => d.GetConnectors()[0]).ToList();
//List> listTuple = new List>();
//foreach (var connector in connectors)
//{
// double length = connector.Shape == ConnectorProfileType.Round ? connector.Radius * 2 : connector.Height;
// listTuple.Add(new Tuple(connector.Origin, connector.CoordinateSystem.BasisY, length));
//}
//if (listTuple.Count == 0)
// return flag;
//Tuple baseTuple = listTuple[0];
//if (listTuple.Any(t => !t.Item2.IsParallel(baseTuple.Item2)))
// return 0;
//Line baseLine = baseTuple.Item1.NewLine(baseTuple.Item1.OffsetPoint(baseTuple.Item2, 1000));
//XYZ baseOrigion = baseLine.GetProjectPt(baseTuple.Item1, true);
//if (listTuple.All(t => baseLine.GetProjectPt(t.Item1, true).IsEqual(baseOrigion, 0.001)))
//{
// return 0;
//}
//XYZ bottom = baseTuple.Item1.OffsetPoint(baseTuple.Item2, baseTuple.Item3 / 2);
//XYZ bottomProject = baseLine.GetProjectPt(bottom, true);
//if (
// listTuple.All(
// t =>
// baseLine.GetProjectPt(t.Item1.OffsetPoint(t.Item2, t.Item3 / 2), true)
// .IsEqual(bottomProject, 0.001)))
//{
// return -1;
//}
//XYZ top = baseTuple.Item1.OffsetPoint(baseTuple.Item2.Negate(), baseTuple.Item3 / 2);
//XYZ topProject = baseLine.GetProjectPt(top, true);
//if (
// listTuple.All(
// t =>
// baseLine.GetProjectPt(t.Item1.OffsetPoint(t.Item2.Negate(), t.Item3 / 2), true)
// .IsEqual(topProject, 0.001)))
//{
// return 1;
//}
return flag;
}
///
/// 获取风管实例截面类型
///
/// 指定风管
/// 风管截面形状
public static DuctShape GetDuctShape(this Duct duct)
{
DuctShape shape = DuctShape.Rectangular;
var connector = duct.GetFirstConnector(c=>c.ConnectorType==ConnectorType.End);
switch (connector.Shape)
{
case ConnectorProfileType.Oval:
{
shape = DuctShape.Oval;
break;
}
case ConnectorProfileType.Round:
{
shape = DuctShape.Round;
break;
}
case ConnectorProfileType.Rectangular:
{
shape = DuctShape.Rectangular;
break;
}
}
return shape;
}
///
/// 获取垂直风管角度
///
///
/// 如果风管垂直返回正确角度,如果风管竖直,返回π/2
public static double GetVerAngle(this Duct duct)
{
if (duct == null)
return 0;
double defalut = Math.PI / 2;
var line = duct.GetLocationLine();
XYZ start = line.StartPoint();
XYZ end = line.EndPoint();
if (!start.IsEqual2(end))
{
return defalut;
}
XYZ normal = duct.GetCurveNormal();
//不封装,是为了保证使用的严谨性,时刻记住是三维
return XYZ.BasisX.AngleOnPlaneTo(normal, XYZ.BasisZ);
}
///
/// 根据界面类型获取所需要的尺寸
///
///
///
///
public static List GetDuctSizes(this Document doc, DuctShape shape)
{
List sizes = new List();
DuctSizeSettings setting = DuctSizeSettings.GetDuctSizeSettings(doc);
if (setting != null)
{
sizes = setting[shape].Select(size => size.NominalDiameter.MmFromFt()).ToList();
}
return sizes;
}
///
/// 获取项目中设置的空气密度
///
///
///
public static double GetAirDensity(this Document doc)
{
double airDensity = 1;
List sizes = new List();
DuctSettings setting = DuctSettings.GetDuctSettings(doc);
if (setting != null)
{
airDensity = setting.AirDensity;
}
//单位转换的问题
return 0;//airDensity.FromApi(DisplayUnitType.DUT_KILOGRAMS_PER_CUBIC_METER);
}
///
/// 获取制定风管类型的截面类型
///
/// 风管类型
///
public static DuctShape GetDuctShape(this DuctType ductType)
{
string strShape = ductType.GetParameterString(BuiltInParameter.ALL_MODEL_FAMILY_NAME);
do
{
if ("矩形风管".Equals(strShape))
{
return DuctShape.Rectangular;
}
if ("圆形风管".Equals(strShape))
{
return DuctShape.Round;
}
if ("椭圆形风管".Equals(strShape))
{
return DuctShape.Oval;
}
} while (false);
throw new Exception("获取风管类型截面出错");
}
///
/// 获取项目中所有风管类型
///
///
///
public static List GetDuctTypes(this Document doc)
{
List list = doc.GetElements();
return list;
}
///
/// 获取项目中所有风管类型
///
///
///
///
public static List GetDuctTypes(this Document doc, DuctShape shape)
{
List list = doc.GetDuctTypes();
return list.Where(type => type.GetDuctShape() == shape).ToList();
}
///
/// 获取所有的软管类型
///
///
///
public static List GetFlexDuctTypes(this Document doc)
{
List list = doc.GetElements();
return list;
}
}
}