[RhinoCommon] Plugin.ReadDocument - possible bug in doc variable?


I noticed some strange behavior when opening files having plug-in data.
I override in my plug-in the method ReadDocument and listen for RhinoDoc.BeginOpenDocument event.

public MyPlugIn()
    Instance = this;
    RhinoDoc.BeginOpenDocument += OnBeginOpenDocument;

private void OnBeginOpenDocument(object sender, DocumentOpenEventArgs openEventArgs)
     RhinoApp.WriteLine("Begin open document: " + openEventArgs.FileName);
     RhinoApp.WriteLine(openEventArgs.FileName+" is {0}a reference", openEventArgs.Reference ? String.Empty : "not ");

protected override bool ShouldCallWriteDocument(FileWriteOptions options)
    return true;

protected override void WriteDocument(RhinoDoc doc, BinaryArchiveWriter archive, FileWriteOptions options)

protected override void ReadDocument(RhinoDoc doc, BinaryArchiveReader archive, FileReadOptions options)
    bool empty = String.IsNullOrWhiteSpace(doc.Name);
    if (!empty)
        RhinoApp.WriteLine("doc variable name "+doc.Name);
        RhinoApp.WriteLine("doc variable name [empty]");
    String name = archive.ReadString();
    RhinoApp.WriteLine("name when written "+name);

Output when reading file A.3dm after Rhino was started:

Begin open document: D:\temp\A.3dm
D:\temp\A.3dm is not a reference
doc variable name [empty]
Name when written A.3dm

Output when reading file B.3dm, when A.3dm is loaded:

Begin open document: D:\temp\B.3dm
D:\temp\B.3dm is not a reference
doc variable name [empty]
Name when written B.3dm

Output when attaching B as a reference with A active:

Begin open document: D:\temp\B.3dm
D:\temp\B.3dm is a reference
doc variable name A.3dm
Name when written B.3dm

Here is what I find strange:

  1. when reading a file, the doc variable’s name is empty
  2. the “Name when written” only shows up if a file is saved twice - after the first save it is empty when loaded.
  3. when attaching a reference model, the doc variable has the name of the active model, not the reference model. The documentation of ReadDocument (see http://4.rhino3d.com/5/rhinocommon/html/M_Rhino_PlugIns_PlugIn_ReadDocument.htm) clearly states that the doc variable is the document being loaded

Is this a bug? Is the documentation correct or incorrect?

When reading a file, the document name is one of the last fields (in the Rhino document) to be filled in.

The same goes when writing new files. If you have a file that has not been saved, then the document name field will not be filled in until the file saving is complete.

Does this help?

Yes that helps with explaining #1 and #2. Still, when reading a reference file and having the active docement as a reference is weird (#3). Or is the reference correct, but the name still on the previous value?

Don’t confuse “file” and “document”. There is only ever one document open in Rhino. But it can create other files.

Is there a problem you are trying to solve?

Aha - file and document! Of course, that makes sense.

The problem I was trying to solve (it is solved in the mean time) is to skip reading plug-in info when a file is read as reference. This is needed because the plug-in info goes to a “system-wide” panel with metadata for the user to edit. When reading in a reference, this panel’s content should not change. Therefore the plug-in info reading must be skipped.

During the development I was putting in some debug statements similar to the ones listed above and got totally confused by the the fact that the document name did not correspond to the file name I was reading in as reference.