Site Tools


Create a Box From a Curve's Bounding Box

Developer: .NET
Summary: Demonstrates how to create a BRep a box from a curve's bounding box.
Notice: The Rhino.NET SDK is deprecated in Rhino 5. This example adapted for the new RhinoCommon SDK is here.

Question

I am trying to draw a box. I've used the sample code for acquiring a bounding box. When I try to create the box I get complaints that there are no values in the array. Is there a better way to do this?

Answer

See the following example code below:

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 the selection
  IRhinoObject obj = go.Object(0).Object();
  if (null == obj)
    return IRhinoCommand.result.failure;
 
  // Get the active view
  MRhinoView view = RhUtil.RhinoApp().ActiveView();
  if (null == view)
    return IRhinoCommand.result.failure;
 
  // Get the construction plane from the active view
  OnPlane plane = new OnPlane(view.ActiveViewport().ConstructionPlane().m_plane);
 
  // Create a construction plane aligned bounding box
  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;
 
  // Validate bounding box
  if (0 != bbox.IsDegenerate())
  {
    RhUtil.RhinoApp().Print("Curve's tight bounding box is degenerate.\n");
    return IRhinoCommand.result.nothing;
  }
 
  // ON_BrepBox wants 8 points defining the box corners
  // arranged in this order:
  //
  //          v7______________v6
  //           |\             |\
  //           | \            | \
  //           |  \ _____________\ 
  //           |   v4         |   v5
  //           |   |          |   |
  //           |   |          |   |
  //          v3---|---------v2   |
  //           \   |          \   |
  //            \  |           \  |
  //             \ |            \ |
  //              \v0____________\v1
  //
  On3dPoint[] box_corners = new On3dPoint[8];
  box_corners[0] = bbox.Corner(0, 0, 0);
  box_corners[1] = bbox.Corner(1, 0, 0);
  box_corners[2] = bbox.Corner(1, 1, 0);
  box_corners[3] = bbox.Corner(0, 1, 0);
  box_corners[4] = bbox.Corner(0, 0, 1);
  box_corners[5] = bbox.Corner(1, 0, 1);
  box_corners[6] = bbox.Corner(1, 1, 1);
  box_corners[7] = bbox.Corner(0, 1, 1);
 
  // Transform points to the world-xy plane
  OnXform p2w = new OnXform();
  p2w.ChangeBasis(plane, OnUtil.On_xy_plane);
  for (int i = 0; i < 8; i++)
    box_corners[i].Transform(p2w);
 
  // Make a brep box
  OnBrep brep = OnUtil.ON_BrepBox(box_corners); 
  if (null != brep) 
  {
    context.m_doc.AddBrepObject(brep);
    context.m_doc.Redraw(); 
  }
 
  return IRhinoCommand.result.success;
}


developer/curvebrepbox.txt ยท Last modified: 2016/07/11 by sandy