123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- 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
- {
- /// <summary>
- /// 是否是水平的
- /// </summary>
- /// <param name="mepCurve"></param>
- /// <returns></returns>
- public static bool IsHorizontal(this MEPCurve mepCurve)
- {
- var line = mepCurve.GetLocationCurve() as Line;
- if (line == null)
- return false;
- return line.Direction.IsVertical(XYZ.BasisZ);
- }
- /// <summary>
- /// 是否垂直的
- /// </summary>
- /// <param name="mepCurve"></param>
- /// <returns></returns>
- public static bool IsVertical(this MEPCurve mepCurve)
- {
- var line = mepCurve.GetLocationCurve() as Line;
- if (line == null)
- return false;
- return line.Direction.IsParallel(XYZ.BasisZ);
- }
- /// <summary>
- /// 复制指定的mepCurve
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="mepCurve"></param>
- /// <param name="start"></param>
- /// <param name="end"></param>
- /// <returns></returns>
- public static T Copy<T>(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;
- }
- /// <summary>
- /// 更新指定mepCurve的迹线
- /// </summary>
- /// 2015-09-15 xls
- /// <param name="mepCurve"></param>
- /// <param name="start"></param>
- /// <param name="end"></param>
- 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);
- }
- /// <summary>
- /// 刷新mepCurve,mepCurve绕迹线旋转360度
- /// </summary>
- /// <param name="mepCurve"></param>
- public static void Refresh(this MEPCurve mepCurve)
- {
- var locationCurve = mepCurve.Location as LocationCurve;
- mepCurve.Rotate(mepCurve.GetLocationLine(), Math.PI * 2);
- }
- /// <summary>
- /// 替换mepCurve指定点位新的坐标
- /// </summary>
- /// <param name="mepCurve">要修改的MepCurve</param>
- /// <param name="oldPoint">将要被替换的点的位置</param>
- /// <param name="newPoint">新的点的位置</param>
- 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);
- }
- }
- /// <summary>
- /// 获取距离给定点最近的点connector
- /// </summary>
- /// <param name="element"></param>
- /// <param name="point"></param>
- /// <returns></returns>
- public static Connector GetNearnessConnector(this MEPCurve element, XYZ point)
- {
- var line = element.GetLocationCurve() as Line;
- return element.GetConnectorByOrigin(line.GetNearnessPoint(point));
- }
- /// <summary>
- /// 获取距离给定点最远的点connector
- /// </summary>
- /// <param name="element"></param>
- /// <param name="point"></param>
- /// <returns></returns>
- public static Connector GetFarnessConnector(this MEPCurve element, XYZ point)
- {
- var line = element.GetLocationCurve() as Line;
- return element.GetConnectorByOrigin(line.GetFarnessPoint(point));
- }
- /// <summary>
- /// 设置mepCurve的朝向(有效构件为方向风管或者桥架)
- /// </summary>
- /// <param name="mepCurve"></param>
- /// <param name="normal"></param>
- 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);
- }
- }
- }
- }
|