Espressioni

From FreeCAD Documentation
Revision as of 19:14, 8 October 2015 by Renatorivo (talk | contribs) (Created page with "Le funzioni trigonometriche utilizzano i gradi come unità di default; fornire "rad" se si desidera che utilizzino i radianti.")

introduced in version 0.16

Ora è possibile definire le proprietà utilizzando espressioni matematiche. Nella GUI, gli spin box o i campi di input che sono legati alle proprietà contengono un'icona blu File:Sketcher Expressions.png. Cliccando sull'icona, oppure digitando il segno di uguale (=), si porta in primo piano l'editor delle espressioni per quella particolare proprietà.

Una espressione di FreeCAD è un'espressione matematica che segue la normale notazione per gli operatori matematici standard (+, -, * e /) e le funzioni come descritto in seguito. Inoltre, l'espressione può fare riferimento ad altre proprietà, e anche utilizzare le condizioni. I numeri di un'espressione possono opzionalmente essere collegati ad una unità di misura.

Gli operatori e le funzioni sono unit-aware (consapevoli delle unità), e richiedono combinazioni di unità valide, se sono fornite. Ad esempio, 2mm + 4mm è un'espressione valida, mentre 2mm + 4 non lo è (il motivo di questo è che un'espressione come 1in + 4 molto probabilmente viene interpretata come 1in + 4in da un umano, ma tutte le unità vengono convertite nel SI sistema interno, e il sistema non è in grado di indovinare questo).

Funzioni Supportate

Nelle espressioni, sono supportate le seguenti funzioni, usando uno o due argomenti: abs, acos, asin, atan, atan2, cos, cosh, exp, log, log10, mod, pow, sin, sinh, sqrt, tan, tanh, ceil, floor, trunc, round.

Le funzioni trigonometriche utilizzano i gradi come unità di default; fornire "rad" se si desidera che utilizzino i radianti.

The constants pi and e are predefined.

The conditional statement works like this: = condition ? resultTrue : resultFalse

The condition is defined as an expression that evaluates to either 0 (for false) or non-zero for true. The following comparison operators are defined: ==, !=, >, <, >=, and <=.

Formulas are written as e.g follows: 2 * Length, where Length is a property in the object holding the expression.

Reference To CAD-Data

It is possible to use data from other parts of the construction in an expression. The syntax for a reference to a field is as follows: object.property. If the property is a compound of fields, they can be accessed by e.g object.property.field.

The following table shows some examples:

CAD-Data Call in expression Result
Parametric Length of a Part-Workbench Cube Cube.Length Length with units mm
Volume of the Cube Cube.Shape.Volume Volume in mm³ without units
Type of the Cube-shape Cube.Shape.ShapeType String: Solid
Label of the Cube Cube.Label String: Cube
x-coordinate of center of mass of the Cube Cube.Shape.CenterOfMass.x x-coordinate in mm without units
Value of constraint in a sketch Constraints.Width Numeric value of the named constraint 'Width' in the sketch

Global variables

There is no concept of global variables in FreeCAD at the moment. Instead, arbitrary variables can be defined as cells in a spreadsheet using the Spreadsheet workbench, and then be given a name using the alias property for the cell (right-click on cell). Then they can be accessed from any expression just as any other object property.

Known issues / remaining tasks

  • The dependency graphs is based on the relationship between document objects, not properties. This means that you cannot provide data to and query the same object in e.g a spreadsheet, even though there are no cyclic dependency when just the properties are considered. As a work-around, use multiple spreadsheets, e.g one to drive your model, and one for reporting.
  • There is no expression manager implemented where all expressions in a document are listed, and can be created, deleted, queries, etc.
  • The Data properties tab does not yet support expressions.
  • Renaming aliases or inserting/removing rows/columns in a spreadsheet will not propagate changes to expressions in the document.