Sdk Mesh Volume
Last changed: -204.177.179.116

.
DeveloperC++, .NET
VersionRhino 4
SummaryDemonstrates how to calculating the volumes of mesh objects using Rhino 4.0 SDK.

The following sample code demonstrates how to calculating the volumes of mesh objects. For more information, see opennurbs_mesh.h.

C++

  CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
  {
    CRhinoGetObject go;
    go.SetCommandPrompt( L"Select solid meshes for volume calculation" );
    go.SetGeometryFilter( CRhinoGetObject::mesh_object );
    go.SetGeometryAttributeFilter( CRhinoGetObject::closed_mesh );
    go.EnableSubObjectSelect( FALSE );
    go.EnableGroupSelect();
    go.GetObjects( 1, 0 );
    if( go.CommandResult() != CRhinoCommand::success )
      return go.CommandResult();


    int i;
    ON_SimpleArray<const ON_Mesh*> meshes;
    for( i = 0; i < go.ObjectCount(); i++ )
    {
      const ON_Mesh* mesh = go.Object(i).Mesh();
      if( mesh )
        meshes.Append( mesh );
    }


    const int mesh_count = meshes.Count();
    if( 0 == mesh_count )
      return nothing;


    ON_BoundingBox bbox;
    for( i = 0; i < mesh_count; i++ )
      meshes[i]->GetBoundingBox( bbox, TRUE );
    ON_3dPoint base_point = bbox.Center();


    double total_volume = 0.0;
    double total_error_estimate = 0.0;
    for( i = 0; i < mesh_count; i++ )
    {
      double error_estimate = 0.0;
      double volume = meshes[i]->Volume( base_point, &error_estimate );
      RhinoApp().Print( L"Mesh%d = %f (+/- %f)\n", i, volume, error_estimate );
      total_volume += volume;
      total_error_estimate += error_estimate;
    }


    RhinoApp().Print( L"Total volume = %f (+/- %f)\n", 
                      total_volume, 
                      total_error_estimate );


    return CRhinoCommand::success;
  }

C# (Rhino 4)

  public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
  {
    MRhinoGetObject go = new MRhinoGetObject();
    go.SetCommandPrompt( "Select solid meshes for volume calculation" );
    go.SetGeometryFilter( IRhinoGetObject.GEOMETRY_TYPE_FILTER.mesh_object );
    go.SetGeometryAttributeFilter( IRhinoGetObject.GEOMETRY_ATTRIBUTE_FILTER.closed_mesh );
    go.EnableSubObjectSelect(false);
    go.EnableGroupSelect();
    go.GetObjects( 1, 0 );
    if( go.CommandResult() != IRhinoCommand.result.success )
      return go.CommandResult();


    List<IOnMesh> meshes = new List<IOnMesh>();
    for( int i = 0; i < go.ObjectCount(); i++ )
    {
      IOnMesh mesh = go.Object(i).Mesh();
      if( mesh != null )
        meshes.Add( mesh );
    }
    if( meshes.Count == 0 )
      return IRhinoCommand.result.nothing;


    OnBoundingBox bbox = new OnBoundingBox();
    for( int i = 0; i < meshes.Count; i++ )
      meshes[i].GetBoundingBox( ref bbox, 1 );
    On3dPoint base_point = bbox.Center();


    double total_volume = 0.0;
    double total_error_estimate = 0.0;
    string msg;
    for( int i = 0; i < meshes.Count; i++ )
    {
      double error_estimate = 0.0;
      double volume = meshes[i].Volume( base_point, ref error_estimate );
      msg = string.Format("Mesh {0} = {1:f} (+/- {2:f}\n",i,volume,error_estimate);
      RhUtil.RhinoApp().Print( msg );
      total_volume += volume;
      total_error_estimate += error_estimate;
    }
    msg = string.Format("Total volume = {0:f} (+/- {1:f})\n",
                        total_volume,
                        total_error_estimate);
    RhUtil.RhinoApp().Print( msg );
    return IRhinoCommand.result.success;
  }

VB.NET (Rhino 4)

  Public Overrides Function RunCommand(ByVal context As RMA.Rhino.IRhinoCommandContext) _
    As RMA.Rhino.IRhinoCommand.result
    Dim go As New MRhinoGetObject()
    go.SetCommandPrompt("Select solid meshes for volume calculation")
    go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.mesh_object)
    go.SetGeometryAttributeFilter(IRhinoGetObject.GEOMETRY_ATTRIBUTE_FILTER.closed_mesh)
    go.EnableSubObjectSelect(False)
    go.EnableGroupSelect()
    go.GetObjects(1, 0)
    If (go.CommandResult() <> IRhinoCommand.result.success) Then
      Return go.CommandResult()
    End If


    Dim meshes As New List(Of IOnMesh)()
    For i As Integer = 0 To go.ObjectCount() - 1
      Dim mesh As IOnMesh = go.Object(i).Mesh()
      If (mesh IsNot Nothing) Then meshes.Add(mesh)
    Next
    If (meshes.Count = 0) Then Return IRhinoCommand.result.nothing


    Dim bbox As New OnBoundingBox()
    For i As Integer = 0 To meshes.Count - 1
      meshes(i).GetBoundingBox(bbox, 1)
    Next
    Dim base_point As On3dPoint = bbox.Center()


    Dim total_volume As Double = 0.0
    Dim total_error_estimate As Double = 0.0
    Dim msg As String
    For i As Integer = 0 To meshes.Count - 1
      Dim error_estimate As Double = 0.0
      Dim volume As Double = meshes(i).Volume(base_point, error_estimate)
      msg = String.Format("Mesh {0} = {1:f} (+/- {2:f}", i, volume, error_estimate)
      RhUtil.RhinoApp().Print(msg + vbCrLf)
      total_volume += volume
      total_error_estimate += error_estimate
    Next
    msg = String.Format("Total volume = {0:f} (+/- {1:f})", _
                        total_volume, _
                        total_error_estimate)
    RhUtil.RhinoApp().Print(msg + vbCrLf)


    Return IRhinoCommand.result.success
  End Function