Curve Workbench Macro: Curve to Freehand B-Spline

This Macro takes a selected curve and creates a freehand b-spline which follows its edge.  The macro is the start of creating a freeCAD version of the tween curve in Rhino3D for the Curves workbench.  The first hurdle is to convert a selected curve to a freehand b-spline.  I have decided that I just need to use the curve as an initial guide, so I don't want to connect the new bspline to it, but just follow the curvature.  The next steps would be to apply this to a ISO curve surface, creating multiple freehand b-spline for each edge.





Unlisted Video: https://youtu.be/fycOs-0Oy8M

The way I have tackled this is to take the selected curve and discretize the edge.  If you search for the meaning of this word then you will find numerous explanations, most of which are complicated and far too in depth to understand for the non-mathematical minded (or the average mathematical minded!).  I have found this a lot during my research, when authors explaining mathematical terms, it seems that the material is often consciously or unconsciously explained from the view point of someone who's ether studied or is in the process of studying towards some kind of qualification or degree.  The best and most direct explanation I could find is:

"The process of dividing geometry into finite elements often to prepare for analysis."

And this is exactly what we want to do, divide up the curve into points and assign the 3D coordinates of those points or vertices to the same number control points for a editable b-spline and finally discarding the original curve as we don't need it anymore.

TODO's and Limitations:

- Calculate the numberOfControlPoints automatically depending on the length of curve. If there are not enough, then the freehand b-spline doesn't follow the curve exactly.
- Allow for multi selection to create multiple b-splines.

The final macro from my research is as follows:

from freecad.Curves import gordon_profile_FP as GP 

#increase the points if dealing with more curves
numberOfControlPoints = 6 

#Get edge from selected
edge = Gui.Selection.getSelectionEx()[0].SubObjects[0] 

#Create the control points to add to the data
pts = edge.discretize(Number=numberOfControlPoints) 
typ = [0] * numberOfControlPoints 

#Create the bspline. Name will be appended with a unique extension if already exists
fp = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", "Freehand BSpline") 
GP.GordonProfileFP(fp, [], pts, typ) 
GP.GordonProfileVP(fp.ViewObject) 
fp.recompute() 
Gui.runCommand('Std_ToggleVisibility',0)

Resources:

Chris G the creator of FreeCAD Curves workbench has kindly passed on some documented code to show how the b-spline can be created via python:


from freecad.Curves import gordon_profile_FP as GP
sub = []
# If the Spline is snapped to other shapes
# add a link to these shapes. For example :
# sub = [(obj1, ("Edge1",)), (obj2, ("Face1",))]

# the interpolation points
pts = [FreeCAD.Vector(0, 0, 0), FreeCAD.Vector(5, 0, 0), FreeCAD.Vector(10, 0, 0)]

# the type of each point.
# 0 = free point
# 1 = point snapped to a shape of the sub list
# For example, typ = [1, 0, 1]
# means that the first point will snap to the fist shape of the sub list
# and the third point will snap to the second shape
typ = [0, 0, 0]

fp = FreeCAD.ActiveDocument.addObject("Part::FeaturePython", "Freehand BSpline")
GP.GordonProfileFP(fp, [], pts, typ)
GP.GordonProfileVP(fp.ViewObject)
fp.recompute()


Future Macros

Convert ISO Curve to Editable B-Splines (to come)

Convert ISO Curves to Editable Surface (to Come)

Comments

Popular posts from this blog

Reference Images For Beginners FreeCAD: Exercise 5.1 & 5.2

Beginner FreeCAD Tutorial 14: Reference Document for Exercise 1

FreeCAD For Beginners: CAD Thinking Part 1