This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgcode-parser.js
162 lines (145 loc) · 4.1 KB
/
gcode-parser.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
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
/**
* @fileoverview GCODE parser
* Based on https://github.com/mrdoob/three.js/blob/dev/examples/js/loaders/GCodeLoader.js
* @author tentone
* @author joewalnes
* @author wakeful-cloud
*/
import
{
LineBasicMaterial,
BufferGeometry,
Float32BufferAttribute,
Line
} from 'three';
/**
* @class GCodeParser GCODE parser
*/
export default class GCodeParser
{
/**
* @param {Color} extrusionColor
* @param {Color} pathColor
*/
constructor(extrusionColor, pathColor)
{
this.extrusionMaterial = new LineBasicMaterial({color: extrusionColor});
this.pathMaterial = new LineBasicMaterial({color: pathColor});
}
/**
* Parse GCODE
* @param {String} gcode The raw GCODE
* @returns {Promise<Object>}
*/
parse(gcode)
{
//Parse asynchronously and without blocking Three.JS
return new Promise(resolve =>
{
//Variables
const extrusionVertices = [];
const pathVertices = [];
let state = {x: 0, y: 0, z: 0, e: 0, f: 0};
let relative = false;
//Remove comments
gcode = gcode.replace(/;.+/g, '').split('\n');
//Parse commands
for (let i = 0; i < gcode.length; i++)
{
//Parse tokens
let tokens = gcode[i].split(' ');
const command = tokens[0].toUpperCase();
const args = {};
tokens = tokens.splice(1);
//Parse arguments
for (let i = 0; i < tokens.length; i++)
{
//If not null, store argument
if (tokens[i][0] != null)
{
const key = tokens[i][0].toLowerCase();
const value = parseFloat(tokens[i].substring(1));
args[key] = value;
}
}
//Convert GCODE to Three.JS land
//Linear move
if (command == 'G0' || command == 'G1')
{
const line = {
x: args.x != null ? absolute(relative, state.x, args.x) : state.x,
y: args.y != null ? absolute(relative, state.y, args.y) : state.y,
z: args.z != null ? absolute(relative, state.z, args.z) : state.z,
e: args.e != null ? absolute(relative, state.e, args.e) : state.e,
f: args.f != null ? absolute(relative, state.f, args.f) : state.f
};
//Only push valid coordinates/states
if (!isNaN(line.x) &&
!isNaN(line.y) &&
!isNaN(line.z) &&
!isNaN(line.e) &&
!isNaN(line.f)
)
{
//Extruding
if (delta(relative, state.e, line.e) > 0)
{
extrusionVertices.push(line.x, line.y, line.z);
}
//Path
else
{
pathVertices.push(line.x, line.y, line.z);
}
}
//Update position
state = line;
}
//Absolute positioning
else if (command == 'G90')
{
relative = false;
}
//Relative positioning
else if (command == 'G91')
{
relative = true;
}
//Set position
else if (command == 'G92')
{
state = args;
}
}
//Object generation
const extrusionBuffer = new BufferGeometry();
const pathBuffer = new BufferGeometry();
extrusionBuffer.setAttribute(
'position',
new Float32BufferAttribute(extrusionVertices, 3)
);
pathBuffer.setAttribute(
'position',
new Float32BufferAttribute(pathVertices, 3)
);
const extrusionObject = new Line(extrusionBuffer, this.extrusionMaterial);
const pathObject = new Line(pathBuffer, this.pathMaterial);
//Cleanup
this.extrusionMaterial.dispose();
this.pathMaterial.dispose();
extrusionBuffer.dispose();
pathBuffer.dispose();
return resolve({extrusion: extrusionObject, path: pathObject});
});
}
}
//Calculate the delta between 2 vertices
function delta(relative, vertex1, vertex2)
{
return relative ? vertex2 : vertex2 - vertex1;
}
//Calculate the absolute value between 2 vertices
function absolute(relative, vertex1, vertex2)
{
return relative ? vertex1 + vertex2 : vertex2;
}