@rulecad/printcheck 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +10 -4
  2. package/package.json +1 -1
  3. package/printcheck.mjs +44 -14
package/README.md CHANGED
@@ -19,15 +19,21 @@ triangulated.
19
19
  What it checks:
20
20
 
21
21
  - **Fits the bed** (`bed`, mm) and **sits on it**: lowest point at z = 0, with faces flat on the bed.
22
- - **Overhangs** steeper than `overhang` degrees from vertical.
22
+ - **Overhangs** steeper than `overhang` degrees from vertical (with half a degree to spare: a boolean tips a 45°
23
+ chamfer by hundredths of a degree).
24
+ - **Inside out**: a mesh whose faces all point inwards (corners running clockwise seen from outside) fails as that,
25
+ before anything else is read from it.
23
26
  - **Flat undersides above the bed**: each must start on something. Either it rests on a built-in **breakaway support**
24
27
  (one `support_gap` below a surface), or it **bridges** no wider than `bridge` mm. The bridge is the widest circle that
25
28
  fits in the underside, measured to the edges held up by material below. An underside held by nothing starts in
26
- mid-air. One held on one side only, further out than a print line, is a **ledge**: nothing bridges it, each line
27
- is laid past the last in the air, and it droops (slant its underside in at 45° to the wall instead).
29
+ mid-air. One held on one side only (its supports all within a half turn of it, and a little), further out than a
30
+ print line, is a **ledge**: nothing bridges it, each line is laid past the last in the air, and it droops (slant its
31
+ underside in at 45° to the wall instead). What lies within a breakaway gap or two layers of the bed, or of a
32
+ surface below it, rests on it. Undersides within a layer of each other are one height; what starts in mid-air
33
+ and what hangs as a ledge is said once per height, with how many and the worst, and where.
28
34
  Each underside is taken on its own (the faces at a height joined where they touch), and an edge is held wherever
29
35
  it lies along the top edge of a wall below (booleans leave T-junctions; they don't matter). An underside no wider
30
- than a print line (`line`, 0.4 mm unless given) anywhere is a step of a slope printed in layers, or a sliver the
36
+ than a print line (`line`, 0.42 mm unless given) anywhere is a step of a slope printed in layers, or a sliver the
31
37
  slicer lays nothing on, and is not counted.
32
38
  - **The first layer**: two parts printed in place must stand `print_gap` plus twice `foot` apart on the bed (`foot`
33
39
  is how far the first layer spreads each side as it is squashed on: elephant's foot); nearer, they print as one.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/printcheck",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Is this mesh printable? Bed fit, flat base, overhangs, bridges, breakaway supports and watertightness, for any FDM printer.",
5
5
  "type": "module",
6
6
  "exports": {
package/printcheck.mjs CHANGED
@@ -20,6 +20,8 @@ function areaNormal(poly) {
20
20
  }
21
21
 
22
22
  const OVERHANG_SLACK = 0.5; // degrees
23
+ const LINE = 0.42; // mm: the line width when the printer gives none (a 0.4 mm nozzle's usual line)
24
+ const LEDGE_SLACK = (15 * Math.PI) / 180; // a point's supports all within a half turn and this: a ledge
23
25
 
24
26
  /**
25
27
  * The box around faces.
@@ -79,6 +81,13 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
79
81
  const over = size.filter((s) => s > printer.bed);
80
82
  f(over.length ? 'fail' : 'pass', `${size.map(mm).join(' × ')} mm${over.length ? `: bigger than the ${printer.bed} mm bed` : ', fits the bed'}`, 'print.bed', { size, bed: printer.bed });
81
83
 
84
+ // Faces the wrong way round: a closed solid's faces all point out, so the volume they enclose (signed, by the way
85
+ // their corners run) is positive. Negative, every face points in, and nothing below reads right (the base faces up,
86
+ // the top faces down): say that, not what follows from it
87
+ let signed = 0;
88
+ for (const p of polys) for (let i = 1; i < p.length - 1; i++) { const [a, b, c] = [p[0], p[i], p[i + 1]]; signed += (a[0] * (b[1] * c[2] - b[2] * c[1]) - a[1] * (b[0] * c[2] - b[2] * c[0]) + a[2] * (b[0] * c[1] - b[1] * c[0])) / 6; }
89
+ if (signed < -eps) { f('fail', `the faces point inwards (their corners run clockwise seen from outside): the solid is inside out. Reverse every face's corners, or the exporter's winding`, 'print.inside-out', { volume: signed }); return out; }
90
+
82
91
  // Flat on the bed: the lowest point is z = 0, and faces there point straight down.
83
92
  let base = 0, down = 0, support = 0, zs = [];
84
93
  // Where a finding is: the box round the faces it's about, { lo, hi } (for a reader to point at)
@@ -118,10 +127,18 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
118
127
  return true;
119
128
  };
120
129
  const centroid = (p) => [0, 1].map((k) => p.reduce((s, v) => s + v[k], 0) / p.length);
121
- const held = flatDown.filter((d) => flatUp.some((u) => d.z - u.z > eps && d.z - u.z <= gap * 1.5 + eps && inside(centroid(d.p), u.p)));
130
+ // (a breakaway gap, or two layers: a part printed in place rests on what is two layers under it as it does on the bed)
131
+ const nearBed = Math.max(gap * 1.5, 2 * (printer.layer ?? 0.2));
132
+ const held = flatDown.filter((d) => flatUp.some((u) => d.z - u.z > eps && d.z - u.z <= nearBed + eps && inside(centroid(d.p), u.p)));
122
133
  if (held.length) f('pass', `${mm(held.reduce((s, d) => s + d.area, 0))} mm² rests on a breakaway support (${mm(gap)} mm gap): snip it off after printing`, 'print.breakaway', { area: held.reduce((s, d) => s + d.area, 0), gap });
134
+ // And the bed is a support too: an underside within a breakaway gap of it (a smoothed base's facets a hair up) is
135
+ // squashed on to it with the first layer, not printed in the air
136
+ const onBed = flatDown.filter((d) => !held.includes(d) && d.z - lo[2] <= nearBed + eps);
137
+ if (onBed.length) f('pass', `${mm(onBed.reduce((s, d) => s + d.area, 0))} mm² lies within ${mm(nearBed)} mm of the bed: laid down with the first layers`, 'print.bed-near', { area: onBed.reduce((s, d) => s + d.area, 0) });
123
138
  const levels = new Map();
124
- for (const d of flatDown) if (!held.includes(d)) { const k = d.z.toFixed(2); levels.set(k, [...(levels.get(k) || []), d.p]); }
139
+ // (undersides within a layer of each other are one height: a smoothed surface's facets sit a hair apart)
140
+ const layerH = printer.layer ?? 0.2;
141
+ for (const d of flatDown) if (!held.includes(d) && !onBed.includes(d)) { const k = (Math.round(d.z / layerH) * layerH).toFixed(2); levels.set(k, [...(levels.get(k) || []), d.p]); }
125
142
  // A point on a segment (in the plane), within eps; and a segment lying along another, overlapping it
126
143
  const onSeg = (pt, a, b) => { const dx = b[0] - a[0], dy = b[1] - a[1], l = dx * dx + dy * dy; if (l < eps * eps) return Math.hypot(pt[0] - a[0], pt[1] - a[1]) <= eps; const t = ((pt[0] - a[0]) * dx + (pt[1] - a[1]) * dy) / l; return t >= -eps && t <= 1 + eps && Math.hypot(pt[0] - a[0] - t * dx, pt[1] - a[1] - t * dy) <= eps; };
127
144
  const along = (a, b, c, d) => { // c–d lies on the line of a–b and overlaps it (an edge matched exactly, or by a part of it: a T-junction)
@@ -130,7 +147,7 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
130
147
  const t0 = ((c[0] - a[0]) * dx + (c[1] - a[1]) * dy) / (l * l), t1 = ((d[0] - a[0]) * dx + (d[1] - a[1]) * dy) / (l * l);
131
148
  return Math.min(t0, t1) < 1 - eps && Math.max(t0, t1) > eps;
132
149
  };
133
- const bridgeMax = printer.bridge ?? 5, line = printer.line ?? 0.4;
150
+ const bridgeMax = printer.bridge ?? 5, line = printer.line ?? LINE;
134
151
  // Each underside on its own: the faces at a level joined where they touch, edge to edge or a corner on another's
135
152
  // edge (a T-junction, which a boolean leaves plenty of). Two pockets' ceilings at one height are two bridges, each
136
153
  // measured to its own held edges, not to the other's across the part
@@ -150,21 +167,31 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
150
167
  return [...out.values()];
151
168
  };
152
169
  // The top edges of walls that reach a level from below: an underside's edge lying along one is held up there
153
- const wallsAt = (z) => polys.filter((p) => Math.min(...p.map((v) => v[2])) < z - eps).flatMap((p) => p.flatMap((a, i) => { const b = p[(i + 1) % p.length]; return Math.abs(a[2] - z) <= eps && Math.abs(b[2] - z) <= eps ? [[a, b]] : []; }));
170
+ const wallsAt = (z, tol = eps) => polys.filter((p) => Math.min(...p.map((v) => v[2])) < z - tol).flatMap((p) => p.flatMap((a, i) => { const b = p[(i + 1) % p.length]; return Math.abs(a[2] - z) <= tol && Math.abs(b[2] - z) <= tol ? [[a, b]] : []; }));
171
+ // (a smoothed surface has many small undersides at a height: what starts in mid-air and what hangs as a ledge are
172
+ // said once per height, with how many and the worst; each bridge on its own)
154
173
  for (const [z, level] of levels) {
155
- const walls = wallsAt(Number(z));
174
+ const walls = wallsAt(Number(z), layerH / 2 + eps), midAir = [], ledges = []; // (the level's faces lie within half a layer of it)
156
175
  for (const group of regions(level)) {
157
176
  const at = boxOf(group);
158
177
  if (Math.min(at.hi[0] - at.lo[0], at.hi[1] - at.lo[1]) < line - eps) continue; // a sliver narrower than a print line: the slicer lays nothing there
159
178
  if (widest(group) < line - eps) continue; // no wider than a line anywhere (a step of a slope cut in layers, a chamfer's tread): one layer's overhang, which the perimeter takes in its stride
160
179
  const held = (a, b) => walls.some(([c, d]) => along(a, b, c, d));
161
- const under = flatUp.filter((u) => Number(z) - u.z > eps && Number(z) - u.z <= gap * 1.5 + eps).map((u) => u.p); // breakaway supports' tops
162
- const measured = spans(group, held, under), span = measured?.span ?? null;
163
- if (span == null) f('warn', `an underside at z ${mm(Number(z))} starts in mid-air: needs support (or a built-in breakaway support one ${mm(gap)} mm layer below)`, 'print.mid-air', { z: Number(z) }, at);
164
- else if (measured.reach > line + eps) f('warn', `a ledge at z ${mm(Number(z))} reaches ${mm(measured.reach)} mm out past what holds it, held on one side only: it droops. Slant its underside in at 45° to the wall, or support it`, 'print.ledge', { reach: measured.reach, z: Number(z) }, at);
180
+ const under = flatUp.filter((u) => Number(z) - u.z > eps && Number(z) - u.z <= nearBed + eps).map((u) => u.p); // breakaway supports' tops, and what a part printed in place rests on
181
+ const measured = spans(group, held, under), span = measured?.span ?? null, groupArea = group.reduce((s, p) => s + Math.hypot(...areaNormal(p)) / 2, 0);
182
+ if (span == null) midAir.push({ at, area: groupArea });
183
+ else if (measured.reach > line + eps) ledges.push({ at, reach: measured.reach, where: measured.where });
165
184
  else if (span > bridgeMax + eps) f('warn', `${mm(span)} mm bridge at z ${mm(Number(z))}: wider than the ${bridgeMax} mm the printer can span, needs supports`, 'print.bridge', { span, max: bridgeMax, z: Number(z) }, at);
166
185
  else f('pass', `bridge ${mm(span)} mm at z ${mm(Number(z))} (up to ${bridgeMax} mm)`, 'print.bridge', { span, max: bridgeMax, z: Number(z) });
167
186
  }
187
+ if (midAir.length) {
188
+ const biggest = midAir.reduce((m, x) => (x.area > m.area ? x : m)), total = midAir.reduce((s, x) => s + x.area, 0);
189
+ f('warn', `${midAir.length === 1 ? 'an underside' : `${midAir.length} undersides (${mm(total)} mm²)`} at z ${mm(Number(z))} start${midAir.length === 1 ? 's' : ''} in mid-air: needs support (or a built-in breakaway support one ${mm(gap)} mm layer below)`, 'print.mid-air', { z: Number(z), count: midAir.length, area: total }, biggest.at);
190
+ }
191
+ if (ledges.length) {
192
+ const worst = ledges.reduce((m, x) => (x.reach > m.reach ? x : m));
193
+ f('warn', `${ledges.length === 1 ? 'a ledge' : `${ledges.length} ledges`} at z ${mm(Number(z))} reach${ledges.length === 1 ? 'es' : ''} ${ledges.length === 1 ? '' : 'up to '}${mm(worst.reach)} mm out past what holds ${ledges.length === 1 ? 'it' : 'them'}, held on one side only: ${ledges.length === 1 ? 'it droops' : 'they droop'}. Slant the underside in at 45° to the wall, or support it`, 'print.ledge', { reach: worst.reach, z: Number(z), count: ledges.length, where: worst.where }, worst.at);
194
+ }
168
195
  }
169
196
 
170
197
  // One piece, and nothing starting in mid-air, part by part (an assembly's parts are apart on purpose)
@@ -180,7 +207,7 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
180
207
  // (the first layer spreads `foot` each side as it is squashed on to the bed: two parts printed in place must stand
181
208
  // further apart there than the gap that prints apart higher up, or they print as one)
182
209
  const firstGap = printer.foot > 0 && printer.print_gap > 0 ? printer.print_gap + 2 * printer.foot : 0;
183
- const sliced = islands(tris, { layer: printer.layer ?? 0.2, overhang: printer.overhang, line: printer.line ?? 0.42, firstGap }), isl = sliced.islands;
210
+ const sliced = islands(tris, { layer: printer.layer ?? 0.2, overhang: printer.overhang, line: printer.line ?? LINE, firstGap }), isl = sliced.islands;
184
211
  for (const x of sliced.fused) f('warn', `on the first layer, two parts are about ${mm(x.gap)} mm apart at (${mm(x.at[0])}, ${mm(x.at[1])}): the first layer spreads ${printer.foot} mm each side as it is squashed on, so under ${mm(firstGap)} mm they print as one. Keep them ${mm(firstGap)} mm apart on the bed, or lift one off it`, 'print.first-layer', { gap: x.gap, need: firstGap }, { lo: [x.at[0] - 1, x.at[1] - 1, 0], hi: [x.at[0] + 1, x.at[1] + 1, printer.layer ?? 0.2] });
185
212
  if (firstGap && !sliced.fused.length) f('pass', `on the first layer, nothing printed in place stands within ${mm(firstGap)} mm of another part (the first layer spreads ${printer.foot} mm each side)`, 'print.first-layer', { need: firstGap });
186
213
  // Overhang as the printer meets it: layers lying out past the one below, less the flat undersides the bridge check
@@ -234,7 +261,7 @@ function spans(polys, held, under) {
234
261
  return { c, d: Math.hypot(pt[0] - c[0], pt[1] - c[1]) };
235
262
  };
236
263
  const supports = [...outline, ...tops];
237
- let best = 0, reach = 0;
264
+ let best = 0, reach = 0, where = null;
238
265
  const N = 6;
239
266
  for (const p of polys) for (let i = 1; i < p.length - 1; i++) {
240
267
  const [a, b, c] = [p[0], p[i], p[i + 1]];
@@ -250,11 +277,14 @@ function spans(polys, held, under) {
250
277
  const angles = near.filter((x) => x.d > 1e-9).map((x) => Math.atan2(x.c[1] - pt[1], x.c[0] - pt[0])).sort((a, b) => a - b);
251
278
  let gap = angles.length ? angles[0] + 2 * Math.PI - angles.at(-1) : 2 * Math.PI;
252
279
  for (let k = 1; k < angles.length; k++) gap = Math.max(gap, angles[k] - angles[k - 1]);
253
- if (gap > Math.PI + 1e-6) reach = Math.max(reach, first.d);
280
+ // (a bridge between two supports nearly opposite each other, a strap from a wall to a post, leaves a gap a few
281
+ // degrees over a half turn where the post's nearest point is off its line: some slack, LEDGE_SLACK, before it
282
+ // counts as a ledge)
283
+ if (gap > Math.PI + LEDGE_SLACK && first.d > reach) { reach = first.d; where = pt; }
254
284
  }
255
285
  }
256
286
  }
257
- return { span: 2 * best, reach };
287
+ return { span: 2 * best, reach, where };
258
288
  }
259
289
 
260
290
  // seams(polys) -> { gaps, tjunctions, hairlines }: edges not matched by a reverse edge, split into T-junctions (covered
@@ -338,7 +368,7 @@ export function seams(polys, eps = 1e-3) {
338
368
  * undersides included (the bridge check judges those separately)
339
369
  */
340
370
  const SPANS = [5, 20]; // layers: the spans a lean is held over (see islands)
341
- export function islands(polys, { layer = 0.2, overhang = 45, line = 0.42, firstGap = 0, onLayer } = {}) {
371
+ export function islands(polys, { layer = 0.2, overhang = 45, line = LINE, firstGap = 0, onLayer } = {}) {
342
372
  if (!(layer > 0) || !(line > 0) || !(overhang > 0 && overhang < 90)) throw new Error(`islands: layer and line are lengths above 0 and overhang an angle between 0 and 90 (got layer ${layer}, line ${line}, overhang ${overhang})`);
343
373
  if (!polys.length) return { islands: [], layers: 0, overhang: 0 };
344
374
  // The pixel: a quarter of a line, or coarser on a big part so a layer stays under about a million pixels