Adding a NURBS Curve from Control Points
Windows only

Overview

Imagine you would like to create a NURBS curve from a set of control points, such that it looks like this:

NURBS Curve Control Points

There are two methods to achieve this…

Method 1

CRhinoCommand::result CCommandTest::RunCommand(
        const CRhinoCommandContext& context )
{
  ON_3dPointArray points;
  points.Append( ON_3dPoint(0, 0, 0) );
  points.Append( ON_3dPoint(0, 2, 0) );
  points.Append( ON_3dPoint(2, 4, 0) );
  points.Append( ON_3dPoint(4, 2, 0) );
  points.Append( ON_3dPoint(4, 0, 0) );
 
  CRhinoCommand::result res;
  ON_NurbsCurve nc;
  if ( nc.CreateClampedUniformNurbs( 3, 4, points.Count(), points)
    && nc.IsValid())
  {
    context.m_doc.AddCurveObject( nc );
    res = CRhinoCommand::success;
  }
  else
    res = CRhinoCommand::failure;

  context.m_doc.Redraw();
  return res;
}

Method 2

CRhinoCommand::result CCommandTest::RunCommand(
        const CRhinoCommandContext& context )
{
  ON_3dPointArray points;
  points.Append( ON_3dPoint(0, 0, 0) );
  points.Append( ON_3dPoint(0, 2, 0) );
  points.Append( ON_3dPoint(2, 4, 0) );
  points.Append( ON_3dPoint(4, 2, 0) );
  points.Append( ON_3dPoint(4, 0, 0) );

  int dimension = 3;
  bool bIsRat = false;
  int order = 4;
  int cv_count = points.Count();

  ON_NurbsCurve nc(dimension, bIsRat, order, cv_count);
  if( !nc.IsValid() )
  {
    return CRhinoCommand::failure;
  }

  //Set CV points
  nc.ReserveCVCapacity( cv_count );
  for( int i = 0; i < cv_count; i++ )
  {
    nc.SetCV(i, points[i] );
  }

  //Set Knots
  nc.ReserveKnotCapacity( order+cv_count-2 );
  ON_MakeClampedUniformKnotVector( order, cv_count, nc.m_knot );

  CRhinoCommand::result res;

  if( nc.IsValid() )
  {
    context.m_doc.AddCurveObject( nc );
    res = CRhinoCommand::success;
  }
  else
    res = CRhinoCommand::failure;

  context.m_doc.Redraw();
  return res;
}