LearnRevitAPI
Mar 29, 2024
Suppress 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?
💡Let's Learn: How To Suppress Warnings with Revit API!
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
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.
💡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:
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:
💪 And that's how you suppress warnings with Revit API
Join Newsletter
📩 You will be added to Revit API Newsletter
Join Us!
which is already read by 7400+ people!
◼️BLACK FRIDAY DEAL 20% OFF. Ends in: