Best place for a document-specific object store to live

Hi @aske,

You have several options.

You’ve mentioned the first one, which is to store your data on your plug-in object.

Or, you can use a singleton. For example:

public class MyDataTable
{
  /// <summary>
  /// Private constructor
  /// </summary>
  private MyDataTable() {}

  /// <summary>
  /// The one and only MyDataTable object
  /// </summary>
  private static MyDataTable g_my_table;

  /// <summary>
  /// Returns the one and only MyDataTable object
  /// </summary>
  public static MyDataTable Instance => g_my_table ?? (g_my_table = new MyDataTable());
}

Or, you can store your table in the Rhino document runtime dictionary. For example:

public class MyDataTable
{
  /// <summary>
  /// Public constructor
  /// </summary>
  public MyDataTable(uint documentSerialNumber)
  {
    DocumentSerialNumber = documentSerialNumber;
  }

  /// <summary>
  /// The runtime serial number of the document that owns this table.
  /// </summary>
  public uint DocumentSerialNumber { get; }

  /// <summary>
  /// The document that owns this table.
  /// </summary>
  public RhinoDoc Document => RhinoDoc.FromRuntimeSerialNumber(DocumentSerialNumber);
}

//...

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var my_table = doc.RuntimeData.GetValue(typeof(MyDataTable), rhinoDoc => new MyDataTable(rhinoDoc.RuntimeSerialNumber));
  if (null != my_table)
  {
    // TODO...
  }
  return Result.Success;
}

Their may be other ways too.

There are a number of document-related events you can subscribe to.

The Mac support multiple documents. So if you intend your plug-in to run in Rhino for Mac, then you need to ensure your plug-in data takes multiple documents into account. A good way of doing this is to store your data in the document’s runtime dictionary (as I have shown above).

Does this help?

– Dale

1 Like