[solved] Add spline with control points via Simple API

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

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
Irene
Newbie Member
Posts: 3
Joined: Tue Feb 21, 2023 4:02 pm

[solved] Add spline with control points via Simple API

Post by Irene » Tue Feb 21, 2023 4:17 pm

Hello,

Is it possible to add a spline with control points (instead of fit points) via Simple API?
I am trying to create a script for a sewing pattern drawing. So far it works, but i would need control point splines. The script is created from a C# tool, a kind of PoC for sewing pattern "macros".

Best thanks and regards
Irene

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

Re: Add spline with control points via Simple API

Post by andrew » Tue Feb 21, 2023 4:31 pm

Not directly through the Simple API, but for example like this:

Code: Select all

var sp = new RSpline();
sp.setPeriodic(false);    // true for closed, periodic
sp.appendControlPoint(new RVector(x1, y1));
sp.appendControlPoint(new RVector(x2, y2));
...
addShape(sp);
You might want to wrap this into your own function for convenience.

Irene
Newbie Member
Posts: 3
Joined: Tue Feb 21, 2023 4:02 pm

Re: Add spline with control points via Simple API

Post by Irene » Tue Feb 21, 2023 10:12 pm

Hello Andrew,

Many thanks for the quick reply :-)
I don't get an error so i suppose it works somehow, but the spline does not get drawn. Is there anything else to be done?
The spline should show up inside the green rectangle.

Code: Select all

var sp = new RSpline();
sp.setPeriodic(false);
sp.appendControlPoint(new RVector(138, 81.44444));
sp.appendControlPoint(new RVector(129.83333, 81.44444));
sp.appendControlPoint(new RVector(129.83333, 85));
addShape(sp);
I attach my full script just in case. Adding lines and points works like a charm.

Best thanks and regards
Irene
Attachments
Testdrawing.jpg
Testdrawing.jpg (50.2 KiB) Viewed 2424 times
script.js
(1.84 KiB) Downloaded 169 times

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

Re: Add spline with control points via Simple API

Post by andrew » Tue Feb 21, 2023 10:29 pm

The spline degree is 3 (cubic) by default which means the spline needs at least 4 control points. You can add another control point or change the degree to quadratic using setDegree:

Code: Select all

var sp = new RSpline();
sp.setPeriodic(false);
sp.setDegree(2);
sp.appendControlPoint(new RVector(138, 81.44444));
sp.appendControlPoint(new RVector(129.83333, 81.44444));
sp.appendControlPoint(new RVector(129.83333, 85));
addShape(sp);

Irene
Newbie Member
Posts: 3
Joined: Tue Feb 21, 2023 4:02 pm

Re: Add spline with control points via Simple API

Post by Irene » Wed Feb 22, 2023 8:54 pm

Hello Andrew,

Works like a charm, thank you very much :D

Best regards
Irene

Paul D
Newbie Member
Posts: 3
Joined: Fri Apr 05, 2024 10:36 pm

Re: Add spline with control points via Simple API

Post by Paul D » Thu Apr 11, 2024 9:56 pm

Very helpful thread. Thanks.

CVH
Premier Member
Posts: 3503
Joined: Wed Sep 27, 2017 4:17 pm

Re: Add spline with control points via Simple API

Post by CVH » Fri Apr 12, 2024 5:54 am

May I add that a closed spline with a minimal point count doesn't display correctly immediately after creation.
Although on manual creation the preview looks OK.

Code: Select all

var sp = new RSpline();
sp.setPeriodic(true);
sp.setDegree(2);
sp.appendControlPoint(new RVector(138, 81.44444));
sp.appendControlPoint(new RVector(129.83333, 81.44444));
sp.appendControlPoint(new RVector(129.83333, 85));
addShape(sp);
It shows fine when you save the file and reload it. :wink:
https://qcad.org/bugtracker/index.php?d ... sk_id=2453

@Paul D.
Remark that an open spline in degree 2 with 3 control points is a parabola or a part of that. :wink:

Regards,
CVH

Post Reply

Return to “QCAD 'How Do I' Questions”