-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress-example.js
More file actions
92 lines (77 loc) · 3.01 KB
/
progress-example.js
File metadata and controls
92 lines (77 loc) · 3.01 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
/**
* Example: Demonstrate progress callback functionality during slicing.
*
* This example shows how to use the progressCallback option to provide
* real-time feedback during the slicing process. This is especially
* useful for long slices that might take several minutes.
*
* Usage:
* node examples/scripts/progress-example.js
*/
const { Polyslice, Printer, Filament } = require("../../src/index");
const THREE = require("three");
// Simple progress bar utility
function createProgressBar(current, total, barLength = 40) {
const percent = Math.floor((current / total) * 100);
const filled = Math.floor((current / total) * barLength);
const empty = barLength - filled;
const bar = '█'.repeat(filled) + '░'.repeat(empty);
return `[${bar}] ${percent}%`;
}
// Track last stage to add newlines between stages
let lastStage = null;
function main() {
console.log("Polyslice Progress Callback Example");
console.log("====================================\n");
const printer = new Printer("Ender3");
const filament = new Filament("GenericPLA");
console.log("Configuration:");
console.log(`- Printer: ${printer.model}`);
console.log(`- Filament: ${filament.name}`);
console.log();
// Create a test mesh (20mm cube)
const geometry = new THREE.BoxGeometry(20, 20, 20);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
console.log("Creating slicer with custom progress callback...\n");
const slicer = new Polyslice({
printer: printer,
filament: filament,
layerHeight: 0.2,
infillPattern: "hexagons",
infillDensity: 20,
testStrip: false,
verbose: true,
progressCallback: (progressInfo) => {
// Add newline when stage changes
if (lastStage && lastStage !== progressInfo.stage) {
console.log();
}
lastStage = progressInfo.stage;
// Format the output based on the stage
if (progressInfo.stage === "slicing" && progressInfo.currentLayer && progressInfo.totalLayers) {
// Show detailed layer progress
const bar = createProgressBar(progressInfo.currentLayer, progressInfo.totalLayers);
process.stdout.write(`\r${progressInfo.stage.toUpperCase()}: ${bar} - ${progressInfo.message || ''}`);
} else {
// Show simple progress for other stages
const bar = createProgressBar(progressInfo.percent, 100);
process.stdout.write(`\r${progressInfo.stage.toUpperCase()}: ${bar} - ${progressInfo.message || ''}`);
}
// Add newline for completion
if (progressInfo.percent === 100) {
console.log();
}
}
});
console.log("Starting slicing process...\n");
const startTime = Date.now();
const gcode = slicer.slice(cube);
const duration = Date.now() - startTime;
console.log();
console.log("✅ Slicing Complete!");
console.log(` Duration: ${duration} ms`);
console.log(` G-code lines: ${gcode.split('\n').length}`);
console.log(` G-code size: ${(gcode.length / 1024).toFixed(2)} KB`);
}
main();