Mar 29, 2024

Suppress Warnings in Revit API

Learn how to suppress warnings with Revit API when you make changes in the project and get some annoying warnings.

Revit Warnings

While warnings are very important to notify users that something is wrong, Revit Warnings can be just annoying...
Remember those times, when you set the same 'Mark' value and you get a warning where you have to click on OK button?  

Why Suppressing Warnings?

When we work in Revit, we often get annoying Warning messages. 

And while making an extra click might not seem like an issue, imagine if you create a script to change 100 elements in individual transactions, and each one will causes a warning...

Then you would have to click on all 100 warnings or Kill Revit process...

💡That's usually when people start asking:
"How To Suppress Warnings?" 

Create a Warning with Revit API

Before I can show you how to suppress a warning, we need to find a way to create a warning with Revit API.
So every time we click on a button we get a warning, then we can experiment and learn how to suppress it.

I think the easiest way to trigger a warning in Revit is to set the same values in Mark Parameter. Then you will get this warning:

Here is a simple snippet that will give us this warning

#⬇️ Imports
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ObjectType

#📦 Variables
uidoc = __revit__.ActiveUIDocument
doc   = __revit__.ActiveUIDocument.Document

#👉 Pick Walls
ref_picked_walls = uidoc.Selection.PickObjects(ObjectType.Element, WallFilter())
picked_walls     = [doc.GetElement(ref) for ref in ref_picked_walls]

#⚠️ Create a Warning
for wall in picked_walls:
    # 🔓 Start Transaction
    t = Transaction(doc, 'Update Mark')
    t.Start()

    #⚠️ Warning 1
    p_mark = wall.get_Parameter(BuiltInParameter.ALL_MODEL_MARK)
    p_mark.Set('Warning 2')

    t.Commit()

This script does the following:

👉 Prompts user to select Walls
🔓 Starts Transaction for making changes
⚠️Changes Mark Parameter to get a Warnin

💡P.S.
I always recommend to put Transaction outside of loops so your scripts execute much faster. However, in this example I want each wall to give me a warning while I'm in development stage! 
And don't select hundreds of walls!

Suppress All Warnings

Now we have a script that will give us multiple warnings when we execute and select walls. 
Now we can think of how can we can suppress these warnings. We will start by suppressing all Revit warnings, and later I will show you how to suppress specific ones only.

To suppress a warning:
You need to do 2 things:

1️⃣ Create a Warning Handler with IFailuresPreprocessor
2️⃣ Set Warning Handler to your transaction

Let's explore how to do these steps

1️⃣ Create a Warning Handler

Firstly, we need to create a class that will interact with warnings. WE have to use IFailuresPreprocessor Interface for building our Warning Suppressor Class. (Interface - is a blueprint for building a class that has special functionality and methods to be overriden.)

👇 Here is a Sample Code for this class.

#⚠️ Transaction Error Handler
class SupressWarnings(IFailuresPreprocessor):
    def PreprocessFailures(self, failuresAccessor):
        try:
            failures = failuresAccessor.GetFailureMessages()

            for fail in failures: #type: FailureMessageAccessor
                severity    = fail.GetSeverity()
                description = fail.GetDescriptionText()
                fail_id     = fail.GetFailureDefinitionId()

                if severity == FailureSeverity.Warning:

                    if fail_id == BuiltInFailures.GeneralFailures.DuplicateValue:
                        print('✅Suppressed Warning: {}'.format(description))
                        failuresAccessor.DeleteWarning(fail)
        except:
            import traceback
            print(traceback.format_exc())

        return FailureProcessingResult.Continue

💡I know it looks complicated, but you can literally copy this code and change nothing to suppress all warning.

But if you want a brief explanation of all the steps, here we go:

1️⃣ Create a Class and Inherit IFailuresPreprocessor functionality
2️⃣ Override PreprocessFailures method that has (self, failureAccessor) arguments.
3️⃣ Create try/except blocks for development to print error messages if they happen. Because otherwise they won't be shown. (Optional step)
4️⃣ Get Warnings with .GetFailureMEssages
5️⃣ Iterate through warnings and get:
- Severity
- Description
- FailureId
6️⃣ IF severity says Warning, then DeleteWarning 
7️⃣ We need to return FailureProcessingResult.Continue, or otherwise Revit might not let you accept the warning.

So this class is our warning handler that will just delete all Warnings and user won't even see them. But we need to configure Transaction to use our warning handler.

2️⃣ Set Warning Handler to a Transaction

Now let's go to our Transaction and set our custom warning handler. We have to write it between t.Start and t.Commit and it only takes 3 lines and they are self-explanatory:

1️⃣ fail_hand_opts = t.GetFailureHandlingOptions()
2️⃣ fail_hand_opts .SetFailuresPreprocessor(SupressWarnings())
3️⃣ t.SetFailureHandlingOptions(fail_hand_opts)

Here is updated code snippet:

#⚠️ Create a Warning
for wall in picked_walls:
    # 🔓 Start Transaction
    t = Transaction(doc, 'Update Mark')
    t.Start()

    #⚠️ Warning 1
    p_mark = wall.get_Parameter(BuiltInParameter.ALL_MODEL_MARK)
    p_mark.Set('Warning 2')

    #💡 Assign Error Handler
    fail_hand_opts = t.GetFailureHandlingOptions()
    fail_hand_opts.SetFailuresPreprocessor(SupressWarnings())
    t.SetFailureHandlingOptions(fail_hand_opts)

    t.Commit()

💡SuppressWarnings() - Is our warning handler class we made.

By using these 2 snippets, you will be able to suppress any warnings that happen during this specific Transaction!

Give it a Try!

Suppress Specific Warnings

It's not a good practice to suppress all warnings all the time, because we might suppress something we would want to see right away... So instead, let's look how to suppress specific warnings only.

We will need to use fail_id variable that we defined in our class and compare it to Revit API classes.
In my case, I want to suppress warnings for duplicate values and I need to use this property from GeneralFailures Class:
BuiltInFalures.GeneralFailures.DuplicateValue

Here is how to do it:

#⚠️ Transaction Warning Handler
class SupressWarnings(IFailuresPreprocessor):

    def PreprocessFailures(self, failuresAccessor):
        try:
            failures = failuresAccessor.GetFailureMessages()

            for fail in failures: #type: FailureMessageAccessor
                severity    = fail.GetSeverity()
                description = fail.GetDescriptionText()
                fail_id     = fail.GetFailureDefinitionId()

                if severity == FailureSeverity.Warning:
                    print('✅Suppressed Warning: {}'.format(description))
                    failuresAccessor.DeleteWarning(fail)

        except:
            import traceback
            print(traceback.format_exc())

        return FailureProcessingResult.Continue

💪 And that's how you suppress warnings with Revit API

Want to Learn More?

🔥 Amazing Revit API Resources!

Join Revit API

Newsletter Today!

Join Us!

which is already read by 4000+ people!

Get short Revit API Lessons and Tips directly in your mail!