Site Tools


Offsetting a Surface

Developer: C++
Summary: Demonstrates how to offset a surface.

Question

I have two issues with the Rhino 4.0 C++ SDK:

  1. I cannot seem to make the ON_NurbsSurface::Offset() function work. It always returns NULL. Any help here would be appreciated.
  2. Is there any function that reproduces what the Offset command does, expecially with the Solid option?

Answer

Regarding question #2, if you want to create offsets like Rhino, then use the new RhinoOffsetSurface() SDK function found in the Rhino 5.0 SDK.

For question #1, see the following code sample for a demonstration of ON_Surface::Offset.

C++

CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
{
  CRhinoGetObject go;
  go.SetCommandPrompt( L"Select surface to offset" );
  go.SetGeometryFilter( CRhinoGetObject::surface_object );
  go.EnableSubObjectSelect( FALSE );
  go.GetObjects( 1, 1 );
  if( go.CommandResult() != CRhinoCommand::success )
    return go.CommandResult();
 
  const CRhinoObjRef object_ref = go.Object(0);
  const CRhinoObject* object = object_ref.Object();
  const ON_BrepFace* face = object_ref.Face();
  if( 0 == object || 0 == face )
    return CRhinoCommand::failure;
 
  CRhinoGetNumber gn;
  gn.SetCommandPrompt( L"Distance to offset" );
  gn.SetLowerLimit( 0.0, TRUE );
  gn.GetNumber();
  if( gn.CommandResult() != CRhinoCommand::success )
    return gn.CommandResult();
 
  double distance = gn.Number();
  if( face->m_bRev )
    distance = -distance;
 
  double tolerance = context.m_doc.AbsoluteTolerance();
  ON_Surface* offset_srf = face->Offset( distance, tolerance );
  if( 0 == offset_srf )
  {
    RhinoApp().Print( L"Offset failed.\n" );
    return CRhinoCommand::failure;
  }
 
  CRhinoObjectAttributes attrib = object->Attributes();
  memset( &attrib.m_uuid, 0, sizeof( attrib.m_uuid ) );
 
  CRhinoSurfaceObject* offset_object = new CRhinoSurfaceObject( attrib );
  offset_object->SetSurface( offset_srf );
  context.m_doc.AddObject( offset_object );
  context.m_doc.Redraw();
 
  return CRhinoCommand::success;
}


developer/sdksamples/offsetsurface.txt ยท Last modified: 2016/04/08 by sandy