123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Autodesk.Revit.DB;
- using Autodesk.Revit.DB.Mechanical;
- using Autodesk.Revit.DB.Plumbing;
- namespace FWindSoft.Revit
- {
- public static class ElementExtension
- {
- /// <summary>
- /// 获取定位线
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- public static Curve GetLocationCurve(this Element element)
- {
- return element.Location?.ConvertToCurve();
- }
- /// <summary>
- /// 获取定位线
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- public static Line GetLocationLine(this Element element)
- {
- return element.Location?.ConvertToLine();
- }
- /// <summary>
- /// 获取元素定位点
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- public static XYZ GetLocationPoint(this Element element)
- {
- return element.Location?.ConvertToPoint();
- }
- /// <summary>
- /// 获取元素开始点
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- public static XYZ StartPoint(this Element element)
- {
- return element.GetLocationCurve()?.StartPoint();
- }
- /// <summary>
- /// 获取元素结束点
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- public static XYZ EndPoint(this Element element)
- {
- return element.GetLocationCurve()?.EndPoint();
- }
- public static BuiltInCategory GetCategory(this Element elem)
- {
- var bic = BuiltInCategory.INVALID;
- if (elem.Category != null)
- {
- bic = (BuiltInCategory)elem.Category.Id.IntegerValue;
- }
- return bic;
- }
- /// <summary>
- /// 元素旋转信息
- /// </summary>
- /// <param name="element"></param>
- /// <param name="axis"></param>
- /// <param name="angle"></param>
- public static void Rotate(this Element element, Line axis, double angle)
- {
- ElementTransformUtils.RotateElement(element.Document, element.Id, axis, angle);
- }
- /// <summary>
- /// 获取元素的内置分类
- /// </summary>
- /// <param name="elem"></param>
- /// <returns></returns>
- public static BuiltInCategory GetBuiltInCategory(this Element elem)
- {
- var bic = BuiltInCategory.INVALID;
- if (!elem.IsValidObject)
- return BuiltInCategory.INVALID;
- if (elem.Category != null && elem.Category.Id != null)
- {
- bic = (BuiltInCategory)elem.Category.Id.IntegerValue;
- }
- return bic;
- }
- public static bool IsWaterComponment(this Element element)
- {
- return element is Pipe || element is FlexPipe || (element is FamilyInstance && element.GetBuiltInCategory() == BuiltInCategory.OST_PipeFitting);
- }
- public static bool IsHavcComponment(this Element element)
- {
- return element is Duct|| element is FlexDuct || (element is FamilyInstance && element.GetBuiltInCategory() == BuiltInCategory.OST_DuctFitting);
- }
- /// <summary>
- /// 获取图元类型
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- public static ElementType GetElementType(this Element element)
- {
- ElementId id = element.GetTypeId();
- if (id != null && id != ElementId.InvalidElementId)
- {
- Element elementType = element.Document.GetElement(id);
- return elementType as ElementType;
- }
- return null;
- }
- /// <summary>
- /// 获取图元类型
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="element"></param>
- /// <returns></returns>
- public static T GetElementType<T>(this Element element) where T : ElementType
- {
- ElementId id = element.GetTypeId();
- if (id != null && id != ElementId.InvalidElementId)
- {
- Element elementType = element.Document.GetElement(id);
- return elementType as T;
- }
- return null;
- }
- /// <summary>
- /// 获取参照标高
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- public static Level GetLevel(this Element element)
- {
- Level level = null;
- ElementId eId = element.GetLevelId();
- if (eId != ElementId.InvalidElementId)
- {
- level = element.Document.GetElement(eId) as Level;
- }
- return level;
- }
- /// <summary>
- /// 获取参照标高Id
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- public static ElementId GetLevelId(this Element element)
- {
- return element.GetParameterElementId(BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM);
- }
- /// <summary>
- /// 参照标高
- /// </summary>
- /// <param name="element"></param>
- /// <param name="level"></param>
- public static void SetLevel(this Element element, Level level)
- {
- if (level != null)
- {
- element.SetLevel(level.Id);
- }
- }
- /// <summary>
- /// 设置参照标高参照标高
- /// </summary>
- /// <param name="element"></param>
- /// <param name="levelId"></param>
- public static void SetLevel(this Element element, ElementId levelId)
- {
- element.SetParameter(BuiltInParameter.INSTANCE_REFERENCE_LEVEL_PARAM, levelId);
- }
- }
- }
|