Using Formulas in FreeCAD Sketch Constraints
FreeCAD allows users to use mathematical formulas and expressions directly inside sketch dimension constraints. By utilizing the built-in Expression Engine, you can create fully parametric sketches driven by basic arithmetic, trigonometric calculations, or variables linked to other constraints and external spreadsheets. This guide covers how to access the expression editor within the Sketcher workbench, the syntax and functions supported, how to reference other values, and how to handle unit consistency.
Activating the Expression Editor
When applying or editing any dimensional constraint in the Sketcher—such as a distance, length, radius, diameter, or angle—you can invoke the formula editor in two ways:
- Click the small round blue-and-white f(x) icon located on the right side of the constraint input dialog.
- Clear the input box and type an equals sign (
=).
Once opened, the standard numeric input box transforms into the Expression Editor, allowing you to enter formulas rather than static numerical values.
Supported Mathematical Operations and Functions
FreeCAD's Expression Engine supports standard mathematical operators, common constants, and built-in functions:
- Basic Arithmetic: Addition (
+), subtraction (-), multiplication (*), division (/), and exponentiation (^). - Mathematical Constants:
pi(3.14159...) ande(Euler's number). - Common Functions:
sqrt()(square root),abs()(absolute value),ceil()(round up),floor()(round down), andround(). - Trigonometry:
sin(),cos(),tan(),asin(),acos(), andatan(). Angular arguments default to degrees or radians depending on system preferences, but explicit unit declarations prevent ambiguity (e.g.,sin(45 deg)).
Referencing Other Sketch Constraints
Formulas can dynamically reference other constraints within the same sketch:
- Select the driving constraint in the sketch and assign it a name in
the "Name" field of the constraint dialog (for example,
MainWidth). - Open the formula editor in the dependent constraint.
- Enter
Constraints.MainWidthfollowed by your desired math, such asConstraints.MainWidth / 2.
When MainWidth is updated, the dependent constraint
automatically recalculates and adjusts the geometry.
Referencing External Data and Spreadsheets
To drive sketches using global parameters, integrate the FreeCAD Spreadsheet workbench:
- Create a spreadsheet named
Spreadsheetand enter a value into a cell. - Right-click the cell, select Properties, click the
Alias tab, and assign a unique alias (e.g.,
PlateThickness). - In your sketch constraint formula editor, reference the cell by
typing:
Spreadsheet.PlateThickness - Combine this reference with formulas as needed, such as
Spreadsheet.PlateThickness + 2 mm.
Managing Units and Dimensional Compatibility
FreeCAD enforces strict dimensional analysis to prevent unit errors:
- Include Units: Always append appropriate units when
adding raw numbers to dimensional values (e.g.,
10 mm,1.5 in,90 deg). - Unit Consistency: Adding or subtracting values must
use identical dimensions (e.g.,
10 mm + 5 mmis valid;10 mm + 5or10 mm + 5 degwill produce an evaluation error). - Multiplication and Division: Multiplying two
lengths results in an area (
mm^2), which cannot be used for a length constraint. If you need to multiply values, ensure the resulting dimension matches the constraint type, or divide out unwanted units using dimensionless scalar multipliers (e.g.,(10 mm * 20 mm) / 1 mm).