Scripting ZoomToScreenRect

Hi there,

nothing seems to happen in my view when using ZoomToScreenRect in below example script:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
import System

def DoSomething():
    
    rect = System.Drawing.Rectangle(50,50,400,300)
    view = scriptcontext.doc.Views.ActiveView.ActiveViewport
    info = Rhino.DocObjects.ViewportInfo(view)
    info.ZoomToScreenRect(rect)
    
    scriptcontext.doc.Views.ActiveView.Redraw()
    
DoSomething()

How is this supposed to work ?

thanks,
c.

Hi @clement,

One more step…

import Rhino
import scriptcontext
import System

def DoSomething():
    view = scriptcontext.doc.Views.ActiveView
    if view:
        vp_info = Rhino.DocObjects.ViewportInfo(view.ActiveViewport)
        rect = System.Drawing.Rectangle(50,50,400,300)
        vp_info.ZoomToScreenRect(rect)
        view.ActiveViewport.SetViewProjection(vp_info, True)
        view.Redraw()
    
DoSomething()

– Dale

Thank you @dale, i see that it changes the view now. However it also changes the Lens length. Is there a way to zoom to a screen rectangle while keeping the lens length untouched ? (like the _Zoom command which asks for a rectangle)

_
c.

There is always this:

Rhino.RhinoApp.RunScript("_Zoom 50,50 400,300", True)

I don’t believe there is a RhinoCommon equivalent.

– Dale

Hi @dale, thanks. It would be nice to have a scriptable (and working) version. Scripting the command does not work in perspective views. I see the same when just entering the 2d coordinates. It either does not zoom at all or zooms into the wrong location. I was trying to script something like requested here to zoom to the save frame or zoom in a way that object(s) are perfectly centered in the viewport. Zoom Extents or Zoom Selected does not do this.

_
c.

Hi @clement,

Sorry, this is probably a better sample:

import Rhino
import scriptcontext as sc

def test_zoom():
    rc, rect, view = Rhino.Input.RhinoGet.Get2dRectangle(True)
    if rc != Rhino.Commands.Result.Success:
        return
    view = sc.doc.Views.ActiveView
    if not view:
        return
    s2w = view.ActiveViewport.GetTransform(Rhino.DocObjects.CoordinateSystem.Screen,  Rhino.DocObjects.CoordinateSystem.World)
    p0 = Rhino.Geometry.Point3d(rect.Left, rect.Top, 0.0)
    p1 = Rhino.Geometry.Point3d(rect.Right, rect.Bottom, 0.0)
    p0.Transform(s2w)
    p1.Transform(s2w)
    scr = "_Zoom {0} {1}".format(p0, p1)
    Rhino.RhinoApp.RunScript(scr, True)
    
test_zoom()

But yeah, we should have something exposed in RhinoCommon.

https://mcneel.myjetbrains.com/youtrack/issue/RH-75756

– Dale

2 Likes

Thank you @dale, i figured that the coords are 3d so this explains why i had strange results (i passed 2d coordinates before).

_
c.

RH-75756 is fixed in Rhino 7 Service Release 33

1 Like