units module#

Coordinate grid and number formatting for every file this package writes.

Everything is metric. Gerber files declare %FSLAX46Y46*% with %MOMM*%, so a coordinate integer is a nanometer; XNC declares METRIC and writes decimal millimeters off the same integers. All rounding goes through this module so a point shared by a copper file and a drill file serializes identically in both.

NM_PER_MM = 1000000#

Coordinate units per millimeter under FSLAX46Y46 with MOMM.

DECIMALS = 6#

Decimal places in the coordinate format, and in every emitted decimal.

HALF_STEP = 5e-07#

the largest error rounding can hide.

Type:

Half the output grid step in mm

nm(value)[source]#

Quantize a millimeter value onto the 1 nm output grid.

Raises:

ValueError – the value is not finite.

Return type:

int

Parameters:

value (float)

grid(value)[source]#

Snap a millimeter value to the nearest representable grid point.

Return type:

float

Parameters:

value (float)

coord(value)[source]#

Format a millimeter value as a Gerber coordinate field (X, Y, I, J).

Return type:

str

Parameters:

value (float)

decimal(value)[source]#

Format a millimeter or degree value as a Gerber decimal.

Used for AD parameters, AM primitive parameters and XNC tool diameters, all of which take decimals rather than coordinate integers.

Return type:

str

Parameters:

value (float)

xnc_decimal(value)[source]#

Format a millimeter value for an XNC coordinate or tool diameter.

The value comes off the 1 nm grid, so a drill hit and the copper around it agree exactly. Trailing zeros are trimmed, but never the decimal point: XNC permits a bare integer (“zero therefore must be encoded as 0”, 2.6), and readers descended from legacy Excellon reject one, because in that format a coordinate without a decimal point needs a number format the file does not carry. Writing 30.0 rather than 30 is a decimal either way and costs nothing.

Return type:

str

Parameters:

value (float)

quantize(value)[source]#

Round an interning key component onto the output grid.

One rule covers lengths and angles alike: 6 decimals is the nanometer grid for a millimeter and far finer than the 1e-4 degrees §6.5 asks for, so a key can never disagree with the text that gets emitted.

Return type:

float

Parameters:

value (float)