using Autodesk.Revit.DB; using Autodesk.Revit.UI.Selection; namespace Sai.Toolkit.Revit.Helpers; /// /// 任意选择过滤器 /// public class FuncFilter(Func allowElement, Func allowReference = null) : ISelectionFilter { private readonly Func elementFunc = allowElement; private readonly Func referenceFunc = allowReference; public bool AllowElement(Element elem) { return elementFunc(elem); } public bool AllowReference(Reference reference, XYZ position) { return referenceFunc == null || referenceFunc(reference, position); } } /// /// 类型过滤 /// /// public class GenericFilter : ISelectionFilter { public bool AllowElement(Element elem) { return elem is T; } public bool AllowReference(Reference reference, XYZ position) { return true; } } /// /// 获取当前模型或链接模型的实体元素 /// 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().Any(); return /*!isNest &&*/ reference.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_INSTANCE && instance.GraphicsStyleId != ElementId.InvalidElementId; } }