Keeps named grade rules (how far a pattern point moves for each size), converts them between millimeters and inches, and reads and writes them as RUL files.
How the precision is kept
A rule is a pair of 64-bit moves per size, measured from the base size. Applying a rule to a piece converts units with 25.4 or its reciprocal and rounds each move to 6 decimals. Writing a RUL file rounds each move to 2 decimals of the file's unit.
Open item. Write RUL deltas with more decimals, or tell the person on export that moves are rounded to 0.01 of the file's unit.
Calculations under this policy
4 calculations in 1 source file. The code is read from the released app when this page loads. It is the calculation people run, not a copy kept for this page.
Checks a rule from a file or from storage and sets the base size's move to zero.
Values are read with Number() and kept as read. An entry with a value that is not finite or is larger than 1e6 in size is refused. The base size's pair is overwritten with 0, 0 whatever the file held.
A library rule as moves for a piece's own size range and units.
move = round to 6 decimals of (m − base) × k; k = 1, 25.4 or 1 ÷ 25.4
The move is measured from the piece's base size, multiplied by 25.4 (inches to millimeters) or by the rounded reciprocal 1 ÷ 25.4, then rounded with toFixed(6). The value the piece keeps is the rounded one: 6 decimals of a millimeter or of an inch.
addons/apparel/lib/rules.js
lines 51 to 57
CadCraft 0.8.2
/** The entry as a rule for a piece's size range (names matched, measured from the piece's base) in the piece's units. */
export function ruleFor(entry, sizes, u) {
const k = entry.u === u ? 1 : entry.u === 'in' ? 25.4 : 1 / 25.4;
const at = (s) => { const i = entry.sizes.indexOf(s); return i >= 0 ? [entry.rule[2 * i], entry.rule[2 * i + 1]] : null; };
const b = at(sizes.base) || [0, 0];
return sizes.list.flatMap((s) => { const m = at(s) || b; return [+((m[0] - b[0]) * k).toFixed(6), +((m[1] - b[1]) * k).toFixed(6)]; });
}
Rules read from a RUL file
64-bit
Turns a RUL file's rules into library entries measured from the sample size.
move = value for the size − value for the sample size
One 64-bit subtraction per axis and size. Units are inches when the file's units start with ENG, else millimeters; no conversion is done here.
addons/apparel/lib/rules.js
lines 71 to 86
CadCraft 0.8.2
/** A RUL file's rules as entries ("<table> 7", or "<name> 7"). */
export function fromRul(text, name = '') {
const r = parseRul(text);
if (!r) return [];
const base = r.sizes.includes(r.sample) ? r.sample : r.sizes[0];
const bi = r.sizes.indexOf(base);
const label = clean(name || r.name || 'RUL', 60);
const out = [];
for (const [n, pairs] of r.rules) {
if (pairs.length < r.sizes.length) continue;
const rule = r.sizes.flatMap((_, i) => [pairs[i][0] - pairs[bi][0], pairs[i][1] - pairs[bi][1]]);
const e = cleanEntry({ name: label + ' ' + n, u: r.units.startsWith('ENG') ? 'in' : 'mm', sizes: r.sizes, base, rule });
if (e) out.push(e);
}
return out;
}
Rules written to a RUL file
64-bit
Writes the library's rules as the delta lines of a RUL file.
Each move is written with toFixed(2), two decimals of the file's unit, in fields 9 characters wide. A rule read back from this file is therefore rounded to 0.01 mm or 0.01 in.
addons/apparel/lib/rules.js
lines 100 to 101
CadCraft 0.8.2
const pairs = e.sizes.map((_, k) => [e.rule[2 * k], e.rule[2 * k + 1]]);
for (let k = 0; k < pairs.length; k += 4) L.push(pairs.slice(k, k + 4).map(([x, y]) => x.toFixed(2).padStart(9) + y.toFixed(2).padStart(9)).join(''));
In detail
Method
Moves are differences from the base (sample) size; sizes are matched by name. Inches to millimeters × 25.4; millimeters to inches × (1 ÷ 25.4).
Number types
64-bit numbers; text with 2 decimals in a RUL file.
Limits
A RUL file written here holds moves to 0.01 of its unit; 0.01 in is 0.254 mm. A move that is not finite or is above 1e6 in size makes an entry invalid.
Approach: 64-bit, stated limit
The arithmetic is 64-bit, but the method or the data sets the accuracy: a solver tolerance, a mesh, a step size, a datum shift, a table. The limit is stated where the result is shown.