|
| 1 | +/** |
| 2 | + * Example: slice the piggy bank test model from an STL and write G-code to resources/gcode/benchmarks. |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * node examples/scripts/slice-piggy.js |
| 6 | + */ |
| 7 | + |
| 8 | +const { Polyslice, Printer, Filament } = require("../../src/index"); |
| 9 | +const fs = require("fs"); |
| 10 | +const path = require("path"); |
| 11 | +const THREE = require("three"); |
| 12 | + |
| 13 | +async function loadSTLMesh(stlPath) { |
| 14 | + const { STLLoader } = await import("three/examples/jsm/loaders/STLLoader.js"); |
| 15 | + const buffer = fs.readFileSync(stlPath); |
| 16 | + const loader = new STLLoader(); |
| 17 | + const geometry = loader.parse(buffer.buffer); |
| 18 | + geometry.computeVertexNormals?.(); |
| 19 | + const material = new THREE.MeshPhongMaterial({ color: 0x808080, specular: 0x111111, shininess: 200 }); |
| 20 | + const mesh = new THREE.Mesh(geometry, material); |
| 21 | + |
| 22 | + // Place bottom at Z=0 (build plate) |
| 23 | + geometry.computeBoundingBox(); |
| 24 | + const bb = geometry.boundingBox; |
| 25 | + const zShift = -bb.min.z; |
| 26 | + mesh.position.set(0, 0, zShift); |
| 27 | + mesh.updateMatrixWorld(); |
| 28 | + return mesh; |
| 29 | +} |
| 30 | + |
| 31 | +async function main() { |
| 32 | + console.log("Polyslice Piggy Bank Example"); |
| 33 | + console.log("============================\n"); |
| 34 | + |
| 35 | + const stlPath = path.join(__dirname, "../../resources/testing/piggy/bank.test.stl"); |
| 36 | + |
| 37 | + const printer = new Printer("Ender5"); |
| 38 | + const filament = new Filament("GenericPLA"); |
| 39 | + |
| 40 | + console.log("Printer & Filament:"); |
| 41 | + console.log(`- Printer: ${printer.model}`); |
| 42 | + console.log(`- Volume: ${printer.getSizeX()}x${printer.getSizeY()}x${printer.getSizeZ()} mm`); |
| 43 | + console.log(`- Filament: ${filament.name} (${filament.type.toUpperCase()})\n`); |
| 44 | + |
| 45 | + console.log("Loading STL..."); |
| 46 | + console.log(`- ${stlPath}`); |
| 47 | + |
| 48 | + let mesh; |
| 49 | + try { |
| 50 | + mesh = await loadSTLMesh(stlPath); |
| 51 | + } catch (err) { |
| 52 | + console.error("Failed to load STL:", err.message); |
| 53 | + process.exit(1); |
| 54 | + } |
| 55 | + |
| 56 | + const pos = mesh.geometry.attributes.position; |
| 57 | + const triApprox = pos ? ((pos.count / 3) | 0) : null; |
| 58 | + console.log("\u2705 Mesh loaded"); |
| 59 | + console.log(`- Geometry: ${mesh.geometry.type}`); |
| 60 | + console.log(`- Vertices: ${pos ? pos.count : "unknown"}`); |
| 61 | + if (pos) console.log(`- Triangles (approx): ${triApprox}\n`); |
| 62 | + |
| 63 | + const slicer = new Polyslice({ |
| 64 | + printer, |
| 65 | + filament, |
| 66 | + shellSkinThickness: 0.8, |
| 67 | + shellWallThickness: 0.8, |
| 68 | + lengthUnit: "millimeters", |
| 69 | + timeUnit: "seconds", |
| 70 | + infillPattern: "grid", |
| 71 | + infillDensity: 20, |
| 72 | + bedTemperature: 0, |
| 73 | + layerHeight: 0.2, |
| 74 | + testStrip: false, |
| 75 | + metadata: false, |
| 76 | + verbose: true |
| 77 | + }); |
| 78 | + |
| 79 | + console.log("Slicing model..."); |
| 80 | + const t0 = Date.now(); |
| 81 | + const gcode = slicer.slice(mesh); |
| 82 | + const dt = Date.now() - t0; |
| 83 | + console.log(`\u2705 Sliced in ${dt} ms`); |
| 84 | + |
| 85 | + // Save G-code into resources/gcode/benchmarks |
| 86 | + const outDir = path.join(__dirname, "../../resources/gcode/benchmarks"); |
| 87 | + if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true }); |
| 88 | + const outPath = path.join(outDir, "piggy-bank.gcode"); |
| 89 | + fs.writeFileSync(outPath, gcode); |
| 90 | + |
| 91 | + const size = fs.statSync(outPath).size; |
| 92 | + const lines = gcode.split("\n"); |
| 93 | + const layers = lines.filter(l => l.includes("LAYER:")).length; |
| 94 | + |
| 95 | + console.log("G-code Summary:"); |
| 96 | + console.log(`- File: ${outPath}`); |
| 97 | + console.log(`- Size: ${(size/1024).toFixed(1)} KB`); |
| 98 | + console.log(`- Lines: ${lines.length}`); |
| 99 | + console.log(`- Layers: ${layers}`); |
| 100 | + console.log("Done."); |
| 101 | +} |
| 102 | + |
| 103 | +main().catch(err => { |
| 104 | + console.error("Error:", err); |
| 105 | + process.exit(1); |
| 106 | +}); |
0 commit comments