Setting Revit EventHandler from RhinoInside

I’m trying to set a Revit DocumentOpened EventHandler from within a C# component in Grasshopper running Inside.Revit. I can set the EventHandler but nothing happens when a document is opened. I have tested the same code in a Revit PlugIn and it works. I’m not entirely sure of the way forward.

Hope someone can point me in the right direction, best Morten

Edit. So after rebooting Revit and all the stuff inside it, the code seems to work but nothing happens until you click the Grasshopper canvas, after which the component says this: image

Grasshopper C# component code:

using Autodesk.Revit;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.Events;

private void RunScript(bool Attach)
  {

    Autodesk.Revit.UI.UIControlledApplication app = RhinoInside.Revit.Revit.ApplicationUI;

    if(Attach && !set)
    {
      try{
        app.ControlledApplication.DocumentOpened += new EventHandler<Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(application_DocumentOpened);

        set = true;
        // Debug stuff
        Print(app.ControlledApplication.VersionBuild.ToString());
        Print("event set");
      }
      catch     {        Print("Something went wrong!?");      }
    }
    else    {      Print("Nothing was set just waiting");    }

    if(!Attach && set)
    {
      set = false;
      app.ControlledApplication.DocumentOpened -= application_DocumentOpened;
      Print("Event Reset");
    }
  }


public bool set {get; set;}

public void application_DocumentOpened(object sender, DocumentOpenedEventArgs args)
  {
    Grasshopper.Kernel.IGH_Param expireInput = Component.Params.Input[1];
    IGH_DocumentObject component = expireInput.Sources.First().Attributes.GetTopLevel.DocObject;
    component.ExpireSolution(true);
  }

It’s not cleared enough the task should be done. But in generally As you using Rhino-inside with Grasshopper, it’s better to use the event-handler provided Rhino-inside APIs instead of Revit APIs. They better programmed Rhino-inside APIs to deal and trigger all the apps into the environment.
As still lacking of clear manual to the Rhino-Inside APIs so far, you have to dig into the source code and find out the best event handler suits your demand.

1 Like

Yeah I was kind of thinking the same but RhinoInside.Revit doesn’t seem to contain any way to set EventHandlers, but thought there might be a way to get it to work. I’ll take a look at the source code for RhinoInside.Revit…

1 Like