FreeCAD Macro: Create Freehand B-Spline from a Selected Curve
If you're exploring FreeCAD macros or looking to replicate Rhino3D’s tween curve functionality, this guide will walk you through how to create a freehand B-spline from a selected curve using the Curves workbench.
This macro is part of an ongoing effort to bring more organic, editable curve control into FreeCAD.
Goal: Convert a Selected Curve to a Freehand B-Spline
The first step in this project is converting a selected edge or curve into a freehand B-spline, not directly connected, but one that follows the curvature of the original.
This allows for more flexible editing and prepares us for the next stages, like applying this to ISO curves on a surface and generating multiple B-splines per edge.
Watch the Macro in Action
Here’s an unlisted video showing the macro in use:
https://youtu.be/fycOs-0Oy8M
What Is "Discretize" and Why Use It?
To build a B-spline, we first discretize the edge. This simply means:
“Dividing geometry into finite elements, often to prepare for analysis.”
We extract a set number of 3D coordinate points from the original curve and use them as control points for the new editable B-spline. The original curve is then discarded.
This process bridges mathematical complexity with practical CAD modeling.
Macro Code: From Curve to Editable B-Spline
Here’s the final macro (FreeCAD Python) that performs the conversion:
from freecad.Curves import gordon_profile_FP as GP
# Increase this for better curve-following
numberOfControlPoints = 6
# Get edge from selection
edge = Gui.Selection.getSelectionEx()[0].SubObjects[0]
# Discretize edge into points
pts = edge.discretize(Number=numberOfControlPoints)
typ = [0] * numberOfControlPoints
# Create the B-spline object
fp = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", "Freehand BSpline")
GP.GordonProfileFP(fp, [], pts, typ)
GP.GordonProfileVP(fp.ViewObject)
fp.recompute()
Gui.runCommand('Std_ToggleVisibility', 0)
Future Improvements (TODOs)
Some limitations still need attention:
-
Automatically calculate
numberOfControlPoints
based on curve length -
Enable multi-curve selection to generate multiple B-splines at once
Developer Notes and Advanced Resources
Thanks to Chris G, creator of the FreeCAD Curves workbench, for providing this additional code sample that shows how B-splines can be created using GordonProfileFP
:
from freecad.Curves import gordon_profile_FP as GP
sub = []
pts = [
FreeCAD.Vector(0, 0, 0),
FreeCAD.Vector(5, 0, 0),
FreeCAD.Vector(10, 0, 0)
]
typ = [0, 0, 0]
fp = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", "Freehand BSpline")
GP.GordonProfileFP(fp, [], pts, typ)
GP.GordonProfileVP(fp.ViewObject)
fp.recompute()
This can be adapted for snapping points to specific shapes using the sub
parameter.
What’s Coming Next?
Here’s what I plan to work on next with FreeCAD macros:
-
Convert ISO Curve to Editable B-Splines (in progress)
-
Convert ISO Curves to Editable Surface (coming soon)
Final Thoughts
This macro is a foundational step toward creating a Rhino3D-style workflow inside FreeCAD, especially for users who value surface and curve modeling. It's still early days, but the potential is exciting.
Have any feedback or ideas? Drop a comment or subscribe to get updates on future macro releases.
Comments
Post a Comment