Zooming a new view

If you are creating a new view and then want to use the UIView.Zoom method, you need to make the new view the active view and then refresh the view. If you don’t, the attempt to zoom will be futile.

public void createNewViewAndZoom()
{
    Document doc = this.ActiveUIDocument.Document;
    UIDocument uidoc = this.ActiveUIDocument;
    ViewFamilyType vft3d = new FilteredElementCollector(doc).OfClass(typeof(ViewFamilyType)).Cast<ViewFamilyType>().FirstOrDefault(q => q.ViewFamily == ViewFamily.ThreeDimensional);
    View view = null;
    using (Transaction t = new Transaction(doc, "make views"))
    {
        t.Start();
        view = View3D.CreateIsometric(doc, vft3d.Id);
        t.Commit();
    }
    zoom(uidoc, view);            
}

private void zoom(UIDocument uidoc, View view)
{
    // YOU NEED THESE TWO LINES OR THE ZOOM WILL NOT HAPPEN!
    uidoc.ActiveView = view;
    uidoc.RefreshActiveView();
    
    UIView uiview = uidoc.GetOpenUIViews().Cast<UIView>().FirstOrDefault(q => q.ViewId == view.Id);
    uiview.Zoom(5);
}

6 thoughts on “Zooming a new view

  1. Hello Harry,
    First of all thank you so much for this awesome blog.
    I was trying a macro to open a specific view and zoom to fit but the uidoc.RefreshActiveView(); does not seem to work. I need to run it twice to get the zoom effect. Do you why?
    Here is my code:

    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = this.ActiveUIDocument.Document;
    string viewName = “TOOLBOX – GA Plan”;

    View v = new FilteredElementCollector(doc)
    .OfClass(typeof(View))
    .Where(n => n.Name == viewName)
    .Cast().FirstOrDefault();

    uidoc.ActiveView = v;
    uidoc.RefreshActiveView();

    UIView uiview = uidoc.GetOpenUIViews().Cast().FirstOrDefault(q => q.ViewId == v.Id);
    uiview.ZoomToFit();

    Many thanks in advance!

    Angel Ruiz-Peinado Sanchez

      • Hi Harry,
        Thank you very much for you time. I am working with Revit 2019, not sure if this has something to do. I have been testing further and still not working. In my case I have a parent view with content, and some dependant views (childs) cropped to show just part of the content.
        I have reproduced a sample file that you can download from this link:

        https://drive.google.com/file/d/1Vz2q60c3xWWh1MXVEueFdqrzeewgTyyq/view?usp=sharing

        I am using the code above replacing the viewName with “Level 2_CHILD 1”, “Level 2_CHILD 2”, etc. The first time I run the macro it opens the view (without zoom), once the view is open I run the macro again and it does the zoom. It is very strange.

        I really appreciate your time, so if you are busy or don´t feel comfortable testing this I completely understand.

        Kind regards,

        Angel Ruiz-Peinado Sanchez

        • After you activate the new view, try this. It will force Revit to recompute the extents of the view so then view knows what its proper size is when you call ZoomToFit

          using (Transaction t = new Transaction(doc, “View”))
          {
          t.Start();
          bool crop = v.CropBoxActive;
          v.CropBoxActive = !crop;
          v.CropBoxActive = crop;
          t.Commit();
          }
          uiview.ZoomToFit();

          • Thank you very much again for you time. I have tested your solution and I am afraid that it does not work. This is the code:

            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = this.ActiveUIDocument.Document;
            string viewName = “Level 2_CHILD 2”;

            View v = new FilteredElementCollector(doc)
            .OfClass(typeof(View))
            .Where(n => n.Name == viewName)
            .Cast().FirstOrDefault();

            uidoc.ActiveView = v;
            uidoc.RefreshActiveView();

            using (Transaction t = new Transaction (doc, “View”))
            {
            t.Start();
            bool crop = v.CropBoxActive;
            v.CropBoxActive = !crop;
            v.CropBoxActive = crop;
            t.Commit();
            }
            uidoc.RefreshActiveView();
            UIView uiview = uidoc.GetOpenUIViews().Cast().FirstOrDefault(q => q.ViewId == v.Id);
            uiview.ZoomToFit();

            uidoc.RefreshActiveView();

            I have refreshed the view 3 times. I have also tried to put the .ZoomToFit() inside the transaction with no results.

            I have discovered though, that if I use call-outs to the PARENT view instead of dependent views it works even without ZoomToFit(). I don´t want to take more of your time so I may go the “callout” route, but it is frustrating when Revit does not behave as expected.

            Thanks a lot again Harry,

            Angel Ruizz-Peinado Sanchez

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s