three-slicer-viewer 0.3.0 → 0.3.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.
@@ -1,190 +1,234 @@
1
- const it = {
1
+ import { D as Lt, a as Nt } from "./viewer_defaults-B7UUZ582.js";
2
+ import { R as t, e as Z } from "./toolpath_encoding-BuYs95qZ.js";
3
+ import { T as ft } from "./toolpath_palette-B8xEl-qa.js";
4
+ const F = 1e-6, Rt = 254, It = /^(filament|extruder)_colour\s*=\s*(.*)$/i, ut = (i) => i.split(/[;,]/).map((r) => {
5
+ const s = /^"?#?([0-9a-f]{6})(?:[0-9a-f]{2})?"?$/i.exec(r.trim());
6
+ return s ? "#" + s[1].toUpperCase() : null;
7
+ }), At = (i) => "#" + ft[i % ft.length].map((r) => Math.round(r * 255).toString(16).padStart(2, "0")).join("");
8
+ function gt(i, r) {
9
+ let s = [];
10
+ Array.isArray(i?.colors) && (s = i.colors);
11
+ const g = r ?? [], u = Math.max(s.length, g.length, i?.filament_mm_by_tool?.length ?? 0);
12
+ return Array.from({ length: u }, (m, c) => s[c] || g[c] || At(c));
13
+ }
14
+ function wt(i, r) {
15
+ return dt(i.gcode, gt(i.stats, r));
16
+ }
17
+ function dt(i, r) {
18
+ const s = String(i ?? ""), g = [r].flat().filter(Boolean);
19
+ if (!g.length || /^;\s*filament_colour\s*=/mi.test(s)) return s;
20
+ const u = "; filament_colour = " + g.join(";") + `
21
+ `, m = s.lastIndexOf(`
22
+ ; CONFIG_BLOCK_END`);
23
+ if (m >= 0) return s.slice(0, m + 1) + u + s.slice(m + 1);
24
+ let c = `
25
+ `;
26
+ return (!s || s.endsWith(`
27
+ `)) && (c = ""), s + c + u;
28
+ }
29
+ const St = {
2
30
  // OrcaSlicer / PrusaSlicer ;TYPE: names
3
- "outer wall": 1,
4
- "external perimeter": 1,
5
- "inner wall": 1,
6
- perimeter: 1,
7
- "internal perimeter": 1,
8
- "overhang wall": 1,
9
- "overhang perimeter": 1,
10
- "sparse infill": 2,
11
- "internal infill": 2,
12
- "solid infill": 3,
13
- "internal solid infill": 3,
14
- "top solid infill": 3,
15
- "top surface": 3,
16
- "bottom surface": 3,
17
- skirt: 4,
18
- brim: 4,
19
- "skirt/brim": 4,
20
- support: 5,
21
- "support material": 5,
22
- "support material interface": 5,
23
- "support interface": 5,
24
- "support transition": 5,
25
- raft: 6,
26
- "gap fill": 7,
27
- "gap infill": 7,
28
- "thin wall": 8,
29
- bridge: 9,
30
- "bridge infill": 9,
31
- "internal bridge": 9,
32
- "internal bridge infill": 9,
33
- ironing: 10,
34
- "prime tower": 11,
35
- "wipe tower": 11,
36
- custom: 1,
31
+ "outer wall": t.WALL,
32
+ "external perimeter": t.WALL,
33
+ "inner wall": t.WALL,
34
+ perimeter: t.WALL,
35
+ "internal perimeter": t.WALL,
36
+ "overhang wall": t.WALL,
37
+ "overhang perimeter": t.WALL,
38
+ "sparse infill": t.SPARSE,
39
+ "internal infill": t.SPARSE,
40
+ "solid infill": t.SOLID,
41
+ "internal solid infill": t.SOLID,
42
+ "top solid infill": t.SOLID,
43
+ "top surface": t.SOLID,
44
+ "bottom surface": t.SOLID,
45
+ skirt: t.SKIRT,
46
+ brim: t.SKIRT,
47
+ "skirt/brim": t.SKIRT,
48
+ support: t.SUPPORT,
49
+ "support material": t.SUPPORT,
50
+ "support material interface": t.SUPPORT,
51
+ "support interface": t.SUPPORT,
52
+ "support transition": t.SUPPORT,
53
+ raft: t.RAFT,
54
+ "gap fill": t.GAP,
55
+ "gap infill": t.GAP,
56
+ "thin wall": t.THIN,
57
+ bridge: t.BRIDGE,
58
+ "bridge infill": t.BRIDGE,
59
+ "internal bridge": t.BRIDGE,
60
+ "internal bridge infill": t.BRIDGE,
61
+ ironing: t.IRONING,
62
+ "prime tower": t.PRIME,
63
+ "wipe tower": t.PRIME,
64
+ custom: t.WALL,
37
65
  // Cura ;TYPE: names
38
- "wall-outer": 1,
39
- "wall-inner": 1,
40
- fill: 2,
41
- skin: 3,
42
- "support-interface": 5,
43
- "prime-tower": 11
44
- }, nt = [
45
- ["skirt/brim", 4],
46
- ["skirt", 4],
47
- ["walls", 1],
48
- ["thin-wall", 8],
49
- ["gap-fill", 7],
50
- ["bridge", 9],
51
- ["ironing", 10],
52
- ["support", 5],
53
- ["prime tower", 11],
54
- ["wipe_tower_real", 11]
55
- ], st = (w, c) => w === c || c.length < w.length && " (:/".includes(w[c.length]) && w.startsWith(c), at = {
56
- 1: 1,
57
- 2: 1,
58
- 3: 1,
59
- 4: 2,
60
- 5: 3,
61
- 6: 3,
62
- 7: 3,
63
- 8: 10,
64
- 9: 9,
65
- 10: 9,
66
- 11: 7,
67
- 12: 4,
68
- 13: 4,
69
- 14: 5,
70
- 15: 5,
71
- 16: 5,
72
- 17: 11
66
+ "wall-outer": t.WALL,
67
+ "wall-inner": t.WALL,
68
+ fill: t.SPARSE,
69
+ skin: t.SOLID,
70
+ "support-interface": t.SUPPORT,
71
+ "prime-tower": t.PRIME
72
+ }, Pt = [
73
+ ["skirt/brim", t.SKIRT],
74
+ ["skirt", t.SKIRT],
75
+ ["walls", t.WALL],
76
+ ["thin-wall", t.THIN],
77
+ ["gap-fill", t.GAP],
78
+ ["bridge", t.BRIDGE],
79
+ ["ironing", t.IRONING],
80
+ ["support", t.SUPPORT],
81
+ ["prime tower", t.PRIME],
82
+ ["wipe_tower_real", t.PRIME]
83
+ ], Et = (i, r) => i === r || r.length < i.length && " (:/".includes(i[r.length]) && i.startsWith(r), Ot = {
84
+ 1: t.WALL,
85
+ 2: t.WALL,
86
+ 3: t.WALL,
87
+ 4: t.SPARSE,
88
+ 5: t.SOLID,
89
+ 6: t.SOLID,
90
+ 7: t.SOLID,
91
+ 8: t.IRONING,
92
+ 9: t.BRIDGE,
93
+ 10: t.BRIDGE,
94
+ 11: t.GAP,
95
+ 12: t.SKIRT,
96
+ 13: t.SKIRT,
97
+ 14: t.SUPPORT,
98
+ 15: t.SUPPORT,
99
+ 16: t.SUPPORT,
100
+ 17: t.PRIME
73
101
  };
74
- function rt(w, c = {}) {
75
- const V = Math.PI / 4 * (c.filamentDiameter > 0 ? c.filamentDiameter : 1.75) ** 2, tt = c.defaultLayerHeight > 0 ? c.defaultLayerHeight : 0.2;
76
- let f = 0, u = 0, m = 0, L = 0, x = !0, v = !0, E = 0, P = 1, S = 0, p = null;
77
- const g = [];
78
- let z = !1, I = !0, R = NaN, q = 0, H = 0, O = 0;
79
- const j = /* @__PURE__ */ new Set([0]), W = (s) => {
80
- p = { z: s, p: [], w: [] }, g.push(p), I = !1, R = NaN, P = 1, S = 0;
81
- }, et = () => g.length > 1 ? g[g.length - 2].z : 0, C = (s, o, d, b, N, e, t) => {
82
- const a = Math.hypot(b - s, N - o, e - d);
83
- if (!(a < 1e-6))
84
- if (t > 1e-6) {
85
- O += t, I ? W(Number.isNaN(R) ? e : R) : !z && e > p.z + 1e-3 && W(e);
86
- const r = P + E * 16;
87
- p.p.push(s, o, d, r, b, N, e, r);
88
- const i = Math.max(0.02, p.z - et() || tt), F = S > 0 ? S : t * V / (a * i) + i * (1 - Math.PI / 4);
89
- p.w.push(Math.min(3, Math.max(0.05, F))), q++;
90
- } else p && (p.p.push(s, o, d, E * 16, b, N, e, E * 16), p.w.push(0), H++);
102
+ function Gt(i, r = {}) {
103
+ const s = Math.PI / 4 * (r.filamentDiameter > 0 ? r.filamentDiameter : Lt) ** 2, g = r.defaultLayerHeight > 0 ? r.defaultLayerHeight : Nt;
104
+ let u = 0, m = 0, c = 0, b = 0, _ = !0, W = !0, d = 0, w = t.WALL, y = 0, R = null;
105
+ const E = [];
106
+ let D = !1, x = !0, K = NaN, q = 0, X = 0, $ = 0;
107
+ const v = /* @__PURE__ */ new Set([0]), H = [];
108
+ let z = [], k = [], j = !1;
109
+ const V = () => {
110
+ w = t.WALL, y = 0;
111
+ }, U = (o, a = !1) => {
112
+ R = { z: o, p: [], w: [] }, E.push(R), x = !1, K = NaN, (a || !j) && V();
113
+ }, mt = () => E.length > 1 ? E[E.length - 2].z : 0, J = (o, a, S, P, A, n, e) => {
114
+ const p = Math.hypot(P - o, A - a, n - S);
115
+ if (!(p < F))
116
+ if (e > F) {
117
+ $ += e, H[d] = (H[d] ?? 0) + e, x ? U(Number.isNaN(K) ? n : K) : !D && n > R.z + 1e-3 && U(n);
118
+ const h = Z(w, d);
119
+ R.p.push(o, a, S, h, P, A, n, h);
120
+ const f = Math.max(0.02, R.z - mt() || g), L = y > 0 ? y : e * s / (p * f) + f * (1 - Math.PI / 4);
121
+ R.w.push(Math.min(3, Math.max(0.05, L))), q++;
122
+ } else R && (R.p.push(o, a, S, Z(t.TRAVEL, d), P, A, n, Z(t.TRAVEL, d)), R.w.push(0), X++);
91
123
  };
92
- for (const s of w.split(`
124
+ for (const o of i.split(`
93
125
  `)) {
94
- const o = s.indexOf(";"), d = (o < 0 ? s : s.slice(0, o)).trim(), b = o < 0 ? "" : s.slice(o + 1).trim();
95
- if (b && !d) {
96
- const t = b.toLowerCase();
97
- if (t.startsWith("type:")) {
98
- P = it[t.slice(5).trim()] ?? 1;
126
+ const a = o.indexOf(";"), S = (a < 0 ? o : o.slice(0, a)).trim(), P = a < 0 ? "" : o.slice(a + 1).trim();
127
+ if (P && !S) {
128
+ const e = P.toLowerCase(), p = It.exec(P);
129
+ if (p) {
130
+ p[1].toLowerCase() === "filament" ? z = ut(p[2]) : k = ut(p[2]);
131
+ continue;
132
+ }
133
+ if (e.startsWith("type:")) {
134
+ w = St[e.slice(5).trim()] ?? t.WALL, j = !0;
99
135
  continue;
100
136
  }
101
- if (t.startsWith("_extrusion_role:")) {
102
- P = at[parseInt(t.slice(16), 10)] ?? 1;
137
+ if (e.startsWith("_extrusion_role:")) {
138
+ w = Ot[parseInt(e.slice(16), 10)] ?? t.WALL, j = !0;
103
139
  continue;
104
140
  }
105
- if (t.startsWith("width:")) {
106
- const i = parseFloat(t.slice(6));
107
- S = i > 0 ? i : 0;
141
+ if (e.startsWith("width:")) {
142
+ const L = parseFloat(e.slice(6));
143
+ y = L > 0 ? L : 0;
108
144
  continue;
109
145
  }
110
- if (t.startsWith("layer_change") || t.startsWith("layer:")) {
111
- z = !0, I = !0;
146
+ if (e.startsWith("layer_change") || e.startsWith("layer:")) {
147
+ D = !0, x = !0, V();
112
148
  continue;
113
149
  }
114
- if (t.startsWith("z:")) {
115
- const i = parseFloat(t.slice(2));
116
- I && i > 0 && (z = !0, W(i));
150
+ if (e.startsWith("z:")) {
151
+ const L = parseFloat(e.slice(2));
152
+ x && L > 0 && (D = !0, U(L));
117
153
  continue;
118
154
  }
119
- const a = /^layer \d+ z([-\d.]+)/.exec(t);
120
- if (a) {
121
- z = !0, W(parseFloat(a[1]));
155
+ const h = /^layer \d+ z([-\d.]+)/.exec(e);
156
+ if (h) {
157
+ D = !0, U(parseFloat(h[1]), !0);
122
158
  continue;
123
159
  }
124
- const r = nt.find(([i]) => st(t, i));
125
- r && (P = r[1]);
160
+ const f = Pt.find(([L]) => Et(e, L));
161
+ f && (w = f[1]);
126
162
  continue;
127
163
  }
128
- if (!d) continue;
129
- const N = d.split(/\s+/), e = N[0].toUpperCase();
130
- if (e === "G90") {
131
- x = !0;
164
+ if (!S) continue;
165
+ const A = S.split(/\s+/), n = A[0].toUpperCase();
166
+ if (n === "G90") {
167
+ _ = !0;
132
168
  continue;
133
169
  }
134
- if (e === "G91") {
135
- x = !1;
170
+ if (n === "G91") {
171
+ _ = !1;
136
172
  continue;
137
173
  }
138
- if (e === "M82") {
139
- v = !0;
174
+ if (n === "M82") {
175
+ W = !0;
140
176
  continue;
141
177
  }
142
- if (e === "M83") {
143
- v = !1;
178
+ if (n === "M83") {
179
+ W = !1;
144
180
  continue;
145
181
  }
146
- if (/^T\d+$/.test(e)) {
147
- E = parseInt(e.slice(1), 10), j.add(E);
182
+ if (/^T\d+$/.test(n)) {
183
+ const e = parseInt(n.slice(1), 10);
184
+ e <= Rt && (d = e, v.add(d));
148
185
  continue;
149
186
  }
150
- if (e === "G92" || e === "G0" || e === "G1" || e === "G2" || e === "G3") {
151
- let t = NaN, a = NaN, r = NaN, i = NaN, F = 0, D = 0;
152
- for (let h = 1; h < N.length; h++) {
153
- const n = N[h][0], l = parseFloat(N[h].slice(1));
154
- Number.isNaN(l) || (n === "X" || n === "x" ? t = l : n === "Y" || n === "y" ? a = l : n === "Z" || n === "z" ? r = l : n === "E" || n === "e" ? i = l : n === "I" || n === "i" ? F = l : (n === "J" || n === "j") && (D = l));
187
+ if (n === "G92" || n === "G0" || n === "G1" || n === "G2" || n === "G3") {
188
+ let e = NaN, p = NaN, h = NaN, f = NaN, L = 0, tt = 0;
189
+ for (let I = 1; I < A.length; I++) {
190
+ const l = A[I][0], N = parseFloat(A[I].slice(1));
191
+ Number.isNaN(N) || (l === "X" || l === "x" ? e = N : l === "Y" || l === "y" ? p = N : l === "Z" || l === "z" ? h = N : l === "E" || l === "e" ? f = N : l === "I" || l === "i" ? L = N : (l === "J" || l === "j") && (tt = N));
155
192
  }
156
- if (e === "G92") {
157
- Number.isNaN(t) || (f = t), Number.isNaN(a) || (u = a), Number.isNaN(r) || (m = r), Number.isNaN(i) || (L = i);
193
+ if (n === "G92") {
194
+ Number.isNaN(e) || (u = e), Number.isNaN(p) || (m = p), Number.isNaN(h) || (c = h), Number.isNaN(f) || (b = f);
158
195
  continue;
159
196
  }
160
- const k = Number.isNaN(t) ? f : x ? t : f + t, A = Number.isNaN(a) ? u : x ? a : u + a, Z = Number.isNaN(r) ? m : x ? r : m + r, K = Number.isNaN(i) ? 0 : v ? i - L : i;
161
- if (e === "G2" || e === "G3") {
162
- const h = f + F, n = u + D, l = Math.hypot(f - h, u - n), _ = Math.atan2(u - n, f - h);
163
- let M = Math.atan2(A - n, k - h);
164
- const T = e === "G3";
165
- T && M <= _ + 1e-6 && (M += 2 * Math.PI), !T && M >= _ - 1e-6 && (M -= 2 * Math.PI);
166
- const y = Math.max(1, Math.min(360, Math.ceil(Math.abs(M - _) * l / 0.3)));
167
- let X = f, Y = u, J = m;
168
- for (let G = 1; G <= y; G++) {
169
- const U = _ + (M - _) * G / y, $ = G === y ? k : h + l * Math.cos(U), B = G === y ? A : n + l * Math.sin(U), Q = m + (Z - m) * G / y;
170
- C(X, Y, J, $, B, Q, K / y), X = $, Y = B, J = Q;
197
+ const C = Number.isNaN(e) ? u : _ ? e : u + e, B = Number.isNaN(p) ? m : _ ? p : m + p, Y = Number.isNaN(h) ? c : _ ? h : c + h, et = Number.isNaN(f) ? 0 : W ? f - b : f;
198
+ if (n === "G2" || n === "G3") {
199
+ const I = u + L, l = m + tt, N = Math.hypot(u - I, m - l), G = Math.atan2(m - l, u - I);
200
+ let O = Math.atan2(B - l, C - I);
201
+ const nt = n === "G3";
202
+ nt && O <= G + F && (O += 2 * Math.PI), !nt && O >= G - F && (O -= 2 * Math.PI);
203
+ const M = Math.max(1, Math.min(360, Math.ceil(Math.abs(O - G) * N / 0.3)));
204
+ let it = u, rt = m, ot = c;
205
+ for (let T = 1; T <= M; T++) {
206
+ const st = G + (O - G) * T / M, at = T === M ? C : I + N * Math.cos(st), lt = T === M ? B : l + N * Math.sin(st), ct = c + (Y - c) * T / M;
207
+ J(it, rt, ot, at, lt, ct, et / M), it = at, rt = lt, ot = ct;
171
208
  }
172
209
  } else
173
- C(f, u, m, k, A, Z, K);
174
- f = k, u = A, m = Z, Number.isNaN(i) || (L = v ? i : L + i);
210
+ J(u, m, c, C, B, Y, et);
211
+ u = C, m = B, c = Y, Number.isNaN(f) || (b = W ? f : b + f);
175
212
  }
176
213
  }
214
+ const pt = Math.max(z.length, k.length), Q = Array.from({ length: pt }, (o, a) => k[a] || z[a] || null), ht = Array.from({ length: Math.max(...v) + 1 }, (o, a) => H[a] || 0);
177
215
  return {
178
- layers: g.map((s) => ({ z: s.z, paths: Float32Array.from(s.p), widths: Float32Array.from(s.w) })),
216
+ layers: E.map((o) => ({ z: o.z, paths: Float32Array.from(o.p), widths: Float32Array.from(o.w) })),
179
217
  stats: {
180
- layers: g.length,
218
+ layers: E.length,
181
219
  path_segments: q,
182
- travel_segments: H,
183
- filament_mm: O,
184
- tools: [...j].sort((s, o) => s - o)
220
+ travel_segments: X,
221
+ filament_mm: $,
222
+ tools: [...v].sort((o, a) => o - a),
223
+ // The kernel's own stat name, so the Filament-view switch and the stats card's per-tool split work unchanged.
224
+ filament_mm_by_tool: ht,
225
+ ...Q.some(Boolean) && { colors: Q }
185
226
  }
186
227
  };
187
228
  }
188
229
  export {
189
- rt as parseGcode
230
+ wt as exportedGcode,
231
+ Gt as parseGcode,
232
+ gt as resultToolColors,
233
+ dt as withFilamentColours
190
234
  };