Sdk Mesh Objects
Last changed: dale@mcneel.com-204.177.179.99

.
DeveloperC++, .NET
SummaryDemonstrates how to mesh surface and polysurface objects using the RhinoMeshObjects SDK function.

The RhinoMeshObjects function allows SDK developers access to the same interface used by Rhino's Mesh command and many mesh export plug-ins. For details on this function, see rhinoSdkUtilities.h.

The following sample code demonstrates how to use this function.

  CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
  {
    // Select some geometry to mesh
    CRhinoGetObject go;
    go.SetCommandPrompt( L"Select surface or polysurface to mesh" );
    go.SetGeometryFilter( CRhinoGetObject::surface_object | CRhinoGetObject::polysrf_object );
    go.GetObjects( 1, 1 );
    if( go.CommandResult() != success )
      return go.CommandResult();


    ON_SimpleArray<const CRhinoObject*> objects;
    objects.Append( go.Object(0).Object() );


    // RhinoMeshObjects need to know how to mesh the objects. This information is provided
    // by passing the function a ON_MeshParameters object. For details on this class, see
    // opennurbs_mesh.h. 


    // In this example, instead of making up our own default mesh parameters, we will just
    // get some existing ones that we know work well.
    const CRhinoAppRenderMeshSettings& rms = RhinoApp().AppSettings().RenderMeshSettings();
    //ON_MeshParameters mp = rms.QualityMeshParameters();
    ON_MeshParameters mp = rms.FastMeshParameters();


    // Set the user interface style.
    int ui_style = 0; // simple ui


    ON_ClassArray<CRhinoObjectMesh> meshes;


    // Mesh the selected objects.
    CRhinoCommand::result rc = RhinoMeshObjects( objects, mp, ui_style, meshes );
    if( rc == success )
    {
      int i;
      for( i = 0; i < meshes.Count(); i++ )
      {
        CRhinoObjectMesh& mesh = meshes[i];
        CRhinoMeshObject* mesh_object = new CRhinoMeshObject( mesh.m_mesh_attributes );
        mesh_object->SetMesh( mesh.m_mesh );
        mesh.m_mesh = 0;
        context.m_doc.AddObject( mesh_object );
      }


      context.m_doc.Redraw();
    }


    return rc;
  }

VB.NET (Rhino 4)

  Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
    As IRhinoCommand.result
    ' Select some geometry to mesh
    Dim go As New MRhinoGetObject()
    go.SetCommandPrompt("Select surface or polysurface to mesh")
    go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.surface_object Or _
                          IRhinoGetObject.GEOMETRY_TYPE_FILTER.polysrf_object)
    go.GetObjects(1, 1)
    If (go.CommandResult() <> IRhinoCommand.result.success) Then
      Return go.CommandResult()
    End If


    Dim objects(0) As IRhinoObject
    objects(0) = go.Object(0).Object()


    ' RhinoMeshObjects need to know how to mesh the objects. This information is provided
    ' by passing the function a ON_MeshParameters object. For details on this class, see
    ' opennurbs_mesh.h. 


    ' In this example, instead of making up our own default mesh parameters, we will just
    ' get some existing ones that we know work well.
    Dim rms As IRhinoAppRenderMeshSettings = RhUtil.RhinoApp.AppSettings.RenderMeshSettings()
    ' dim mp as New OnMeshParameters( rms.QualityMeshParameters() )
    Dim mp As New OnMeshParameters(rms.FastMeshParameters())
    ' Set the user interface style.
    Dim ui_style As Integer = 0 'simple ui
    Dim meshes As New ArrayMRhinoObjectMesh()
    ' Mesh the selected objects.
    Dim rc As IRhinoCommand.result = RhUtil.RhinoMeshObjects(objects, mp, ui_style, meshes)
    If (rc = IRhinoCommand.result.success) Then
      For i As Integer = 0 To meshes.Count() - 1
        context.m_doc.AddMeshObject(meshes(i).GetMesh(), meshes(i).m_mesh_attributes)
      Next
      context.m_doc.Redraw()
    End If
    Return IRhinoCommand.result.success
  End Function

C# (Rhino 4)

  public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
  {
    // Select some geometry to mesh
    MRhinoGetObject go = new MRhinoGetObject();
    go.SetCommandPrompt( "Select surface or polysurface to mesh" );
    go.SetGeometryFilter( IRhinoGetObject.GEOMETRY_TYPE_FILTER.surface_object |
                          IRhinoGetObject.GEOMETRY_TYPE_FILTER.polysrf_object );
    go.GetObjects( 1, 1 );
    if( go.CommandResult() != IRhinoCommand.result.success )
      return go.CommandResult();


    IRhinoObject[] objects = new IRhinoObject[1];
    objects[0] = go.Object(0).Object();


    // RhinoMeshObjects need to know how to mesh the objects. This information is provided
    // by passing the function a ON_MeshParameters object. For details on this class, see
    // opennurbs_mesh.h. 


    // In this example, instead of making up our own default mesh parameters, we will just
    // get some existing ones that we know work well.
    IRhinoAppRenderMeshSettings rms = RhUtil.RhinoApp().AppSettings().RenderMeshSettings();
    //ON_MeshParameters mp = rms.QualityMeshParameters();
    OnMeshParameters mp = new OnMeshParameters(rms.FastMeshParameters());
    // Set the user interface style.
    int ui_style = 0; // simple ui
    ArrayMRhinoObjectMesh meshes = new ArrayMRhinoObjectMesh();
    // Mesh the selected objects.
    IRhinoCommand.result rc = RhUtil.RhinoMeshObjects(objects,
                                                      ref mp,
                                                      ref ui_style,
                                                      ref meshes);
    if( rc == IRhinoCommand.result.success )
    {
      for( int i=0; i<meshes.Count(); i++ )
      {
        context.m_doc.AddMeshObject( meshes[i].GetMesh(),
                                     meshes[i].m_mesh_attributes );
      }
      context.m_doc.Redraw();
    }
    return IRhinoCommand.result.success;
  }