Draw a BezierCurve in dxf

Use this forum to ask questions about how to do things in dxflib.

Moderator: andrew

Post Reply
kayoot
Registered Member
Posts: 1
Joined: Thu Nov 26, 2009 11:05 am

Draw a BezierCurve in dxf

Post by kayoot » Thu Nov 26, 2009 11:13 am

Hello

I've got a problem with export to dxf. I need to export bezier curve, i have coordinates of start point, end point ang control points. Unfortunately, on the dxf entiti section there is no it.

Could anyone help me with this ?

THX a lot.


EDIT:

I try to do it by SPLINE but it isn't working...

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Post by andrew » Thu Nov 26, 2009 3:30 pm

Code: Select all

// write spline entity
dxf.writeSpline(writer, 
    DL_SplineData(degreeOfSpline,
                      numKnots,
                      numCtrl,
                      flags),
        attributes);

// write spline knots (example, might vary in your case):
int k = degreeOfSpline +1;
DL_KnotData kd;
for (int i=1; i<=numKnots; i++) {
        if (i<=k) {
            kd = DL_KnotData(0.0);
        } else if (i<=numKnots-k) {
            kd = DL_KnotData(1.0/(numKnots-2*k+1) * (i-k));
        } else {
            kd = DL_KnotData(1.0);
        }
        dxf.writeKnot(writer, kd);
}

// write spline control points (numCtrl control points):
dxf.writeControlPoint(writer, DL_ControlPointData(x, y, z));
dxf.writeControlPoint(writer, DL_ControlPointData(x, y, z));
...


wolfhergang
Junior Member
Posts: 11
Joined: Tue Oct 08, 2013 11:55 pm

Re: Draw a BezierCurve in dxf

Post by wolfhergang » Wed Oct 09, 2013 12:10 am

andrew wrote:

Code: Select all

// write spline entity
dxf.writeSpline(writer, 
    DL_SplineData(degreeOfSpline,
                      numKnots,
                      numCtrl,
                      flags),
        attributes);

// write spline knots (example, might vary in your case):
int k = degreeOfSpline +1;
DL_KnotData kd;
for (int i=1; i<=numKnots; i++) {
        if (i<=k) {
            kd = DL_KnotData(0.0);
        } else if (i<=numKnots-k) {
            kd = DL_KnotData(1.0/(numKnots-2*k+1) * (i-k));
        } else {
            kd = DL_KnotData(1.0);
        }
        dxf.writeKnot(writer, kd);
}

// write spline control points (numCtrl control points):
dxf.writeControlPoint(writer, DL_ControlPointData(x, y, z));
dxf.writeControlPoint(writer, DL_ControlPointData(x, y, z));
...

How did you calculated the knots in that code? im also trying to draw a bezier spline.

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: Draw a BezierCurve in dxf

Post by andrew » Thu Oct 10, 2013 10:48 am

The knot vector is part of what defines your spline.

You can find a good explanation and examples for useful knot vectors at:
http://www.cl.cam.ac.uk/teaching/2000/A ... 0000000000

wolfhergang
Junior Member
Posts: 11
Joined: Tue Oct 08, 2013 11:55 pm

Re: Draw a BezierCurve in dxf

Post by wolfhergang » Mon Oct 14, 2013 10:42 pm

Hello again Andrew, in your previous code you wrote this little formula:
kd = DL_KnotData(1.0/(numKnots-2*k+1) * (i-k));

to calculate the knot value in the mid section of the knot array, im reading the document you posted for me but i cant figure out how to assign that "constant", any advise?

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: Draw a BezierCurve in dxf

Post by andrew » Mon Oct 14, 2013 11:44 pm

This is just a way to evenly distribute the knot values and clamp the knot vector at both ends (make sure spline starts at first control point and ends at last control point).

For example:
0, 0, 0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.0, 1.0

wolfhergang
Junior Member
Posts: 11
Joined: Tue Oct 08, 2013 11:55 pm

Re: Draw a BezierCurve in dxf

Post by wolfhergang » Tue Oct 15, 2013 10:16 pm

Something wierd is happening to me now, in the bezier curve definition says that splineDegree=nControlPoints, but if i try to draw any spline with degree higher that 3 doesnt work.

im testing my writing program using QCAD by the way, it has something to do with QCAD methods?

im using the same formula Andrew implemented for knot calculation.

pd: sorry about my english, its not my native lenguage.

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: Draw a BezierCurve in dxf

Post by andrew » Tue Oct 15, 2013 10:43 pm

wolfhergang wrote:splineDegree=nControlPoints
The spline degree is either 2 (quadratic spline) or 3 (cubic spline). Higher-degree splines exist but are not supported by QCAD or dxflib (and I've never encountered any spline curves with a higher degree than 3 in real life applications).

However, what you might have read somewhere is that a spline of degree 2 (quadratic) needs at least 3 control points to be defined. A spline of degree 3 (cubic) needs at least 4 control points to be defined.

wolfhergang
Junior Member
Posts: 11
Joined: Tue Oct 08, 2013 11:55 pm

Re: Draw a BezierCurve in dxf

Post by wolfhergang » Tue Oct 15, 2013 10:57 pm

i did, and you've just saved me probably another 3 days of research about splines in just a min, i really thank you for your time and your wonderfull library, greeting from méxico.

wolfhergang
Junior Member
Posts: 11
Joined: Tue Oct 08, 2013 11:55 pm

Re: Draw a BezierCurve in dxf

Post by wolfhergang » Tue Oct 15, 2013 11:50 pm

I actually run into a new and even worse problem, Bezier curve definition says that Bezier curves k curve degree must always be equal to n control points... so if i want to draw a Bezier spline with 4+ control points im.... lost?

n= k = 4, so its not dxflib/QCAD compatible?

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: Draw a BezierCurve in dxf

Post by andrew » Wed Oct 16, 2013 12:09 am

The spline curves in QCAD / dxflib are NURBS (Non-uniform rational Basis-spline).

Bezier curves are related to NURBS as follows: every Bezier curve is also a NURBS. NURBS can be split up into multiple Bezier curves.

NURBS can have any number of control points (>degree).

User avatar
andrew
Site Admin
Posts: 9036
Joined: Fri Mar 30, 2007 6:07 am

Re: Draw a BezierCurve in dxf

Post by andrew » Tue Aug 18, 2015 9:24 am

algrass: I've split your unrelated question about spline control arms into a separate topic in the appropriate forum section:
viewtopic.php?f=32&t=3645

Post Reply

Return to “dxflib 'How Do I' Questions”