Debian Codegraph @

examples/nodejs/parallel_finish_zig.js

raw
1 const fs = require('fs')
2 const {
3 STLSurf,
4 STLReader,
5 CylCutter,
6 BallCutter,
7 BullCutter,
8 ConeCutter,
9 AdaptivePathDropCutter,
10 Path,
11 Line,
12 Point
13 } = require('@opencamlib/opencamlib')
14
15 function assert(condition, message) {
16 if (!condition) {
17 throw message || "Assertion failed";
18 }
19 }
20
21 // create a simple "Zig" pattern where we cut only in one direction.
22 // the first line is at ymin
23 // the last line is at ymax
24 function YdirectionZigPath(xmin, xmax, ymin, ymax, Ny) {
25 const paths = []
26 const dy = (ymax - ymin) / (Ny - 1) // the y step-over
27 for (let n = 0; n < Ny; n++) {
28 const path = new Path()
29 const y = ymin + n * dy // current y-coordinate
30 if (n === Ny - 1) {
31 assert(y == ymax)
32 } else if (n === 0) {
33 assert(y == ymin)
34 }
35 const p1 = new Point(xmin, y, 0) // start-point of line
36 const p2 = new Point(xmax, y, 0) // end-point of line
37 const l = new Line(p1, p2) // line-object
38 path.append(l) // add the line to the path
39 paths.push(path)
40 }
41 return paths
42 }
43
44 // run the actual drop-cutter algorithm
45 function adaptivePathDropCutter(surface, cutter, paths) {
46 const apdc = new AdaptivePathDropCutter()
47 apdc.setSTL(surface)
48 apdc.setCutter(cutter)
49 // maximum sampling or "step-forward" distance
50 // should be set so that we don't loose any detail of the STL model
51 // i.e. this number should be similar or smaller than the smallest triangle
52 apdc.setSampling(0.04)
53 // minimum sampling or step-forward distance
54 // the algorithm subdivides "steep" portions of the toolpath
55 // until we reach this limit.
56 apdc.setMinSampling(0.01)
57 let cl_paths = []
58 paths.forEach(function (path) {
59 apdc.setPath(path)
60 apdc.run()
61 cl_points = apdc.getCLPoints()
62 cl_paths.push(cl_points)
63 })
64 return cl_paths
65 }
66
67 const surface = new STLSurf()
68 const stlContents = fs.readFileSync(__dirname + '/../../stl/gnu_tux_mod.stl')
69 new STLReader(stlContents, surface)
70 // choose a cutter for the operation:
71 // http://www.anderswallin.net/2011/08/opencamlib-cutter-shapes/
72 const diameter = 0.25
73 const length = 5
74 // const cutter = new BallCutter(diameter, length)
75 // const cutter = new CylCutter(diameter, length)
76 // const corner_radius = 0.05
77 // const cutter = new BullCutter(diameter, corner_radius, length)
78 const angle = Math.PI / 4
79 const cutter = new ConeCutter(diameter, angle, length)
80 // const cutter = cutter.offsetCutter(0.4)
81
82 // toolpath is contained in this simple box
83 const ymin = 0
84 const ymax = 12
85 const xmin = 0
86 const xmax = 10
87 const Ny = 40 // number of lines in the y-direction
88
89 const paths = YdirectionZigPath(xmin, xmax, ymin, ymax, Ny)
90
91 // now project onto the STL surface
92 const toolpaths = adaptivePathDropCutter(surface, cutter, paths)
93 toolpaths.forEach(function (points) {
94 console.log('G00 X' + Math.round(points[0][0] * 10000) / 10000 + ' Y' + Math.round(points[0][1] * 10000) / 10000)
95 console.log('G01 Z' + Math.round(points[0][2] * 10000) / 10000)
96 points.forEach(function (point) {
97 console.log('G01 X' + Math.round(point[0] * 10000) / 10000 + ' Y' + Math.round(point[1] * 10000) / 10000 + ' Z' + Math.round(point[2] * 10000) / 10000)
98 })
99 console.log('G00 Z3')
100 })