Viewport dimension in world view

Hello,

Is there a way to get the width and height dimensions of the current viewport in python? I.e. measuring the extent of the view of the visible area in model units (in this case mm):

So when I zoom out, I can re-run the script and get the updated values:

Thank you in advance,

Shahe

Hi -
You posted this in the general “Rhino for Windows” category and not in “Scripting” so I’m not sure if you want to write something yourself or are looking for a script that someone wrote. For what it’s worth, there’s a plug-in that does something similar that you can download from here:

-wim

1 Like

Thank you Wim and apologies, I completely missed the scripting category. Will repost there. Thanks.

Shahe

No need - I’ll just recategorize this one.
-wim

Thank you @wim, I am looking for the equivalent of “rs.ViewSize()” for the model view so I can incorporate it into my script.

Shahe

Hi @shahe,

Are you looking for something like this?

import Rhino
import scriptcontext as sc

def get_viewport_size():
    
    # Get the active view
    view = sc.doc.Views.ActiveView
    if not view:
        return
    
    # Probably only makes senses in parallel projected views
    if not view.ActiveViewport.IsParallelProjection:
        return
    
    # Get the view size in screen coordinates (e.g. pixels)
    rect = view.ClientRectangle
    
    # Get viewport screen coordinate to world coordinate transformation
    screen_cs = Rhino.DocObjects.CoordinateSystem.Screen
    world_cs = Rhino.DocObjects.CoordinateSystem.World
    screen_to_world = view.ActiveViewport.GetTransform(screen_cs, world_cs)
    
    # Make some points like this:
    # p3----------------p2
    # |                  |     
    # |                  |
    # |                  |
    # |                  |
    # p0-----------------p1
    
    p0 = Rhino.Geometry.Point3d(rect.Left, rect.Bottom, 0.0)
    p0.Transform(screen_to_world)
    
    p1 = Rhino.Geometry.Point3d(rect.Right, rect.Bottom, 0.0)
    p1.Transform(screen_to_world)
    
    p2 = Rhino.Geometry.Point3d(rect.Right, rect.Top, 0.0)
    p2.Transform(screen_to_world)
    
    p3 = Rhino.Geometry.Point3d(rect.Left, rect.Top, 0.0)
    p3.Transform(screen_to_world)
    
    us = sc.doc.ModelUnitSystem
    str = Rhino.UI.Localization.UnitSystemName(us, False, True, True)
    
    # Print sizes
    print("Viewport width: {0} pixels".format(rect.Size.Width))
    print("Viewport height: {0} pixels".format(rect.Size.Height))
    print("Viewport width: {0} {1}".format(p0.DistanceTo(p1), str))
    print("Viewport height: {0} {1}".format(p0.DistanceTo(p3), str))

if __name__ == "__main__":
    get_viewport_size()

– Dale

1 Like

Hi @dale , that is exactly what I was looking for!! Thank you very much for you help.

Shahe