Site Tools


Sample: Zoom to a Selected Object

C#

public static Rhino.Commands.Result ZoomToObject(Rhino.RhinoDoc doc)
{
  Rhino.DocObjects.ObjRef rhObject;
  var rc = Rhino.Input.RhinoGet.GetOneObject("Select object to zoom", false, Rhino.DocObjects.ObjectType.None, out rhObject);
  if (rc != Rhino.Commands.Result.Success)
    return rc;
 
  var obj = rhObject.Object();
  var view = doc.Views.ActiveView;
  if (obj == null || view == null)
    return Rhino.Commands.Result.Failure;
 
  var bbox = obj.Geometry.GetBoundingBox(true);
 
  const double pad = 0.05;    // A little padding...
  double dx = (bbox.Max.X - bbox.Min.X) * pad;
  double dy = (bbox.Max.Y - bbox.Min.Y) * pad;
  double dz = (bbox.Max.Z - bbox.Min.Z) * pad;
  bbox.Inflate(dx, dy, dz);
  view.ActiveViewport.ZoomBoundingBox(bbox);
  view.Redraw();
  return Rhino.Commands.Result.Success;
}

VB.NET

Public Shared Function ZoomToObject(doc As Rhino.RhinoDoc) As Rhino.Commands.Result
  Dim rhObject As Rhino.DocObjects.ObjRef
  Dim rc = Rhino.Input.RhinoGet.GetOneObject("Select object to zoom", False, Rhino.DocObjects.ObjectType.None, rhObject)
  If rc <> Rhino.Commands.Result.Success Then
    Return rc
  End If
 
  Dim obj = rhObject.Object()
  Dim view = doc.Views.ActiveView
  If obj Is Nothing OrElse view Is Nothing Then
    Return Rhino.Commands.Result.Failure
  End If
 
  Dim bbox = obj.Geometry.GetBoundingBox(True)
 
  Const pad As Double = 0.05
  ' A little padding...
  Dim dx As Double = (bbox.Max.X - bbox.Min.X) * pad
  Dim dy As Double = (bbox.Max.Y - bbox.Min.Y) * pad
  Dim dz As Double = (bbox.Max.Z - bbox.Min.Z) * pad
  bbox.Inflate(dx, dy, dz)
  view.ActiveViewport.ZoomBoundingBox(bbox)
  view.Redraw()
  Return Rhino.Commands.Result.Success
End Function

Python

import Rhino
import scriptcontext
 
def ZoomToObject():
    rc, rhobject = Rhino.Input.RhinoGet.GetOneObject("Select object to zoom", False, Rhino.DocObjects.ObjectType.None)
    if rc != Rhino.Commands.Result.Success: return
 
    obj = rhobject.Object()
    view = scriptcontext.doc.Views.ActiveView
    if not obj or not view: return
 
    bbox = obj.Geometry.GetBoundingBox(True)
 
    pad = 0.05  #A little padding...
    dx = (bbox.Max.X - bbox.Min.X) * pad
    dy = (bbox.Max.Y - bbox.Min.Y) * pad
    dz = (bbox.Max.Z - bbox.Min.Z) * pad
    bbox.Inflate(dx, dy, dz);
    view.ActiveViewport.ZoomBoundingBox(bbox)
    view.Redraw()
 
if __name__=="__main__":
    ZoomToObject()
developer/rhinocommonsamples/zoomtoobject.txt ยท Last modified: 2020/08/14 (external edit)