![]() |
omdl
v1.0
OpenSCAD Mechanical Design Library
|
Length units and conversions. More...
Collaboration diagram for Length Units:Files | |
| file | length.scad |
| Length units and conversions. | |
Shorts | |
Convenience wrappers around length that fix the | |
| function | l_mm (v, d=1) |
| Shorthand length conversion for millimeters. More... | |
| function | l_in (v, d=1) |
| Shorthand length conversion for inches. More... | |
Functions and/or Modules | |
| function | length_unit_name (u=length_unit_default, d=1, w=false) |
| Return the name for a length unit identifier with dimension. More... | |
| function | length (v, from=length_unit_default, to=length_unit_base, d=1) |
| Convert a length value from one unit to another, with optional dimensional scaling. More... | |
| function | length_inv (v, from=length_unit_base, to=length_unit_default, d=1) |
| Recover a linear length from an area or volume value, then convert units. More... | |
Variables | |
| length_unit_base = "mm" | |
| length_unit_default = "mm" | |
Length units and conversions.
| file | group | script | results | no test | skipped | passed | failed | warning |
|---|---|---|---|---|---|---|---|---|
| units/length.scad | Length Units | Script | Results | 3 | 0 | 0 | 0 | 0 |
See complete validation results.
| Requires: |
|---|
| include <omdl-base.scad>; |
Parameter naming
v is the numeric value to convert. It has no default and must always be supplied by the caller. For d = 1 it may be a scalar or a list; the conversion is applied element-wise. For d = 2 or d = 3 it must be a scalar.from identifies the source unit; defaults to length_unit_default.to identifies the target unit; defaults to length_unit_base.d is the dimensional exponent: 1 (linear), 2 (area), 3 (volume). Values outside [1|2|3] yield undef.u is a unit-identifier string used in name-lookup functions; defaults to length_unit_default.w selects word (true) vs symbol (false) format in name-return functions; defaults to false.Return values
Global configuration
"mm" and is intended to be overridden at the top of a design file or within a child scope before any length function is called.from or u is not specified. It also defaults to "mm" and may be overridden in the same way.Unit identifiers
"mm", "in")."thou" and "mil" denote the same physical unit (one thousandth of an inch, 0.0254 mm) and are interchangeable in all conversion functions.Shorthand functions
l_mm() and l_in() helpers are convenience wrappers that fix from to their named unit and to to length_unit_base. The d parameter is forwarded and defaults to 1. Use the full length function when a non-default target unit is required.These functions allow for lengths to be specified with units. Lengths specified with units are independent of (length_unit_base). There are also unit conversion functions for converting from one unit to another.
The table below enumerates the supported units.
| units id | description |
|---|---|
| pm | picometer |
| nm | nanometer |
| um | micrometer |
| mm | millimeter |
| cm | centimeter |
| dm | decimeter |
| m | meter |
| km | kilometer |
| thou, mil | thousandth of an inch |
| in | inch |
| ft | feet |
| yd | yard |
| mi | mile |
Length base units example script
length_unit_base=mm script output
length_unit_base=cm script output
length_unit_base=mil script output
length_unit_base=in script output
| top |
|---|
![]() |
| function length_unit_name | ( | u | = length_unit_default, |
| d | = 1, |
||
| w | = false |
||
| ) |
Return the name for a length unit identifier with dimension.
| u | <string> A length unit identifier. |
| d | <integer> The unit dimension. One of [1|2|3]. |
| w | <boolean> true for word and false for symbol format. |
| function length | ( | v | , |
| from | = length_unit_default, |
||
| to | = length_unit_base, |
||
| d | = 1 |
||
| ) |
Convert a length value from one unit to another, with optional dimensional scaling.
| v | <decimal-list | decimal> The value to convert. May be a list when d = 1; must be a scalar when d = 2 or 3. |
| from | <string> The units of the value to be converted. Defaults to length_unit_default. |
| to | <string> The units to which the value should be converted. Defaults to length_unit_base. |
| d | <integer> The unit dimension. One of [1|2|3]. |
d = 1 returns the converted value; for d = 2 or 3 returns the converted linear value raised to the corresponding power. Returns undef for identifiers, dimensions, or input types that are not defined.
Here is the caller graph for this function:| function length_inv | ( | v | , |
| from | = length_unit_base, |
||
| to | = length_unit_default, |
||
| d | = 1 |
||
| ) |
Recover a linear length from an area or volume value, then convert units.
| v | <decimal> The area or volume value to invert. Must be a scalar for d = 2 or 3; may be a list for d = 1. |
| from | <string> The units of the input value v. Defaults to length_unit_base. |
| to | <string> The units to which the recovered linear value should be converted. Defaults to length_unit_default. |
| d | <integer> The unit dimension. One of [1|2|3]. |
d = 1 converts v directly; for d = 2 takes the square root of v before converting; for d = 3 takes the cube root. Returns undef for identifiers, dimensions, or input types that are not defined. | function l_mm | ( | v | , |
| d | = 1 |
||
| ) |
Shorthand length conversion for millimeters.
| v | <decimal> The value to convert. |
| d | <integer> The unit dimension. One of [1|2|3]. |
| function l_in | ( | v | , |
| d | = 1 |
||
| ) |
Shorthand length conversion for inches.
| v | <decimal> The value to convert. |
| d | <integer> The unit dimension. One of [1|2|3]. |
| length_unit_base = "mm" |
<string> The base units for value storage.
to parameter will convert to this unit. Definition at line 982 of file length.scad.
| length_unit_default = "mm" |
<string> The default units when unspecified.
from or u parameter will assume this unit. Definition at line 988 of file length.scad.