Activate a 3D View

Here is a simple question on making use of the ActiveView setter, which is new in Revit 2012:

Question: I heard that Revit 2012 exposes an API to change the active view.

I would like to change the active view to 3D with the Detail Level set to Fine and Visual Style set to Realistic.

Could you please show me the correct way to change the active view to the one I want?

Answer: I already discussed

setting the visual style for a view
,
so we can make use of that here.

To answer the rest of your query, I created a little sample application for you which says (and does) it all.

It is short and sweet, so I can present it here in its entire glory;
first, here is a helper method Get3dView to retrieve a suitable 3D view using a filtered element collector:


  /// <summary>
  /// Retrieve a suitable 3D view from document.
  /// </summary>
  View3D Get3dView( Document doc )
  {
    FilteredElementCollector collector
      = new FilteredElementCollector( doc )
        .OfClass( typeof( View3D ) );
 
    foreach( View3D v in collector )
    {
      Debug.Assert( null != v,
        "never expected a null view to be returned"
        + " from filtered element collector" );
 
      // Skip view template here because view 
      // templates are invisible in project 
      // browser
 
      if( !v.IsTemplate )
      {
        return v;
      }
    }
    return null;
  }

The external command Execute mainline makes use of this method to determine and activate the 3D view to use and set up the Detail Level and Visual Style parameters:


[Transaction( TransactionMode.Manual )]
public class Command : IExternalCommand
{
  public Result Execute(
    ExternalCommandData commandData,
    ref string message,
    ElementSet elements )
  {
    UIApplication uiapp = commandData.Application;
    UIDocument uidoc = uiapp.ActiveUIDocument;
    Application app = uiapp.Application;
    Document doc = uidoc.Document;
 
    // Find a suitable 3D view:
 
    View3D view = Get3dView( doc );
 
    if( null == view )
    {
      message = "Sorry, no suitable 3D view found";
 
      return Result.Failed;
    }
    else
    {
      // Must set view before starting the transaction,
      // otherwise an exception is thrown saying 
      // "Cannot change the active view of a modifiable 
      // document (with a transaction currently open)."
 
      uidoc.ActiveView = view;
 
      // Must start a transaction in order to set the 
      // parameters on the view:
 
      Transaction t = new Transaction( doc );
      t.Start( "Change to 3D view" );
 
      view.get_Parameter( BuiltInParameter
        .VIEW_DETAIL_LEVEL ).Set( 3 );
 
      view.get_Parameter( BuiltInParameter
        .MODEL_GRAPHICS_STYLE ).Set( 6 );
 
      t.Commit();
 
      return Result.Succeeded;
    }
  }
}

The one and only slightly tricky part is that writing to the ActiveView property to switch the view requires no transaction to be active, whereas setting the parameters does.

For completeness sake, here is
ChangeTo3dView.zip containing
the entire project, source code and add-in manifest file for this command.


Comments

16 responses to “Activate a 3D View”

  1. Nice and simple! Is there away to assign a view template to the current view?

  2. Dear Brett,
    Yes, it is, isn’t it? Thank you for your appreciation!
    And for assigning a view template to the current view, have you had a look at the View.ApplyTemplate method?
    Cheers, Jeremy.

  3. cool i’ll check it out thanks!

  4. Dear Jeremy
    I have a question about pickpoint() function,In 3DView,when I select “Front” or “Back” or “Left” or “Right” view,the pickpoint() will not return a point,because it can’t not pick a point on curent Workplane,is there any ideas to solve this problem?
    Thanks.
    Guming.

  5. Dear Jeremy
    I have already solved the problem with the method which creating another workplane to replace the old one,if you have good suggestion,please tell me.
    many thanks.
    Guming.

  6. Dear Guming,
    Congratulations on solving, it, sounds good to me!
    Cheers, Jeremy.

  7. Hi Jeremy,
    A quick question (just out of curiosity) regarding not wrapping the doc.ActiveView assignment. Technically by reassigning the active view one actually alters the database?
    Thanks,
    Dima

  8. Dear Dima,
    I have no idea what the reasoning behind this is, all I can do is speculate. Possibly setting the active view is such a precarious operation or somehow involves some mixture of internal user interface and database operations that Revit prefers to have full control over wrapping it into a transaction (or not) itself. Your guess is as good as mine.
    Cheers, Jeremy.

  9. hi Jeremy, change View Template through API is currently unavailable, right? thanks and have a great weekends! Ning

  10. Dear Ning,
    I am not sure you are right. Doesn’t the View.ApplyTemplate method fulfill your need?
    Cheers, Jeremy.

  11. Hi Jeremy,
    How would you Activate a 3D View while you are in IExternalApplication?
    Thanks,
    –Chi

  12. Dear Chi,
    If you mean ‘in the OnStartup event handler’ when you say ‘while you are in IExternalApplication’, then I would answer ‘not possible’.
    Cheers, Jeremy.

  13. TypePad HTML Email
    Hi Jeremy,
     
    Thanks for your help.
     
    Best,
     

  14. Hi,
    Followed your code and got “Setting active view is temporarily disabled”. Any ideas?
    Thanks.

  15. Dear Liz,
    Yes, sure. The system may be busy with something else. :-)
    Cheers, Jeremy.

  16. Peter Wu Avatar
    Peter Wu

    I have been waiting for View3d to provide one information that is missing ever since born.
    It is the orthographic scale, or perspective angle (both can be replaced by an image plane size).
    Without this, I cannot correctly draw my own points that satisfies the view (level of detail depends on the current view camera parameters).

Leave a Reply to gumingCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading