Repository navigation
Expand file tree
/
Copy pathfabrication.js
More file actions
272 lines (238 loc) Β· 10.4 KB
/
Copy pathfabrication.js
File metadata and controls
272 lines (238 loc) Β· 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* βββ SVG FABRICATION VIEW ββββββββββββββββββββββββββββββββββββββββββββββββ */
const Fabrication = (() => {
const MARGIN = 24;
const PAD = 10; // gap between panel effective bboxes
const CUT = '#0C0C0B';
const ENGRAVE = '#888';
const HINGE = '#E85D04';
const GROOVE = '#1A7A3F';
const FILLS = ['#F6F5F3','#EDECE9','#E8E7E4','#E2E1DC','#DDD8CF'];
let _svgEl = null;
let _bounds = { w: 800, h: 600 };
let _current = null;
function init(svgEl) {
_svgEl = svgEl;
window.addEventListener('resize', () => { if (_current) render(_current); });
}
function render(data) {
_current = data;
if (!_svgEl) return;
const { s } = data;
_bounds = { w: _svgEl.clientWidth || 800, h: _svgEl.clientHeight || 600 };
const panels = BoxTypes.getPanels(s);
const t = s.thickness;
const k = s.kerf;
const fc = s.fingerCount;
const jt = s.joint;
const layout = _layout(panels, t);
const scale = _fitScale(layout.totalW, layout.totalH);
_svgEl.innerHTML = '';
_svgEl.setAttribute('viewBox', `0 0 ${_bounds.w} ${_bounds.h}`);
const root = _el('g');
const ox = (_bounds.w - layout.totalW * scale) / 2;
const oy = (_bounds.h - layout.totalH * scale) / 2;
root.setAttribute('transform', `translate(${ox},${oy}) scale(${scale})`);
_svgEl.appendChild(root);
_drawGrid(root, layout.totalW, layout.totalH, scale);
layout.placements.forEach((pl, i) => {
const panel = panels[i];
const g = _el('g');
g.setAttribute('transform', `translate(${pl.x},${pl.y})`);
root.appendChild(g);
// main outline
const pts = DXF._panelPoints(panel.w, panel.h, t, k, fc, jt, panel.edges);
const d = _pts2path(pts);
const path = _el('path');
path.setAttribute('d', d);
path.setAttribute('fill', FILLS[panel.tint] ?? FILLS[0]);
path.setAttribute('stroke', CUT);
path.setAttribute('stroke-width', `${0.8 / scale}`);
path.setAttribute('stroke-linejoin', 'miter');
g.appendChild(path);
// special features
if (panel.special) _renderSpecial(g, panel, t, scale);
// labels
_addLabel(g, panel.w, panel.h, panel.label, scale);
});
}
/* ββ special feature renderers βββββββββββββββββββββββββββββββββββββββββ */
function _renderSpecial(g, panel, t, scale) {
const sp = panel.special;
const sw = `${0.6 / scale}`;
if (sp.type === 'hinge') {
// living hinge lines
const segs = DXF._hingeLines(panel.w, sp.y, sp.hingeH, t);
segs.forEach(([[x1,y1],[x2,y2]]) => {
const l = _el('line');
l.setAttribute('x1', x1); l.setAttribute('y1', y1);
l.setAttribute('x2', x2); l.setAttribute('y2', y2);
l.setAttribute('stroke', HINGE);
l.setAttribute('stroke-width', `${0.5 / scale}`);
g.appendChild(l);
});
// hinge zone boundary
_dashRect(g, 0, sp.y, panel.w, sp.hingeH, HINGE, scale);
// lid zone boundary
_dashRect(g, 0, sp.lidY, panel.w, sp.lidH, '#888', scale);
_addSmallLabel(g, panel.w / 2, sp.y + sp.hingeH / 2, 'HINGE', HINGE, scale);
_addSmallLabel(g, panel.w / 2, sp.lidY + sp.lidH / 2, 'LID', '#888', scale);
}
if (sp.type === 'groove') {
// two lines across panel top indicating groove position
const segs = DXF._grooveLine(panel.w, panel.h, sp);
segs.forEach(([[x1,y1],[x2,y2]]) => {
const l = _el('line');
l.setAttribute('x1', x1); l.setAttribute('y1', y1);
l.setAttribute('x2', x2); l.setAttribute('y2', y2);
l.setAttribute('stroke', GROOVE);
l.setAttribute('stroke-width', sw);
l.setAttribute('stroke-dasharray', `${3/scale},${2/scale}`);
g.appendChild(l);
});
_addSmallLabel(g, panel.w / 2, sp.grooveY + sp.grooveH / 2, 'GROOVE', GROOVE, scale);
}
if (sp.type === 'sliderLid') {
// label it
_addSmallLabel(g, panel.w / 2, panel.h / 2, 'SLIDER LID', '#888', scale);
}
if (sp.type === 'divider') {
sp.slots.forEach(slot => {
const pts2 = DXF._dividerSlot(panel.w, panel.h, slot);
const d2 = _pts2path(pts2) + ' Z';
const r = _el('path');
r.setAttribute('d', d2);
r.setAttribute('fill', 'none');
r.setAttribute('stroke', CUT);
r.setAttribute('stroke-width', `${0.8 / scale}`);
g.appendChild(r);
});
}
}
function _dashRect(g, x, y, w, h, color, scale) {
const r = _el('rect');
r.setAttribute('x', x); r.setAttribute('y', y);
r.setAttribute('width', w); r.setAttribute('height', h);
r.setAttribute('fill', 'none');
r.setAttribute('stroke', color);
r.setAttribute('stroke-width', `${0.5 / scale}`);
r.setAttribute('stroke-dasharray', `${4/scale},${3/scale}`);
r.setAttribute('opacity', '0.5');
g.appendChild(r);
}
/* ββ layout engine βββββββββββββββββββββββββββββββββββββββββββββββββββββ */
function _layout(panels, t) {
// Group panels by col
const cols = {};
panels.forEach(p => {
if (!cols[p.col]) cols[p.col] = [];
cols[p.col].push(p);
});
// For each panel, compute effective bbox (includes finger protrusions)
function eff(p) {
const proL = p.edges.L === 'f' ? t : 0;
const proR = p.edges.R === 'f' ? t : 0;
const proT = p.edges.T === 'f' ? t : 0;
const proB = p.edges.B === 'f' ? t : 0;
return { w: p.w + proL + proR, h: p.h + proT + proB, offX: proL, offY: proT };
}
// Lay out each column: stack panels vertically
let curX = 0;
const placements = new Array(panels.length);
const colKeys = Object.keys(cols).map(Number).sort((a, b) => a - b);
let maxTotalH = 0;
colKeys.forEach(colKey => {
const colPanels = cols[colKey];
const colEffs = colPanels.map(eff);
const colW = Math.max(...colEffs.map(e => e.w));
let curY = 0;
colPanels.forEach((panel, ci) => {
const e = colEffs[ci];
const idx = panels.indexOf(panel);
// Centre panel horizontally within column
const cx = curX + (colW - e.w) / 2 + e.offX;
const cy = curY + e.offY;
placements[idx] = { x: cx, y: cy };
curY += e.h + PAD;
});
maxTotalH = Math.max(maxTotalH, curY - PAD);
curX += colW + PAD;
});
return { placements, totalW: curX - PAD, totalH: maxTotalH };
}
function _fitScale(tw, th) {
const avW = _bounds.w - MARGIN * 2;
const avH = _bounds.h - MARGIN * 2;
return Math.min(avW / tw, avH / th, 3);
}
/* ββ label helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββ */
function _addLabel(g, w, h, label, scale) {
const fs = Math.max(5.5, Math.min(9 / scale, 9));
const tx = _el('text');
tx.setAttribute('x', `${4 / scale}`);
tx.setAttribute('y', `${(fs + 3) / scale}`);
tx.setAttribute('font-family', 'IBM Plex Mono, monospace');
tx.setAttribute('font-size', `${fs / scale}`);
tx.setAttribute('fill', ENGRAVE); tx.setAttribute('opacity', '0.6');
tx.textContent = label;
g.appendChild(tx);
const td = _el('text');
td.setAttribute('x', `${w / 2}`);
td.setAttribute('y', `${h + 12 / scale}`);
td.setAttribute('text-anchor', 'middle');
td.setAttribute('font-family', 'IBM Plex Mono, monospace');
td.setAttribute('font-size', `${fs * 0.8 / scale}`);
td.setAttribute('fill', ENGRAVE); td.setAttribute('opacity', '0.4');
td.textContent = `${w.toFixed(0)}Γ${h.toFixed(0)}`;
g.appendChild(td);
}
function _addSmallLabel(g, x, y, text, color, scale) {
const fs = Math.max(5, Math.min(7 / scale, 8));
const tx = _el('text');
tx.setAttribute('x', `${x}`); tx.setAttribute('y', `${y}`);
tx.setAttribute('text-anchor', 'middle'); tx.setAttribute('dominant-baseline', 'middle');
tx.setAttribute('font-family', 'IBM Plex Mono, monospace');
tx.setAttribute('font-size', `${fs / scale}`);
tx.setAttribute('fill', color); tx.setAttribute('opacity', '0.7');
tx.textContent = text;
g.appendChild(tx);
}
/* ββ subtle grid βββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
function _drawGrid(root, tw, th, scale) {
const g = _el('g');
g.setAttribute('opacity', '0.05');
for (let x = 0; x <= tw; x += 50) {
const l = _el('line');
l.setAttribute('x1',x); l.setAttribute('y1',0);
l.setAttribute('x2',x); l.setAttribute('y2',th);
l.setAttribute('stroke','#000'); l.setAttribute('stroke-width',`${0.5/scale}`);
g.appendChild(l);
}
for (let y = 0; y <= th; y += 50) {
const l = _el('line');
l.setAttribute('x1',0); l.setAttribute('y1',y);
l.setAttribute('x2',tw); l.setAttribute('y2',y);
l.setAttribute('stroke','#000'); l.setAttribute('stroke-width',`${0.5/scale}`);
g.appendChild(l);
}
root.insertBefore(g, root.firstChild);
}
/* ββ SVG export ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
function exportSVG(s) {
if (!_svgEl || !s.valid) return;
const clone = _svgEl.cloneNode(true);
clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
const blob = new Blob([clone.outerHTML], { type: 'image/svg+xml' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `boxmaker_${s.width}x${s.height}x${s.depth}.svg`;
a.click();
URL.revokeObjectURL(url);
}
/* ββ utilities βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ */
function _pts2path(pts) {
return pts.map((p, i) => `${i === 0 ? 'M' : 'L'} ${p[0].toFixed(3)} ${p[1].toFixed(3)}`).join(' ') + ' Z';
}
function _el(tag) { return document.createElementNS('http://www.w3.org/2000/svg', tag); }
return { init, render, exportSVG };
})();