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:
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);
}