forked from fluidd-core/fluidd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseGcode.ts
More file actions
345 lines (300 loc) · 8.69 KB
/
Copy pathparseGcode.ts
File metadata and controls
345 lines (300 loc) · 8.69 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* eslint-disable no-fallthrough */
import type { ArcMove, BBox, Layer, LinearMove, Move, Part, PositioningMode } from '@/store/gcodePreview/types'
import isKeyOf from '@/util/is-key-of'
import { pick } from 'lodash-es'
import { split } from 'shlex'
const getArgsFromGcodeCommandArgs = (gcodeCommandArgs: string) => {
const args: Record<string, number | undefined> = {}
for (const [, key, value] of gcodeCommandArgs.matchAll(/([a-z])[ \t]*(-?(?:\d+(?:\.\d+)?|\.\d+))?/gi)) {
args[key.toLowerCase()] = value ? +value : undefined
}
return args
}
const getArgsFromMacroCommandArgs = (macroCommandArgs: string) => {
const args: Record<string, string> = {}
for (const entry of split(macroCommandArgs)) {
const eqIndex = entry.indexOf('=')
const key = entry.substring(0, eqIndex)
const value = entry.substring(eqIndex + 1)
args[key.toLowerCase()] = value
}
return args
}
const parseLine = (line: string) => {
const clearedLine = line
.trim()
.split(';', 2)[0]
const [, gcodeCommand, gcodeCommandArgs = ''] = clearedLine
.split(/^([gmt]\d+)\s*/i)
if (gcodeCommand) {
return {
type: 'gcode' as const,
command: gcodeCommand.toUpperCase(),
args: getArgsFromGcodeCommandArgs(gcodeCommandArgs)
}
}
const [, macroCommand, macroCommandArgs = ''] = clearedLine
.split(/^(SET_PRINT_STATS_INFO|EXCLUDE_OBJECT_DEFINE|SET_RETRACTION)\s+/i)
if (macroCommand) {
return {
type: 'macro' as const,
command: macroCommand.toUpperCase(),
args: getArgsFromMacroCommandArgs(macroCommandArgs)
}
}
return {
type: 'other' as const
}
}
const decimalRound = (a: number) => {
return Math.round(a * 10000) / 10000
}
const isPolygonData = (data: unknown): data is [number, number][] => (
Array.isArray(data) &&
data
.every(x => (
Array.isArray(x) &&
x.length === 2 &&
x.every(y => typeof y === 'number')
))
)
const parseGcode = (gcode: string, sendProgress: (filePosition: number) => void) => {
const moves: Move[] = []
const layers: Layer[] = []
const parts: Part[] = []
const tools = new Set<number>()
const lines = gcode.split('\n')
let newLayerForNextMove = false
let extrusionMode: PositioningMode = 'relative'
let positioningMode: PositioningMode = 'absolute'
const toolhead = {
x: 0,
y: 0,
z: 0,
e: 0,
}
let tool = 0
let filePosition = 0
const bounds: BBox = {
x: {
min: Number.POSITIVE_INFINITY,
max: Number.NEGATIVE_INFINITY
},
y: {
min: Number.POSITIVE_INFINITY,
max: Number.NEGATIVE_INFINITY
}
}
// todo get from firmware
// store path: printer.printer.configFile.settings.firmware_retraction
// { retract_length: number; unretract_extra_length: number }
const fwretraction = {
length: 1,
extrudeExtra: 0,
z: 0
}
for (let i = 0; i < lines.length; i++) {
const { type, command, args } = parseLine(lines[i]) ?? {}
let move: Move | null = null
if (type === 'macro') {
switch (command) {
case 'SET_PRINT_STATS_INFO':
if ('current_layer' in args) {
newLayerForNextMove = true
}
break
case 'EXCLUDE_OBJECT_DEFINE':
if ('polygon' in args && args.polygon) {
try {
const data = JSON.parse(args.polygon)
if (isPolygonData(data)) {
const part: Part = {
polygon: data
.map(([x, y]) => ({ x, y }))
}
parts.push(part)
}
} catch {
// ignore invalid JSON
}
}
break
case 'SET_RETRACTION':
if ('retract_length' in args) {
fwretraction.length = +args.retract_length
}
if ('unretract_extra_length' in args) {
fwretraction.extrudeExtra = +args.unretract_extra_length
}
break
}
} else if (type === 'gcode') {
switch (command) {
case 'G0':
case 'G1': {
const params: (keyof LinearMove)[] = [
'x', 'y', 'z', 'e'
]
if (params.some(param => param in args)) {
move = {
...pick(args, params),
tool,
filePosition
} satisfies LinearMove
}
break
}
case 'G2':
case 'G3': {
const params: (keyof ArcMove)[] = [
'x', 'y', 'z', 'e',
'i', 'j', 'k', 'r'
]
if (params.some(param => param in args)) {
move = {
...pick(args, params),
d: command === 'G2'
? 'clockwise'
: 'counter-clockwise',
tool,
filePosition
} satisfies ArcMove
}
break
}
case 'G10':
move = {
e: -fwretraction.length,
tool,
filePosition
} satisfies LinearMove
if (fwretraction.z !== 0) {
move.z = decimalRound(toolhead.z + fwretraction.z)
}
break
case 'G11':
move = {
e: decimalRound(fwretraction.length + fwretraction.extrudeExtra),
tool,
filePosition
} satisfies LinearMove
if (fwretraction.z !== 0) {
move.z = decimalRound(toolhead.z - fwretraction.z)
}
break
case 'G28': {
const hasX = 'x' in args
const hasY = 'y' in args
const hasZ = 'z' in args
const noXYZ = !hasX && !hasY && !hasZ
move = {
tool,
filePosition
} satisfies LinearMove
if (hasX || noXYZ) {
move.x = 0
}
if (hasY || noXYZ) {
move.y = 0
}
if (hasZ || noXYZ) {
move.z = 0
}
break
}
case 'G90':
positioningMode = 'absolute'
case 'M82':
extrusionMode = 'absolute'
toolhead.e = 0
break
case 'G91':
positioningMode = 'relative'
case 'M83':
extrusionMode = 'relative'
break
case 'G92':
if (extrusionMode === 'absolute') {
toolhead.e = args.e ?? toolhead.e
}
if (positioningMode === 'absolute') {
toolhead.x = args.x ?? toolhead.x
toolhead.y = args.y ?? toolhead.y
toolhead.z = args.z ?? toolhead.z
}
break
case 'M207':
fwretraction.length = args.s ?? fwretraction.length
fwretraction.z = args.z ?? fwretraction.z
break
case 'M600':
tools.add(0)
tool = (tool + 1) % 10
tools.add(tool)
break
default:
if (command.startsWith('T')) {
tool = +command.substring(1)
tools.add(tool)
}
break
}
if (move) {
if (extrusionMode === 'absolute' && move.e !== undefined) {
const extrusionLength = decimalRound(move.e - toolhead.e)
toolhead.e = move.e
move.e = extrusionLength
}
if (positioningMode === 'relative') {
if (move.x !== undefined) {
move.x = decimalRound(move.x + toolhead.x)
}
if (move.y !== undefined) {
move.y = decimalRound(move.y + toolhead.y)
}
if (move.z !== undefined) {
move.z = decimalRound(move.z + toolhead.z)
}
}
if (newLayerForNextMove && move.e && move.e > 0) {
const m = move
if (['x', 'y', 'i', 'j'].some(x => isKeyOf(x, m) && m[x] !== 0)) {
const layer: Layer = {
z: toolhead.z,
move: moves.length - 1,
filePosition
}
layers.push(layer)
newLayerForNextMove = false
}
}
toolhead.x = move.x ?? toolhead.x
toolhead.y = move.y ?? toolhead.y
toolhead.z = move.z ?? toolhead.z
moves.push(move)
if (layers.length > 0) {
bounds.x.min = Math.min(bounds.x.min, toolhead.x)
bounds.x.max = Math.max(bounds.x.max, toolhead.x)
bounds.y.min = Math.min(bounds.y.min, toolhead.y)
bounds.y.max = Math.max(bounds.y.max, toolhead.y)
}
}
}
if (i % Math.floor(lines.length / 100) === 0) {
sendProgress(filePosition)
}
filePosition += lines[i].length + 1 // + 1 for newline
}
sendProgress(filePosition)
return {
moves,
layers,
parts,
bounds: layers.length > 0
? bounds
: null,
tools: [...tools]
.sort((a, b) => a - b)
}
}
export default parseGcode