0

I am trying to activate a view using Revit API. What I want to do exactly is to display the level or floor plan view. So the view I want to activate ( I mean, I want this view to be actually shown on screen) already exists, and I can access its Id.

I have seen threads about creating, browsing, filtering views, but nothing on activating it... It's a Floor Plan view. (What I want is that by selecting a level/ floor plan, it displays that level/ floor plan on-screen(it's like opening that floor plan from the already existing Revit model to display on the user screen).

Abhijit
  • 9
  • 4
  • here is the ActiveView Property https://www.revitapidocs.com/2022/b6adb74b-39af-9213-c37b-f54db76b75a3.htm – Sebastian Nov 01 '21 at 15:04

4 Answers4

2

Revit api docs link for RequestViewChange Method

use uidoc.RequestViewChange("Your View") sometimes view can't be changed while on processing transactions. On such case Force Close the Transaction. before this line uidoc.RequestViewChange("Your View")

Rocker-APK
  • 51
  • 6
0

Here is an example how to switch to the default 3d-view https://thebuildingcoder.typepad.com/blog/2011/09/activate-a-3d-view.html

you can do the same with all other available views like this

    UIApplication uiapp = commandData.Application;
    UIDocument uidoc = uiapp.ActiveUIDocument;
  
    uidoc.ActiveView = yourview;

to create a view from a level your code can look like this

ViewFamilyType viewFamilyType = (from elem in new 
    FilteredElementCollector(doc)
    .OfClass(typeof(ViewFamilyType))
    let type = elem as ViewFamilyType
    where type.ViewFamily == ViewFamily.FloorPlan
    select type).FirstOrDefault();

using (Transaction t = new Transaction(doc))
{
    t.Start("Create View");
    var floorPlan = ViewPlan.Create(doc, viewFamilyType.Id, yourLevel.Id);
    floorPlan.Name = "NewView";
    t.Commit();
}
Sebastian
  • 315
  • 1
  • 10
  • I have tried this. but I couldn't get the "view" to set to the "uidoc.ActiveView". – Abhijit Nov 12 '21 at 05:49
  • Unable to cast Autodesk.Revit.DB.Level to Autodesk.Revit.DB.View – Abhijit Nov 12 '21 at 06:11
  • you have to create a view from your level. use one of the available view types https://www.revitapidocs.com/2017/76bee86d-3c34-7ee1-4349-cd7abcbf3d78.htm and then .Create(level.Document, viewTypeId, level.Id); – Sebastian Nov 12 '21 at 07:15
  • I have created a ribbon tab and a button which opens a "WPF" from, in that there is drop down and here I have collected the levels/floor plan(e.g. Level1, Level2, etc. ) from the Revit project. So here I want to open ( I have added a button....click to "Show") a particular level/floor plan by selecting that particular level. – Abhijit Nov 14 '21 at 16:26
  • Can we filter views by Levels ?? – Abhijit Nov 15 '21 at 06:18
  • in the view members there is a LevelId. With this id you can filter your views by level https://www.revitapidocs.com/2019/d8d64cdb-46b7-6486-7cb5-07178b65a87b.htm – Sebastian Nov 15 '21 at 08:45
  • I have created a ribbon tab and a button which opens a "WPF" from, in that there is drop down and here I have collected the levels/floor plan(e.g. Level1, Level2, etc. ) from the Revit project. So here I want to open ( I have added a button....click to "Show") a particular level/floor plan by selecting that particular level. Does anyone get that?? – Abhijit Nov 16 '21 at 06:41
0

Super easy to do:

# normally you have the ui set at the start of your script
ui = __revit__.ActiveUIDocument 

# then just set the ActiveView as your view (not the ViewId)
ui.ActiveView = yourView
Callum
  • 578
  • 3
  • 8
0

FilteredElementCollector viewCollector = new FilteredElementCollector(doc);

                viewCollector.OfClass(typeof(View));

                foreach (Element viewElement in viewCollector)
                {
                        yourview = (View)viewElement;
                      
                        break;  
                }
            }

            uidoc.ActiveView = yourview;
Abhijit
  • 9
  • 4