Unable to open WPF panel from OnLoad

I cannot open a WPF panel from the OnLoad function of the PlugIn class. The call to OpenPanel or OpenPanelAsSibling fails and returns false. However, when I do it from a command it works ok. Is there a way to open the panel automatically when the plugin is loaded?

namespace MyPlugIn
{
	public class MyPlugIn : Rhino.PlugIns.PlugIn
	{
		public MyPlugInPlugIn()
		{
			Instance = this;
		}

		//Gets the only instance of MyPlugIn
		public static MyPlugIn Instance
		{
			get;
			private set;
		}
		
		protected override Rhino.PlugIns.LoadReturnCode OnLoad(ref string errorMessage)
		{
			// Register and open WPF panel
			System.Type panel_type = typeof(PalettePanelHost);
			Rhino.UI.Panels.RegisterPanel(this, panel_type, "MyPanel", MyPlugIn.Properties.Resources.MyIcon);

			bool ok = Rhino.UI.Panels.OpenPanelAsSibling(PalettePanelHost.PanelId, Rhino.UI.PanelIds.Layers);
			if (!ok)
			{
				// The call always fails when called from OnLoad.
				// As a workaround, which I don't like at all, I have to call OpenPanel
				// from a command line command (i.e. "Initialize")
			}
			return Rhino.PlugIns.LoadReturnCode.Success;
		}
	}

	// Rhino framework requires a System.Windows.Forms.IWin32Window derived object for a panel
	[System.Runtime.InteropServices.Guid("AEAC7948-CF21-4FA2-8DD7-330DF7AD16B3")]
	public class PalettePanelHost : RhinoWindows.Controls.WpfElementHost
	{
		public PalettePanelHost()
			: base(new PalettePanel(), null)
		{
		}

		// Returns the ID of this panel
		public static System.Guid PanelId
		{
			get
			{
				return typeof(PalettePanelHost).GUID;
			}
		}
	}
	
}

Hi @jordi,

A couple of tips:

1.) If your plug-in panel is open when Rhino closes, Rhino will attempt to re-open the panel when it starts up again. Thus, you may not want to always open your panel when your plug-in loads.

2.) Keep in mind that the user will want to control the visibility of your panel.

3.) If you want force your panel to be displayed the first time your plug-in is run, then do something like this:

protected override LoadReturnCode OnLoad(ref string errorMessage)
{
  RhinoApp.Initialized += OnRhinoInitialized;
}

private void OnRhinoInitialized(object sender, System.EventArgs e)
{
  var run_once = Settings.GetBool("RunOnce", false);
  if (!run_once)
  {
    ShowMyPlugInPanel();
    Settings.SetBool("RunOnce", true);
  }
}

Does this help?

– Dale

1 Like

Hi Dale,

Yes it helped, thanks a lot, both for the technical solution and for your tips.

Jordi