123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- using System.Collections.Generic;
- using System.Linq;
- using Autodesk.Revit.DB;
- using SAGA.DotNetUtils.Extend;
- using SAGA.RevitUtils.Extends;
- namespace TSZ.RevitBaseDll.Extends
- {
- public static class LevelExtend
- {
- /// <summary>
- ///由低到高排序
- /// </summary>
- /// <param name="level1"></param>
- /// <param name="level2"></param>
- /// <returns></returns>
- public static int CompareTo(this Level level1, Level level2)
- {
- if (level1.Elevation.IsEqual(level2.Elevation))
- {
- return 0;
- }
- return (level1.Elevation > level2.Elevation) ? 1 : -1;
- }
- /// <summary>
- /// 根据Z坐标,获得标高及偏移.标高列表需预先从低到高排好序且不能为空.
- /// </summary>
- /// <param name="listLevel"></param>
- /// <param name="dZ"></param>
- /// <param name="dOffset"></param>
- /// <returns></returns>
- public static Level GetLevel(this List<Level> listLevel, double dZ, out double dOffset)
- {
- var lRtn = listLevel[0];
- for (var i = listLevel.Count - 1; i > 0; i--)
- {
- var level = listLevel[i];
- if (dZ.IsThanEq(level.Elevation))
- {
- lRtn = level;
- break;
- }
- }
- dOffset = dZ - lRtn.Elevation;
- return lRtn;
- }
- /// <summary>
- /// z所之间的两个标高ljy
- /// 当在标高上时,则两标高相同
- /// </summary>
- /// <param name="listLevel"></param>
- /// <param name="dZ"></param>
- /// <param name="top"></param>
- /// <returns></returns>
- public static Level GetLevel(this List<Level> listLevel, double dZ, out Level top)
- {
- Level bottom = null;
- top = null;
- double dOffset = 0;
- bottom = listLevel.GetLevel(dZ, out dOffset);
- var intBottom = listLevel.IndexOf(bottom);
- if (intBottom < listLevel.Count - 1)
- {
- top = listLevel[intBottom + 1];
- }
- else
- {
- top = bottom;
- }
- return bottom;
- }
- /// <summary>
- /// 返回与Z最近的标高ljy
- /// </summary>
- /// <param name="listLevel"></param>
- /// <param name="dZ"></param>
- /// <param name="dOffset"></param>
- /// <returns></returns>
- public static Level GetNearLevel(this List<Level> listLevel, double dZ, out double dOffset)
- {
- Level level = null;
- Level top = null;
- var bottom = listLevel.GetLevel(dZ, out top);
- var dLevel = dZ.NearValue(top.Elevation, bottom.Elevation);
- if (dLevel.IsEqual(top.Elevation))
- {
- level = top;
- }
- else
- {
- level = bottom;
- }
- dOffset = dZ - level.Elevation;
- return level;
- }
- /// <summary>
- /// 名字取标高ljy
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="strLevelName"></param>
- /// <returns></returns>
- public static Level GetLevel(this Document doc, string strLevelName)
- {
- List<Level> mLevels = doc.GetLevels();
- return mLevels.FirstOrDefault(leve => leve.Name == strLevelName);
- }
- /// <summary>
- /// id取标高ljy
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="id"></param>
- /// <returns></returns>
- public static Level GetLevel(this Document doc, ElementId id)
- {
- return doc.GetElement(id) as Level;
- }
- /// <summary>
- /// 构件的标高
- /// </summary>
- /// <param name="element"></param>
- /// <returns></returns>
- public static Level GetLevel(this Element element)
- {
- var result = (element.Document.GetElement(element.LevelId) as Level);
- //2015-9-29 th 增加获取板标高
- if (result == null)
- result = element.GetRefLevel();
- //2015-9-29 th 增加获取屋顶标高
- if (result == null)
- result = element.GetParameterElement(BuiltInParameter.SCHEDULE_LEVEL_PARAM) as Level;
- //2016-1-12 mxg 增加MEPCurve标高
- if (result == null)
- result = element.GetParameterElement(BuiltInParameter.RBS_START_LEVEL_PARAM) as Level;
- if (result == null && element is FamilyInstance)
- {
- //2015-11-12 wzc 常规模型标高
- var id = element.GetParameterElementId(BuiltInParameter.FAMILY_LEVEL_PARAM);
- result = (element.Document.GetElement(id) as Level);
- }
- if (result == null)
- {
- //2016-1-7 th 增加连接模型梁标高获取
- result = element.GetRefLevel();
- }
- return result;
- }
- /// <summary>
- /// 所有标高(从低到高)ljy
- /// </summary>
- /// <param name="doc"></param>
- /// <returns></returns>
- public static List<Level> GetLevels(this Document doc)
- {
- List<Level> mLevels = null;
- mLevels = doc.FilterElements<Level>();
- return mLevels.OrderBy(p => p.Elevation).ToList();
- }
- /// <summary>
- /// 所有标高z从大到小ljy
- /// </summary>
- /// <param name="doc"></param>
- /// <returns></returns>
- public static List<Level> GetLevels2(this Document doc)
- {
- List<Level> listLevel = doc.GetLevels();
- listLevel.Reverse();
- return listLevel;
- }
- /// <summary>
- /// 标高类型ljy
- /// </summary>
- /// <param name="doc"></param>
- /// <returns></returns>
- public static List<LevelType> GetLevelTypes(this Document doc)
- {
- return doc.FilterElements<LevelType>();
- }
- /// <summary>
- /// 标高类型ljy
- /// </summary>
- /// <param name="doc"></param>
- /// <param name="strName"></param>
- /// <returns></returns>
- public static LevelType GetLevelType(this Document doc, string strName)
- {
- LevelType levelType = null;
- List<LevelType> listLevelType = doc.GetLevelTypes();
- var intIndex = listLevelType.FindIndex(p => p.Name == strName);
- if (intIndex > -1)
- {
- levelType = listLevelType[intIndex];
- }
- return levelType;
- }
- /// <summary>
- /// 判断标高是否被使用
- /// </summary>
- /// <param name="level"></param>
- /// <returns></returns>
- public static bool IsUsed(this Level level)
- {
- var allList = level.Document.GetAllElements();
- allList.RemoveAll(p => p.GetLevel() == null);
- //柱有上下两个标高,特殊判断 2017-2-28 th
- var colList = allList.FindAll(p => p.IsStColumn() || p.IsAcColumn());
- if (colList.Count > 0)
- {
- foreach (Element item in colList)
- {
- FamilyInstance fi = item as FamilyInstance;
- if (fi == null)
- continue;
- Level topLevel = fi.GetTopLevel();
- if (topLevel != null && topLevel.IsEqual(level))
- return true;
- Level baseLevel = fi.GetBaseLevel();
- if (baseLevel != null && baseLevel.IsEqual(level))
- return true;
- }
- }
- return allList.Exists(p => p.GetLevel().Id.IsEqual(level.Id));
- }
- /// <summary>
- /// 返回距离dz最近上下两层标高
- /// Create by zhs 2017-10-26
- /// </summary>
- /// <param name="levels"></param>
- /// <param name="dz"></param>
- /// <param name="topLevel"></param>
- /// <param name="downLevel"></param>
- public static void GetTopDownLevel(this List<Level> levels, double dz, out Level topLevel, out Level downLevel)
- {
- double offset;
- topLevel = null;
- downLevel = null;
- Level l = levels.GetNearLevel(dz, out offset);
- if (l != null)
- {
- int i = levels.IndexOf(l);
- if (dz.IsLessEq(l.Elevation))
- {
- topLevel = l;
- if(i>0)
- downLevel = levels[i - 1];
- else
- downLevel = l;
- }
- if (dz.IsThan(l.Elevation))
- {
- downLevel = l;
- if (i < levels.Count-1)
- topLevel = levels[i + 1];
- else
- topLevel = l;
- }
- }
- }
- /// <summary>
- /// 根据返回最大最小值之间的所有标高(level需已排序)
- /// Create by zhs 2017-11-29
- /// </summary>
- /// <param name="levels"></param>
- /// <param name="minElv">最小位置z</param>
- /// <param name="maxElv">最大位置z</param>
- /// <returns></returns>
- public static List<Level> GetMiddleLevels(this List<Level> levels,double minElv,double maxElv)
- {
- List<Level> result = new List<Level>();
- int i1 = levels.FindIndex(p => p.Elevation.IsThanEq(minElv));
- int i2 = levels.FindLastIndex(p => p.Elevation.IsLessEq(maxElv));
- if (i1 >= 0 && i2 < levels.Count && i1 <= i2)
- {
- for (int i = i1; i <= i2; i++)
- {
- result.Add(levels[i]);
- }
- }
- return result;
- }
- }
- }
|