Get Warnings with RevitAPI

How to get Warnings and associated elements with Revit API + Python.

Tags:  Revit API
Sep 2022

Get and Sort Warnings by Type

Getting warning is not complicated, but it would make more sense to sort them by warnings types. Whenever you are going to work with warnings, you will want to focus on some types more than on others. So let's get them and sort in a dictionary.

code
from collections import defaultdict

all_warnings = defaultdict(list)

# Sort Warnings by Type
for w in doc.GetWarnings():
    description = w.GetDescriptionText()
    all_warnings[description].append(w)

Now we have a dictionary that has warning description as a Key and a list of all warnings as a value.
We might want to get elements associated with these warnings with the following snippet.

code
for warning_type in all_warnings:
    warnings = all_warnings[war_typ]

    for w in warnings:
        element_ids = list(w.GetFailingElements()) + list(w.GetAdditionalElements())
        elements = [doc.GetElement(e_id) for e_id in element_ids]

Verify Element

Lastly, before making changes to the set of associated elements, make sure that these elements are modifiable.
For example, if you would want to Hide or Isolate these elements, you might need to check if that would even be possible.

e.g. element.CanBeHidden(View)

⌨️ Happy Coding!

— Erik Frits