Skip to content

Instantly share code, notes, and snippets.

@ricaun
Last active May 3, 2023 12:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricaun/14ec0730e7efb3cc737f2134475e2539 to your computer and use it in GitHub Desktop.
Save ricaun/14ec0730e7efb3cc737f2134475e2539 to your computer and use it in GitHub Desktop.
Revit Api Filter for BoundingBox element in a View
using Autodesk.Revit.DB;
namespace RevitAddin
{
public class BoundingBoxViewIntersectsFilter
{
private readonly Outline outline;
private readonly View view;
private readonly bool inverted = false;
public double Tolerance { get; set; } = 0.0;
public Outline GetBoundingBox() => outline;
public BoundingBoxViewIntersectsFilter(Outline outline, View view)
{
this.outline = outline;
this.view = view;
}
public BoundingBoxViewIntersectsFilter(Outline outline, View view, double tolerance)
{
this.outline = outline;
this.view = view;
this.Tolerance = tolerance;
}
public BoundingBoxViewIntersectsFilter(Outline outline, View view, bool inverted)
{
this.outline = outline;
this.view = view;
this.inverted = inverted;
}
public BoundingBoxViewIntersectsFilter(Outline outline, View view, double tolerance, bool inverted)
{
this.outline = outline;
this.view = view;
this.Tolerance = tolerance;
this.inverted = inverted;
}
public bool PassesFilter(Document document, ElementId id)
{
return PassesFilter(document.GetElement(id));
}
public bool PassesFilter(Element element)
{
var box = element.get_BoundingBox(view);
if (box == null) return inverted;
return outline.Intersects(new Outline(box.Min, box.Max), Tolerance) != inverted;
}
}
}
using Autodesk.Revit.DB;
namespace RevitAddin
{
public class BoundingBoxViewIsInsideFilter
{
private readonly Outline outline;
private readonly View view;
private readonly bool inverted = false;
public double Tolerance { get; set; } = 0.0;
public Outline GetBoundingBox() => outline;
public BoundingBoxViewIsInsideFilter(Outline outline, View view)
{
this.outline = outline;
this.view = view;
}
public BoundingBoxViewIsInsideFilter(Outline outline, View view, double tolerance)
{
this.outline = outline;
this.view = view;
this.Tolerance = tolerance;
}
public BoundingBoxViewIsInsideFilter(Outline outline, View view, bool inverted)
{
this.outline = outline;
this.view = view;
this.inverted = inverted;
}
public BoundingBoxViewIsInsideFilter(Outline outline, View view, double tolerance, bool inverted)
{
this.outline = outline;
this.view = view;
this.Tolerance = tolerance;
this.inverted = inverted;
}
public bool PassesFilter(Document document, ElementId id)
{
return PassesFilter(document.GetElement(id));
}
public bool PassesFilter(Element element)
{
var box = element.get_BoundingBox(view);
if (box == null) return inverted;
return outline.ContainsOtherOutline(new Outline(box.Min, box.Max), Tolerance) != inverted;
}
}
}
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using System.Linq;
namespace RevitAddin.Commands
{
[Transaction(TransactionMode.Manual)]
public class CommandWireIsInside : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document document = uidoc.Document;
View view = uidoc.ActiveView;
var wires = new FilteredElementCollector(document)
.OfCategory(BuiltInCategory.OST_Wire)
.WhereElementIsNotElementType()
.OfType<Wire>()
.ToList();
System.Console.WriteLine($"Wires: {wires.Count}");
var tolerance = 1e3;
var viewBox = view.CropBox;
var outline = new Outline(viewBox.Min, viewBox.Max);
var boundingBoxFilter = new BoundingBoxViewIsInsideFilter(outline, view, tolerance);
var wiresBox = wires
.Where(boundingBoxFilter.PassesFilter)
.ToList();
System.Console.WriteLine($"WiresBox: {wiresBox.Count}");
TaskDialog.Show($"Wires: {wires.Count}", $"WiresBox: {wiresBox.Count}");
return Result.Succeeded;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment