Expressions

Make the values in your design calculate themselves. Type a formula in a property or a layout rule, and Flux updates the value when its inputs change.

Flux has a math expression engine in properties and PCB layout rules. Expressions work like spreadsheet formulas.

Overview

Expressions let you enter math functions as values in many contexts, including object properties and object rules.

Getting Started

This page covers the use cases for expressions:

  • What is an Expression? learn how to integrate expressions in your project

  • Expression Results: learn how Flux shows the result of an expression

  • Basic expressions: learn how to create basic math operations and unit management.

  • Advanced expressions: learn how to work with more advanced math functions and expressions.

  • Functions: learn how to use Flux-specific additional functions.

  • Accessing object data: learn how to access properties and rules inside objects.

  • Accessing project data: learn how to access project data like a URL or description

What is an Expression?

An expression puts dynamic data into a property or rule. Flux evaluates the expression in real time to calculate the value of the property or rule. For example:

Example: Position Rule

To set the position of an element, change the "Position Y" rule. Usually, you type the final position in the rule, for example 20mm.


With an expression, you can calculate the "Position Y" rule from another rule, like "Position X":


Expression Results

Flux displays an expression result below the input for object properties and object-specific rules, which produce a single value.


Selector-based rules can return multiple results. Flux displays the Outputs multiple values legend in that case.

Basic Expressions

//////// Expressions with units ////////
//Example 1 - Basic unit workflow
= 12mV
//This example returns: 0.012


// Example 2 - Convert units
= 22ohm to mOhm
//This example returns: 0.000022


//Example 3 - Flux uses default units if none are specified
= 22mm + 1
//This example returns: 1.866142inches

//////// Expressions with basic math ////////
//Example 4 - Basic Math
= 22deg * 2
//This example returns: 44deg

Advanced Expressions

Expressions support math functions, units, unit conversion, and strings.

//////// Working with advanced math formulas ////////
// Example 1 - Sine function
= sin(45 deg)
// This example returns: 0.7071067811865475

// Example 2 - Mixed math and units
= 9.81 m/s^2 * 5 s to mi/h
// This example returns: 109.72172512527


//////// Working with strings ////////
// Example 1 - Strings can be used in the evaluate function, to parse expressions inside the expression parser
= evaluate("2 + 3")
//This example returns: 5

Flux-specific Function Helpers

Flux adds functions for electronics calculations.

//////// Working with the Function ledBallast ////////
// Example 1 - Calculate the current-limiting resistor necessary for a 0.02A current flow and 2.9V voltage drop (Vsupply - Vforward)
= ledBallast(0.02, 2.9)
//This example returns: 145ohm


//////// Calculating E series values for resistors ////////
//////// Available functions: toE6, toE12, toE24, toE48, toE96 and toE192 ////////
// Example 2 - Round 17 to nearst E6 type Resistor
= toE6(17)
//This example returns: 15

Accessing Object Data

How you access object data depends on whether you use a layout rule or an object property.

Accessing Object Data from a Rule

In a layout rule, the .this directive has this structure:

this: {
uid: string;
parentUid: string;
type: string;
designator: string;
rules: {[key: string]: PcbLayoutRuleValue};
activeRules: {[key: string]: PcbLayoutRuleValue};
bakedRules?: IPcbLayoutBakedData;
// element represents the associated element the node belongs to
element?: {
designator?: string;
defaultPropertyValue?: string;
properties: {[pascalCasePropertyName: string]: PropertyValues};
part: {
name?: string;
url?: string;
};
};

Examples

// Example 1 - Get the objects uid
= this.uid
// This example returns: "<your objects uid>"

// Example 2 - Get the objects type
= this.type
// This example returns: "element"

// Example 3 - Get the objects position rule
= this.rules.position
// This example returns: "12mm 3mm"

// Example 4 - Get an object's element property called ManufacturerPartNumber
// property names will be convert to Pascal case. eg. "My property name" becomes "MyPropertyName"
= this.element.properties.ManufacturerPartNumber
// This example returns: "<Manufacturar Part Number>"

// Example 5 - get the objects element part name
= this.element.part.name
// This example returns: "Samsung Resistor"

Accessing Object Data from a Property

In a property, the .this directive has this structure:

this: {
designator?: string;
defaultPropertyValue?: string;
properties: {[pascalCasePropertyName: string]: PropertyValues};
part: {
name?: string;
url?: string;
};
// Careful, not all elements have an associated node
node?: {
uid: string;
parentUid: string;
type: string;
designator: string;
rules: {[key: string]: PcbLayoutRuleValue};
activeRules: {[key: string]: PcbLayoutRuleValue};
bakedRules?: IPcbLayoutBakedData;
};
}

Examples

// Example 1 - Get the object's designator
= this.designator
// This example returns: "<your objects designator>"

// Example 2 - Get an object's property called ManufacturerPartNumber
// Property names will be convert to Pascal case. eg. "My property name" becomes "MyPropertyName"
= this.properties.ManufacturerPartNumber
// This example returns: "<Manufacturar Part Number>"

// Example 3 - Get the object's node UID
= this.node.uid
// This example returns: "<UID>"

// Example 4 - Get the object's node rule called Position
= this.node.rules.position
// This example returns: "<Element position>"

Accessing Project Data

Project Data Schema

project: {
name: string;
description: string;
properties: {[pascalCasePropertyName: string]: PropertyValues};
url: string;
slug: string;
owner: {
handle: string;
url: string;
};
changeHistory: {
currentChangeUid: string;
currentChangeShortUid: string;
};
}

Examples

// Example 1 - get the project's name
= project.name
// This example returns: <your projects name>

// Example 2 - get the project's description
= project.description

//returns <your projects description>
// Example 3 - get the project's properties
// property names will be convert to Pascal case. eg. "My property name" becomes "MyPropertyName"
= project.properties.MyPropertyName
// This example returns: <my properties value>

// Example 4 - get the project's url
= project.url
// This example returns: <url of the project>

// Example 4 - get the project's slug
= project.slug
// This example returns: <slug of the project>