Create SubDFriendly curve by code

Hi;
I try to create a SubDFriendly curve by code, Which friend has a good idea ?

        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));

	ON_NurbsCurve* nc = ON_NurbsCurve::New();
	nc->CreateClampedUniformNurbs(3, 4, points.Count(), points);


	nc->IsSubDFriendly(true);//Try setting this parameter to work
	nc->SetSubDFriendlyTag(true);// And try setting this parameter to work


	if (nc->IsValid())
	{
		context.m_doc.AddCurveObject(*nc);
		context.m_doc.Redraw();
	}

	RhinoApp().ActiveDoc()->Redraw();
	return CRhinoCommand::success;

Hi @suc_kiet,

For example:

CRhinoCommand::result CCommandTest::RunCommand(const CRhinoCommandContext& context)
{
  ON_3dPointArray points;
  points.Append(ON_3dPoint(0.0, 0.0, 0.0));
  points.Append(ON_3dPoint(0.0, 2.0, 0.0));
  points.Append(ON_3dPoint(2.0, 4.0, 0.0));
  points.Append(ON_3dPoint(4.0, 2.0, 0.0));
  points.Append(ON_3dPoint(4.0, 0.0, 0.0));

  ON_NurbsCurve nurb;
  nurb.CreateClampedUniformNurbs(3, 4, points.Count(), points.Array());

  if (nurb.IsValid())
  {
    ON_NurbsCurve nurb_friendly;
    if (ON_SubD::CreateSubDFriendlyCurve(nurb, &nurb_friendly))
      context.m_doc.AddCurveObject(nurb_friendly);
    else
      context.m_doc.AddCurveObject(nurb);
  }

  context.m_doc.Redraw();

  return CRhinoCommand::success;
}

Note, I’ve re-written your sample because it was leaking memory. CRhinoDoc::AddCurveObject makes a copy of the input curve. So make sure to delete the pointer you allocated with ON_NurbsCurve::New().

– Dale

Hi@dale;
Thank you, it run good.

 So make sure to delete the pointer you allocated with ON_NurbsCurve::New().

So I must call “delete nc” after call “context.m_doc.AddCurveObject(*nc);”, right ?

Hi @suc_kiet,

You can do this:

ON_NurbsCurve curve;
// TODO...
context.m_doc.AddCurveObject(curve);

or this:

ON_NurbsCurve* ptr_curve = ON_NurbsCurve::New();
// TODO...
context.m_doc.AddCurveObject(*ptr_curve); // Copies ptr_curve data
delete ptr_curve; // Don't leak...

or this:

ON_NurbsCurve* ptr_curve = ON_NurbsCurve::New();
// TODO...
CRhinoCurveObject* ptr_curve_obj = new CRhinoCurveObject();
ptr_curve_obj->SetCurve(ptr_curve); // ptr_curve_obj now owns ptr_curve_obj
context.m_doc.AddObject(ptr_curve_obj);

Let me know if you have any qustions.

– Dale

Hi@dale;
Thank you for your help

1 Like