Version 0.65.0

Unit Conversion

trCAD contains an internal mechanism to support a range of commonly used length units (see list below). You can set a length unit by using the document variable document.unit. By setting this value, you declare that this script is meant to be represented in this unit. The system then takes care of any necessary conversion on demand.

The Document Unit

The document.unit variable in the main script defines the reference length unit for the entire model. It is a string value with the following allowed values:

Value Description
"mu" Micron (1 μm = 0.001 mm)
"mm" Millimeter (default)
"cm" Centimeter (1 cm = 10 mm)
"in" Inch (1 in = 25.4 mm)
"ft" Foot (1 ft = 304.8 mm)
"m" Meter (1 m = 1000 mm)

The default value is "mm". When the document unit is set to a value other than "mm", all unit-qualified values in the model are interpreted in that unit. For example, if document.unit is set to "in", a float value of 12.0 represents 12 inches (304.8 mm), and a solid's volume is expressed in cubic inches.

Unit Qualifiers

Some data types can represent values with different unit semantics. A float could be a ratio, a weight, an angle (no unit), or it could be a length, an area, or a volume. A vector could be a direction (no unit) or a position/offset (length). For these ambiguous types, you should explicitly declare the unit qualifier when the value carries a unit. Such declaration is only required for open and public values that can be accessed from outside of the current script.

Types whose unit is unambiguous — such as solid, sketch, curve, sheet — do not require a qualifier: their geometric content is always expressed in the document unit. Types that carry no unit at all — int, bool, string, color, matrix (linear part) — do not require a qualifier as well.

The available unit qualifiers are:

Qualifier Meaning Applies to
length The value is a length float, vector
area The value is a surface area float
volume The value is a volume float

Applying the Qualifier

The unit qualifier is written between the data type and the variable name (or function name). It can be applied to the following declarations:

Open Parameters

Example

open float length edge_length
{
  name  = "Edge Length"
  descr = "Length of the edge"
  value = 12.0
}

Public Parameters

Example

public float length dLen = 100.0
public vector length dPos = vector( 10.0, 20.0, 30.0 )

Public Functions

The qualifier after function declares the unit of the return value. The qualifier after an argument's data type declares the unit of that argument:

Example

public function length getDiagonal( float length side )
{
  return side * 1.4142
}

Automatic Conversion Across Packages

When you import a package with #import (see multiple script files) and that package works in a different unit than the importing script, values crossing the boundary are automatically converted. Within each package, you simply think in your own unit.

Consider the following two files:

File "cube_pkg.sps":

Example

document.unit = "mm"
public float length dSide = 100.0
public function area getArea( )
{
  return dSide * dSide
}

Main script:

Example

document.unit = "m"
#import( "cube_pkg.sps" )
echo( cube_pkg::dSide )
echo( cube_pkg::getArea() )

Output

0.1
0.01

In the lower package, dSide is set to 100 (millimeters) and the area is computed as 10000 (square millimeters). In the main script, which works in meters, the value is read back as 0.1 m and the area as 0.01 m². No manual conversion is needed — the system handles the millimeter-to-meter scaling automatically when the values cross the package boundary.

See also

🗙

Search results for