Resources Numerical accuracy Calculation policy

Lineweights in a DWG file

Converts a lineweight between its value in hundredths of a millimeter and the index a DWG file stores.

How the precision is kept

A DWG file stores a lineweight as an index into a fixed list of 24 widths. Writing picks the list value nearest to the lineweight asked for, so a width that is not in the list is changed to its neighbor. Reading returns the list value for the index.

Calculations under this policy

2 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.

dwg/model.js CadCraft 0.8.2 Exact 2 calculations Reviewed

Tests None found by the review

Lineweight to stored index

Exact

The index written for a lineweight, or the codes for by layer, by block and default.

index of the list value with the smallest |value − lineweight|

A lineweight between two list values is stored as the nearer one: 0.45 mm becomes 0.40 mm, because the comparison is strict and the first of two equally near values wins. A lineweight above 2.11 mm becomes 2.11 mm.

  • dwg/model.js
  • lines 212 to 219
  • CadCraft 0.8.2
export function lineweightIndex(lw) {
  if (lw === -1) return 29;
  if (lw === -2) return 30;
  if (lw === -3 || lw == null) return 31;
  let best = 0;
  for (let i = 0; i < LINEWEIGHTS.length; i++) if (Math.abs(LINEWEIGHTS[i] - lw) < Math.abs(LINEWEIGHTS[best] - lw)) best = i;
  return best;
}

Stored index to lineweight

Exact

The lineweight for an index read from a file.

A direct lookup. Indexes 24 to 28 and any index outside the list are returned as by layer (−1).

  • dwg/model.js
  • lines 205 to 211
  • CadCraft 0.8.2
export function lineweightFromIndex(i) {
  if (i === 29) return -1;
  if (i === 30) return -2;
  if (i === 31) return -3;
  // Spec gap: indexes 24–28 are not lineweights; the ODA File Converter reports them as ByLayer.
  return LINEWEIGHTS[i] ?? -1;
}

In detail

Method
Table lookup; nearest value by absolute difference when writing.
Number types
Whole numbers: widths in 1/100 mm, indexes 0 to 31.
Limits
Only the 24 listed widths from 0 to 2.11 mm can be stored. On a tie the smaller width is chosen.

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.

Every policy that takes this approach