-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·109 lines (85 loc) · 2.94 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
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env node
const program = require('commander');
const fs = require('fs')
program
.version('1.0.0')
.usage('[options] <input file> [output file]')
.option('-t, --tool-off-gcode [gcode]', 'Tool Off GCODE', 'G1 Z3')
.option('-e, --extend [millimeters]', 'Extend each path by how many millimeters', 4)
.parse(process.argv);
let mmExtension = program.extend; // How many mm to extend the path by
var file = program.args[0]
var outputFile = program.args[1]
var gcode = fs.readFileSync(file).toString();
var toolOff = program.toolOffGcode;
// console.log('-- BEGIN ---')
var chunks = gcode.split(toolOff)
let newChunks = chunks.map(function(chunk, chunkIndex){
// Ignore the last chunk
let theMatch = chunk.match(/Pass \d+ Path \d+/)
let newChunk = chunk + "\n"
if(theMatch){
newChunk += "; INJECTION START\n"
// console.log('hi', chunk)
let totalDistance = 0
let initialLine = chunk.match(/G0 X([0-9.]+) Y([0-9.]+)/)
if (!initialLine) {
// console.log(chunkIndex)
throw new Error(`No initial line for chunk #${chunkIndex}`)
}
let initialX = initialLine[1]
let initialY = initialLine[2]
// console.log('Initial Coordinates:', initialX, initialY)
var lines = chunk.match(/G1 X.*/g)
let lineArray = lines.map(function(line, lineIndex){
let lineMatch = line.match(/X([0-9.]+) Y([0-9.]+)/)
if (!lineMatch) throw new Error('Line Match Corrupted')
return {
raw: line,
x: lineMatch[1],
y: lineMatch[2]
}
})
let upUntilLineNumber = lineArray.length - 1
lineArray.every(function(lineObj, lineIndex){
let X1 = 0
let Y1 = 0
let X2 = lineObj.x
let Y2 = lineObj.y
if(lineIndex === 0){
X1 = initialX
Y1 = initialY
} else {
X1 = lineArray[lineIndex-1].x
Y1 = lineArray[lineIndex-1].y
}
let distance = Math.sqrt( (X1 - X2)*(X1 - X2) + (Y1 - Y2)*(Y1 - Y2) )
totalDistance += distance
// console.log('Line #', lineIndex)
// console.log('-- Prev', X1, Y1)
// console.log('-- Now', X2, Y2)
// console.log('-- Distance', distance)
// console.log('-- Total Distance', totalDistance)
if(totalDistance >= mmExtension) {
upUntilLineNumber = lineIndex
return false
}
return true
})
// console.log('Up Until Line Number:', upUntilLineNumber, 'of', lineArray.length)
for(let i = 0; i <= upUntilLineNumber; i++) {
newChunk += lines[i].match(/G1 X[0-9.]+ Y[0-9.]+/g)[0] + "\n"
}
newChunk += `; END INJECTION (Added ${upUntilLineNumber-1} lines of the total ${lines.length} lines in this path)\n\n`
}
if(chunkIndex !== chunks.length -1){
newChunk += toolOff
}
return newChunk
})
var finalOutputGCODE = newChunks.join("")
if (outputFile) {
fs.writeFileSync(outputFile, finalOutputGCODE)
} else {
console.log(finalOutputGCODE)
}