Plug-in listen when file is opened

Is it possible to have a plug-in that listens when the user opens a new file and executes code automatically based on the file opened? How can it be done in c# in Rhino for Windows?
Thanks,
Victor

There are a number of events on RhinoDoc, that you can subscribe to, including:

RhinoDoc.BeginOpenDocument
RhinoDoc.EndOpenDocument

The samples in the developer samples repo on GitHub demonstrates these.

– Dale

Dale,

I have the following in a UserControl, but I get an empty document path.

    private void OnLoad(object sender, EventArgs e) {
        RhinoApp.Idle += OnIdle;
    }
    private void OnIdle(object sender, EventArgs e) {
        Rhino.RhinoDoc.EndOpenDocument += (s, ee) => {
            Rhino.RhinoApp.WriteLine("TEST");
            Rhino.RhinoApp.WriteLine(RhinoDoc.ActiveDoc.Path);
        };
    }

If I use EndSaveDocument it does work, but that is not what I need, I need the document’s path as soon as a new file is opened. I don’t know what I’m doing wrong. Any ideas? Perhaps I’m not subscribing the event in the right place?

Thanks,

Victor

Hi @Goodriver,

I guess I was thinking of something more like this:

/// <summary>
/// This is the user control that is embedded in our tabbed, docking panel
/// </summary>
[System.Runtime.InteropServices.Guid("3afd7d25-77a8-4b79-b9eb-ebdab0e6bff9")]
public partial class MyPanelUserControl : UserControl
{
  /// <summary>
  /// Constructor
  /// </summary>
  public MyPanelUserControl()
  {
    InitializeComponent();

    // Hook up our RhinoDoc.EndOpenDocument handler
    RhinoDoc.EndOpenDocument += OnEndOpenDocument;
  }

  /// <summary>
  /// Called when a document opening process has ended.
  /// Note, never modify the Rhino document in this handler.
  /// If you need to change the Rhino document or post update messages
  /// to controls, then your RhinoDoc.EndOpenDocument handler
  /// should record what happened in an efficient way and then make
  /// any changes that are required in a RhinoApp.Idle event hander.
  /// </summary>
  private void OnEndOpenDocument(object sender, Rhino.DocumentOpenEventArgs e)
  {
    // Hook up our RhinoApp.Idle handler
    RhinoApp.Idle += OnIdle;
  }

  /// <summary>
  /// Called when Rhino has entered an idle state.
  /// </summary>
  private void OnIdle(object sender, EventArgs e)
  {
    // Unhook our RhinoApp.Idle handler
    RhinoApp.Idle -= OnIdle;
    // TODO: do something interesting
    RhinoApp.WriteLine(RhinoDoc.ActiveDoc.Path);
  }
}

– Dale

2 Likes

Perfect!