LevelExtend.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Autodesk.Revit.DB;
  4. using SAGA.DotNetUtils.Extend;
  5. using SAGA.RevitUtils.Extends;
  6. namespace TSZ.RevitBaseDll.Extends
  7. {
  8. public static class LevelExtend
  9. {
  10. /// <summary>
  11. ///由低到高排序
  12. /// </summary>
  13. /// <param name="level1"></param>
  14. /// <param name="level2"></param>
  15. /// <returns></returns>
  16. public static int CompareTo(this Level level1, Level level2)
  17. {
  18. if (level1.Elevation.IsEqual(level2.Elevation))
  19. {
  20. return 0;
  21. }
  22. return (level1.Elevation > level2.Elevation) ? 1 : -1;
  23. }
  24. /// <summary>
  25. /// 根据Z坐标,获得标高及偏移.标高列表需预先从低到高排好序且不能为空.
  26. /// </summary>
  27. /// <param name="listLevel"></param>
  28. /// <param name="dZ"></param>
  29. /// <param name="dOffset"></param>
  30. /// <returns></returns>
  31. public static Level GetLevel(this List<Level> listLevel, double dZ, out double dOffset)
  32. {
  33. var lRtn = listLevel[0];
  34. for (var i = listLevel.Count - 1; i > 0; i--)
  35. {
  36. var level = listLevel[i];
  37. if (dZ.IsThanEq(level.Elevation))
  38. {
  39. lRtn = level;
  40. break;
  41. }
  42. }
  43. dOffset = dZ - lRtn.Elevation;
  44. return lRtn;
  45. }
  46. /// <summary>
  47. /// z所之间的两个标高ljy
  48. /// 当在标高上时,则两标高相同
  49. /// </summary>
  50. /// <param name="listLevel"></param>
  51. /// <param name="dZ"></param>
  52. /// <param name="top"></param>
  53. /// <returns></returns>
  54. public static Level GetLevel(this List<Level> listLevel, double dZ, out Level top)
  55. {
  56. Level bottom = null;
  57. top = null;
  58. double dOffset = 0;
  59. bottom = listLevel.GetLevel(dZ, out dOffset);
  60. var intBottom = listLevel.IndexOf(bottom);
  61. if (intBottom < listLevel.Count - 1)
  62. {
  63. top = listLevel[intBottom + 1];
  64. }
  65. else
  66. {
  67. top = bottom;
  68. }
  69. return bottom;
  70. }
  71. /// <summary>
  72. /// 返回与Z最近的标高ljy
  73. /// </summary>
  74. /// <param name="listLevel"></param>
  75. /// <param name="dZ"></param>
  76. /// <param name="dOffset"></param>
  77. /// <returns></returns>
  78. public static Level GetNearLevel(this List<Level> listLevel, double dZ, out double dOffset)
  79. {
  80. Level level = null;
  81. Level top = null;
  82. var bottom = listLevel.GetLevel(dZ, out top);
  83. var dLevel = dZ.NearValue(top.Elevation, bottom.Elevation);
  84. if (dLevel.IsEqual(top.Elevation))
  85. {
  86. level = top;
  87. }
  88. else
  89. {
  90. level = bottom;
  91. }
  92. dOffset = dZ - level.Elevation;
  93. return level;
  94. }
  95. /// <summary>
  96. /// 名字取标高ljy
  97. /// </summary>
  98. /// <param name="doc"></param>
  99. /// <param name="strLevelName"></param>
  100. /// <returns></returns>
  101. public static Level GetLevel(this Document doc, string strLevelName)
  102. {
  103. List<Level> mLevels = doc.GetLevels();
  104. return mLevels.FirstOrDefault(leve => leve.Name == strLevelName);
  105. }
  106. /// <summary>
  107. /// id取标高ljy
  108. /// </summary>
  109. /// <param name="doc"></param>
  110. /// <param name="id"></param>
  111. /// <returns></returns>
  112. public static Level GetLevel(this Document doc, ElementId id)
  113. {
  114. return doc.GetElement(id) as Level;
  115. }
  116. /// <summary>
  117. /// 构件的标高
  118. /// </summary>
  119. /// <param name="element"></param>
  120. /// <returns></returns>
  121. public static Level GetLevel(this Element element)
  122. {
  123. var result = (element.Document.GetElement(element.LevelId) as Level);
  124. //2015-9-29 th 增加获取板标高
  125. if (result == null)
  126. result = element.GetRefLevel();
  127. //2015-9-29 th 增加获取屋顶标高
  128. if (result == null)
  129. result = element.GetParameterElement(BuiltInParameter.SCHEDULE_LEVEL_PARAM) as Level;
  130. //2016-1-12 mxg 增加MEPCurve标高
  131. if (result == null)
  132. result = element.GetParameterElement(BuiltInParameter.RBS_START_LEVEL_PARAM) as Level;
  133. if (result == null && element is FamilyInstance)
  134. {
  135. //2015-11-12 wzc 常规模型标高
  136. var id = element.GetParameterElementId(BuiltInParameter.FAMILY_LEVEL_PARAM);
  137. result = (element.Document.GetElement(id) as Level);
  138. }
  139. if (result == null)
  140. {
  141. //2016-1-7 th 增加连接模型梁标高获取
  142. result = element.GetRefLevel();
  143. }
  144. return result;
  145. }
  146. /// <summary>
  147. /// 所有标高(从低到高)ljy
  148. /// </summary>
  149. /// <param name="doc"></param>
  150. /// <returns></returns>
  151. public static List<Level> GetLevels(this Document doc)
  152. {
  153. List<Level> mLevels = null;
  154. mLevels = doc.FilterElements<Level>();
  155. return mLevels.OrderBy(p => p.Elevation).ToList();
  156. }
  157. /// <summary>
  158. /// 所有标高z从大到小ljy
  159. /// </summary>
  160. /// <param name="doc"></param>
  161. /// <returns></returns>
  162. public static List<Level> GetLevels2(this Document doc)
  163. {
  164. List<Level> listLevel = doc.GetLevels();
  165. listLevel.Reverse();
  166. return listLevel;
  167. }
  168. /// <summary>
  169. /// 标高类型ljy
  170. /// </summary>
  171. /// <param name="doc"></param>
  172. /// <returns></returns>
  173. public static List<LevelType> GetLevelTypes(this Document doc)
  174. {
  175. return doc.FilterElements<LevelType>();
  176. }
  177. /// <summary>
  178. /// 标高类型ljy
  179. /// </summary>
  180. /// <param name="doc"></param>
  181. /// <param name="strName"></param>
  182. /// <returns></returns>
  183. public static LevelType GetLevelType(this Document doc, string strName)
  184. {
  185. LevelType levelType = null;
  186. List<LevelType> listLevelType = doc.GetLevelTypes();
  187. var intIndex = listLevelType.FindIndex(p => p.Name == strName);
  188. if (intIndex > -1)
  189. {
  190. levelType = listLevelType[intIndex];
  191. }
  192. return levelType;
  193. }
  194. /// <summary>
  195. /// 判断标高是否被使用
  196. /// </summary>
  197. /// <param name="level"></param>
  198. /// <returns></returns>
  199. public static bool IsUsed(this Level level)
  200. {
  201. var allList = level.Document.GetAllElements();
  202. allList.RemoveAll(p => p.GetLevel() == null);
  203. //柱有上下两个标高,特殊判断 2017-2-28 th
  204. var colList = allList.FindAll(p => p.IsStColumn() || p.IsAcColumn());
  205. if (colList.Count > 0)
  206. {
  207. foreach (Element item in colList)
  208. {
  209. FamilyInstance fi = item as FamilyInstance;
  210. if (fi == null)
  211. continue;
  212. Level topLevel = fi.GetTopLevel();
  213. if (topLevel != null && topLevel.IsEqual(level))
  214. return true;
  215. Level baseLevel = fi.GetBaseLevel();
  216. if (baseLevel != null && baseLevel.IsEqual(level))
  217. return true;
  218. }
  219. }
  220. return allList.Exists(p => p.GetLevel().Id.IsEqual(level.Id));
  221. }
  222. /// <summary>
  223. /// 返回距离dz最近上下两层标高
  224. /// Create by zhs 2017-10-26
  225. /// </summary>
  226. /// <param name="levels"></param>
  227. /// <param name="dz"></param>
  228. /// <param name="topLevel"></param>
  229. /// <param name="downLevel"></param>
  230. public static void GetTopDownLevel(this List<Level> levels, double dz, out Level topLevel, out Level downLevel)
  231. {
  232. double offset;
  233. topLevel = null;
  234. downLevel = null;
  235. Level l = levels.GetNearLevel(dz, out offset);
  236. if (l != null)
  237. {
  238. int i = levels.IndexOf(l);
  239. if (dz.IsLessEq(l.Elevation))
  240. {
  241. topLevel = l;
  242. if(i>0)
  243. downLevel = levels[i - 1];
  244. else
  245. downLevel = l;
  246. }
  247. if (dz.IsThan(l.Elevation))
  248. {
  249. downLevel = l;
  250. if (i < levels.Count-1)
  251. topLevel = levels[i + 1];
  252. else
  253. topLevel = l;
  254. }
  255. }
  256. }
  257. /// <summary>
  258. /// 根据返回最大最小值之间的所有标高(level需已排序)
  259. /// Create by zhs 2017-11-29
  260. /// </summary>
  261. /// <param name="levels"></param>
  262. /// <param name="minElv">最小位置z</param>
  263. /// <param name="maxElv">最大位置z</param>
  264. /// <returns></returns>
  265. public static List<Level> GetMiddleLevels(this List<Level> levels,double minElv,double maxElv)
  266. {
  267. List<Level> result = new List<Level>();
  268. int i1 = levels.FindIndex(p => p.Elevation.IsThanEq(minElv));
  269. int i2 = levels.FindLastIndex(p => p.Elevation.IsLessEq(maxElv));
  270. if (i1 >= 0 && i2 < levels.Count && i1 <= i2)
  271. {
  272. for (int i = i1; i <= i2; i++)
  273. {
  274. result.Add(levels[i]);
  275. }
  276. }
  277. return result;
  278. }
  279. }
  280. }