Last active
May 3, 2023 12:09
-
-
Save ricaun/14ec0730e7efb3cc737f2134475e2539 to your computer and use it in GitHub Desktop.
Revit Api Filter for BoundingBox element in a View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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