Grasshopper

algorithmic modeling for Rhino

Hi,

Couldn't find much information about this on the forum and I'm still quite a noob...

Is there a way to embed meshes into a C# plugin? I mean, using resources - same way as the icons are specified - is there a method to turn standard mesh formats (.obj, .ply, .stl, etc.) into Rhino meshes without writing my own import functions? 

Thanks!

Views: 2197

Replies to This Discussion

Yes this can be done, in several ways (from most recommended to least recommended):

  • Save the meshes using standard .NET serialization as byte arrays and put these bytes in a file. Then import this file into the resources, read out the bytes at runtime and recreate the mesh. This requires some coding on your part to create the byte-array, but it is the neatest, cleanest approach.
  • Embed entire 3dm files into your resources and parse them at runtime. This involves embedding a lot of data you don't need, which both increases the size of your dll and has additional overhead. It is however easier to create 3dm files by hand.
  • Convert a mesh to a custom data stream, for example by only storing vertex coordinates and face indices (whether to use textual or binary data is up to you). You can then embed this data in the resources and parse them at runtime, recreating the meshes from scratch. This requires the most custom development but it does give you full control over the data you're embedding.

You can use the CommonObjectToByteArray and ByteArrayToCommonObject methods in the Grasshopper.Kernel.GH_Convert class to help you with the first approach.

--

David Rutten

david@mcneel.com

A simple 2 block C# implementation of the first idea:

First block:

private void RunScript(GeometryBase x, ref object A)
  {
    A = System.Convert.ToBase64String( Grasshopper.Kernel.GH_Convert.CommonObjectToByteArray(x););
  }

Second block:

  private void RunScript(string x, ref object A)
  {
    A = GH_Convert.ByteArrayToCommonObject<GeometryBase>(System.Convert.FromBase64String(x));
  }

See attachments.

Since the mesh used in this example has many duplicate vertices, there's a good chance it can become quite a bit smaller using easily available techniques (GZipping the bytearray).

It's quite trivial to zip byte arrays using C#, see http://www.dotnetperls.com/gzipstream

Attachments:

Big thanks, both! That's very helpful. Serialization makes a lot of sense, shouldn't be too hard to do.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service