Files
ShrlAlgoToolkit/Sai.Toolkit.Revit/Helpers/Filters.cs
2024-09-22 11:05:41 +08:00

132 lines
3.4 KiB
C#

using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
namespace Sai.Toolkit.Revit.Helpers;
/// <summary>
/// 任意选择过滤器
/// </summary>
public class FuncFilter(Func<Element, bool> allowElement, Func<Reference, XYZ, bool> allowReference = null)
: ISelectionFilter
{
private readonly Func<Element, bool> elementFunc = allowElement;
private readonly Func<Reference, XYZ, bool> referenceFunc = allowReference;
public bool AllowElement(Element elem)
{
return elementFunc(elem);
}
public bool AllowReference(Reference reference, XYZ position)
{
return referenceFunc == null || referenceFunc(reference, position);
}
}
/// <summary>
/// 类型过滤
/// </summary>
/// <typeparam name="T"></typeparam>
public class GenericFilter<T> : ISelectionFilter
{
public bool AllowElement(Element elem)
{
return elem is T;
}
public bool AllowReference(Reference reference, XYZ position)
{
return true;
}
}
/// <summary>
/// 获取当前模型或链接模型的实体元素
/// </summary>
public class ElementInLinkOrCurrentDocument : ISelectionFilter
{
public ElementInLinkOrCurrentDocument(Document doc)
{
this.doc = doc;
}
private readonly Document doc;
public bool LastCheckedWasFromLink => null != LinkedDocument;
public Document LinkedDocument { get; private set; }
public bool AllowElement(Element e)
{
return true;
}
public bool AllowReference(Reference r, XYZ p)
{
LinkedDocument = null;
var e = doc.GetElement(r);
if (e is RevitLinkInstance li)
{
LinkedDocument = li.GetLinkDocument();
e = LinkedDocument.GetElement(r.LinkedElementId);
}
return e != null
&& e.CanHaveTypeAssigned()
&& e.HasPhases()
&& e.get_BoundingBox(null) != null
&& e.Category is { Parent: null }
&& e is not Panel;
}
}
public class DwgBlockSelection : ISelectionFilter
{
private Element e;
public bool AllowElement(Element elem)
{
e = elem;
return e.Document.GetElement(e.GetTypeId()) is CADLinkType;
}
public bool AllowReference(Reference reference, XYZ position)
{
//块
var instance = e.GetGeometryObjectFromReference(reference) as GeometryInstance;
if (instance == null)
{
return false;
}
////dwg
//foreach (var dwgIns in e.get_Geometry(new Options()))
//{
// if (dwgIns is not GeometryInstance item) continue;
// //遍历dwg包含的所有内容
// foreach (var obj in item.SymbolGeometry)
// {
// return obj is GeometryInstance ins && !ins.SymbolGeometry.Any();
// }
//}
//if (selectBlock == null)
//{
// return false;
//}
//块是否包含子块
//foreach (var item in instance.SymbolGeometry)
//{
// if (item is GeometryInstance)
// {
// return false;
// }
//}
//var isNest = selectBlock.SymbolGeometry.OfType<GeometryInstance>().Any();
return /*!isNest &&*/ reference.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_INSTANCE
&& instance.GraphicsStyleId != ElementId.InvalidElementId;
}
}