Serializing hatches (C#)


#1

Hi there,

I am trying to serialize hatches in order to use them as a resource in Visual Studio.
It works fine for simple hatches, but as soon as I go a bit more complicated (more than 5 corners), I get an exception.

Code snippet:

using (MemoryStream stream = new MemoryStream())
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, some_hatch);
            stream.Position = 0;
            return (Hatch)formatter.Deserialize(stream);
        }

It throws this exception on the serialize-line:
"
A first chance exception of type ‘System.ArgumentNullException’ occurred in RhinoCommon.dll

Additional information: Value cannot be null.

If there is a handler for this exception, the program may be safely continued.
"

Any ideas?

All the best,

Gustav Good


(Dale Fugier) #2

Just to clarify, are you trying to serialize a Rhino.Geometry.HatchObject or a Rhino.DocObject.HatchPattern?


#3

Rhino.Geometry.Hatch that is.


(Dale Fugier) #4

This seems to work:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var pattern_index = doc.HatchPatterns.CurrentHatchPatternIndex;
  if (pattern_index < 0)
  {
    RhinoApp.WriteLine("No hatch patterns found in the document.");
    return Result.Nothing;
  }

  var circle = new Circle(Plane.WorldXY, 5.0);
  var arc_curve = new ArcCurve(circle);

  var hatch = Rhino.Geometry.Hatch.Create(arc_curve, pattern_index, 0.0, 1.0);
  if (null != hatch && hatch.Length > 0)
  {
    using (var stream = new MemoryStream())
    {
      var formatter = new BinaryFormatter();
      formatter.Serialize(stream, hatch[0]);
      stream.Position = 0;

      var some_hatch = formatter.Deserialize(stream) as Hatch;
      if (null != some_hatch && some_hatch.IsValid)
      {
        doc.Objects.AddHatch(some_hatch);
        doc.Views.Redraw();
      }
    }        
  }

  return Result.Success;
}

Let me know if you find otherwise.

– Dale


#5

Dale,
Thank you for a quick reply.

Here is a link to a hatch that fails with this approach:

https://drive.google.com/file/d/0B6ntVYN1MZ8QRzNnem80M1V5akk/view?usp=sharing

All the best,

Gustav


(Dale Fugier) #6

Hi Gustav,

If I run this code:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var go = new GetObject();
  go.SetCommandPrompt("Select hatch");
  go.GeometryFilter = ObjectType.Hatch;
  go.Get();
  if (go.CommandResult() != Result.Success)
    return go.CommandResult();

  var hatch = go.Object(0).Hatch();
  if (null == hatch)
    return Result.Failure;

  using (var stream = new MemoryStream())
  {
    var formatter = new BinaryFormatter();
    formatter.Serialize(stream, hatch);
        
    stream.Position = 0;

    var some_hatch = formatter.Deserialize(stream) as Hatch;
    if (null != some_hatch && some_hatch.IsValid)
    {
      doc.Objects.AddHatch(some_hatch);
      doc.Views.Redraw();
    }
  }        

  return Result.Success;
}

and pick the hatch in your model, I end up with two identical hatch objects.

What am I missing?


#7

Resolved by updating Rhino from SR8 to SR11.

Thank you