Tagging elements in view

Hi All,

I have an issue that I cannot fully understand. I have a simple script to tag elements in view. When using it in an elevation for example, all elements visible or not are being tagged. When using the native Revit Tag All tool. This doesnt happen, it would be much appreciated if anyone has an idea how the Tag All tool filters this. Thank you

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import FilteredElementCollector, ViewType, IndependentTag, Reference,\
    Transaction, XYZ, BuiltInCategory, TagMode, TagOrientation, Outline, BoundingBoxIntersectsFilter
    
from pyrevit import forms
clr.AddReference('System')

def tag_element(doc, v, element, offset):
    try:
        loc = element.Location
        if loc:
            p = loc.Point if hasattr(loc, 'Point') else loc.Curve.GetEndPoint(0)
            tag = IndependentTag.Create(doc, v.Id, Reference(element), True, TagMode.TM_ADDBY_CATEGORY, TagOrientation.Horizontal, XYZ(p.X + offset, p.Y + offset, p.Z))
            return offset + 150
    except Exception:
        pass
    return offset

def get_elements(doc, category):
    return FilteredElementCollector(doc).OfCategory(category).WhereElementIsNotElementType().ToElements()

def tag_elements_in_view(doc, v, elements, offset):
    for element in elements:
        # Get the bounding box of the element
        bbox = element.get_BoundingBox(v)
        # Check if the element is visible in the view
        if bbox:
            outline = Outline(bbox.Min, bbox.Max)
            filter = BoundingBoxIntersectsFilter(outline)
            if filter.PassesFilter(element):
                offset = tag_element(doc, v, element, offset)

# Set Document
doc = __revit__.ActiveUIDocument.Document
views = forms.select_views()
if views:
    WINDOWS = get_elements(doc, BuiltInCategory.OST_Windows)
    WALLS = get_elements(doc, BuiltInCategory.OST_Walls)
    DOORS = get_elements(doc, BuiltInCategory.OST_Doors)
    # Start Transaction
    t = Transaction(doc, 'Tag Doors, Walls & Windows')

    # Call Functions
    for v in views:
        if v.IsTemplate == False and v.ViewType != ViewType.ThreeD:
            t.Start()
            offset = 0
            tag_elements_in_view(doc, v, WINDOWS, offset)
            tag_elements_in_view(doc, v, WALLS, offset)
            tag_elements_in_view(doc, v, DOORS, offset)

            # Complete Transaction
            t.Commit()

1 Like

Likely your filtered element collector should be the override method which includes a view input to get just the elements visible in the view, as opposed to all elements.

I use a similar concept that @jacob.small is referring to. Pull all elements in the active view, filter that list down to what I want tagged and then use the Tag.ByElementandLocation Revit node. Haven’t had any issues with it not tagging any elements.

1 Like