Failure Processing / Warnings Swallower only for specific files

Failure Processing / Warnings Swallower only for specific files

Anonymous
Not applicable
2,066 Views
5 Replies
Message 1 of 6

Failure Processing / Warnings Swallower only for specific files

Anonymous
Not applicable

Hey everyone!

 

I'm working on a plug-in that enables this process: the user opens an empty Revit file / a template > the user runs my plug-in > the plug-in opens a file in the background > the plug-in extracts some data from the document > the plug-in closes the document > the plug-in open the next document > and so on...

 

I've had some problems when running multiple files overnight cause the plug-in was trying to open one file, but that gave a warning (like 81 rooms not enclosed for example) and expected the user to simply click Ok and the whole night was wasted as the plug-in just waited for input.

 

Luckily I found these two resources: https://www.revitapidocs.com/2015/446796ca-d8f5-0f8a-7b82-a4ec6e5aa7a0.htm   and    https://thebuildingcoder.typepad.com/blog/2016/09/warning-swallower-and-roomedit3d-viewer-extension....

so I implemented this "warnings swallower" that solves this issue:

In App.cs

        public Result OnStartup(UIControlledApplication application)
        {
            // Load toolbox at startup
            AddPluginPanel(application);

            //Create an API application that subscribes to FailuresProcessing event for the lifetime of the session.
            application.ControlledApplication.FailuresProcessing += 
                new EventHandler<FailuresProcessingEventArgs>(ResolveOpeningWarnings);

            return Result.Succeeded;
        }

        private void ResolveOpeningWarnings(object sender, FailuresProcessingEventArgs e)
        {
            FailuresAccessor fa = e.GetFailuresAccessor();
            IList<FailureMessageAccessor> failList = fa.GetFailureMessages();
            foreach (FailureMessageAccessor failure in failList)
            {
                FailureSeverity fSeverity = failure.GetSeverity();

                if (fSeverity == FailureSeverity.Warning)
                {
                    fa.DeleteWarning(failure);
                }
                else
                {
                    fa.ResolveFailure(failure);
                }
            }
        }

which actually works a treat.

 

The real problem is this: Now everytime I'll open any file and I'll encounter a warning I won't be aware of it (I could write them to some text file for example, but it's not ideal). Same for all the people I'll give this plug-in to. And they'll get angry 😄

 

Is there a way to be selective with this swallower and apply it only to the files I'm opening in the background through that specific plugin? And not to the files that get opened by any Revit application that has that plug-in loaded?

I hope this actually makes sense.

 

Thanks,

 

Andrea

0 Likes
Accepted solutions (2)
2,067 Views
5 Replies
Replies (5)
Message 2 of 6

jeremytammik
Autodesk
Autodesk
Accepted solution

Just as you use += to subscribe to the event, you can use -= to unsubscribe again at any time.

 

Thus, you can turn on and off your warning swallower at will.

 

For instance, you can turn it on and off when specific documents are opened and closed.

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 6

Anonymous
Not applicable

So there's no way to "assign" the event to specific documents when the button's method is called? Instead of "assigning" it at StartUp?

0 Likes
Message 4 of 6

Anonymous
Not applicable
Accepted solution

@jeremytammik I tried something, which actually looks quite unorthodox to me and I don't know if it's the right thing to do...but it works!

I'll write it down here:

 

Instead of subscribing the event from the OnStartup method, I created a public static variable in the App class and I assign to it the UIControlledApplication to it in OnStartup.

class App : IExternalApplication
{
    public static UIControlledApplication app;

    public Result OnStartup(UIControlledApplication application)
    {
app = application;
// Load toolbox at startup AddPluginPanel(application); return Result.Succeeded; } }

Then, in the Execute method, if the user starts the process I subscribe the event to the application, and when the process finishes I unsubscribe the event in the way you showed in your comment:

        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
            Document doc = uidoc.Document;

            // Prompt user to set theaudit up
            Setup setup = new Setup();
            setup.ShowDialog();

            if (setup.started)
            {
                // Assign method to suppress opening warnings when audit process starts
                App.app.ControlledApplication.FailuresProcessing +=
                    new EventHandler<FailuresProcessingEventArgs>(ResolveOpeningWarnings);

                success = ProcessRun.Run(app);
            }
            else
            {
                Messages.ErrorMessage("User cancelled");

                // Unsubscribe method when it's done
                App.app.ControlledApplication.FailuresProcessing -=
                    new EventHandler<FailuresProcessingEventArgs>(ResolveOpeningWarnings);

                return Result.Cancelled;
            }
        }

I hope the above is understandable.

 

Is it bad?

 

Thanks,

 

Andrea

Message 5 of 6

jeremytammik
Autodesk
Autodesk

Dear Andrea,

 

Thank you for your update.

 

No, by no way bad, I think it is perfect.

 

Not in any way unorthodox at all.

 

Perfect solution, a very nice example of flexible  and optimised event handling.

 

Best regards,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 6 of 6

Anonymous
Not applicable

I might cry now 😄

 

Thanks for the help and the right direction Jeremy!