Third party file storage in Rhino Document


(Sarath Kithiyon) #1

Hi,

Any third party file storage available in rhino document ???

-Sarath


(Menno Deij - van Rijswijk) #2

You can read/write a byte array to a 3dm document from a plug-in.


(Vijay Archelaus) #3

can you please elaborate the way to store it?


(Menno Deij - van Rijswijk) #4

Sure. When you have a plug-in (I am assuming you’re using C# with RhinoCommon), the plug-in has three methods you can override: ShouldCallWriteDocument, WriteDocument and ReadDocument.

Find below the approach we use, that includes versioning of the stuff that you write. Every time you change what you write to the file, you increase the versioning and write an additional WriteDocumentXY and ReadDocumentXY method, so that older versions of files written with your plug-in can still be written.

protected override bool ShouldCallWriteDocument(FileWriteOptions options)
{
  // here you decide if you want to write something to file
  return true;
}

private const int Major = 0;
private const int Minor = 0;

protected override void WriteDocument(RhinoDoc doc, BinaryArchiveWriter archive, FileWriteOptions options)
{
  archive.Write3dmChunkVersion(Major, Minor);
  WriteDocument00(doc, archive, options);
}

private void WriteDocument00(RhinoDoc doc, BinaryArchiveWriter archive, FileWriteOptions options)
{
  byte[] bytesToWrite = GetBytesToWrite(); // you need to implement this
  archive.WriteByteArray(bytes);
  return base.WriteDocument(doc, archive, options);
}

protected override void ReadDocument(RhinoDoc doc, BinaryArchiveReader archive, FileReadOptions options)
{
  if (options.ImportMode || options.InsertMode || options.ImportReferenceMode)
  {
  // you may want to do something different if a document is not read like a new document, but inserted, imported, or referenced.
  }

  int major, minor;
  archive.Read3dmChunkVersion(out major, out minor);
  if (major == 0 && minor == 0)
    ReadDocument00(doc, archive, options);
}

private void ReadDocument00(RhinoDoc doc, BinaryArchiveReader archive, FileReadOptions options)
{
  byte[] bytes = archive.ReadByteArray();
  // now you have the written bytes again.
}