BBox intersection Filter with RevitLinkInstance not working

bb_filter below in the code is not finding any intersection with RevitLinkInstances… When set to invert (True) I see them. Also if I change the Class to say walls in the filter it finds them, so the Outline is properly sized. I have also used the “tolerance” property both negative and positive with no results where a RevitLinkInstance is found. I am missing something fundamental here… Any help on the concept(s) I am missing would be apreciated.

The goal is to find all the Unit links in the 3D View and tag them. By the way I am struggleing with the regeneration within the transaction of every tag being procced. First things first, the BoundingBoxIntersectsFilter(), I need to understand. Note: I commented out the tag feature as it will take forever with the regenerations during testing and the xxx is just for checking the output.

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

from System.Collections.Generic import *

# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

# Define constant offsets in feet
OFFSET_HORIZONTAL_FEET = 10.0
OFFSET_VERTICAL_FEET = 20

xxx = []

# Get all levels in the current document that are not from a linked model
levels = FilteredElementCollector(doc).OfClass(Level)
levels_not_linked = []
for level in levels:
    if not level.Document.IsLinked:
        levels_not_linked.append(level)

# Get all floors on each level and find the one with the largest area
largest_floors = {}
for level in levels_not_linked:
    floors = FilteredElementCollector(doc).OfClass(Floor).WherePasses(ElementLevelFilter(level.Id)).ToElements()
    if floors:  # Check if there are any floors on the level
        max_area = 0
        largest_floor = None
        for floor in floors:
            area = floor.get_Parameter(BuiltInParameter.HOST_AREA_COMPUTED).AsDouble()  # Get area as a double
            if area > max_area:
                max_area = area
                largest_floor = floor
        largest_floors[level.Name] = largest_floor

#get ViewFamilyType for a 3D View
collector = FilteredElementCollector(doc)
viewTypeColl = collector.OfClass(ViewFamilyType)
for i in viewTypeColl:
    if i.ViewFamily == ViewFamily.ThreeDimensional:
        viewType = i
    else:
        continue

# "Start" the transaction
TransactionManager.Instance.EnsureInTransaction(doc)

for level_name, floor in largest_floors.items():
    # Set viewName to match the level name with a prefix of "Iso - "
    viewName = f"Iso - {level_name}"

    # Get the bounding box of the largest floor
    bbox_floor = floor.get_BoundingBox(doc.ActiveView)

    # Get the thickness parameter of the largest floor
    largest_floor_thickness = floor.get_Parameter(BuiltInParameter.FLOOR_ATTR_THICKNESS_PARAM)
    if largest_floor_thickness:
        thickness_value = largest_floor_thickness.AsDouble()
        adjusted_thickness = thickness_value - 0.333

    # Define bounding box enclosing the largest floor with offset and adjusted thickness
    bbox = BoundingBoxXYZ()
    bbox.Min = XYZ((bbox_floor.Min.X - OFFSET_HORIZONTAL_FEET), (bbox_floor.Min.Y - OFFSET_HORIZONTAL_FEET), (bbox_floor.Min.Z + adjusted_thickness))
    bbox.Max = XYZ((bbox_floor.Max.X + OFFSET_HORIZONTAL_FEET), (bbox_floor.Max.Y + OFFSET_HORIZONTAL_FEET), bbox_floor.Max.Z + OFFSET_VERTICAL_FEET)
    
    # Convert the bounding box to an outline
    outline = Outline(bbox.Min, bbox.Max)

    # Create 3D View
    view = View3D.CreateIsometric(doc, viewType.Id)
    view.Name = viewName
    
    # Ensure that the Section Box is active
    view.IsSectionBoxActive = True
    
    # Update the section box to match the size defined in the bbox herein
    view.SetSectionBox(bbox)
    
    # Lock the view (bottom center of Revit screen house with padlock icon)
    #view.Pinned = True
    
    # Locks the view and saves its orientation
    view.SaveOrientationAndLock()
    
    # Get all link instances in the document
    collectorOfRVTLinks = FilteredElementCollector(doc).OfClass(RevitLinkInstance)

    # Create a filter for bounding box intersection
    bb_filter = BoundingBoxIntersectsFilter(outline, 0.0, False)
    
    # Apply the filter to the collector
    links_in_bb = collectorOfRVTLinks.WherePasses(bb_filter).ToElements()
    xxx.append(links_in_bb)
    
    # Filter links by name
    links_with_U = [link for link in collectorOfRVTLinks if '-U' in link.Name]
    xxx.append(links_with_U)
    
    for e in links_with_U:
        r = Reference(e)
        v = view
        m = TagMode.TM_ADDBY_CATEGORY
        o = TagOrientation.Horizontal
        l = e.GetTransform().Origin
        #tag = IndependentTag.Create(doc, v.Id, r, False, m, o, l)

# "End" the transaction
TransactionManager.Instance.TransactionTaskDone()

# Assign your output to the OUT variable
OUT =xxx

In the Revit API in RevitLinkInstance Members > Properties there is a BoundingBox for which can be intersected (I think)

Just tried BoundingBoxIsInsideFilter, with no results.

# Create a filter for bounding box intersection
bb_filter_inside = BoundingBoxIsInsideFilter(outline)

Below is a simple version of test files. I was running this on a project and it did not work so I created a simple test case and it still did not work. If you wish to test it, see the attached Dynamo Graph, and Revit project files (v23)

ApartmentBuilding.rvt (5.2 MB)
ApartmentUnit.rvt (5.2 MB)
sectionbox5.dyn (7.7 KB)

Could the issue be that the BoundingBoxIntersectsFilter uses the bounding box of an element in the context of the ACTIVE VIEW when the filter is applied, and the view is not active as it is created in the transaction?

Going to try ElementIntersectsElementFilter next…

@DarickBrokaw ,

can you also use BIMorph packages … i think you will find a way…

KR

Andreas

@andreas.draxl Thank you. However, while this gets the result in revit it will not assist me in my growth of learning to code. I use Python not to forgo the amazing nodes but to learn these gritty details that I am currently stuck in, hoping to make the leap to C# and more one day. Thanks again for the vote of confidence @Draxl_Andreas

hi,

a workaround, checking if link’s elements intersect with “local” floors

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

from System.Collections.Generic import *

# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

xxx = []

# Get all link instances in the document
collectorOfRVTLinks = FilteredElementCollector(doc).OfClass(RevitLinkInstance)

# Get all levels in the current document that are not from a linked model
levels = FilteredElementCollector(doc).OfClass(Level)
levels_not_linked = []
for level in levels:
    if not level.Document.IsLinked:
        floors = FilteredElementCollector(doc).OfClass(Floor).WherePasses(ElementLevelFilter(level.Id)).ToElements()
        if floors:
            largest_floor = max(floors, key = lambda x : x.get_Parameter(BuiltInParameter.HOST_AREA_COMPUTED).AsDouble())
            bbx_floor = largest_floor.get_BoundingBox(None)
            #
            for rvt_link_instance in collectorOfRVTLinks:
                doclnk = rvt_link_instance.GetLinkDocument()
                tf_link = rvt_link_instance.GetTotalTransform()
                tf_reverse = tf_link.Inverse 
                #
                myOutLn  = Outline(tf_reverse.OfPoint(bbx_floor.Min), tf_reverse.OfPoint(bbx_floor.Max))   
                #create LogicalOrFilter with BoundingBox 
                filterBbxInside = BoundingBoxIsInsideFilter(myOutLn)
                filterBbxInters = BoundingBoxIntersectsFilter(myOutLn)
                filterBbx = LogicalOrFilter(filterBbxInside, filterBbxInters)
                # find link elements intersect with this filter
                link_elems = FilteredElementCollector(doclnk).OfCategory(BuiltInCategory.OST_Floors).WherePasses(filterBbxInters).WhereElementIsNotElementType().ToElements()
                print(link_elems)
                if link_elems:
                    xxx.append([level, largest_floor, rvt_link_instance])

OUT = xxx

@c.poupin Thank you, I will review yourpost soon and respond tomorrow. Thanks for redirect to floors.