SDK Set Active View
Last changed: stevebaer-63.226.222.185

.
SummaryDemonstrates how to get and set the active view using the Rhino SDK.

The following code sample demonstrates how to get and set the active view using the Rhino SDK.

C++

  CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
  {
    // Get the active view
    CRhinoView* active_view = ::RhinoApp().ActiveView();
    if( !active_view )
      return CRhinoCommand::failure;


    // Get the name of the active view
    ON_wString active_view_name = active_view->Viewport().Name();


    // Get a list of all views
    ON_SimpleArray<CRhinoView*> view_list;
    context.m_doc.GetViewList( view_list );


    // Build a list of view names
    ON_ClassArray<ON_wString> view_names;
    int i;


    for( i = 0; i < view_list.Count(); i++ )
    {
      CRhinoView* view = view_list[i];
      if( view && view != active_view )
        view_names.Append( view->Viewport().Name() );
    }


    // Prompt the user for the view to set active
    CRhinoGetString gs;
    gs.SetCommandPrompt( L"Name of view to set active" );
    gs.AcceptNothing();
    gs.SetDefaultString( active_view_name );
    // Add view names as pickable command options
    for( i = 0; i < view_names.Count(); i++ )
      gs.AddCommandOption( RHCMDOPTNAME(view_names[i]) );


    // Do the getstring
    CRhinoGet::result res = gs.GetString();


    // User pressed ESC
    if( res == CRhinoGet::cancel )
      return CRhinoCommand::cancel;


    // User pressed ENTER
    if( res == CRhinoGet::nothing )
      return CRhinoCommand::nothing;


    ON_wString new_view_name;


    // New typed a string
    if( res == CRhinoGet::string )
      new_view_name = gs.String();


    // User clicked or typed an option
    if( res == CRhinoGet::option )
    {
      const CRhinoCommandOption* option = gs.Option();
      if( !option )
        return CRhinoCommand::failure;


      int option_index = option->m_option_index;
      if( option_index < 1 && option_index > view_names.Count() )
        return CRhinoCommand::failure;


      new_view_name = view_names[option_index-1];
    }


    // Compare view names
    if( new_view_name.CompareNoCase(active_view_name) == 0 )
      return CRhinoCommand::nothing;


    // Find the specified view by name
    active_view = 0;
    for( i = 0; i < view_list.Count(); i++ )
    {
      CRhinoView* view = view_list[i];
      if( view && view != active_view )
      {
        if( new_view_name.CompareNoCase(view->Viewport().Name()) == 0 )
        {
          active_view = view;
          break;
        }
      }
    }


    // Set the new active view
    if( active_view )
      ::RhinoApp().SetActiveView( active_view );
    else
      ::RhinoApp().Print( L"The \"%s\" view was not found.\n", new_view_name );
    return CRhinoCommand::success;
  }

VB.NET (Rhino 4)

  Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
    As IRhinoCommand.result
    ' Get the active view
    Dim active_view As MRhinoView = RhUtil.RhinoApp.ActiveView
    If (active_view Is Nothing) Then Return IRhinoCommand.result.failure


    ' Get the name of the active view
    Dim active_view_name As String = active_view.MainViewport().Name()
    ' Get a list of all views
    Dim view_list As MRhinoView() = Nothing
    context.m_doc.GetViewList(view_list, True, False)
    If (view_list Is Nothing) Then Return IRhinoCommand.result.failure


    ' Build a list of view names
    Dim view_names As New System.Collections.Generic.List(Of String)
    For Each view As MRhinoView In view_list
      Dim name As String = view.MainViewport().Name()
      If (name <> active_view_name) Then view_names.Add(name)
    Next


    ' Prompt the user for the view to set active
    Dim gs As New MRhinoGetString()
    gs.SetCommandPrompt("Name of view to set active")
    gs.AcceptNothing()
    gs.SetDefaultString(active_view_name)
    ' Add view names as pickable command options
    For Each name As String In view_names
      gs.AddCommandOption(New MRhinoCommandOptionName(name, name))
    Next


    ' Do the getstring
    Dim res As IRhinoGet.result = gs.GetString()


    ' User pressed ESC
    If (res = IRhinoGet.result.cancel) Then Return IRhinoCommand.result.cancel
    ' User pressed ENTER
    If (res = IRhinoGet.result.nothing) Then Return IRhinoCommand.result.nothing


    Dim new_name As String = ""
    ' New typed a string
    If (res = IRhinoGet.result.string) Then new_name = gs.String()


    ' User clicked or typed an option
    If (res = IRhinoGet.result.option) Then
      Dim get_option As IRhinoCommandOption = gs.Option()
      If (get_option Is Nothing) Then Return IRhinoCommand.result.failure
      Dim option_index As Integer = get_option.m_option_index
      If (option_index < 1 And option_index > view_names.Count) Then
        Return IRhinoCommand.result.failure
      End If
      new_name = view_names(option_index - 1)
    End If


    ' Compare view names
    If (new_name.Equals(active_view_name, StringComparison.OrdinalIgnoreCase)) Then
      Return IRhinoCommand.result.nothing
    End If


    ' Find the specified view by name
    active_view = Nothing
    For Each view As MRhinoView In view_list
      If (new_name.Equals(view.MainViewport().Name(), _
                          StringComparison.OrdinalIgnoreCase)) Then
        active_view = view
        Exit For
      End If
    Next


    If (active_view IsNot Nothing) Then
      RhUtil.RhinoApp().SetActiveView(active_view)
    Else
      RhUtil.RhinoApp().Print("The " + new_name + " view was not found" + vbCrLf)
    End If
    Return IRhinoCommand.result.success
  End Function

C# (Rhino 4)

  public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
  {
    // Get the active view
    MRhinoView active_view = RhUtil.RhinoApp().ActiveView();
    if (active_view == null)
      return IRhinoCommand.result.failure;


    // Get the name of the active view
    string active_view_name = active_view.MainViewport().Name();
    // Get a list of all views
    MRhinoView[] view_list = null;
    context.m_doc.GetViewList(ref view_list, true, false);
    if (view_list == null)
      return IRhinoCommand.result.failure;


    // Build a list of view names
    System.Collections.Generic.List<string> view_names =
      new System.Collections.Generic.List<string>();
    for (int i = 0; i < view_list.Length; i++)
    {
      MRhinoView view = view_list[i];
      if (view != null)
      {
        string name = view.MainViewport().Name();
        if( name != active_view_name)
          view_names.Add(name);
      }
    }


    // Prompt the user for the view to set active
    MRhinoGetString gs = new MRhinoGetString();
    gs.SetCommandPrompt( "Name of view to set active" );
    gs.AcceptNothing();
    gs.SetDefaultString(active_view_name);
    // Add view names as pickable command options
    for (int i = 0; i < view_names.Count; i++)
      gs.AddCommandOption(new MRhinoCommandOptionName(view_names[i], view_names[i]));


    // Do the getstring
    IRhinoGet.result res = gs.GetString();


    // User pressed ESC
    if (res == IRhinoGet.result.cancel)
      return IRhinoCommand.result.cancel;
    // User pressed ENTER
    if (res == IRhinoGet.result.nothing)
      return IRhinoCommand.result.nothing;


    string new_name = "";
    // New typed a string
    if (res == IRhinoGet.result.@string)
      new_name = gs.String();


    // User clicked or typed an option
    if (res == IRhinoGet.result.option)
    {
      IRhinoCommandOption option = gs.Option();
      if (option == null)
        return IRhinoCommand.result.failure;


      int option_index = option.m_option_index;
      if (option_index < 1 && option_index > view_names.Count)
        return IRhinoCommand.result.failure;
      new_name = view_names[option_index - 1];
    }


    // Compare view names
    if (new_name.Equals(active_view_name, StringComparison.OrdinalIgnoreCase))
      return IRhinoCommand.result.nothing;


    // Find the specified view by name
    active_view = null;
    for (int i = 0; i < view_list.Length; i++)
    {
      MRhinoView view = view_list[i];
      if (view != null)
      {
        if (new_view_name.Equals(view.MainViewport().Name(),
                                 StringComparison.OrdinalIgnoreCase))
        {
          active_view = view;
          break;
        }
      }
    }
    if (active_view != null)
      RhUtil.RhinoApp().SetActiveView(active_view);
    else
      RhUtil.RhinoApp().Print("The " + new_view_name + " view was not found\n");
    return IRhinoCommand.result.success;
  }