Grasshopper

algorithmic modeling for Rhino

Possile to Serialize/Dserialize RhinoCommon Objects using XML

Has anyone managed to deserialize or serialize a rhino common object such as a curve using XML? Is it possible?

 

 

 

Views: 1253

Replies to This Discussion

You can Serialize RhinoCommon geometry classes, but they won't look like "pretty" XML if that is what you are expecting.  These classes all use low level binary serialization through OpenNURBS, so your XML is just going to contain a Base64 encoded string representing the geometry data.  Here's some C# code that reads/writes a LineCurve to disk using both a BinaryFormatter and a SoapFormatter.

 

//////////////////////////////////////////////////

// Serialized data will write using the current version of Rhino

// If you want to write data in Rhino 5 that can be read into Rhino 4,

// then you need to use the SerializationOptions class to enforce this

var options = new Rhino.FileIO.SerializationOptions();

options.RhinoVersion = 4;

var context = new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.All, options);

 

var write_crv = new Rhino.Geometry.LineCurve(new Point3d(0, 0, 0), new Point3d(1, 2, 3));

 

// use a BinaryFormatter to write and then read the curve

var stream = System.IO.File.Open(@"C:\\mycrv.bin", System.IO.FileMode.Create);

var bin_formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(null, context);

bin_formatter.Serialize(stream, write_crv);

stream.Close();

stream = System.IO.File.Open(@"C:\\mycrv.bin", System.IO.FileMode.Open);

var read_crv = bin_formatter.Deserialize(stream) as Rhino.Geometry.LineCurve;

stream.Close();

 

// Use a SoapFormatter to write and then read the curve

stream = System.IO.File.Open(@"C:\\mycrv_soap.txt", System.IO.FileMode.Create);

var soap_formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(null,context);

soap_formatter.Serialize(stream, write_crv);

stream.Close();

stream = System.IO.File.Open(@"C:\\mycrv_soap.txt", System.IO.FileMode.Open);

read_crv = soap_formatter.Deserialize(stream) as Rhino.Geometry.LineCurve;

stream.Close();

Hey Steve

 

Thanks for that. It helps a lot.

 

Just a quickee; is there an difference when serializing to bianry when using Rhino5 32 bit vs Rhino5 64 bit? Are the bytes and hence binary stream written differently?

 

Sorry to bug you again, but last time I looked, I still was uanble to get the miter option to work for the sweep method in RhinoCommon. Is it something wrong on my end?

 

thanks again

There should be no difference between 32 and 64 bit for what bits get written to a stream.

 

Sorry,  I haven't had the time to look into the miter option for sweep in RhinoCommon yet.

-Steve

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service