Get bad curves when open the 3dm file which exported with openNURBS

my curve export code:

for(MyBSplineCurve* pCurve : curves) {
    if(pCurve; int pntCnt = pCurve->CtrlPoints().size()) {
        ON_NurbsCurve* rhinoCurve = new ON_NurbsCurve(
            3, // dimension
            true, // true if rational
            pCurve->Degree()+1,     // order = degree+1
            pntCnt      // number of control vertices
        for(int j = 0; j < pntCnt; j++) {
            ON_3dPoint pt(pCurve->CtrlPoints()[j].GetPos().x(), 
            rhinoCurve->SetCV(j, pt);
        // ON_NurbsCurve's have order+cv_count-2 knots
        int nKnots = pCurve->KnotVec().GetCount();
        lxd::print("order:{}, cv:{}, knots:{}\n", pCurve->Degree() + 1, pntCnt, nKnots);
        for(int k = 1; k < nKnots - 1; k++) {
            rhinoCurve->SetKnot(k, pCurve->KnotVec().GetKnots()[k]);
        auto name = fmt::format(L"RefCurve {}_{}", pair.first, w++);
            Internal_CreateManagedAttributes(layerRefCurve[i], name.c_str())

the 3dm file is attached

85RT_P1_20220915160131_15.3dm (328.1 KB)

Hi @tracyma,

Before adding your curve to the document, call it’s IsValid member like this:

ON_wString str;
ON_TextLog log(str);
bool rc = curve->IsValid(&log);
if (!rc)
  // TODO: dump "str" to a debug console

You’ll see this that the knot vector in each curve is invalid.

Knot vector order=4 and knot[2]=0 >= knot[3]=0 (should have knot[order-2] < knot[order-1]).
ON_NurbsCurve.m_knot[] is not a valid knot vector.

I also see you are creating rational NURBS curve, but you are specifying Euclidean points, not homogeneous points, where the 4-D representation is (x, y, z, w). You might have a look at this.

– Dale

1 Like

in openNURBS
nKnots = nCV + order - 2
in OpenGL
nKnots = nCV + order
remove the Superfluous Knots fix my errors, thanks