CRhinoCommand::result CCommandSamplePan::RunCommand( const CRhinoCommandContext& context )
{
  const CRhinoAppViewSettings& view_settings = RhinoApp().AppSettings().ViewSettings();
  double d = view_settings.m_pan_increment;
  if (view_settings.m_pan_reverse_keyboard)
    d = -d;

  CRhinoGetOption go;
  go.SetCommandPrompt(L"Select pan option");
  go.AcceptNothing();

  const int down_option_index  = go.AddCommandOption(RHCMDOPTNAME(L"Down"));
  const int left_option_index  = go.AddCommandOption(RHCMDOPTNAME(L"Left"));
  const int right_option_index = go.AddCommandOption(RHCMDOPTNAME(L"Right"));
  const int up_option_index    = go.AddCommandOption(RHCMDOPTNAME( L"Up"));
  const int in_option_index    = go.AddCommandOption(RHCMDOPTNAME(L"In"));
  const int out_option_index   = go.AddCommandOption(RHCMDOPTNAME(L"Out"));

  for(;;)
  {
    CRhinoGet::result res = go.GetOption();

    if (res != CRhinoGet::option)
      break;

    CRhinoView* view = go.View(); 
    const CRhinoCommandOption* option = go.Option();

    if (0 != view && 0 != option && 0.0 != d)
    {
      CRhinoViewport* viewport = &(view->ActiveViewport());
      if (0 != viewport && viewport->View().m_bLockedProjection)
        viewport = &(view->MainViewport());

      if (0 != viewport)
      {
        if (down_option_index == option->m_option_index)
          viewport->DownUpDolly(-d);
        else if (up_option_index == option->m_option_index)
          viewport->DownUpDolly(d);
        else if (left_option_index == option->m_option_index)
          viewport->LeftRightDolly(-d);
        else if (right_option_index == option->m_option_index)
          viewport->LeftRightDolly(d);
        else if (in_option_index == option->m_option_index)
          viewport->InOutDolly(d);
        else if (out_option_index == option->m_option_index)
          viewport->InOutDolly(-d);
      }

      view->Redraw();
      RhinoApp().SetActiveView(view);
    }
  }  
  
  return CRhinoCommand::success;
}
CRhinoCommand::result CCommandSampleDrawBitmap::RunCommand( const CRhinoCommandContext& context )
{
  CRhinoCommandOptionValue enable_opts[] = { RHCMDOPTVALUE(L"Yes"), RHCMDOPTVALUE(L"No"), RHCMDOPTVALUE(L"Toggle") };
  for(;;)
  {
    bool bEnable = m_conduit.IsEnabled();
	  int current_index = bEnable ? 0 : 1;

    CRhinoGetOption go;
    go.SetCommandPrompt( L"Choose command option" );
    go.AddCommandOptionList( RHCMDOPTNAME(L"Enable"), 3, enable_opts, current_index );
    go.AcceptNothing();

    CRhinoGet::result res = go.GetOption();
    if( res == CRhinoGet::option )
    {
      const CRhinoCommandOption* option = go.Option();
      if( 0 == option )
        return CRhinoCommand::failure;

      current_index = option->m_list_option_current;
      if( 0 == current_index )
      {
        if( !bEnable )
        {
          m_conduit.Enable();
          context.m_doc.Regen();
        }
      }
      else if( 1 == current_index )
      {
        if( bEnable )
        {
          m_conduit.Disable();
          context.m_doc.Regen();
        }
      }
      else // if( 2 == current_index )
      {
        if( bEnable )
          m_conduit.Disable();
        else
          m_conduit.Enable();
        context.m_doc.Regen();
      }

      continue;
    }

    break;
  }

  return CRhinoCommand::success;
}
CRhinoCommand::result CCommandSampleTabbedDockBar::RunCommand( const CRhinoCommandContext& context )
{
  ON_UUID tabId = CSampleTabbedDockBarDialog::ID();

  if( context.IsInteractive() )
  {
    CRhinoTabbedDockBarDialog::OpenDockbarTab( tabId );
  }
  else
  {
    bool bVisible = CRhinoTabbedDockBarDialog::IsTabVisible( tabId );
 
    ON_wString str;
    str.Format( L"%s is %s. New value", LocalCommandName(), bVisible ? L"visible" : L"hidden" );
 
    CRhinoGetOption go;
    go.SetCommandPrompt( str );
    int h_option = go.AddCommandOption( RHCMDOPTNAME(L"Hide") );
    int s_option = go.AddCommandOption( RHCMDOPTNAME(L"Show") );
    int t_option = go.AddCommandOption( RHCMDOPTNAME(L"Toggle") );
    go.GetOption();
    if( go.CommandResult() != CRhinoCommand::success )
      return go.CommandResult();
 
    const CRhinoCommandOption* option = go.Option();
    if( 0 == option )
      return CRhinoCommand::failure;
 
    int option_index = option->m_option_index;

    if( h_option == option_index && bVisible )
      CRhinoTabbedDockBarDialog::ShowDockbarTab( tabId, false );
    else if( s_option == option_index && !bVisible  )
      CRhinoTabbedDockBarDialog::ShowDockbarTab( tabId, true );
    else if( t_option == option_index )
      CRhinoTabbedDockBarDialog::ShowDockbarTab( tabId, !bVisible );
  }

  return CRhinoCommand::success;
}
CRhinoCommand::result CCommandSampleMenuToggle::RunCommand( const CRhinoCommandContext& context )
{
  BOOL bVisible = SampleMenuPlugIn().IsSampleMenuVisible();
 
  ON_wString str;
  str.Format( L"%s is %s. New value", LocalCommandName(), bVisible ? L"visible" : L"hidden" );
 
  CRhinoGetOption go;
  go.SetCommandPrompt( str );
  int h_option = go.AddCommandOption( RHCMDOPTNAME(L"Hide") );
  int s_option = go.AddCommandOption( RHCMDOPTNAME(L"Show") );
  int t_option = go.AddCommandOption( RHCMDOPTNAME(L"Toggle") );
  go.GetOption();
  if( go.CommandResult() != CRhinoCommand::success )
    return go.CommandResult();
 
  const CRhinoCommandOption* option = go.Option();
  if( 0 == option )
    return CRhinoCommand::failure;
 
  int option_index = option->m_option_index;

  if( h_option == option_index && bVisible )
    SampleMenuPlugIn().HideSampleMenu();
  else if( s_option == option_index && !bVisible  )
    SampleMenuPlugIn().ShowSampleMenu();
  else if( t_option == option_index )
  {
    if( bVisible )
      SampleMenuPlugIn().HideSampleMenu();
    else
      SampleMenuPlugIn().ShowSampleMenu();
  }

  return CRhinoCommand::success;
}
CRhinoCommand::result CCommandSampleCageEdit::RunCommand( const CRhinoCommandContext& context )
{
  ON_Workspace ws;
  CRhinoCommand::result rc = CRhinoCommand::success;
 
  // Get the captive object
  CRhinoGetObject go;
  go.SetCommandPrompt( L"Select captive surface or polysurface" );
  go.SetGeometryFilter( CRhinoGetObject::surface_object | CRhinoGetObject::polysrf_object );
  go.GetObjects( 1, 1 );
  rc = go.CommandResult();
  if( CRhinoCommand::success != rc )
    return rc;
 
  const CRhinoObject* captive = go.Object(0).Object();
  if( 0 == captive )
    return CRhinoCommand::failure;
 
  // Define the control line
  ON_Line line;
  CArgsRhinoGetLine args;
  rc = RhinoGetLine( args, line );
  if( CRhinoCommand::success != rc )
    return rc;
 
  // Get the curve parameters
  int degree = 3;
  int cv_count = 4;
  for(;;)
  {
    CRhinoGetOption gl;
    gl.SetCommandPrompt( L"NURBS Parameters" );
    gl.AcceptNothing();
    int d_opt = gl.AddCommandOptionInteger( RHCMDOPTNAME(L"Degree"), &degree, L"Curve degree", 1.0, 100.0 );
    int p_opt = gl.AddCommandOptionInteger( RHCMDOPTNAME(L"PointCount"), &cv_count, L"Number of control points", 2.0, 100.0 );
    gl.GetOption();
    rc = gl.CommandResult();
    if( CRhinoCommand::success != rc )
      return rc;
 
    if( CRhinoGet::nothing == gl.Result() )
      break;
 
    if( cv_count <= degree )
    {
      if( CRhinoGet::option != gl.Result() )
        continue;
      const CRhinoCommandOption* opt = go.Option();
      if( 0 == opt )
        continue;
      if( d_opt == opt->m_option_index )
        cv_count = degree + 1;
      else 
        degree = cv_count - 1;
    }
  }
 
  // Set up morph control
  ON_MorphControl* control = new ON_MorphControl();
  control->m_varient = 1; // 1= curve
 
  // Specify the source line curve
  control->m_nurbs_curve0.Create( 3, false, 2, 2 );
  control->m_nurbs_curve0.MakeClampedUniformKnotVector();
  control->m_nurbs_curve0.SetCV( 0, line.from );
  control->m_nurbs_curve0.SetCV( 1, line.to );
 
  // Specify the destination NURBS curve
  control->m_nurbs_curve.Create( 3, false, degree + 1, cv_count );
  control->m_nurbs_curve.MakeClampedUniformKnotVector();
  double* g = ws.GetDoubleMemory( control->m_nurbs_curve.m_cv_count );
  control->m_nurbs_curve.GetGrevilleAbcissae( g );
  ON_Interval d = control->m_nurbs_curve.Domain();
  double s = 0.0;
  int i;
  for( i = 0; i < control->m_nurbs_curve.m_cv_count; i++ )
  {
    s = d.NormalizedParameterAt( g[i] );
    control->m_nurbs_curve.SetCV( i, line.PointAt(s) );
  }
 
  // Make sure domains match
  s = line.Length();
  if( s > ON_SQRT_EPSILON )
    control->m_nurbs_curve0.SetDomain( 0.0, s );
  d = control->m_nurbs_curve0.Domain();
  control->m_nurbs_curve.SetDomain( d[0], d[1] );
 
  // Create the morph control object
  CRhinoMorphControl* control_object = new CRhinoMorphControl();
  control_object->SetControl( control );
  context.m_doc.AddObject( control_object );
 
  // Set up the capture
  RhinoCaptureObject( control_object, const_cast<CRhinoObject*>(captive) );
 
  // Clean up display
  context.m_doc.UnselectAll();
 
  // Turn on the control grips
  control_object->EnableGrips( true );
  context.m_doc.Redraw( CRhinoView::mark_display_hint );
 
  return rc;
}
CRhinoCommand::result CCommandSampleConvertQuadsToTriangles::RunCommand( const CRhinoCommandContext& context )
{
  CRhinoGetObject go;
  go.SetCommandPrompt( L"Select meshes to convert" );
  go.SetGeometryFilter( CRhinoObject::mesh_object );
  if( go.GetObjects(1,0) != CRhinoGet::object )
    return CRhinoCommand::cancel;
  
  CRhinoGetOption gs;
  gs.AcceptNothing();
  gs.SetCommandPrompt( L"Delete input?" );
  gs.SetDefaultString( (m_delete_input)?L"Yes":L"No" );
  int n_index = gs.AddCommandOption( RHCMDOPTNAME(L"No") );
  int y_index = gs.AddCommandOption( RHCMDOPTNAME(L"Yes") );
  switch( gs.GetOption() )
  {
    case CRhinoGet::option:
      if( gs.Option()->m_option_index == n_index )
        m_delete_input = false;
      else
        m_delete_input = true;
      break;
    
    case CRhinoGet::string:
    case CRhinoGet::nothing:
      break;
    
    default:
      return CRhinoCommand::cancel;
  }
  
  int num_converted = 0;
  for( int i = 0; i < go.ObjectCount(); i++ )
  {
    CRhinoObjRef objref = go.Object(i);
    if( const CRhinoObject* pObject = objref.Object() )
    {
      if( const ON_Mesh* pMesh = ON_Mesh::Cast(pObject->Geometry()) )
      {
        ON_Mesh mesh(*pMesh);
        if( mesh.QuadCount() <= 0 )
          continue;
        
        if( !mesh.ConvertQuadsToTriangles() )
          continue;
        
        const CRhinoMeshObject* mesh_object = NULL;
        if( m_delete_input )
          mesh_object = context.m_doc.ReplaceObject( objref, mesh );
        else
          mesh_object = context.m_doc.AddMeshObject( mesh );
        
        if( mesh_object )
          num_converted++;
      }
    }
  }
  
  if( num_converted > 0 )
    context.m_doc.Redraw();
  
  RhinoApp().Print( L"%d meshes selected, %d meshes converted.\n", go.ObjectCount(), num_converted );
  
  return CRhinoCommand::success;
}