shahe
(Shahé)
September 30, 2021, 10:37am
1
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
wim
(Wim Dekeyser)
September 30, 2021, 12:14pm
2
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:
hi @simon9 ,
i coded up a small plug-in for that as i also often thougth this would be nice to have:
[image]
I’ll upload the current version here right now and add some more options later on.
Rhino 6 Windows
ScaleOverlay.rhp (44.5 KB)
Rhino 6 Mac
ScaleOverlay.macrhi (17.7 KB)
The Plugin Adds just one Command: ToggleScaleOverlay which enables (or disables) the overlay.
Let me know what you think i plan on adding a simple options page for color, textheight, margins, etc…
-wim
1 Like
shahe
(Shahé)
September 30, 2021, 1:34pm
3
Thank you Wim and apologies, I completely missed the scripting category. Will repost there. Thanks.
Shahe
wim
(Wim Dekeyser)
September 30, 2021, 1:35pm
4
shahe:
Will repost there.
No need - I’ll just recategorize this one.
-wim
shahe
(Shahé)
September 30, 2021, 2:24pm
5
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
dale
(Dale Fugier)
September 30, 2021, 7:02pm
6
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
shahe
(Shahé)
September 30, 2021, 10:31pm
7
Hi @dale , that is exactly what I was looking for!! Thank you very much for you help.
Shahe