Search code examples
c#wpfevent-handlingrevit-api

How do I raise an external event in the Revit API from a WPF app?


I have been trying to create a Revit plugin that allows the user to view issues that are stored on a remote server on the local version of the file. It should create a new 3D perspective view from the stored data on the server and open it in Revit. However, whenever I attempt to run it, I get an exception warning thusly:

Exception thrown: 'Autodesk.Revit.Exceptions.InvalidOperationException' in RevitAPIUI.dll
An unhandled exception of type 'Autodesk.Revit.Exceptions.InvalidOperationException' occurred in RevitAPIUI.dll
Attempting to create an ExternalEvent outside of a standard API execution

I think I understand vaguely what this means, but I am unsure what exactly needs to be changed to fix it. I am defining a custom ExternalEventHandler and implementing its Execute method:

class CameraEventHandler : IExternalEventHandler
    {
        Issue issue;
        int i;

        public CameraEventHandler(Issue issue, int index)
        {
            this.issue = issue;
            this.i = index;
        }

        public void Execute(UIApplication app)
        {
            Document doc = app.ActiveUIDocument.Document;
            using (Transaction t = new Transaction(doc, "CameraTransaction"))
            {
                t.Start();
                ...
                //Irrelevant code to set camera position programmatically
                ...
                t.Commit();
            }
        }

        public string GetName()
        {
            return "Camera event handler";
        }
    }

And then in one of my WPF forms, I create an ExternalEvent and calling the Raise method:

private void RevitViewButton_Click(object sender, RoutedEventArgs e)
        {
            CameraEventHandler handler = new CameraEventHandler(issue, issueIndex);
            ExternalEvent cameraEvent = ExternalEvent.Create(handler);
            cameraEvent.Raise();
        }

However, the exception is thrown when it reaches the ExternalEvent.Create method.

Edit: I feel it is worth mentioning that the WPF app I am using is launched as a Revit plugin.


Solution

  • Reading this blog it would appear to be a bug in Revit.

    The solution appears to be to create your custom handler during IExternalCommand.Execute or IExternalApplication.OnStartup, rather than at the time of raising the event.