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:

  1. Click the small round blue-and-white f(x) icon located on the right side of the constraint input dialog.
  2. 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...) and e (Euler's number).
  • Common Functions: sqrt() (square root), abs() (absolute value), ceil() (round up), floor() (round down), and round().
  • Trigonometry: sin(), cos(), tan(), asin(), acos(), and atan(). 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:

  1. Select the driving constraint in the sketch and assign it a name in the "Name" field of the constraint dialog (for example, MainWidth).
  2. Open the formula editor in the dependent constraint.
  3. Enter Constraints.MainWidth followed by your desired math, such as Constraints.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:

  1. Create a spreadsheet named Spreadsheet and enter a value into a cell.
  2. Right-click the cell, select Properties, click the Alias tab, and assign a unique alias (e.g., PlateThickness).
  3. In your sketch constraint formula editor, reference the cell by typing: Spreadsheet.PlateThickness
  4. 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 mm is valid; 10 mm + 5 or 10 mm + 5 deg will 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).