Grasshopper

algorithmic modeling for Rhino

Hi!

I need to serialize hatches in order to use them as a resource in visual studio.

With some code snippets I found on the forum, I've gotten it to work for very simple hatches, but as soon as they become more complicated, RhinoCommon.dll throws an exception. It's stopping on the byte-conversion using GH_Convert.

Excteption: "
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?


Hatch hatch;

Rhino.DocObjects.RhinoObject[] objects = Rhino.RhinoDoc.ActiveDoc.Objects.FindByLayer("Default");
List hatches = new List();

for (int i = 0; i < objects.Length; i++)
{
try
{
hatches.Add((Hatch)objects[i].Geometry.Duplicate());
}

catch
{
continue;
}
}

string path = @"C:\\testing\\points.bin";


System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Create);
System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);

writer.Write(hatches.Count);

for (int i = 0; i < hatches.Count; i++)
{
byte[] hatchData = GH_Convert.CommonObjectToByteArray(hatches[i]);
if (hatchData == null || hatchData.Length == 0)
return;


writer.Write("hatchname");
writer.Write(hatchData.Length);
writer.Write(hatchData);


}


writer.Dispose();
stream.Dispose();

List outputHatches = new List();

// Step 3. Read data from disk.

stream = new System.IO.FileStream(path, System.IO.FileMode.Open);
System.IO.BinaryReader reader = new System.IO.BinaryReader(stream);

int hatchCount = reader.ReadInt32();

for (int i = 0; i < hatchCount; i++)
{
string hatchName = reader.ReadString();
int dataLength = reader.ReadInt32();
if (dataLength <= 0)
return;

byte[] hatchData = reader.ReadBytes(dataLength);


hatch = GH_Convert.ByteArrayToCommonObject(hatchData);


outputHatches.Add(hatch);
}

reader.Dispose();
stream.Dispose();

Views: 455

Replies to This Discussion

I wrote a script similar to yours, seems to work so far. If you can post a hatch that fails with this I'd like to see it.

Attachments:

Thanks for a quick reply David.

I've attached a hatch that fails in your script.

Attachments:

It seems to work here. What versions of Rhino and Grasshopper are you using?

Rhino:

Version 5 SR8 64-bit
(5.8.40315.18095, 03/15/2014)

GH:

0.9.0076

Are you running a newer build of rhinocommon btw?

I'm running Rhino5 SR11, and bugs in the (de)serialization of OpenNurbs data via RhinoCommon may have been fixed between SR8 and SR11.

Resolved by updating Rhino.

Thank you

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service