forked from williamsokol/FoldOverBoxGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape.js
More file actions
452 lines (381 loc) · 15.9 KB
/
shape.js
File metadata and controls
452 lines (381 loc) · 15.9 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
var thickness = 4.8
const tabLength = 5
// Settings in MM
const handleHeight = 15
var width = 100
var length = 100
var height = 100
var boxType = "lid"
const form = document.querySelector('form');
form.reset();
function syncDimensionsFromForm() {
thickness = parseFloat(document.getElementById('thickness').value)
length = parseFloat(document.getElementById('length').value)
width = parseFloat(document.getElementById('width').value)
height = parseFloat(document.getElementById('height').value)
const boxTypeInput = document.getElementById('boxType')
if (boxTypeInput) {
boxType = boxTypeInput.value
}
thickness = clamp(thickness, 0.1, 50)
length = clamp(length, 30, 1000)
width = clamp(width, 30, 1000)
height = clamp(height, 50, 1000)
document.getElementById('thickness').value = thickness
document.getElementById('length').value = length
document.getElementById('width').value = width
document.getElementById('height').value = height
if (boxTypeInput) {
boxTypeInput.value = boxType
}
const topClearance = getTopClearance()
myCanvas.width = height*2+thickness*2+length+height*2+thickness*2
myCanvas.height = height*2+width*3+topClearance+40 // Evita retallar la part superior de la tapa
view.viewSize = new Size(myCanvas.width, myCanvas.height)
updateDisplayDimensions()
}
function updateDisplayDimensions() {
const widthEl = document.getElementById('patternWidth')
const heightEl = document.getElementById('patternHeight')
if (widthEl) widthEl.textContent = Math.round(myCanvas.width)
if (heightEl) heightEl.textContent = Math.round(myCanvas.height)
}
form.addEventListener('submit', (event) => {
event.preventDefault(); // Prevent the form from submitting normally
syncDimensionsFromForm()
// console.log(event.submitter.value)
if(event.submitter && event.submitter.value == "Export"){
start()
window.process(getExportFileName())
}
start()
});
// form.dispatchEvent("submit")
syncDimensionsFromForm()
start()
function start(){
project.clear()
// Logic
const topClearance = getTopClearance()
const offset = new Point(height*2+thickness*2,height+width+topClearance) // Més espai per la tapa superior
const LRSizes = new Size(height,width)
const TBSizes = new Size(length-thickness*2,height)
const TB_LRSizes = new Size(width/2,height-thickness)
const handleTemp = new Path.Rectangle(new Rectangle(new Point(0,0), new Size(handleHeight,width/4)),handleHeight/2)
const handleTemp2 = new Path.Rectangle(new Rectangle(new Point(0,0), new Size(handleHeight+5,width/4+5)),handleHeight/2)
// handleTemp.strokeColor = "#000000"
var base = new Path.Rectangle(offset,new Size(length,width))
var baseTabHole1 = new Path.Rectangle(offset+new Point(0,width*1/6),new Size(thickness*2,width/6))
var baseTabHole2 = new Path.Rectangle(offset+new Point(0,width*4/6),new Size(thickness*2,width/6))
var baseTabHole3 = new Path.Rectangle(offset+new Point(length-thickness*2,width*4/6),new Size(thickness*2,width/6))
var baseTabHole4 = new Path.Rectangle(offset+new Point(length-thickness*2,width*1/6),new Size(thickness*2,width/6))
var sideL = new Path.Rectangle(offset-new Point(height,0) ,LRSizes)
var sideL1_5 = new Path.Rectangle(offset-new Point(height+thickness,0) ,new Size(thickness*1.5,LRSizes.height))
var sideL2 = new Path.Rectangle(offset-new Point(height*2+thickness,0),LRSizes)
var sideLTab1 = new Path.Rectangle(offset-new Point(height*2+thickness*2,-width*1/6),new Size(thickness,width/6))
var sideLTab2 = new Path.Rectangle(offset-new Point(height*2+thickness*2,-width*4/6),new Size(thickness,width/6))
var handleL1 = handleTemp.clone()
handleL1.position = offset-new Point(height+thickness/2-height/3,-width/2)
var handleL2 = handleTemp.clone()
handleL2.position = offset-new Point(height+thickness/2+height/3,-width/2)
var sideR = new Path.Rectangle(offset+new Point(length,0),LRSizes)
var sideR1_5 = new Path.Rectangle(offset+new Point(length+height,0),new Size(thickness*1.5,LRSizes.height))
var sideR2 = new Path.Rectangle(offset+new Point(length+height+thickness,0),LRSizes)
var sideRTab1 = new Path.Rectangle(offset+new Point(length+height*2+thickness,width*1/6),new Size(thickness,width/6))
var sideRTab2 = new Path.Rectangle(offset+new Point(length+height*2+thickness,width*4/6),new Size(thickness,width/6))
var handleR1 = handleTemp.clone()
handleR1.position = offset+new Point(length+height+thickness/2-height/3,width/2)
var handleR2 = handleTemp.clone()
handleR2.position = offset+new Point(length+height+thickness/2+height/3,width/2)
var sideT = new Path.Rectangle(offset-new Point(-thickness,height),TBSizes)
var sideTL = new Path.Rectangle(offset-new Point(TB_LRSizes.width-thickness,height),TB_LRSizes)
var sideTR = new Path.Rectangle(offset-new Point(-TBSizes.width-thickness,height),TB_LRSizes)
var handleTL = handleTemp2.clone()
handleTL.position = offset-new Point(TB_LRSizes.width-thickness,thickness+height * 2/3)
handleTL.rotate(90)
// handleTL.scale(1.5)
var handleTR = handleTemp2.clone()
handleTR.position = offset-new Point(-TBSizes.width-TB_LRSizes.width-thickness,thickness+height * 2/3)
handleTR.rotate(90)
// handleTR.scale(1.5)
var sideB = new Path.Rectangle(offset+new Point(thickness,width),TBSizes)
var sideBL = new Path.Rectangle(offset-new Point(TB_LRSizes.width-thickness,-width-thickness),TB_LRSizes)
var sideBR = new Path.Rectangle(offset-new Point(-length+thickness,-width-thickness),TB_LRSizes)
var handleBL = handleTemp2.clone()
handleBL.position = offset-new Point(TB_LRSizes.width-thickness,-width-thickness-height* 2/3)
handleBL.rotate(90)
// handleBL.scale(1.5)
var handleBR = handleTemp2.clone()
handleBR.position = offset-new Point(-TBSizes.width-TB_LRSizes.width-thickness,-width-thickness-height* 2/3)
handleBR.rotate(90)
// handleBR.scale(1.5)
base2 = base.clone()
// base2.strokeColor = "#000000"
// base.strokeColor = "#000000"
// baseTabHole1.strokeColor = "#000000"
// baseTabHole2.strokeColor = "#000000"
// baseTabHole3.strokeColor = "#000000"
// baseTabHole4.strokeColor = "#000000"
// sideL.strokeColor = "#000000"
// sideL1_5.strokeColor = "#000000"
// sideL2.strokeColor = "#000000"
// sideLTab1.strokeColor = "#000000"
// sideLTab2.strokeColor = "#000000"
// handleL1.strokeColor = "#000000"
// handleL2.strokeColor = "#000000"
// sideR.strokeColor = "#000000"
// sideR1_5.strokeColor = "#000000"
// sideR2.strokeColor = "#000000"
// sideRTab1.strokeColor = "#000000"
// sideRTab2.strokeColor = "#000000"
// handleR1.strokeColor = "#000000"
// handleR2.strokeColor = "#000000"
// sideT.strokeColor = "#000000"
// sideTL.strokeColor = "#000000"
// sideTR.strokeColor = "#000000"
// handleTL.strokeColor = "#000000"
// handleTR.strokeColor = "#000000"
// sideB.strokeColor = "#000000"
// sideBL.strokeColor = "#000000"
// sideBR.strokeColor = "#000000"
// handleBL.strokeColor = "#000000"
// handleBR.strokeColor = "#000000"
base = base.subtract(baseTabHole1)
base = base.subtract(baseTabHole2)
base = base.subtract(baseTabHole3)
base = base.subtract(baseTabHole4)
base = base.unite(sideL)
base = base.unite(sideL1_5)
base = base.unite(sideL2)
base = base.unite(sideLTab1)
base = base.unite(sideLTab2)
base = base.unite(sideR)
base = base.unite(sideR1_5)
base = base.unite(sideR2)
base = base.unite(sideRTab1)
base = base.unite(sideRTab2)
base = base.unite(sideT)
base = base.unite(sideTR)
base = base.unite(sideTL)
base = base.unite(sideB)
base = base.unite(sideBR)
base = base.unite(sideBL)
// Els forats de mànec només existeixen al model original (sense tapa)
if (!hasLid()) {
base = base.subtract(handleL1)
base = base.subtract(handleL2)
base = base.subtract(handleR1)
base = base.subtract(handleR2)
base = base.subtract(handleTL)
base = base.subtract(handleTR)
base = base.subtract(handleBL)
base = base.subtract(handleBR)
}
handleL1.remove()
handleL2.remove()
handleR1.remove()
handleR2.remove()
handleTL.remove()
handleTR.remove()
handleBL.remove()
handleBR.remove()
handleTemp.remove()
handleTemp2.remove()
if (hasLid()) {
// Crear tapa tipus "mailer": panell superior + solapa de tancament + solapes laterals
const lidWidth = length - thickness * 2
const lidPanelHeight = width
const lidOffset = offset + new Point(thickness, -height - lidPanelHeight)
const flapDepth = getLidFlapDepth()
// Panell principal de la tapa (enganxat al costat superior)
var lidPanel = new Path.Rectangle(lidOffset, new Size(lidWidth, lidPanelHeight))
// Solapa de tancament superior: ampla (fins a vores) i curta (25% de la tapa)
const lockTabHeight = flapDepth
const lockTabShoulder = Math.max(3, thickness * 1.2)
const lockTabCornerRadius = Math.min(8, lockTabHeight * 0.35)
const lockTabTopInset = Math.max(2, lockTabCornerRadius * 0.9)
var lidLockTab = new Path([
lidOffset + new Point(0, 0),
lidOffset + new Point(0, -lockTabShoulder),
lidOffset + new Point(lockTabShoulder + lockTabTopInset, -lockTabHeight),
lidOffset + new Point(lidWidth - lockTabShoulder - lockTabTopInset, -lockTabHeight),
lidOffset + new Point(lidWidth, -lockTabShoulder),
lidOffset + new Point(lidWidth, 0)
])
lidLockTab.closed = true
// Arrodonir només les cantonades superiors de la solapa de tancament
lidLockTab.segments[2].handleIn = new Point(-lockTabCornerRadius, 0)
lidLockTab.segments[2].handleOut = new Point(lockTabCornerRadius, 0)
lidLockTab.segments[3].handleIn = new Point(-lockTabCornerRadius, 0)
lidLockTab.segments[3].handleOut = new Point(lockTabCornerRadius, 0)
// Solapes laterals: més llargues, en diagonal, arribant a les cantonades de la tapa
const sideWingDepth = flapDepth
const sideWingInset = clamp(flapDepth * 0.35, 4, lidPanelHeight * 0.35)
const cornerRadius = Math.min(6, sideWingInset * 0.5)
var lidSideL = new Path([
lidOffset + new Point(0, 0),
lidOffset + new Point(-sideWingDepth, sideWingInset),
lidOffset + new Point(-sideWingDepth, lidPanelHeight - sideWingInset),
lidOffset + new Point(0, lidPanelHeight)
])
lidSideL.closed = true
// Arrodonir només les cantonades externes (segments 1 i 2)
lidSideL.segments[1].handleIn = new Point(0, -cornerRadius)
lidSideL.segments[1].handleOut = new Point(0, cornerRadius)
lidSideL.segments[2].handleIn = new Point(0, -cornerRadius)
lidSideL.segments[2].handleOut = new Point(0, cornerRadius)
var lidSideR = new Path([
lidOffset + new Point(lidWidth, 0),
lidOffset + new Point(lidWidth + sideWingDepth, sideWingInset),
lidOffset + new Point(lidWidth + sideWingDepth, lidPanelHeight - sideWingInset),
lidOffset + new Point(lidWidth, lidPanelHeight)
])
lidSideR.closed = true
// Arrodonir només les cantonades externes (segments 1 i 2)
lidSideR.segments[1].handleIn = new Point(0, -cornerRadius)
lidSideR.segments[1].handleOut = new Point(0, cornerRadius)
lidSideR.segments[2].handleIn = new Point(0, -cornerRadius)
lidSideR.segments[2].handleOut = new Point(0, cornerRadius)
// Unir tota la geometria de la tapa a la peça principal
base = base.unite(lidPanel)
base = base.unite(lidLockTab)
base = base.unite(lidSideL)
base = base.unite(lidSideR)
}
base.strokeColor = "#ff0000"
if (hasLid()) {
const lidWidth = length - thickness * 2
const lidPanelHeight = width
const lidOffset = offset + new Point(thickness, -height - lidPanelHeight)
// Línia de plegat entre costat superior i panell de tapa
var lidBaseFold = new Path.Line(
offset + new Point(thickness, -height),
offset + new Point(length - thickness, -height)
)
var lidBaseFoldD = dashPath(lidBaseFold)
lidBaseFoldD.strokeColor = "#ff0000"
lidBaseFold.remove()
// Línia de plegat entre panell de tapa i solapa de tancament
var lidTabFold = new Path.Line(
lidOffset + new Point(0, 0),
lidOffset + new Point(lidWidth, 0)
)
var lidTabFoldD = dashPath(lidTabFold)
lidTabFoldD.strokeColor = "#ff0000"
lidTabFold.remove()
// Línies de plegat de les solapes laterals
var lidSideLFold = new Path.Line(
new Point(lidOffset.x, lidOffset.y),
new Point(lidOffset.x, lidOffset.y + lidPanelHeight)
)
var lidSideLFoldD = dashPath(lidSideLFold)
lidSideLFoldD.strokeColor = "#ff0000"
lidSideLFold.remove()
var lidSideRFold = new Path.Line(
new Point(lidOffset.x + lidWidth, lidOffset.y),
new Point(lidOffset.x + lidWidth, lidOffset.y + lidPanelHeight)
)
var lidSideRFoldD = dashPath(lidSideRFold)
lidSideRFoldD.strokeColor = "#ff0000"
lidSideRFold.remove()
}
var base2D = dashPath(base2)
var sideL1_5D = dashPath(sideL1_5)
var sideR1_5D = dashPath(sideR1_5)
// Pestanyes interiors: dibuixar només la línia de plegat d'unió
var sideTLFold = new Path.Line(
new Point(sideTL.bounds.right, sideTL.bounds.top),
new Point(sideTL.bounds.right, sideTL.bounds.bottom)
)
var sideTRFold = new Path.Line(
new Point(sideTR.bounds.left, sideTR.bounds.top),
new Point(sideTR.bounds.left, sideTR.bounds.bottom)
)
var sideBLFold = new Path.Line(
new Point(sideBL.bounds.right, sideBL.bounds.top),
new Point(sideBL.bounds.right, sideBL.bounds.bottom)
)
var sideBRFold = new Path.Line(
new Point(sideBR.bounds.left, sideBR.bounds.top),
new Point(sideBR.bounds.left, sideBR.bounds.bottom)
)
var sideTLFoldD = dashPath(sideTLFold)
var sideTRFoldD = dashPath(sideTRFold)
var sideBLFoldD = dashPath(sideBLFold)
var sideBRFoldD = dashPath(sideBRFold)
sideTLFold.remove()
sideTRFold.remove()
sideBLFold.remove()
sideBRFold.remove()
base2D.strokeColor = "#ff0000"
sideL1_5D.strokeColor = "#ff0000"
sideR1_5D.strokeColor = "#ff0000"
sideTLFoldD.strokeColor = "#ff0000"
sideTRFoldD.strokeColor = "#ff0000"
sideBLFoldD.strokeColor = "#ff0000"
sideBRFoldD.strokeColor = "#ff0000"
}
// process()
function dashPath(path){
// console.log(path.segments)
const dashline = 8
const dashgap = 7
var dashPos = dashgap
compoundPath = new CompoundPath()
while (path.length > dashPos+8){
var skip = false
path.segments.forEach(s => {
// console.log(path.getOffsetOf(s.point))
if(dashPos-dashgap/2 < path.getOffsetOf(s.point) && dashPos+dashline+dashgap/2 > path.getOffsetOf(s.point)){
skip = true
}
});
var segments = [path.getPointAt(dashPos), path.getPointAt(dashPos+dashline)];
dashPos += dashline + dashgap
if(skip){ continue }
var p = new Path(segments);
compoundPath.addChild(new Path(p.segments))
}
// compoundPath.selected = true
return compoundPath
}
function process()
{
project.activeLayer.scale(3.779528)
// project.activeLayer.scale(2)
project.activeLayer.position = project.activeLayer.bounds.size/2;
// myCanvas is an Id made in html doc
myCanvas.width = project.activeLayer.bounds.width+100
myCanvas.height = project.activeLayer.bounds.height+10
view.viewSize = new Size(myCanvas.width, myCanvas.height)
// downloadAsSVG()
// console.log(project.exportSVG())
// rect.strokeColor = "#000000"
}
function clamp(number, min, max) {
return Math.max(min, Math.min(number, max));
}
function getLidFlapDepth() {
return Math.max(10, width * 0.25)
}
function getTopClearance() {
// Deixa espai extra només quan la tapa està activa.
return hasLid() ? getLidFlapDepth() + 40 : 0
}
function hasLid() {
return boxType === "lid"
}
function formatDimensionForFile(value) {
const rounded = Math.round(value * 100) / 100
if (Number.isInteger(rounded)) {
return String(rounded)
}
return String(rounded).replace('.', '_')
}
function getExportFileName() {
const boxSuffix = hasLid() ? "tapa" : "normal"
const l = formatDimensionForFile(length)
const w = formatDimensionForFile(width)
const h = formatDimensionForFile(height)
return l + "x" + w + "x" + h + boxSuffix + ".svg"
}