Grasshopper

algorithmic modeling for Rhino

If you are creating geometry outside of Rhino using Rhino3dmIO and you want to bring the geometry into Grasshopper this post describes one of the possible ways of doing it.

In your outside class you can stream your objects using a binary formatter as follows:

public class Class1
{
   private Vector3d myVector;
   private LineCurve myCurve;

   private List<object> myGeometryObjects = new List<object>();

   public Class1( )
   {
       myGeometryObjects.Clear( );
       myVector = Vector3d.XAxis;
       myCurve = new LineCurve( new Point2d( 0, 0 ), new Point2d( 1, 1 ) );
       myGeometryObjects.Add( myCurve );
       myGeometryObjects.Add( myVector );
    }

    public void MyObjects(Stream stream)
   {
        var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        formatter.Serialize( stream, myGeometryObjects );
    }
}

Then on Grasshopper side you need to deserialize the objects to RhinoCommon instead of OpenNURBS. Without this it gives a type cast error.

error: [A]Rhino.Geometry.LineCurve cannot be cast to [B]Rhino.Geometry.LineCurve. Type A originates from 'Rhino3dmIO, Version=5.1.30000.14, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\ProjectDirectory\Rhino3dmIO.dll'. Type B originates from 'RhinoCommon, Version=5.1.30000.17, Culture=neutral, PublicKeyToken=552281e97c755530' in the context 'Default' at location 'C:\Program Files\Rhinoceros 5 (64-bit)\System\RhinoCommon.dll'. (line: 139)

The trick is to use a SerializationBinder to convert OpenNURBS objects to RhinoCommon objects.

private void RunScript(object x, ref object B)

{
   string RhinoCommonAssembly = Assembly.GetAssembly(typeof(Rhino.Geometry.Curve)).FullName;
   using(var stream = new MemoryStream())
   {
        var oClass1 = new Class1();
        oClass1.MyObjects(stream);
        var bin_formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
        bin_formatter.Binder = new OpenNurbsToRhino(RhinoCommonAssembly);
        stream.Position = 0;
        var readObjects = bin_formatter.Deserialize(stream);
        stream.Close();
        B = readObjects;
    }


}

// <Custom additional code>
class OpenNurbsToRhino : SerializationBinder
{
   private readonly string myAssembly;
   public OpenNurbsToRhino(string Assembly)
  {
      myAssembly = Assembly;
   }
   public override Type BindToType(string assemblyName, string typeName)
   {
       Type typeToDeserialize = null;
       typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
       typeName, myAssembly));
       return typeToDeserialize;
   }
}

Views: 381

Comment

You need to be a member of Grasshopper to add comments!

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service