Site Tools


Bounding Box of a Curve Object

Summary: Demonstrates how to calculate the bounding box of a curve object.

Question

How I can obtain the bounding box of a curve object based on a reference plane using VB.NET?

Answer

Use the RhinoGetTightBoundingBox SDK function. By default, this function will compute the bounding box of an array of objects in world coordinates. But, if you specify the optional “plane” argument, the bounding box will be computed based on that plane.

Example

The following example code demonstrates how to calculate the bounding box of a curve object based on a reference plane.

VB.NET

' Select a curve object
Dim go As New MRhinoGetObject
go.SetCommandPrompt("Select curve")
go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.curve_object)
go.GetObjects(1, 1)
If (go.CommandResult() <> IRhinoCommand.result.success) Then
  Return go.CommandResult()
End If
 
' Validate selection
Dim obj As IRhinoObject = go.Object(0).Object()
If (obj Is Nothing) Then
  Return IRhinoCommand.result.failure
End If
 
' Get the active view
Dim view As MRhinoView = RhUtil.RhinoApp.ActiveView()
If (view Is Nothing) Then
  Return IRhinoCommand.result.failure
End If
 
' Get the active view's construction plane
Dim plane As New OnPlane(view.ActiveViewport().ConstructionPlane().m_plane)
 
' Compute the tight bounding box of the curve based on the 
' active view's construction plane
Dim bbox As New OnBoundingBox
Dim objs(0) As IRhinoObject
objs(0) = obj
Dim rc As Boolean = RhinoGetTightBoundingBox(objs, bbox, False, plane)
If (rc = False) Then
  Return IRhinoCommand.result.failure
End If
 
' Print the min and max box coordinates in world coordinates
Dim min As On3dPoint = bbox.Min()
Dim max As On3dPoint = bbox.Max()
RhUtil.RhinoApp.Print(String.Format("World min: {0},{1},{2}" + vbCrLf, min.x, min.y, min.z))
RhUtil.RhinoApp.Print(String.Format("World max: {0},{1},{2}" + vbCrLf, max.x, max.y, max.z))
 
' Create a world to construction plane transformation
Dim world_to_plane As New OnXform
world_to_plane.ChangeBasis(plane, OnUtil.On_xy_plane)
 
' Transform the bounding box
bbox.Transform(world_to_plane)
 
' Print the min and max box coordinates in cplane coordinates
min = bbox.Min()
max = bbox.Max()
RhUtil.RhinoApp.Print(String.Format("CPlane min: {0},{1},{2}" + vbCrLf, min.x, min.y, min.z))
RhUtil.RhinoApp.Print(String.Format("CPlane max: {0},{1},{2}" + vbCrLf, max.x, max.y, max.z))

C#

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  // Select a curve object
  MRhinoGetObject go = new MRhinoGetObject();
  go.SetCommandPrompt("Select curve");
  go.SetGeometryFilter(IRhinoGetObject.GEOMETRY_TYPE_FILTER.curve_object);
  go.GetObjects(1, 1);
  if (go.CommandResult() != IRhinoCommand.result.success)
    return go.CommandResult();
 
  // Validate selection
  IRhinoObject obj = go.Object(0).Object();
  if( obj == null )
    return IRhinoCommand.result.failure;
 
  // Get the active view
  MRhinoView view = RhUtil.RhinoApp().ActiveView();
  if( view == null )
    return IRhinoCommand.result.failure;
 
  // Get the active view's construction plane
  OnPlane plane = new OnPlane(view.ActiveViewport().ConstructionPlane().m_plane);
 
  // Compute the tight bounding box of the curve based on the 
  // active view's construction plane
  OnBoundingBox bbox = new OnBoundingBox();
  IRhinoObject[] objs = new IRhinoObject[1]{obj};
  bool rc = RhUtil.RhinoGetTightBoundingBox(objs, ref bbox,false, plane);
  if( rc == false)
    return IRhinoCommand.result.failure;
 
  // Print the min and max box coordinates in world coordinates
  On3dPoint min = bbox.Min();
  On3dPoint max = bbox.Max();
  RhUtil.RhinoApp().Print(string.Format("World min: {0}\n",min));
  RhUtil.RhinoApp().Print(string.Format("World max: {0}\n",max));
 
  // Create a world to construction plane transformation
  OnXform world_to_plane = new OnXform();
  world_to_plane.ChangeBasis(plane, OnUtil.On_xy_plane);
 
  // Transform the bounding box
  bbox.Transform(world_to_plane);
 
  // Print the min and max box coordinates in cplane coordinates
  min = bbox.Min();
  max = bbox.Max();
  RhUtil.RhinoApp().Print(string.Format("CPlane min: {0}\n",min));
  RhUtil.RhinoApp().Print(string.Format("CPlane max: {0}\n",max));
  return IRhinoCommand.result.success;
}
developer/sdksamples/curveboundingbox.txt · Last modified: 2015/09/14 (external edit)