Debian Codegraph @

examples/nodejs/test.js

raw
1 const fs = require('fs')
2 const {
3 Waterline,
4 AdaptiveWaterline,
5 PathDropCutter,
6 AdaptivePathDropCutter,
7 STLReader,
8 STLSurf,
9 CylCutter,
10 Path,
11 Point,
12 Line
13 } = require('@opencamlib/opencamlib')
14
15 function pointToXYZ(point) {
16 return `X${Math.round(point[0] * 100000) / 100000} Y${Math.round(point[1] * 100000) / 100000} Z${Math.round(point[2] * 100000) / 100000}`
17 }
18
19 async function waterline(surface, cutter, z, sampling) {
20 const wl = new Waterline()
21 wl.setSTL(surface)
22 wl.setCutter(cutter)
23 wl.setZ(z)
24 wl.setSampling(sampling)
25 wl.run()
26 let gcode = ''
27 const loops = wl.getLoops()
28 for (var i = 0; i < loops.length; i++) {
29 const loop = loops[i];
30 for (var j = 0; j < loop.length; j++) {
31 const point = loop[j]
32 gcode += 'G01 ' + pointToXYZ(point) + '\n'
33 }
34 }
35 console.log(gcode)
36 }
37
38 async function adaptiveWaterline(surface, cutter, z, sampling, minSampling) {
39 console.log('AdaptiveWaterline')
40 const awl = new AdaptiveWaterline()
41 awl.setSTL(surface)
42 awl.setCutter(cutter)
43 awl.setZ(z)
44 awl.setSampling(sampling)
45 awl.setMinSampling(minSampling)
46 awl.run()
47 let gcode = ''
48 const loops = awl.getLoops()
49 for (var i = 0; i < loops.length; i++) {
50 const loop = loops[i]
51 for (var j = 0; j < loop.length; j++) {
52 const point = loop[j]
53 gcode += 'G01 ' + pointToXYZ(point) + '\n'
54 }
55 }
56 console.log(gcode)
57 }
58
59 async function pathDropCutter(surface, cutter, sampling, path) {
60 console.log('PathDropCutter')
61 const pdc = new PathDropCutter()
62 pdc.setSTL(surface)
63 pdc.setCutter(cutter)
64 pdc.setPath(path)
65 pdc.setZ(0)
66 pdc.setSampling(sampling)
67 pdc.run()
68 const points = pdc.getCLPoints()
69 let gcode = ''
70 for (var j = 0; j < points.length; j++) {
71 const point = points[j]
72 gcode += 'G01 ' + pointToXYZ(point) + '\n'
73 }
74 console.log(gcode)
75 }
76
77 async function adaptivePathDropCutter(surface, cutter, sampling, minSampling, path) {
78 console.log('AdaptivePathDropCutter')
79 const apdc = new AdaptivePathDropCutter()
80 apdc.setSTL(surface)
81 apdc.setCutter(cutter)
82 apdc.setPath(path)
83 apdc.setZ(0)
84 apdc.setSampling(sampling)
85 apdc.setMinSampling(minSampling)
86 apdc.run()
87 const points = apdc.getCLPoints()
88 let gcode = ''
89 for (var j = 0; j < points.length; j++) {
90 const point = points[j]
91 gcode += 'G01 ' + pointToXYZ(point) + '\n'
92 }
93 console.log(gcode)
94 }
95
96 async function main() {
97 const surface = new STLSurf()
98 const stlContents = fs.readFileSync(__dirname + '/../../stl/gnu_tux_mod.stl')
99 new STLReader(stlContents, surface)
100 const cutter = new CylCutter(4, 20)
101 await waterline(surface, cutter, 1, 0.1)
102 await adaptiveWaterline(surface, cutter, 1, 0.1, 0.001)
103 const path = new Path()
104 const p1 = new Point(-2, 4, 0)
105 const p2 = new Point(11, 4, 0)
106 const l = new Line(p1, p2)
107 path.append(l)
108 await pathDropCutter(surface, cutter, 0.1, path)
109 await adaptivePathDropCutter(surface, cutter, 0.04, 0.01, path)
110 }
111
112 main()