How to Plot Mathematical Functions in FreeCAD
This guide explains how to generate 2D and 3D mathematical function plots directly within FreeCAD. You will learn the two most effective methods: using the GUI-based Curves Workbench for parametric equations, and utilizing FreeCAD’s built-in Python console to programmatically generate precise curves and B-splines from custom mathematical formulas.
Method 1: Using the Curves Workbench (GUI Method)
The Curves Workbench provides a dedicated tool for plotting parametric equations without writing external code.
Step 1: Install the Curves Workbench
- Open FreeCAD.
- Navigate to Tools > Addon manager.
- In the Workbenches tab, search for Curves.
- Select it, click Install, and restart FreeCAD when prompted.
Step 2: Create a Parametric Curve
- Switch to the Curves workbench using the workbench dropdown menu.
- In the toolbar, click the Parametric Curve icon (a curve marked with a formula symbol).
- Select the created
ParametricCurveobject in the Model tree to inspect its Data properties in the Property View panel. - Modify the properties to define your function:
- Formula X: Enter the formula for the X coordinate
as a function of parameter
t(e.g.,cos(t)). - Formula Y: Enter the formula for the Y coordinate
as a function of parameter
t(e.g.,sin(t)). - Formula Z: Enter the formula for the Z coordinate
(e.g.,
t * 0.1for a helix, or0for a flat 2D plot). - Start: Set the minimum value for
t(e.g.,0). - End: Set the maximum value for
t(e.g.,6.28for \(2\pi\)). - Number of points: Increase this value (e.g.,
100to500) to improve curve smoothness.
- Formula X: Enter the formula for the X coordinate
as a function of parameter
- Press F5 or click the Recompute button to render the curve in the 3D viewport.
Method 2: Using the Python Console (Scripting Method)
For standard explicit functions (such as \(y = f(x)\)) or complex datasets, you can generate points using FreeCAD’s internal Python API and connect them with an interpolated B-spline.
Step 1: Open the Python Console
- Go to View > Panels.
- Ensure Python console is checked.
Step 2: Run the Plotting Script
Paste the following script into the console, adjusting the formula in the loop as needed:
import FreeCAD as App
import Part
import math
doc = App.activeDocument()
if not doc:
doc = App.newDocument("MathPlot")
# Define plot bounds and resolution
x_start = -10.0
x_end = 10.0
steps = 200
step_size = (x_end - x_start) / steps
points = []
for i in range(steps + 1):
x = x_start + i * step_size
# Define your function here: y = f(x)
# Example: y = sin(x) / x (Sinc function)
y = math.sin(x) if x != 0 else 1.0
z = 0.0
points.append(App.Vector(x, y, z))
# Create an interpolated B-Spline curve through the generated points
curve = Part.BSplineCurve()
curve.interpolate(points)
# Create a Part feature in the document
plot_object = doc.addObject("Part::Feature", "FunctionPlot")
plot_object.Shape = curve.toShape()
doc.recompute()Converting Plots to Usable 3D Geometry
Once your mathematical curve appears in the viewport:
- Pad/Extrude: Switch to the Part Workbench and use the Extrude tool to turn a 2D plot into a flat sheet or wall.
- Sweep: Create a small circular profile at one end of the curve in the Sketcher Workbench, then use the Part Sweep tool to create a solid wire, pipe, or spring along the function trajectory.