@rulecad/printcheck 0.1.1 → 0.1.3
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.
- package/README.md +1 -1
- package/package.json +2 -2
- package/printcheck.d.mts +17 -2
- package/printcheck.mjs +24 -15
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @rulecad/printcheck
|
|
2
2
|
|
|
3
3
|
Is this mesh printable on an FDM printer? Hand it triangles and your printer's limits; get back findings in plain
|
|
4
|
-
words. No dependencies; browsers and Node
|
|
4
|
+
words. No dependencies; browsers and Node 20+.
|
|
5
5
|
|
|
6
6
|
```js
|
|
7
7
|
import { check } from '@rulecad/printcheck';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rulecad/printcheck",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
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": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"stl"
|
|
23
23
|
],
|
|
24
24
|
"engines": {
|
|
25
|
-
"node": ">=
|
|
25
|
+
"node": ">=20"
|
|
26
26
|
},
|
|
27
27
|
"sideEffects": false,
|
|
28
28
|
"repository": {
|
package/printcheck.d.mts
CHANGED
|
@@ -6,7 +6,11 @@
|
|
|
6
6
|
/** @typedef {[number, number, number]} Point a corner in mm, Z up */
|
|
7
7
|
/** @typedef {Point[]} Face a polygon: three or more corners, counter-clockwise seen from outside */
|
|
8
8
|
/** @typedef {{ bed: number, overhang: number, layer?: number, support_gap?: number, bridge?: number }} Printer mm and degrees */
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* A finding: its level and words, and for programs a code (print.bridge, fit.gap, …), the numbers behind it, and where
|
|
11
|
+
* it is when that's known (the box round the faces it's about, in mm).
|
|
12
|
+
* @typedef {{ level: 'pass' | 'warn' | 'fail' | 'info', text: string, code?: string, values?: Record<string, any>, at?: { lo: number[], hi: number[] }, refs?: Record<string, any> }} Finding
|
|
13
|
+
*/
|
|
10
14
|
export function bounds(polys: any): {
|
|
11
15
|
lo: number[];
|
|
12
16
|
hi: number[];
|
|
@@ -61,7 +65,18 @@ export type Printer = {
|
|
|
61
65
|
support_gap?: number;
|
|
62
66
|
bridge?: number;
|
|
63
67
|
};
|
|
68
|
+
/**
|
|
69
|
+
* A finding: its level and words, and for programs a code (print.bridge, fit.gap, …), the numbers behind it, and where
|
|
70
|
+
* it is when that's known (the box round the faces it's about, in mm).
|
|
71
|
+
*/
|
|
64
72
|
export type Finding = {
|
|
65
|
-
level: "pass" | "warn" | "fail";
|
|
73
|
+
level: "pass" | "warn" | "fail" | "info";
|
|
66
74
|
text: string;
|
|
75
|
+
code?: string;
|
|
76
|
+
values?: Record<string, any>;
|
|
77
|
+
at?: {
|
|
78
|
+
lo: number[];
|
|
79
|
+
hi: number[];
|
|
80
|
+
};
|
|
81
|
+
refs?: Record<string, any>;
|
|
67
82
|
};
|
package/printcheck.mjs
CHANGED
|
@@ -25,7 +25,11 @@ const OVERHANG_SLACK = 0.5; // degrees
|
|
|
25
25
|
/** @typedef {[number, number, number]} Point a corner in mm, Z up */
|
|
26
26
|
/** @typedef {Point[]} Face a polygon: three or more corners, counter-clockwise seen from outside */
|
|
27
27
|
/** @typedef {{ bed: number, overhang: number, layer?: number, support_gap?: number, bridge?: number }} Printer mm and degrees */
|
|
28
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* A finding: its level and words, and for programs a code (print.bridge, fit.gap, …), the numbers behind it, and where
|
|
30
|
+
* it is when that's known (the box round the faces it's about, in mm).
|
|
31
|
+
* @typedef {{ level: 'pass' | 'warn' | 'fail' | 'info', text: string, code?: string, values?: Record<string, any>, at?: { lo: number[], hi: number[] }, refs?: Record<string, any> }} Finding
|
|
32
|
+
*/
|
|
29
33
|
|
|
30
34
|
export function bounds(polys) {
|
|
31
35
|
const lo = [Infinity, Infinity, Infinity], hi = [-Infinity, -Infinity, -Infinity]; // a loop: spreading big models overflows the stack
|
|
@@ -44,7 +48,7 @@ export function bounds(polys) {
|
|
|
44
48
|
* @returns {Finding[]}
|
|
45
49
|
*/
|
|
46
50
|
export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
47
|
-
const out = [], f = (level, text) => out.push({ level, text });
|
|
51
|
+
const out = [], f = (level, text, code, values, at) => out.push({ level, text, code, ...(values && { values }), ...(at && { at }) });
|
|
48
52
|
const solids = (parts || []).filter((p) => p.role !== 'support').map((p) => ({ tris: p.triangles, box: bounds(p.triangles) }));
|
|
49
53
|
// Is the point inside one of the parts? A ray straight up crosses its surface an odd number of times
|
|
50
54
|
const inPart = (pt) => solids.length > 1 && solids.some(({ tris, box }) => {
|
|
@@ -64,10 +68,14 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
|
64
68
|
const mm = (n) => `${Number(n.toFixed(1))}`;
|
|
65
69
|
|
|
66
70
|
const over = size.filter((s) => s > printer.bed);
|
|
67
|
-
f(over.length ? 'fail' : 'pass', `${size.map(mm).join(' × ')} mm${over.length ? `: bigger than the ${printer.bed} mm bed` : ', fits the bed'}
|
|
71
|
+
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 });
|
|
68
72
|
|
|
69
73
|
// Flat on the bed: the lowest point is z = 0, and faces there point straight down.
|
|
70
74
|
let base = 0, down = 0, support = 0, zs = [];
|
|
75
|
+
// Where a finding is: the box round the faces it's about, { lo, hi } (for a reader to point at)
|
|
76
|
+
const overBox = { lo: [Infinity, Infinity, Infinity], hi: [-Infinity, -Infinity, -Infinity] };
|
|
77
|
+
const grow = (box, p) => { for (const v of p) for (let k = 0; k < 3; k++) { box.lo[k] = Math.min(box.lo[k], v[k]); box.hi[k] = Math.max(box.hi[k], v[k]); } return box; };
|
|
78
|
+
const boxOf = (faces) => faces.reduce((box, p) => grow(box, p), { lo: [Infinity, Infinity, Infinity], hi: [-Infinity, -Infinity, -Infinity] });
|
|
71
79
|
const flatDown = [], flatUp = [];
|
|
72
80
|
// A face overhanging past the limit (measured from vertical) points down by more than its sine; half a degree to
|
|
73
81
|
// spare, since booleans tip a 45° chamfer by hundredths of a degree. (Cosine was used before: the same at 45° only.)
|
|
@@ -80,14 +88,15 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
|
80
88
|
if (nz < -eps && zmax - lo[2] > eps && rests(p)) continue; // on another part (a legend on its keycap)
|
|
81
89
|
if (nz < -1 + eps && zmax - lo[2] < eps) base += area;
|
|
82
90
|
else if (nz < -1 + eps) flatDown.push({ p, z: zmin, area }); // a flat underside above the bed
|
|
83
|
-
else if (nz < -limit) { support += area; zs.push(zmin); }
|
|
91
|
+
else if (nz < -limit) { support += area; zs.push(zmin); grow(overBox, p); }
|
|
84
92
|
if (nz > 1 - eps) flatUp.push({ p, z: zmax });
|
|
85
93
|
if (nz < 0) down += area;
|
|
86
94
|
}
|
|
87
|
-
if (Math.abs(lo[2]) > eps) f('fail', `lowest point is at z = ${mm(lo[2])} mm, not on the bed (z = 0)
|
|
88
|
-
f(base > 0 ? 'pass' : 'fail', base > 0 ? `${mm(base)} mm² flat on the bed` : 'nothing sits flat on the bed');
|
|
89
|
-
|
|
90
|
-
|
|
95
|
+
if (Math.abs(lo[2]) > eps) f('fail', `lowest point is at z = ${mm(lo[2])} mm, not on the bed (z = 0)`, 'print.off-bed', { z: lo[2] });
|
|
96
|
+
f(base > 0 ? 'pass' : 'fail', base > 0 ? `${mm(base)} mm² flat on the bed` : 'nothing sits flat on the bed', 'print.base', { area: base });
|
|
97
|
+
const overAt = support > 0.5 ? overBox : null;
|
|
98
|
+
if (support > 0.5) f('warn', `${mm(support)} mm² overhangs past ${printer.overhang}° (z ${mm(Math.min(...zs))} to ${mm(Math.max(...zs))} mm): needs supports or another orientation`, 'print.overhang', { area: support, angle: printer.overhang, z: [Math.min(...zs), Math.max(...zs)] }, overAt);
|
|
99
|
+
else f('pass', `no overhangs past ${printer.overhang}°`, 'print.overhang', { area: support, angle: printer.overhang });
|
|
91
100
|
|
|
92
101
|
// Flat undersides: on a breakaway support, or bridged
|
|
93
102
|
const gap = printer.support_gap ?? 0.2, inside = (pt, poly) => { // point in the xy footprint of a convex face
|
|
@@ -100,7 +109,7 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
|
100
109
|
};
|
|
101
110
|
const centroid = (p) => [0, 1].map((k) => p.reduce((s, v) => s + v[k], 0) / p.length);
|
|
102
111
|
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)));
|
|
103
|
-
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
|
|
112
|
+
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 });
|
|
104
113
|
const levels = new Map();
|
|
105
114
|
for (const d of flatDown) if (!held.includes(d)) { const k = d.z.toFixed(2); levels.set(k, [...(levels.get(k) || []), d.p]); }
|
|
106
115
|
// Which faces meet at each edge, so an underside's edges can be checked for material below them
|
|
@@ -111,10 +120,10 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
|
111
120
|
const mine = new Set(group);
|
|
112
121
|
const held = (a, b) => (byEdge.get([vkey(a), vkey(b)].sort().join('|')) || []).some((p) => !mine.has(p) && Math.min(...p.map((v) => v[2])) < Number(z) - eps);
|
|
113
122
|
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
|
|
114
|
-
const span = widest(group, held, under);
|
|
115
|
-
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)
|
|
116
|
-
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
|
|
117
|
-
else f('pass', `bridge ${mm(span)} mm at z ${mm(Number(z))} (up to ${bridgeMax} mm)
|
|
123
|
+
const span = widest(group, held, under), at = boxOf(group);
|
|
124
|
+
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);
|
|
125
|
+
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);
|
|
126
|
+
else f('pass', `bridge ${mm(span)} mm at z ${mm(Number(z))} (up to ${bridgeMax} mm)`, 'print.bridge', { span, max: bridgeMax, z: Number(z) });
|
|
118
127
|
}
|
|
119
128
|
|
|
120
129
|
// Closed: every edge is matched by an edge running the other way. An edge that meets the middle of a longer edge
|
|
@@ -122,8 +131,8 @@ export function check(polys, printer, { eps = 1e-3, parts = null } = {}) {
|
|
|
122
131
|
const { gaps, tjunctions, hairlines } = parts && parts.length > 1
|
|
123
132
|
? parts.map((p) => seams(p.triangles, eps)).reduce((s, x) => ({ gaps: s.gaps + x.gaps, tjunctions: s.tjunctions + x.tjunctions, hairlines: s.hairlines + x.hairlines }), { gaps: 0, tjunctions: 0, hairlines: 0 })
|
|
124
133
|
: seams(polys, eps);
|
|
125
|
-
if (gaps) f('warn', `${gaps} edges with nothing on the other side: holes in the surface (slicers may repair them)
|
|
126
|
-
else f('pass', `closed solid${tjunctions || hairlines ? ` (${[tjunctions && `${tjunctions} T-junction edges`, hairlines && `${hairlines} hairline seams under ${HAIRLINE} mm`].filter(Boolean).join(', ')}: watertight for printing)` : ''}
|
|
134
|
+
if (gaps) f('warn', `${gaps} edges with nothing on the other side: holes in the surface (slicers may repair them)`, 'print.closed', { gaps });
|
|
135
|
+
else f('pass', `closed solid${tjunctions || hairlines ? ` (${[tjunctions && `${tjunctions} T-junction edges`, hairlines && `${hairlines} hairline seams under ${HAIRLINE} mm`].filter(Boolean).join(', ')}: watertight for printing)` : ''}`, 'print.closed', { gaps, tjunctions, hairlines });
|
|
127
136
|
return out;
|
|
128
137
|
}
|
|
129
138
|
|