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.
Nice and simple! Is there away to assign a view template to the current view?
Posted by: Brett | September 22, 2011 at 06:17
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.
Posted by: Jeremy Tammik | September 22, 2011 at 06:32
cool i'll check it out thanks!
Posted by: Brett | September 22, 2011 at 06:33
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.
Posted by: guming | September 22, 2011 at 21:24
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.
Posted by: guming | September 23, 2011 at 02:33
Dear Guming,
Congratulations on solving, it, sounds good to me!
Cheers, Jeremy.
Posted by: Jeremy Tammik | September 23, 2011 at 02:38
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
Posted by: Dima | September 26, 2011 at 13:09
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.
Posted by: Jeremy Tammik | September 29, 2011 at 06:30
hi Jeremy, change View Template through API is currently unavailable, right? thanks and have a great weekends! Ning
Posted by: ning | December 02, 2011 at 18:39
Dear Ning,
I am not sure you are right. Doesn't the View.ApplyTemplate method fulfill your need?
Cheers, Jeremy.
Posted by: Jeremy Tammik | December 03, 2011 at 13:29
Hi Jeremy,
How would you Activate a 3D View while you are in IExternalApplication?
Thanks,
--Chi
Posted by: chi | January 21, 2013 at 18:32
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.
Posted by: Jeremy Tammik | January 29, 2013 at 08:12
TypePad HTML Email
Hi Jeremy,
Thanks for your help.
Best,
Posted by: chi | January 29, 2013 at 12:59
Hi,
Followed your code and got "Setting active view is temporarily disabled". Any ideas?
Thanks.
Posted by: Liz | June 24, 2013 at 16:53
Dear Liz,
Yes, sure. The system may be busy with something else. :-)
Cheers, Jeremy.
Posted by: Jeremy Tammik | June 27, 2013 at 07:58
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).
Posted by: Peter Wu | December 01, 2014 at 21:54