Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

[SOLVED] Defining the start view using Revit API

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
_CraigF
524 Views, 9 Replies

[SOLVED] Defining the start view using Revit API

Hi,

Is it possible to define the start view using the API ?
At the moment, I can only find a way to access the parameter and find out if a view is defined as the start view.

Thanks 😉

9 REPLIES 9
Message 2 of 10
jeremy_tammik
in reply to: _CraigF

Yes, you can. You could probably subscribe to and use the ApplicationInitialized event and the UIDocument.ActiveView property to achieve this:

 

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 10
_CraigF
in reply to: _CraigF

Thank you for your answer.

It's not quite what i want to do. with your solution the start view will be activated provided the plugin is installed. I prefer "Specify the Starting View for a Model".

 

Do you think it's possible ?

Message 4 of 10
jeremy_tammik
in reply to: _CraigF

If you want a solution that does not require programming, I am sorry to say that this is the wrong place to ask. Sorry.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 5 of 10
_CraigF
in reply to: _CraigF

Of course, I want to program.

I simply want to define the starting view once through programming, and then each time the file is opened, no plug-in is required (for example, if I send my mail to a customer).

Message 6 of 10
jeremy_tammik
in reply to: _CraigF

Aha. Well, as always, perform some research first to ensure that this functionality is present manually in the Revit end user interface. If not, the API will probably not be able to help.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 7 of 10
_CraigF
in reply to: _CraigF

Sure this is possible manually, as explained here : Specify the Starting View for a Model

The GetStartingViewSettings method lets you know if a view is already defined as the starting view. But I'd like to be able to define the starting view automatically via the API ?

Message 8 of 10
studio-a-int
in reply to: _CraigF

use this method to set your Starting View

void SetStartingView ()
{
// Code provided courtesy of:
// Studio A International, LLC
// http://www.studio-a-int.com
// The below code set the Starting View to a specific view that exists in Active Project
FilteredElementCollector feCollector = new FilteredElementCollector(activeDoc);
myView = feCollector.OfClass(typeof(Autodesk.Revit.DB.View)).Cast<Autodesk.Revit.DB.View>().Where<Autodesk.Revit.DB.View>(v => ViewType.ThreeD == v.ViewType && v.IsTemplate == false && v.Name == "my3DStartingView").ToList().FirstOrDefault();
FilteredElementCollector svsCollector = new FilteredElementCollector(activeDoc);
Autodesk.Revit.DB.StartingViewSettings svs = svsCollector.OfClass(typeof(StartingViewSettings))
.Cast<Autodesk.Revit.DB.StartingViewSettings>().ToList().FirstOrDefault();
if (myView is object)
{
ElementId myViewId = new ElementId(Convert.ToInt32((myView.Id.ToString())));
if (svs.IsAcceptableStartingView(myViewId))
{
using (Transaction t = new Transaction(activeDoc, "Set Starting View"))
{
t.Start("Set Starting View");
svs.ViewId = myViewId;
t.Commit();
}
}
}
}

Message 9 of 10
_CraigF
in reply to: _CraigF

@studio-a-int It works perfectly ! Thanks a lot ! 😲😁

Message 10 of 10

Just out of principle, to continue my endless (and hopeless?) struggle against unnecessary calls to ToList, may I point out that you can replace .ToList().FirstOrDefault() by a more efficient simple single call to FirstElement:

  

https://www.revitapidocs.com/2024/c8c1cae0-4ac8-a309-e915-6d491137d47e.htm

  

If your model happens to contain 1000 views, the call to ToList will marshal all 1000 views objects and their entire data into a list before throwing out the 999 unneeded ones.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report