How to control points of a B-spline breaking it in multi-splines?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

How to control points of a B-spline breaking it in multi-splines?

Post by easyw-fc »

Hi,
referring to this thread [new feature] Bspline knots
I learned that
Chris_G wrote: Wed Apr 12, 2017 9:39 am It is always possible to increase the degree of a BSpline curve, while keeping its exact shape.
The algorithm will ( usually ? always ? I'm not sure ) add 1 control point for each degree increase step.
On the other hand, in the general case, there is no way to decrease the degree of the curve AND keep the original shape.
So what I would need in my user case is to break a part a Bspline into sub-splines all of the same degree (in my need 3), but with always the same number of control points (in my need 4 .
This is because I noticed that KiCAD, when importing a DXF file with a complex Bspline, always breaks it in sub-splines, all with degree = 3 and 4 control points.
I'm attaching a Bspline imported from a dxf file in FC and the counter part imported by KiCAD and then re-exported broken in sub-splines.
The question is:
Is it possible to break the first single spline sketch into multi-splines sketch within the degree=3 and with 4 control points by using a python script?

I'm attaching the FC file with the two Sketches and the images of the differences.
6knots-bspline.FCStd
(6.85 KiB) Downloaded 20 times
spline-comparison.png
spline-comparison.png (319.77 KiB) Viewed 1121 times
User avatar
Chris_G
Veteran
Posts: 2602
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: How to control points of a B-spline breaking it in multi-splines?

Post by Chris_G »

Here is an example :

Code: Select all

import FreeCAD
from FreeCAD import Vector
import Part

# Create and display an example BSpline Curve

poles0 = [Vector (-0.028185, 0.0, 0.0), Vector (1.775631, 5.242338, 0.0), Vector (11.104738, 6.454277, 0.0), Vector (16.065231, 0.338215, 0.0), Vector (10.428308, -3.917662, 0.0), Vector (7.356185, -4.509538, 0.0)]
weights0 = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
knots0 = [0.0, 0.3333333333333333, 0.6666666666666666, 1.0]
mults0 = [4, 1, 1, 4]
periodic0 = False
degree0 = 3
rational0 = False
bs = Part.BSplineCurve()
bs.buildFromPolesMultsKnots(poles0, mults0, knots0, periodic0, degree0, weights0, rational0)
obj = FreeCAD.ActiveDocument.addObject("Part::Spline","BSplineCurve")
obj.Shape = bs.toShape()
print("Input curve : %s (degree : %d / nb poles : %d)"%(bs, bs.Degree, bs.NbPoles))

# Set degree
if bs.Degree < 3:
    bs.increaseDegree(3)
elif bs.Degree > 3:
    # degree too high. We need to approximate the curve
    bs = bs.approximateBSpline(0.001,999,3) # (tolerance, maxSegments, maxDegree)

# Generate to a list of bezier curves
bezier_list = bs.toBezier()

# Check the result
for bc in bezier_list:
    print("%s (degree : %d / nb poles : %d)"%(bc, bc.Degree, bc.NbPoles))
    Part.show(bc.toShape())

chrisb
Veteran
Posts: 54305
Joined: Tue Mar 17, 2015 9:14 am

Re: How to control points of a B-spline breaking it in multi-splines?

Post by chrisb »

Could this be an approach to points and tangents on a B-spline or is the approximation not exact enough?
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
easyw-fc
Veteran
Posts: 3633
Joined: Thu Jul 09, 2015 9:34 am

Re: How to control points of a B-spline breaking it in multi-splines?

Post by easyw-fc »

Chris_G wrote: Sun Apr 14, 2019 1:35 pm Here is an example :
...
Hi @Chris,
thanks a lot as always!
Your code fulfills perfectly my user case. :D
Maurice
Last edited by easyw-fc on Mon Apr 15, 2019 8:30 am, edited 1 time in total.
User avatar
Chris_G
Veteran
Posts: 2602
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: How to control points of a B-spline breaking it in multi-splines?

Post by Chris_G »

chrisb wrote: Sun Apr 14, 2019 4:51 pm Could this be an approach to points and tangents on a B-spline or is the approximation not exact enough?
I don't know much about the sketcher internals.
But I don't think this is an approximation problem (you can choose the approx tolerance).
Post Reply