2025-02-10 20:53:40 +08:00
|
|
|
|
using Autodesk.Revit.DB;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
using Autodesk.Revit.DB.Architecture;
|
|
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
namespace ShrlAlgoToolkit.Revit.Assists;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
public static class ElementCollectorExtensions
|
2024-09-22 11:05:41 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 多类别的元素集合
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="collector"></param>
|
|
|
|
|
|
/// <param name="categories"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static FilteredElementCollector OfCategories(this FilteredElementCollector collector, params BuiltInCategory[] categories) =>
|
|
|
|
|
|
collector.WherePasses(new ElementMulticategoryFilter(categories));
|
|
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
///// 基于视图的类别收集器
|
2024-09-22 11:05:41 +08:00
|
|
|
|
///// </summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
///// <param name="view">收集的视图</param>
|
|
|
|
|
|
///// <param name="builtInCategory">元素的类别</param>
|
|
|
|
|
|
///// <returns>过滤元素收集器</returns>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
//public static FilteredElementCollector OfCategory(this View view, BuiltInCategory builtInCategory) =>
|
|
|
|
|
|
// view.OfCollector().OfCategory(builtInCategory);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 类型的元素集合
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="doc"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static FilteredElementCollector OfClass<T>(this Document doc)
|
|
|
|
|
|
where T : Element => doc.OfCollector().OfClass(typeof(T));
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 基于视图的类型收集器
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// </summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// <typeparam name="T">元素的类</typeparam>
|
|
|
|
|
|
/// <param name="view">收集的视图</param>
|
|
|
|
|
|
/// <returns>过滤元素收集器</returns>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
public static FilteredElementCollector OfClass<T>(this View view)
|
|
|
|
|
|
where T : Element => view.OfCollector().OfClass(typeof(T));
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 多类型的元素集合
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="doc"></param>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// <param name="types">指定类型</param>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static FilteredElementCollector OfClasses(this Document doc, params Type[] types) =>
|
|
|
|
|
|
doc.OfCollector().WherePasses(new ElementMulticlassFilter(types));
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 基于视图的多类别收集器
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// </summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// <param name="view">收集的视图</param>
|
|
|
|
|
|
/// <param name="types">多个类别</param>
|
|
|
|
|
|
/// <returns>过滤元素收集器</returns>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
public static FilteredElementCollector OfClasses(this View view, params Type[] types) =>
|
|
|
|
|
|
view.OfCollector().WherePasses(new ElementMulticlassFilter(types));
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 视图元素收集器(临时隐藏也算不可见)
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// </summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// <param name="view">收集的视图</param>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static FilteredElementCollector OfCollector(this View view) =>
|
|
|
|
|
|
view == null
|
2025-02-10 20:53:40 +08:00
|
|
|
|
? throw new ArgumentNullException(nameof(view), "收集视图为空")
|
2024-09-22 11:05:41 +08:00
|
|
|
|
: FilteredElementCollector.IsViewValidForElementIteration(view.Document, view.Id)
|
|
|
|
|
|
? new FilteredElementCollector(view.Document, view.Id)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
: throw new ArgumentException($"视图{view.Id}不可应用收集器");
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 文档的元素收集器
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="document"></param>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// <returns>过滤元素收集器</returns>
|
|
|
|
|
|
public static FilteredElementCollector OfCollector(this Document document) => document == null ? throw new ArgumentNullException(nameof(document), "文档为空") : new(document);
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 基于视图的实例收集器
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// </summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// <param name="view">收集的视图</param>
|
|
|
|
|
|
/// <param name="builtInCategory">元素的类别</param>
|
|
|
|
|
|
/// <returns>过滤元素收集器</returns>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
public static FilteredElementCollector OfElementType(this FilteredElementCollector collector) =>
|
|
|
|
|
|
collector.WhereElementIsElementType();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 基于过滤器元素集合
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="collector"></param>
|
|
|
|
|
|
/// <param name="filter"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static FilteredElementCollector OfFilter(this FilteredElementCollector collector, ElementFilter filter) =>
|
|
|
|
|
|
collector.WherePasses(filter);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 多个过滤器的收集器
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// </summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// <param name="collector">收集器</param>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// <param name="filters"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static FilteredElementCollector OfFilters(
|
|
|
|
|
|
this FilteredElementCollector collector,
|
|
|
|
|
|
List<ElementFilter> filters,
|
|
|
|
|
|
bool logicalAnd = false
|
|
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
ElementLogicalFilter filter = logicalAnd ? new LogicalAndFilter(filters) : new LogicalOrFilter(filters);
|
|
|
|
|
|
collector.WherePasses(filter);
|
|
|
|
|
|
return collector;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 基于视图的实例收集器
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// </summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// <param name="collector">收集器</param>
|
|
|
|
|
|
/// <returns>过滤元素收集器</returns>
|
2024-12-22 10:26:12 +08:00
|
|
|
|
public static FilteredElementCollector OfInstances(this FilteredElementCollector collector) =>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
collector.WhereElementIsNotElementType();
|
2024-12-22 10:26:12 +08:00
|
|
|
|
public static FilteredElementCollector OfModelCollector(this Document doc)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (doc == null)
|
|
|
|
|
|
{
|
2025-02-10 20:53:40 +08:00
|
|
|
|
throw new ArgumentNullException(nameof(doc), "文档为空");
|
2024-12-22 10:26:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
var li = doc.OfCollector()
|
|
|
|
|
|
.WhereElementIsNotElementType()
|
|
|
|
|
|
.Where(
|
|
|
|
|
|
e => e is TopographySurface ||
|
|
|
|
|
|
e is DirectShape ||
|
|
|
|
|
|
(e.CanHaveTypeAssigned()
|
|
|
|
|
|
&& e.IsValidObject
|
|
|
|
|
|
&& e.HasPhases()
|
|
|
|
|
|
&& e.get_BoundingBox(null) != null
|
|
|
|
|
|
&& e.Category is { Parent: null, CategoryType: CategoryType.Model }
|
|
|
|
|
|
&& e is not Panel
|
|
|
|
|
|
&& e is not Mullion
|
|
|
|
|
|
&& e is not RevitLinkInstance)).Select(e => e.Id).ToList();
|
2025-02-10 20:53:40 +08:00
|
|
|
|
if (li.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
return new FilteredElementCollector(doc, li).WhereElementIsNotElementType();
|
2024-12-22 10:26:12 +08:00
|
|
|
|
}
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 过滤项目文件最顶层的模型元素
|
2024-09-22 11:05:41 +08:00
|
|
|
|
/// </summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// <returns>元素集合</returns>
|
2024-12-22 10:26:12 +08:00
|
|
|
|
public static IEnumerable<Element> OfParentModelElements(this Document doc) =>
|
|
|
|
|
|
doc.OfCollector()
|
2024-09-22 11:05:41 +08:00
|
|
|
|
.WhereElementIsNotElementType()
|
|
|
|
|
|
.Where(
|
|
|
|
|
|
e => e is TopographySurface ||
|
2024-12-22 10:26:12 +08:00
|
|
|
|
e is DirectShape ||
|
2024-09-22 11:05:41 +08:00
|
|
|
|
(e.CanHaveTypeAssigned()
|
|
|
|
|
|
&& e.IsValidObject
|
|
|
|
|
|
&& e.HasPhases()
|
|
|
|
|
|
&& e.get_BoundingBox(null) != null
|
2024-12-22 10:26:12 +08:00
|
|
|
|
&& e.Category is { CategoryType: CategoryType.Model, AllowsBoundParameters: true }
|
|
|
|
|
|
&& (e is HostObject || e.Category.Parent == null)
|
2024-09-22 11:05:41 +08:00
|
|
|
|
&& e is not Panel
|
|
|
|
|
|
&& e is not Mullion
|
|
|
|
|
|
&& e is not RevitLinkInstance)
|
|
|
|
|
|
);
|
2024-12-22 10:26:12 +08:00
|
|
|
|
/// <summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// 过滤项目文件的所有模型元素
|
2024-12-22 10:26:12 +08:00
|
|
|
|
/// </summary>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// <returns>元素集合</returns>
|
2024-12-22 10:26:12 +08:00
|
|
|
|
public static IEnumerable<Element> OfAllModelElements(this Document doc) =>
|
|
|
|
|
|
doc.OfCollector()
|
|
|
|
|
|
.WhereElementIsNotElementType()
|
|
|
|
|
|
.Where(
|
|
|
|
|
|
e => e is TopographySurface ||
|
|
|
|
|
|
e is DirectShape ||
|
|
|
|
|
|
(e.CanHaveTypeAssigned()
|
|
|
|
|
|
&& e.IsValidObject
|
|
|
|
|
|
&& e.HasPhases()
|
|
|
|
|
|
&& e.ViewSpecific == false
|
|
|
|
|
|
&& e.Category is { CategoryType: CategoryType.Model, AllowsBoundParameters: true }
|
|
|
|
|
|
&& e.get_BoundingBox(null) != null
|
|
|
|
|
|
&& e is not RevitLinkInstance)
|
|
|
|
|
|
);
|
2024-09-22 11:05:41 +08:00
|
|
|
|
}
|