This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
97 lines (79 loc) · 2.59 KB
/
index.js
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
/**
* @fileoverview Cura WASM Demo
*/
//Imports
import {CuraWASM} from '../src/index.ts';
import {FileFormats} from 'unified-3d-loader';
import {resolveDefinition} from 'cura-wasm-definitions';
//Update file input accept attribute
const extensions = [];
for (const format of Object.values(FileFormats))
{
extensions.push(...format.extensions.map(extension => `.${extension}`));
}
document.getElementById('upload').accept = extensions;
//Getters and handlers (These are overwritten by Cypress)
window.getFile = async () =>
{
return await document.getElementById('upload').files[0];
};
window.handleFinish = async gcode =>
{
//Create the download link and download the file
const blob = new Blob([gcode], {
type: 'text/plain'
});
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'Output.gcode';
link.click();
link.remove();
};
window.transferFile = true;
//Upload
document.getElementById('slice').addEventListener('click', async () =>
{
//Get the file
const file = await window.getFile();
const bytes = await file.arrayBuffer();
const extension = file.name.split('.').pop();
//Create a slicer
const slicer = new CuraWASM({
command: window.command,
definition: resolveDefinition('ultimaker2'),
overrides: window.overrides,
transfer: window.transferFile
});
//Add progress handler
slicer.on('progress', percent =>
{
document.getElementById('progress').value = percent;
document.getElementById('percent').innerText = `${percent}%`;
});
//Slice
const start = Date.now();
const {gcode, metadata} = await slicer.slice(bytes, extension);
const end = Date.now();
console.log(metadata);
//Used by E2E tests
window.afterFile = bytes;
//Calculate elapsed time
const elapsed = new Date(end - start);
//Format estimated time
const estimatedTime = `${Math.floor(metadata.printTime / 3600)}h ${Math.floor(metadata.printTime % 3600 / 60)}m ${Math.floor(metadata.printTime % 3600 % 60)}s`;
//Display metadata
document.getElementById('metadata').innerText = `Elapsed time: ${elapsed.valueOf()}ms
GCODE flavor: ${metadata.flavor}
Estimated print time: ${estimatedTime}
Nozzle Size: ${metadata.nozzleSize}mm
Filament Usage: ${metadata.filamentUsage}mm³
Material 1 usage: ${metadata.material1Usage}mm³
Material 2 usage: ${metadata.material2Usage}mm³`;
//Get download button
const download = document.getElementById('download');
//Set download callback
download.disabled = false;
download.addEventListener('click', () => window.handleFinish(gcode, metadata));
//Cleanup
await slicer.destroy();
});