using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
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 MepCurveExtension
{
///
/// 是否是水平的
///
///
///
public static bool IsHorizontal(this MEPCurve mepCurve)
{
var line = mepCurve.GetLocationCurve() as Line;
if (line == null)
return false;
return line.Direction.IsVertical(XYZ.BasisZ);
}
///
/// 是否垂直的
///
///
///
public static bool IsVertical(this MEPCurve mepCurve)
{
var line = mepCurve.GetLocationCurve() as Line;
if (line == null)
return false;
return line.Direction.IsParallel(XYZ.BasisZ);
}
///
/// 复制指定的mepCurve
///
///
///
///
///
///
public static T Copy(this T mepCurve, XYZ start, XYZ end) where T : MEPCurve
{
var doc = mepCurve.Document;
var ids = ElementTransformUtils.CopyElement(doc, mepCurve.Id, XYZ.Zero);
T copy = null;
if (ids.Count > 0)
{
copy = doc.GetElement(ids.FirstOrDefault()) as T;
}
copy.UpdateLocation(start, end);
return copy;
}
///
/// 更新指定mepCurve的迹线
///
/// 2015-09-15 xls
///
///
///
public static void UpdateLocation(this MEPCurve mepCurve, XYZ start, XYZ end)
{
if (start.IsEqual(end))
return;
var locationCurve = mepCurve.Location as LocationCurve;
Line newLine =start.NewLine(end);
locationCurve.Curve = newLine;
//mepCurve.Rotate(newLine, Math.PI * 2);
}
///
/// 刷新mepCurve,mepCurve绕迹线旋转360度
///
///
public static void Refresh(this MEPCurve mepCurve)
{
var locationCurve = mepCurve.Location as LocationCurve;
mepCurve.Rotate(mepCurve.GetLocationLine(), Math.PI * 2);
}
///
/// 替换mepCurve指定点位新的坐标
///
/// 要修改的MepCurve
/// 将要被替换的点的位置
/// 新的点的位置
public static void ReplaceLocation(this MEPCurve mepCurve, XYZ oldPoint, XYZ newPoint)
{
//按最接近的点,找到需要替换的点的位置
var oldLine = mepCurve.GetLocationLine();
oldPoint = oldLine.GetNearnessPoint(oldPoint);
if (oldPoint.IsEqual(newPoint))
return;
Line newLine = null;
if (oldLine.StartPoint().IsEqual(oldPoint))
{
newLine = newPoint.NewLine(oldLine.EndPoint());
}
else
{
newLine = oldLine.StartPoint().NewLine(newPoint);
}
if (newLine != null)
{
var locationCurve = mepCurve.Location as LocationCurve;
locationCurve.Curve = newLine;
//mepCurve.Rotate(newLine, Math.PI * 2);
}
}
///
/// 获取距离给定点最近的点connector
///
///
///
///
public static Connector GetNearnessConnector(this MEPCurve element, XYZ point)
{
var line = element.GetLocationCurve() as Line;
return element.GetConnectorByOrigin(line.GetNearnessPoint(point));
}
///
/// 获取距离给定点最远的点connector
///
///
///
///
public static Connector GetFarnessConnector(this MEPCurve element, XYZ point)
{
var line = element.GetLocationCurve() as Line;
return element.GetConnectorByOrigin(line.GetFarnessPoint(point));
}
///
/// 设置mepCurve的朝向(有效构件为方向风管或者桥架)
///
///
///
public static void SetCurveNormal(this MEPCurve mepCurve, XYZ normal)
{
if (mepCurve is CableTray cableTray)
{
cableTray.CurveNormal = normal;
}
else if (mepCurve is Duct duct)
{
duct.SetCurveNormal(normal);
}
}
}
}