How to add actions to the InstanceDefinitionTableEvent?

Hi, this might be a quite stupid question.
I am trying to add a void/actions when a blockdefinition is created for instance.

Of course I found this:
https://developer.rhino3d.com/api/RhinoCommo/html/E_Rhino_RhinoDoc_InstanceDefinitionTableEvent.htm

But somehow I do not really know how to work with this.

Thanks,
T.

Hi @tobias.stoltmann,

My apologies, but I don’t understand. Can you clarify? What problem are you having or trying to solve?

Thanks,

– Dale

Hi @dale ,
my apologies. Maybe I have been way too vague.
Maybe this might be a bit embarrassing for me now, but I am stuck here:

In a plugin I want something to be done, once the user adds, removes or edits a block.

So I would define this in the constructor…

Rhino.RhinoDoc.InstanceDefinitionTableEvent += ThingsToDo();

And then…

private void ThingsToDo(object sender, Rhino.DocObjects.Tables.InstanceDefinitionTableEventArgs args)
{
    RhinoApp.WriteLine("Oh boy!");
}

So I guess my whole approach is wrong, right?
And besides solving this problem, do you know a good read to understand those things better?

Thanks,
T.

Hi @tobias.stoltmann,

Your approach is correct. Is something not working?

– Dale

@dale, it says:

|Error|CS0029|Cannot implicitly convert type ‘void’ to ‘System.EventHandler<Rhino.DocObjects.Tables.InstanceDefinitionTableEventArgs>’

and

|Error|CS7036|There is no argument given that corresponds to the required formal parameter ‘sender’ of ‘WpfPanel.ThingsToDo(object, InstanceDefinitionTableEventArgs)’|

Hi @tobias.stoltmann,

See this:

TestTobias.zip (23.6 KB)

– Dale

@dale, thanks.
I just help but feel that I made a more systematic mistake here.

This is the Wpf-Panel I used.

As I can see you are using this…
protected override LoadReturnCode OnLoad(ref string errorMessage)

Do I maybe have to use an equivalent method for the Wpf-Panel? Because right now I am doing this in the constructor and as I can see this might definitely not be the right place…

Hi @tobias.stoltmann,

You might consider hooking the event from the panel’s view model:

using Rhino;
using Rhino.DocObjects.Tables;
using Rhino.UI;
using System;

namespace SampleCsWpf.ViewModels
{
  internal class SampleCsWpfPaneViewModel : ViewModel
  {
    private static EventHandler<InstanceDefinitionTableEventArgs> m_idef_table_event = null;

    /// <summary>
    /// Constructor
    /// </summary>
    public SampleCsWpfPaneViewModel(uint documentSerialNumber)
    {
      DocumentRuntimeSerialNumber = documentSerialNumber;
      Panels.Show += OnShow;
      Panels.Closed += OnClosed;
    }

    private uint DocumentRuntimeSerialNumber { get; }

    /// <summary>
    /// This event is raised when a panel host is closed.
    /// </summary>
    private void OnClosed(object sender, Rhino.UI.PanelEventArgs e)
    {
      if (null != m_idef_table_event)
      {
        RhinoDoc.InstanceDefinitionTableEvent -= m_idef_table_event;
        m_idef_table_event = null;
      }
    }

    /// <summary>
    /// This event is called when a panel is shown or hidden.
    /// </summary>
    private void OnShow(object sender, ShowPanelEventArgs args)
    {
      if (args.Show)
      {
        if (null == m_idef_table_event)
          RhinoDoc.InstanceDefinitionTableEvent += m_idef_table_event = OnInstanceDefinitionTableEvent;
      }
      else
      {
        if (null != m_idef_table_event)
        {
          RhinoDoc.InstanceDefinitionTableEvent -= m_idef_table_event;
          m_idef_table_event = null;
        }
      }
    }

    /// <summary>
    /// This event is raised when any modification happens to a document's instance definition table.
    /// </summary>
    private void OnInstanceDefinitionTableEvent(object sender, InstanceDefinitionTableEventArgs args)
    {
      RhinoApp.WriteLine("> InstanceDefinitionTableEvent");
      RhinoApp.WriteLine("    EventType = {0}", args.EventType);
    }

– Dale

@dale
sorry for the late reply. Working fine.
Thanks!