Can't Serialize Polyline


#1

I can Serialize GeometryBase and Point3d, but not Polyline.

error: Type 'Rhino.Collections.Point3dList+XAccess' in Assembly 'RhinoCommon, Version=5.1.30000.17, Culture=neutral, PublicKeyToken=552281e97c755530' is not marked as serializable. (line: 0)

Is Polyline a special case because of the PointList parent? Or is there some pattern about what can be serialized and what not?

I assume (hope) everything derived from GeometryBase can be serialized?


(Dale Fugier) #2

Hi @atair,

What serialization format are you looking for? Can you share some source code that is not working for you?

– Dale


(Menno Deij - van Rijswijk) #3

If you make a PolyLineCurve it will be seralizable, because that inherits from GeometryBase.


#4

It is from a GH component - I guess that should not matter much.
Haven’t tried yet, but I can live with a PolyLineCurve as menno suggested

private void RunScript(ref object A)
{
    data = new TestStruct(new Point3d(16, 0, 0));
    byte[] serialData = ObjectToByteArray(data);

    TestStruct newData = ByteArrayToObject<TestStruct>(serialData);
    A = newData.a;
  }

  // <Custom additional code> 
  TestStruct data;

  [Serializable]
  public struct TestStruct {
    public Point3d a;
    public Polyline pline;

    public TestStruct(Point3d p) {
      a = p;
      pline = new Polyline();
      pline.Add(a);
    }
  }

  public static byte[] ObjectToByteArray(Object obj) {
    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    using (var ms = new MemoryStream()) {
      bf.Serialize(ms, obj);
      return ms.ToArray();
    }
  }

  public static T ByteArrayToObject<T>(byte[] arrBytes) {
    using (var memStream = new MemoryStream()) {
      var binForm = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
      memStream.Write(arrBytes, 0, arrBytes.Length);
      memStream.Seek(0, SeekOrigin.Begin);
      return (T) binForm.Deserialize(memStream);
    }
  }