Zoom to Fit in Dynamo?

Is there a way to do Zoom a view to Fit in Dynamo. Equivalent to mouse middle button double-click.

There should be a Zoom to Fit or Fit to Screen option in the contextual menu…

Looking for Zoom to Fit, programmatically in Dynamo

1 Like

Oh sorry, that wasn’t clear, this info could be useful to anyone coming here through the search field anyway… :slight_smile:

You can select all elements in the view and use this API method : http://www.revitapidocs.com/2015/6c40c35b-1b2b-1741-dafa-5ab6b1744634.htm

(similar to what happens when you click the green label in watch nodes)

The UIView.ZoomToFit Method is probably even better:

http://www.revitapidocs.com/2017/535f4910-37e0-0f46-924a-443899587d95.htm

import clr

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

for uv in uidoc.GetOpenUIViews():
	if uv.ViewId.Equals( doc.ActiveView.Id ):
		uiview = uv
		break
uiview.ZoomToFit()

OUT = 0
11 Likes

Hi, I am trying out python script. My intend is to get selected views to be zoom to fit. I have selected my views using dynamo nodes, and have made a script. It does not have any respond, only an error that the object does not have attribute zoom to fit. Would need some help here… Thanks alot

import clr

clr.AddReference(‘RevitAPIUI’)
from Autodesk.Revit.UI import *

clr.AddReference(‘RevitServices’)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

views = []
for i in IN[0]:
break
i.ZoomToFit()

TransactionManager.Instance.TransactionTaskDone()

OUT=views

1 Like

Hello @Loy_Seah.

I Believe that the ZoomToFit method refers to the UIView class from the Autodesk.Revit.UI namespace.
As far as I understand it, this means that you can only “ZoomToFit” views that are opened in the model, so that they are part of your Revit UI.

Ref:

There is a discussion about opening views through Dynamo, but it does not seem to have a great solution, but read here:

1 Like

I have used this code for ZoomToFit. Now, I am trying to output the results to save and export as an image. How can I output the view from this node?

1 Like

Hi.
You could use this routine to export the image:

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
views = FilteredElementCollector(doc).OfClass(View).ToElements()
for v in views:
    if v.Name == '*ViewName*':
        TransactionManager.Instance.ForceCloseTransaction()
        uiapp.ActiveUIDocument.RequestViewChange(v)
        TransactionManager.Instance.EnsureInTransaction(doc)
        options = ImageExportOptions()
        options.ExportRange = ExportRange.CurrentView
        options.HLRandWFViewsFileType = ImageFileType.JPEGLossless
        options.FitDirection = FitDirectionType.Horizontal
        options.ZoomType = ZoomFitType.FitToPage
        options.PixelSize = 2048
        options.ImageResolution.DPI_600
        options.FilePath = r'FILE PATH'  
        exportImage = doc.ExportImage(options)
        TransactionManager.Instance.TransactionTaskDone()

I hope this would be useful.
Cheers!

1 Like