I would like to capture a viewport with a fixed px/meters value. Viewport dimensions in pixels can be easily retrieved I guess. But I cannot figure out how to get/set the viewport zoom (top, parallel view) so it matches a desired area in meters. I tried that example: http://wiki.mcneel.com/developer/rhinocommonsamples/zoomtoobject. But even without padding, the viewport is still a little bigger than the bounding box of the object - so I cannot set the scale that way.
Am I missing something?
I just realized ViewNearCorners is better for your goal:
def ViewNearCorners(view=None):
"""Return 3d corners of a view's near clipping plane rectangle. Useful
in determining the "real world" size of a parallel-projected view
Parameters:
view:[opt] title or id of the view. If omitted, current active view is used
Returns:
Four Point3d that define the corners of the rectangle (counter-clockwise order)
"""
view = __viewhelper(view)
rc = view.ActiveViewport.GetNearRect()
return rc[0], rc[1], rc[2], rc[3]
I think the ViewRadius python method will get the information you want:
def ViewRadius(view=None, radius=None):
"""Returns or sets the radius of a parallel-projected view. Useful
when you need an absolute zoom factor for a parallel-projected view
Parameters:
view:[opt] title or id of the view. If omitted, current active view is used
radius:[opt] the view radius
Returns:
if radius is not specified, the current view radius for the specified view
if radius is specified, the previous view radius for the specified view
"""
view = __viewhelper(view)
viewport = view.ActiveViewport
if not viewport.IsParallelProjection: return scriptcontext.errorhandler()
fr = viewport.GetFrustum()
frus_right = fr[2]
frus_top = fr[4]
old_radius = min(frus_top, frus_right)
if radius is None: return old_radius
magnification_factor = radius / old_radius
d = 1.0 / magnification_factor
viewport.Magnify(d)
view.Redraw()
return old_radius
EDIT: bear in mind that when calculating with pixel values you have a fairly large error margin.
eg: 1000 pixels for a 100 meter area will result in a pixel representing 10 cm.