Site Tools


Creating Aligned Dimensions

.NET

Summary: Demonstrates how to create linear dimensions lined up with two points using the Rhino SDK.

Question

I would like to create aligned dimensions from my plug-in but I am not sure how. Can you help?

Answer

There are two ways you can create aligned dimensions. You can use the MArgsRhinoDimLinear class and the RhinoGetDimLinear function to interactively create a MRhinoLinearDimension object. Or, if you know the dimension points in advance, just create the MRhinoLinearDimension object by hand.

To interactivly create an aligned dimension, you can do something like the following:

C#

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  MArgsRhinoDimLinear args = new MArgsRhinoDimLinear();
  args.SetFirstPointPrompt( "First dimension point" );
  args.SetSecondPointPrompt( "Second dimension point" );
  args.SetDragPointPrompt( "Dimension location" );
  args.SetStyle( IArgsRhinoDimLinear.eStyle.aligned );
  args.SetIsInteractive( context.IsInteractive() ? true : false );
 
  MRhinoLinearDimension dim = new MRhinoLinearDimension();
  IRhinoCommand.result rc = RhUtil.RhinoGetDimLinear( ref args, out dim );
  if (rc == IRhinoCommand.result.success)
  {
    context.m_doc.AddObject( dim );
    context.m_doc.Redraw();
  }
 
  return rc;
}

If you know the dimension points in advance, you can create an aligned dimension by hand as follows:

C#

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  // Get the active view
  MRhinoView view = RhUtil.RhinoApp().ActiveView();
  if (view == null)
    return IRhinoCommand.result.failure;
 
  // Our hard-coded 3-D dimension points
  On3dPoint p0 = new On3dPoint(1, 1, 0);
  On3dPoint p2 = new On3dPoint(11, 11, 0);
  On3dPoint pt = new On3dPoint((p2.x-p0.x)/2.0, 3, 0);
 
  // Calculate the dimension plane
  OnPlane plane = new OnPlane(view.ActiveViewport().ConstructionPlane().m_plane);
  plane.SetOrigin(p0);
 
  // Calculate rotation angle
  double[] u = new double[2] { 0.0, 0.0 };
  double[] v = new double[2] { 0.0, 0.0 };
  plane.ClosestPointTo(p0, ref u[0], ref v[0]);
  plane.ClosestPointTo(p2, ref u[1], ref v[1]);
  On2dVector angle = new On2dVector(u[1]-u[0], v[1]-v[0]);
  angle.Unitize();
 
  // Rotate the plane
  plane.Rotate(angle.y, angle.x, plane.Normal());
 
  // Create new linear (aligned) dimension
  MRhinoLinearDimension dim = new MRhinoLinearDimension();
  dim.m_linear_dimension.m_type = IOn.eAnnotationType.dtDimAligned;
 
  // Set the dimension plane
  dim.SetPlane(plane);
 
  // Set the first plane-based point
  dim.SetPoint(0, new On2dPoint(0.0, 0.0));
 
  // Set the second plane-based point
  double s = 0, t = 0;
  plane.ClosestPointTo(p2, ref s, ref t);
  dim.SetPoint(2, new On2dPoint(s, t));
 
  // Set the remaining plane-based point
  plane.ClosestPointTo(pt, ref s, ref t);
  On2dPoint pt2d = new On2dPoint(s, t);
  dim.UpdateDimPoints(ref pt2d);
 
  // Calculate text location and format string
  dim.UpdateText();
 
  // Add to document
  context.m_doc.AddObject( dim );
  context.m_doc.Redraw();
 
  return IRhinoCommand.result.success;
}
developer/sdksamples/dimaligned.txt ยท Last modified: 2015/09/14 (external edit)