using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FWindSoft.Revit.Mep
{
///
/// 构件初始坐标接口(匹配connector的连接点)
///
public interface IFittingCoordinate
{
///
/// 获取指定实例的连接点坐标
///
///
///
Tuple GetCoordinate(FamilyInstance fi);
}
///
/// 连接件坐标生成工厂
///
public static class FittingCoordinateFactory
{
///
/// 根据内置分类,获取相应的构件坐标
///
///
///
public static IFittingCoordinate CreateFittingCoordinate(BuiltInCategory builtCategory)
{
if (builtCategory == BuiltInCategory.OST_ConduitFitting)
{
return new ConduitFittingCoordinate();
}
else if (builtCategory == BuiltInCategory.OST_PipeFitting)
{
return new PipeFittingCoordinate();
}
return null;
}
///
/// 获取实例的坐标体系
///
///
///
public static Tuple GetCoordinate(FamilyInstance fi)
{
var builtCategory = fi.GetBuiltInCategory();
var realFittingCoordinate = CreateFittingCoordinate(builtCategory);
return realFittingCoordinate?.GetCoordinate(fi);
}
}
///
/// 线管连接件坐标
///
internal class ConduitFittingCoordinate : IFittingCoordinate
{
public Tuple GetCoordinate(FamilyInstance fi)
{
return new Tuple(XYZ.BasisX, XYZ.BasisY);
}
}
///
/// 水管管连接件坐标
///
internal class PipeFittingCoordinate : IFittingCoordinate
{
public Tuple GetCoordinate(FamilyInstance fi)
{
var connectors = fi.GetConnectors(Domain.DomainPiping);
Connector primaryConnector = connectors.FirstOrDefault(c => c.GetMEPConnectorInfo().IsPrimary);
Connector secondConnector = null;
foreach (Connector connector in connectors)
{
if (connector == primaryConnector)
{
continue;
}
if (!connector.CoordinateSystem.BasisZ.IsParallel(primaryConnector.CoordinateSystem.BasisZ))
{
secondConnector = connector;
break;
}
}
if(primaryConnector==null||secondConnector==null)
{
return null;
}
#region 特殊处理
try
{
if (connectors.Count == 2)
{
secondConnector.Angle = Math.PI / 2;
}
else if (connectors.Count == 3)
{
secondConnector.Angle = Math.PI / 2;
}
}
catch (Exception e)
{
//如果没有参照角可能会发生异常;四通设置90度有问题。设置0度是不是可以?
//throw;
}
#endregion
var firstDirection = primaryConnector.CoordinateSystem.BasisZ;
var secondDirection = secondConnector.CoordinateSystem.BasisZ;
if (!firstDirection.IsVertical(secondDirection))
{
var useSecond = secondDirection - secondDirection.DotProduct(firstDirection) * firstDirection;
secondDirection = useSecond.Normalize();
}
return new Tuple(firstDirection, secondDirection);
}
}
}