Sdk Add Mesh
Last changed: -204.177.179.138

.
SummaryDemonstrates how to add a simple mesh object to Rhino.

Overview

To create an ON_Mesh:

  1. Create the ON_Mesh object. The constructor requires the number of faces and vertices, and whether or not you have vertex normals or texture coordinates.
  2. Fill in the mesh vertex array, ON_Mesh::m_V. You can also use ON_Mesh::SetVertex.
  3. Fill in the mesh faces array, ON_Mesh::m_F. You can also use ON_Mesh::SetTriangle and ON_Mesh::SetQuad.
  4. If you have vertex normals, fill in the normals array, ON_Mesh::m_N. You can also use ON_Mesh::SetVertexNormal.
  5. If you have texture coordinates, fill in the texture coordinate array, ON_Mesh::m_T. You can also use ON_Mesh::SetTextureCoordinate.
  6. If you did not specify vertex normals, have Rhino compute them for you using ON_Mesh::ComputeVertexNormals().
  7. Clean up everything using ON_Mesh::Compact.

You are now ready to add this mesh object to the document. The opennurbs_mesh.h header file is well documented. It's worth reading through at least once.

C++

  CRhinoCommand::result CCommandTest::RunCommand(const CRhinoCommandContext& context)
  {
    int face_count = 6;
    int vertex_count = 12;
    BOOL bVertexNormals = FALSE;
    BOOL bTextureCoordinates = FALSE;


    ON_Mesh mesh( face_count, vertex_count, bVertexNormals, bTextureCoordinates );
    mesh.SetVertex(  0, ON_3fPoint(0.0f, 0.0f, 1.0f) );
    mesh.SetVertex(  1, ON_3fPoint(1.0f, 0.0f, 1.0f) );
    mesh.SetVertex(  2, ON_3fPoint(2.0f, 0.0f, 1.0f) );
    mesh.SetVertex(  3, ON_3fPoint(3.0f, 0.0f, 0.0f) );
    mesh.SetVertex(  4, ON_3fPoint(0.0f, 1.0f, 1.0f) );
    mesh.SetVertex(  5, ON_3fPoint(1.0f, 1.0f, 2.0f) );
    mesh.SetVertex(  6, ON_3fPoint(2.0f, 1.0f, 1.0f) );
    mesh.SetVertex(  7, ON_3fPoint(3.0f, 1.0f, 0.0f) );
    mesh.SetVertex(  8, ON_3fPoint(0.0f, 2.0f, 1.0f) );
    mesh.SetVertex(  9, ON_3fPoint(1.0f, 2.0f, 1.0f) );
    mesh.SetVertex( 10, ON_3fPoint(2.0f, 2.0f, 1.0f) );
    mesh.SetVertex( 11, ON_3fPoint(3.0f, 2.0f, 1.0f) );
    mesh.SetQuad( 0, 0, 1,  5,  4 );
    mesh.SetQuad( 1, 1, 2,  6,  5 );
    mesh.SetQuad( 2, 2, 3,  7,  6 );
    mesh.SetQuad( 3, 4, 5,  9,  8 );
    mesh.SetQuad( 4, 5, 6, 10,  9 );
    mesh.SetQuad( 5, 6, 7, 11, 10 );
    mesh.ComputeVertexNormals();
    mesh.Compact();


    context.m_doc.AddMeshObject( mesh );
    context.m_doc.Redraw();
    return CRhinoCommand::success;
  }

VB.NET (Rhino 4)

  Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) As IRhinoCommand.result
    Dim face_count As Integer = 6
    Dim vertex_count As Integer = 12
    Dim bVertexNormals As Boolean = False
    Dim bTextureCoordinates As Boolean = False


    Dim mesh As New OnMesh(face_count, vertex_count, bVertexNormals, bTextureCoordinates)
    mesh.SetVertex(0, New On3fPoint(0.0F, 0.0F, 1.0F))
    mesh.SetVertex(1, New On3fPoint(1.0F, 0.0F, 1.0F))
    mesh.SetVertex(2, New On3fPoint(2.0F, 0.0F, 1.0F))
    mesh.SetVertex(3, New On3fPoint(3.0F, 0.0F, 0.0F))
    mesh.SetVertex(4, New On3fPoint(0.0F, 1.0F, 1.0F))
    mesh.SetVertex(5, New On3fPoint(1.0F, 1.0F, 2.0F))
    mesh.SetVertex(6, New On3fPoint(2.0F, 1.0F, 1.0F))
    mesh.SetVertex(7, New On3fPoint(3.0F, 1.0F, 0.0F))
    mesh.SetVertex(8, New On3fPoint(0.0F, 2.0F, 1.0F))
    mesh.SetVertex(9, New On3fPoint(1.0F, 2.0F, 1.0F))
    mesh.SetVertex(10, New On3fPoint(2.0F, 2.0F, 1.0F))
    mesh.SetVertex(11, New On3fPoint(3.0F, 2.0F, 1.0F))
    mesh.SetQuad(0, 0, 1, 5, 4)
    mesh.SetQuad(1, 1, 2, 6, 5)
    mesh.SetQuad(2, 2, 3, 7, 6)
    mesh.SetQuad(3, 4, 5, 9, 8)
    mesh.SetQuad(4, 5, 6, 10, 9)
    mesh.SetQuad(5, 6, 7, 11, 10)
    mesh.ComputeVertexNormals()
    mesh.Compact()
    context.m_doc.AddMeshObject(mesh)
    context.m_doc.Redraw()
    Return IRhinoCommand.result.success
  End Function

C# (Rhino 4)

  public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
  {
    int face_count = 6;
    int vertex_count = 12;
    bool bVertexNormals = false ;
    bool bTextureCoordinates = false;


    OnMesh mesh = new OnMesh(face_count, vertex_count, bVertexNormals, bTextureCoordinates);
    mesh.SetVertex(  0, new On3fPoint(0.0f, 0.0f, 1.0f) );
    mesh.SetVertex(  1, new On3fPoint(1.0f, 0.0f, 1.0f) );
    mesh.SetVertex(  2, new On3fPoint(2.0f, 0.0f, 1.0f) );
    mesh.SetVertex(  3, new On3fPoint(3.0f, 0.0f, 0.0f) );
    mesh.SetVertex(  4, new On3fPoint(0.0f, 1.0f, 1.0f) );
    mesh.SetVertex(  5, new On3fPoint(1.0f, 1.0f, 2.0f) );
    mesh.SetVertex(  6, new On3fPoint(2.0f, 1.0f, 1.0f) );
    mesh.SetVertex(  7, new On3fPoint(3.0f, 1.0f, 0.0f) );
    mesh.SetVertex(  8, new On3fPoint(0.0f, 2.0f, 1.0f) );
    mesh.SetVertex(  9, new On3fPoint(1.0f, 2.0f, 1.0f) );
    mesh.SetVertex( 10, new On3fPoint(2.0f, 2.0f, 1.0f) );
    mesh.SetVertex( 11, new On3fPoint(3.0f, 2.0f, 1.0f) );
    mesh.SetQuad( 0, 0, 1, 5, 4 );
    mesh.SetQuad( 1, 1, 2, 6, 5 );
    mesh.SetQuad( 2, 2, 3, 7, 6 );
    mesh.SetQuad( 3, 4, 5, 9, 8 );
    mesh.SetQuad( 4, 5, 6, 10, 9 );
    mesh.SetQuad( 5, 6, 7, 11, 10 );
    mesh.ComputeVertexNormals();
    mesh.Compact();
    context.m_doc.AddMeshObject( mesh );
    context.m_doc.Redraw();
    return IRhinoCommand.result.success;
  }