Hello
I create a plugin to send some data (like active view, mouse position,…) from Rhino to Grasshopper.
This work fine but the c# component always alive like when we use timer.
Is there a way to make the c# component run only when a new data received from Rhino?
Or maybe a way to receive data without importing the plugin?
Code in the plugin:
using Rhino.Display;
using Rhino.PlugIns;
namespace RhinoToGrasshopper
{
public class RhinoToGrasshopperPlugin : PlugIn
{
public RhinoToGrasshopperPlugin()
{
Instance = this;
RhinoView.SetActive += OnViewSetActive;
RhinoView.Modified += OnViewModified;
}
public override PlugInLoadTime LoadTime => PlugInLoadTime.AtStartup;
public static RhinoToGrasshopperPlugin Instance { get; private set; }
public static string viewname;
private static void OnViewSetActive(object sender, ViewEventArgs args)
{
viewname = args.View.ActiveViewport.Name;
}
private static void OnViewModified(object sender, ViewEventArgs args)
{
viewname = args.View.ActiveViewport.Name;
}
public static void ActiveView()
{
using (var args = new Rhino.Runtime.NamedParametersEventArgs())
{
args.Set("activeview_name", viewname);
Rhino.Runtime.HostUtils.ExecuteNamedCallback("RhinoActiveView", args);
}
}
}
}
Code in Grasshopper:
using RhinoToGrasshopper; // import RhinoToGrasshopper.rhp
-------------------------------------------------------------------------------
RhinoToGrasshopperPlugin.ActiveView(); // import this from the plugin to enable receiving data
Receive(Component);
Active_View_Name = str;
-------------------------------------------------------------------------------
string str = null;
bool registered = false;
IGH_Component comp = null;
void Receive(IGH_Component component)
{
if(!registered)
{
Rhino.Runtime.HostUtils.RegisterNamedCallback("RhinoActiveView", RiRhino);
comp = component;
registered = true;
}
}
void RiRhino(object sender, Rhino.Runtime.NamedParametersEventArgs args)
{
string values;
if (args.TryGetString("activeview_name", out values))
{
str = values;
}
comp.ExpireSolution(true);
}