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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use core::f64;
use core::f64::consts::TAU;
use core::fmt::Display;
use crate::ArcParams;
use crate::MM_PER_ARC_SEGMENT;
use crate::PositionMode;
use crate::command::Command;
use crate::compute_arc;
use crate::params::head::PosVal;
/// SVG representation of a G-Code file.
///
/// wraps the min and max x, y values of the SVG.
#[derive(Debug, Clone, PartialEq)]
pub struct Svg {
min_x: f64,
min_y: f64,
max_x: f64,
max_y: f64,
parts: Vec<String>,
}
impl Default for Svg {
fn default() -> Self {
Self {
min_x: f64::INFINITY,
max_x: -f64::INFINITY,
min_y: f64::INFINITY,
max_y: -f64::INFINITY,
parts: Vec::default(),
}
}
}
impl Svg {
fn update_view_box(&mut self, proj_x: f64, proj_y: f64) {
// Record min max x, y
if proj_x > self.max_x {
self.max_x = proj_x;
}
if proj_x < self.min_x {
self.min_x = proj_x;
}
if proj_y > self.max_y {
self.max_y = proj_y;
}
if proj_y < self.min_y {
self.min_y = proj_y;
}
}
}
// A line could not be decoded as an G-Code command
// #[derive(Debug, Clone)]
// struct GCodeError;
// impl std::fmt::Display for GCodeError {
// fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
// write!(f, "invalid g-code statement")
// }
// }
impl Display for Svg {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let width = self.max_x - self.min_x;
let height = self.max_y - self.min_y;
// An empty gcode file will not change min_x/y or max_x/y from
// its default of +/-INF respectively.
//
let parameters = if width.is_finite() && height.is_finite() {
format!(
"width=\"{width}\" height=\"{height}\" viewBox=\"{} {} {} {}\"",
self.min_x, self.min_y, width, height
)
} else {
// In this case silently fail by returning a empty SVG element, without a viewBox parameter.
String::new()
};
writeln!(
f,
"<svg xmlns=\"http://www.w3.org/2000/svg\" {parameters} >"
)?;
write!(f, " <path d=\"")?;
for part in &self.parts {
write!(f, "{part}")?;
}
write!(
f,
r#""
style="fill:none;stroke:green;stroke-width:0.05" />
</svg>"#
)?;
Ok(())
}
}
/// Returns a SVG given a collection of G-Code commands.
///
/// TODO: Want to iterate over something more flexible
/// ie. Drop String for something more generic `AsRef<&str>`?
impl FromIterator<String> for Svg {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = String>,
{
let mut svg = Self::default();
// Invalid if the <path>'s d string does not start with a move.
svg.parts.push("M0 0".to_string());
let part_id = Some(0);
let mut is_extruding = false;
// Positioning mode for all axes (A, B, C), (U, V, W), (X, Y, Z).
let mut position_mode = PositionMode::default();
// X and Y position of tool head (before projection).
let mut current_x = 0_f64;
let mut current_y = 0_f64;
let mut current_z = 0_f64;
let mut origin_x = 0_f64;
let mut origin_y = 0_f64;
let mut origin_z = 0_f64;
for line in iter {
let (_, command) = Command::parse_line(&line).expect("Command is not parsable");
match command {
// Treat G0 and G1 command identically.
//
// A G0 is a non-printing move but E is present in files seen in the wild.
// (In the assets directory see the gears and benchy2 files.)
Command::G0(mut payload) | Command::G1(mut payload) => {
// Candidate value of params X<number> Y<number>
let mut x_param = f64::NAN;
let mut y_param = f64::NAN;
let mut z_param = f64::NAN;
for param in payload.drain() {
match param {
PosVal::X(val) => x_param = val,
PosVal::Y(val) => y_param = val,
PosVal::Z(val) => z_param = val,
// Negative values the extruder is "wiping"
// or sucking filament back into the extruder.
PosVal::E(val) => is_extruding = val > 0_f64,
_ => {}
}
}
if !x_param.is_nan() {
current_x = match position_mode {
PositionMode::Absolute => x_param,
PositionMode::Relative => current_x + x_param,
}
}
if !y_param.is_nan() {
current_y = match position_mode {
PositionMode::Absolute => y_param,
PositionMode::Relative => current_y + y_param,
}
}
if !z_param.is_nan() {
current_z = match position_mode {
PositionMode::Absolute => z_param,
PositionMode::Relative => current_z + z_param,
};
}
let proj_x =
f64::midpoint(origin_y, current_y) + f64::midpoint(origin_x, current_x);
let proj_y = -(origin_z + current_z) - f64::midpoint(origin_y, current_y)
+ f64::midpoint(origin_x, current_x);
svg.update_view_box(proj_x, proj_y);
if is_extruding && part_id.is_some() {
svg.parts.push(format!("L{proj_x:.3} {proj_y:.3}"));
} else {
svg.parts.push(format!("M{proj_x:.3} {proj_y:.3}"));
}
}
Command::G2(arc_form) => {
// Clockwise arc
let ArcParams {
center,
radius,
mut theta_start,
theta_end,
} = compute_arc(current_x, current_y, &arc_form);
// Regarding the Ambiguity/Equivalence of the angles 0 and 2PI
// All values here are in the range 0<=theta<2PI
// We are rotating clockwise
// in this cased the start angle of 0 should be read as 2PI
if theta_start == 0_f64 {
theta_start = TAU;
}
let delta_theta = if theta_start < theta_end {
// Adjust for zero crossing
// say 115 -> 304 degrees
// delta_theta = 115 + (360 - 304 ) = 170
theta_start + (TAU - theta_end)
} else {
theta_start - theta_end
};
let total_arc_length = delta_theta * radius;
// n_steps must be a number > 0
let n_steps = (total_arc_length / MM_PER_ARC_SEGMENT).ceil();
let theta_step = delta_theta / n_steps;
// x,y are the position of the head in absolute units.
let mut x = f64::NAN;
let mut y = f64::NAN;
// For loop: f64 has a problem with numerical accuracy
// specifically the comparing limit.
// rust idiomatically insists on indexed here
for i in 0..=n_steps as u64 {
let theta = (i as f64).mul_add(-theta_step, theta_start) % TAU;
x = radius.mul_add(theta.cos(), center.0);
y = radius.mul_add(theta.sin(), center.1);
let proj_x = (origin_x + x + origin_y + y) / 2_f64;
let proj_y = -(origin_z + current_z) - f64::midpoint(origin_y, y)
+ f64::midpoint(origin_x, x);
svg.update_view_box(proj_x, proj_y);
match position_mode {
PositionMode::Absolute => {
svg.parts.push(format!("L{proj_x:.3} {proj_y:.3}"));
}
PositionMode::Relative => {
svg.parts.push(format!("l{proj_x:.3} {proj_y:.3}"));
}
}
}
current_x = x;
current_y = y;
}
Command::G3(arc_form) => {
// Anti-Clockwise arc
let ArcParams {
center,
radius,
theta_start,
mut theta_end,
} = compute_arc(current_x, current_y, &arc_form);
// Regarding the Ambiguity/Equivalence of the angles 0 and 2PI
// All values here are in the range 0<=theta<2PI
// We are rotating anticlockwise
// in this cased the final angle of 0 should be read as 2PI
if theta_end == 0_f64 {
theta_end = TAU;
}
let delta_theta = if theta_start > theta_end {
// Adjust for zero crossing
// say 306 -> 115 degrees
// delta_theta = (360 - 305 ) + 115 = 170
TAU - theta_start + theta_end
} else {
theta_end - theta_start
};
let total_arc_length = delta_theta * radius;
// n_steps must be a number > 0
let n_steps = (total_arc_length / MM_PER_ARC_SEGMENT).ceil();
let theta_step = delta_theta / n_steps;
// For loop with f64 have a problem with numerical accuracy
// specifically the comparing limit.
// rust idiomatically insists on indexed here
let mut x = f64::NAN;
let mut y = f64::NAN;
for i in 0..=n_steps as u64 {
let theta = (i as f64).mul_add(theta_step, theta_start) % TAU;
x = radius.mul_add(theta.cos(), center.0);
y = radius.mul_add(theta.sin(), center.1);
let proj_x = (origin_x + x + origin_y + y) / 2.;
let proj_y = -(origin_z + current_z) - f64::midpoint(origin_y, y)
+ f64::midpoint(origin_x, x);
svg.update_view_box(proj_x, proj_y);
match position_mode {
PositionMode::Absolute => {
svg.parts.push(format!("L{proj_x:.3} {proj_y:.3}"));
}
PositionMode::Relative => {
svg.parts.push(format!("l{proj_x:.3} {proj_y:.3}"));
}
}
}
current_x = x;
current_y = y;
}
Command::G90 => position_mode = PositionMode::Absolute,
Command::G91 => position_mode = PositionMode::Relative,
// If the current position is at X=4 and G92 X7 is programmed,
// the current position is redefined as X=7, effectively
// moving the origin of the coordinate system -3 units in X.""
Command::G92(mut params) => {
// The extrude rate is going to zero
// enter MoveMode ..ie not laying down filament.
for param in params.drain() {
match param {
PosVal::E(val) => {
// Negative values the extruder is "wiping"
// or sucking filament back into the extruder.
is_extruding = val > 0_f64;
}
PosVal::X(val) => match position_mode {
PositionMode::Absolute => {
origin_x = current_x - val;
current_x = val;
}
PositionMode::Relative => {
unimplemented!("Relative position mode origin adjust ");
}
},
PosVal::Y(val) => match position_mode {
PositionMode::Absolute => {
origin_y = current_x - val;
current_y = val;
}
PositionMode::Relative => {
unimplemented!("Relative position mode origin adjust ");
}
},
PosVal::Z(val) => match position_mode {
PositionMode::Absolute => {
origin_z = current_z - val;
current_z = val;
}
PositionMode::Relative => {
unimplemented!("Relative position mode origin adjust ");
}
},
_ => { /* Silently drop. */ }
}
}
// Set Position is by definition a move only.
let proj_x = (origin_x + current_x + origin_y + current_y) / 2.;
let proj_y = -(origin_z + current_z) - f64::midpoint(origin_y, current_y)
+ f64::midpoint(origin_x, current_x);
svg.update_view_box(proj_x, proj_y);
svg.parts.push(format!("M{proj_x} {proj_y}"));
}
_ => {}
}
}
svg
}
}
#[cfg(test)]
mod svg {
use super::*;
use crate::command::Command;
use insta::assert_debug_snapshot;
// The first few lines of assets/3dBench.gcode
static INPUT: &str = r"
; generated by Slic3r 1.2.9 on 2015-10-01 at 20:51:53
; external perimeters extrusion width = 0.40mm
; perimeters extrusion width = 0.67mm
; infill extrusion width = 0.67mm
; solid infill extrusion width = 0.67mm
; top infill extrusion width = 0.67mm
M107
M190 S65 ; set bed temperature
M104 S205 ; set temperature
G28 ; home all axes
G1 Z5 F5000 ; lift nozzle
M109 S205 ; wait for temperature to be reached
G21 ; set units to millimeters
G90 ; use absolute coordinates
M82 ; use absolute distances for extrusion
G92 E0
G1 E-1.00000 F1800.00000
G92 E0
G1 Z0.350 F7800.000
";
#[test]
fn nothing_unhandled() {
// The first few lines of the benchy file must be recognized.
for line in INPUT.lines() {
assert!(Command::parse_line(line).is_ok());
}
}
#[test]
fn arc_clockwise() {
// SNAPSHOT tests
//
// Simple pattern essential for code coverage
//
// Ensures calculated theta values are in the range 0..360
// as measured in anticlockwise from the positive x-axis.
//
// 0 and 360 are the same point
// This test asserts that the cases where 360 must be used are correct.
let buffer = include_str!("../../../../assets/g3_box_rounded_anticlockwise.gcode");
let svg = buffer
.lines()
.map(std::string::ToString::to_string)
.collect::<Svg>();
assert_debug_snapshot!(svg);
}
#[test]
fn arc_anti_clockwise() {
// SNAPSHOT tests
//
// Simple pattern essential for code coverage
//
// Ensures calculated theta values are in the range 0..360
// as measured in anticlockwise from the positive x-axis.
//
// 0 and 360 are the same point
// This test asserts that the cases where 360 must be used are correct.
let buffer = include_str!("../../../../assets/g2_box_nibble_clockwise.gcode");
let svg = buffer
.lines()
.map(std::string::ToString::to_string)
.collect::<Svg>();
assert_debug_snapshot!(svg);
}
#[test]
fn arc_demo() {
// SNAPSHOT tests
let buffer = include_str!("../../../../assets/arc_demo.gcode");
let svg = buffer
.lines()
.map(std::string::ToString::to_string)
.collect::<Svg>();
assert_debug_snapshot!(svg);
}
#[test]
fn zero_crossing() {
// SNAPSHOT tests
//
// Complex model with lots of curves.
//
// NB This is the only test that covers both clockwise and anticlockwise
// zero crossings.
let buffer = include_str!("../../../../assets/both.gcode");
let svg = buffer
.lines()
.map(std::string::ToString::to_string)
.collect::<Svg>();
assert_debug_snapshot!(svg);
}
}