g_win/
lib.rs

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
// include readme in docs
#![doc = include_str!("../README.md")]

pub mod emit;
mod file;
mod parsers;
mod tests;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use microns::Microns;
use std::{io::Write, path::Path};
/// Default basic annotations for G1 moves, generated automatically
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
pub enum Tag {
    Retraction,
    DeRetraction,
    Travel,
    RaiseZ,
    LowerZ,
    Wipe,
    Extrusion,
    Feedrate,
    #[default]
    Uninitialized,
}

/// Struct to store G1 params as optional strings
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct G1 {
    pub x: Option<Microns>,
    pub y: Option<Microns>,
    pub z: Option<Microns>,
    pub e: Option<Microns>,
    pub f: Option<Microns>,
    pub tag: Tag,
}

/// Enum to represent all possible gcode commands that we would
/// like to handle, leaving any unknown commands as raw strings.
/// Specific structs to store information for each command can
/// be added as needed.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Command {
    G1(G1),
    G90,
    G91,
    M82,
    M83,
    Raw(String),
}

impl Command {
    pub fn tag(&self) -> Tag {
        match self {
            Command::G1(g1) => g1.tag,
            _ => Tag::Uninitialized,
        }
    }
}

/// Struct to store a single line of gcode, with an id, command,
/// and comments
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct GCodeLine {
    pub id: Id,
    pub command: Command,
    pub comments: String,
}

/// Struct to store all information for a .gcode file,
/// specifically calling out relative vs absolute positioning
/// and extrusion and with a counter to generate line ids
///
//~ NOTE: this struct is generated through the FromStr trait
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct GCodeModel {
    pub lines: Vec<GCodeLine>, // keep track of line order
    pub rel_xyz: bool,
    pub rel_e: bool,
    pub id_counter: Counter,
}

impl std::str::FromStr for GCodeModel {
    type Err = parsers::GCodeParseError;
    fn from_str(mut s: &str) -> Result<Self, Self::Err> {
        let gcode = parsers::gcode_parser(&mut s);
        match gcode {
            Ok(gcode) => Ok(gcode),
            Err(e) => Err(e),
        }
    }
}

impl GCodeModel {
    pub fn from_file(path: &Path) -> Result<Self, Box<dyn std::error::Error>> {
        Ok(file::open_gcode_file(path)?.parse()?)
    }
    pub fn write_to_file(&self, path: &Path) -> Result<(), std::io::Error> {
        use emit::Emit;
        use std::fs::File;
        let out = self.emit(false);
        let mut f = File::create(path)?;
        f.write_all(out.as_bytes())?;
        println!("save successful");
        Ok(())
    }
    pub fn tag_g1(&mut self) {
        let mut prev = [
            Microns::ZERO,
            Microns::ZERO,
            Microns::ZERO,
        ];
        for line in self.lines.iter_mut() {
            if let Command::G1(G1 { x, y, z, e, f, tag }) = &mut line.command {
                let curr = [
                    prev[0] + x.unwrap_or(Microns::ZERO),
                    prev[1] + y.unwrap_or(Microns::ZERO),
                    prev[2] + z.unwrap_or(Microns::ZERO),
                ];

                let dx = curr[0] - prev[0];
                let dy = curr[1] - prev[1];
                let dz = curr[2] - prev[2];
                let de = e.unwrap_or(Microns::ZERO);
                let f = f.unwrap_or(Microns::ZERO);

                *tag = {
                    if de > Microns::ZERO {
                        if dx.abs() > Microns::ZERO || dy.abs() > Microns::ZERO {
                            Tag::Extrusion
                        } else { Tag::DeRetraction }
                    } else if de == Microns::ZERO {
                        if dx.abs() > Microns::ZERO || dy.abs() > Microns::ZERO {
                            Tag::Travel
                        } else if dz > Microns::ZERO {
                            Tag::RaiseZ
                        } else if dz < Microns::ZERO {
                            Tag::LowerZ
                        } else if f > Microns::ZERO {
                            Tag::Feedrate
                        } else { Tag::Uninitialized }
                    } else {
                        if dx.abs() > Microns::ZERO || dy.abs() > Microns::ZERO {
                            Tag::Wipe
                        } else { Tag::Retraction }
                    }
                };
                prev = curr;
            }
        }
    }
}

#[test]
fn tag_test() {
    let mut gcode = GCodeModel::default();
    gcode.lines.push(GCodeLine {
        id: gcode.id_counter.get(),
        command: Command::G1(G1 {
            x: Some(Microns::from(10.0)),
            y: Some(Microns::from(10.0)),
            z: Some(Microns::from(10.0)),
            e: Some(Microns::from(10.0)),
            f: Some(Microns::from(10.0)),
            tag: Tag::Uninitialized,
        }),
        comments: String::new(),
    });
    gcode.tag_g1();
    assert_eq!(gcode.lines[0].command.tag(), Tag::Extrusion);
    gcode.lines.push(GCodeLine {
        id: gcode.id_counter.get(),
        command: Command::G1(G1::default()),
        comments: String::new(),
    });
    gcode.tag_g1();
    assert_eq!(gcode.lines[1].command.tag(), Tag::Uninitialized);
    gcode.lines.push(GCodeLine {
        id: gcode.id_counter.get(),
        command: Command::G1(G1 {
            e: Some(Microns::from(-10.0)),
            ..Default::default()
        }),
        comments: String::new(),
    });
    gcode.tag_g1();
    assert_eq!(gcode.lines[2].command.tag(), Tag::Retraction);
    gcode.lines.push(GCodeLine {
        id: gcode.id_counter.get(),
        command: Command::G1(G1 {
            e: Some(Microns::from(-10.0)),
            x: Some(Microns::from(10.0)),
            y: Some(Microns::from(10.0)),
            ..Default::default()
        }),
        comments: String::new(),
    });
    gcode.tag_g1();
    assert_eq!(gcode.lines[3].command.tag(), Tag::Wipe);
    gcode.lines.push(GCodeLine {
        id: gcode.id_counter.get(),
        command: Command::G1(G1 {
            e: Some(Microns::from(-10.0)),
            z: Some(Microns::from(10.0)),
            ..Default::default()
        }),
        comments: String::new(),
    });
    gcode.tag_g1();
    assert_eq!(gcode.lines[4].command.tag(), Tag::Retraction);
    gcode.lines.push(GCodeLine {
        id: gcode.id_counter.get(),
        command: Command::G1(G1 {
            e: Some(Microns::from(-10.0)),
            z: Some(Microns::from(-10.0)),
            ..Default::default()
        }),
        comments: String::new(),
    });
    gcode.tag_g1();
    assert_eq!(gcode.lines[5].command.tag(), Tag::Retraction);
    gcode.lines.push(GCodeLine {
        id: gcode.id_counter.get(),
        command: Command::G1(G1 {
            f: Some(Microns::from(10.0)),
            ..Default::default()
        }),
        comments: String::new(),
    });
    gcode.tag_g1();
    assert_eq!(gcode.lines[6].command.tag(), Tag::Feedrate);
    gcode.lines.push(GCodeLine {
        id: gcode.id_counter.get(),
        command: Command::G1(G1 {
            e: Some(Microns::from(10.0)),
            ..Default::default()
        }),
        comments: String::new(),
    });
    gcode.tag_g1();
    assert_eq!(gcode.lines[7].command.tag(), Tag::DeRetraction);
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Counter {
    count: u32,
}

impl Counter {
    fn get(&mut self) -> Id {
        let out = self.count;
        self.count += 1;
        Id(out)
    }
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
pub struct Id(u32);

impl Id {
    pub fn get(&self) -> u32 {
        self.0
    }
}