-
Notifications
You must be signed in to change notification settings - Fork 25
/
post_process.rs
416 lines (372 loc) · 12.6 KB
/
post_process.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
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
use std::collections::VecDeque;
use std::ffi::OsString;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};
use clap::Parser;
use regex::Regex;
use lib_klipper::gcode::{
parse_gcode, GCodeCommand, GCodeOperation, GCodeReader, GCodeTraditionalParams,
};
use lib_klipper::planner::{Planner, PlanningOperation};
use lib_klipper::slicer::SlicerPreset;
use crate::Opts;
#[derive(Parser, Debug)]
pub struct PostProcessCmd {
#[clap(parse(try_from_str))]
filename: PathBuf,
}
trait GCodeInterceptor: std::fmt::Debug {
fn post_command(&mut self, command: &GCodeCommand, result: &mut PostProcessEstimationResult) {
let _ = command;
let _ = result;
}
fn output_process(
&mut self,
command: &GCodeCommand,
result: &PostProcessEstimationResult,
) -> Option<GCodeCommand> {
let _ = command;
let _ = result;
None
}
}
#[derive(Debug, Default)]
struct NoopGCodeInterceptor {}
impl GCodeInterceptor for NoopGCodeInterceptor {}
#[derive(Debug, Default)]
struct M73GcodeInterceptor {
time_buffer: VecDeque<f64>,
}
impl GCodeInterceptor for M73GcodeInterceptor {
fn post_command(&mut self, command: &GCodeCommand, result: &mut PostProcessEstimationResult) {
if matches!(
command.op,
GCodeOperation::Traditional {
letter: 'M',
code: 73,
..
}
) {
self.time_buffer.push_back(result.total_time);
}
}
fn output_process(
&mut self,
command: &GCodeCommand,
result: &PostProcessEstimationResult,
) -> Option<GCodeCommand> {
if !matches!(
command.op,
GCodeOperation::Traditional {
letter: 'M',
code: 73,
..
}
) {
return None;
}
let next = self.time_buffer.pop_front()?;
let params = vec![
(
'P',
format!("{}", (next / result.total_time * 100.0).round()),
),
(
'R',
format!("{}", ((result.total_time - next) / 60.0).round()),
),
];
Some(GCodeCommand {
op: GCodeOperation::Traditional {
letter: 'M',
code: 73,
params: GCodeTraditionalParams::from_vec(params),
},
comment: None,
})
}
}
#[derive(Debug, Default)]
struct PSSSGCodeInterceptor {
m73_interceptor: M73GcodeInterceptor,
}
impl PSSSGCodeInterceptor {
fn format_dhms(mut time: f64) -> String {
use std::fmt::Write;
let mut out = String::new();
time = time.ceil();
let d = (time / 86400.0).floor();
if d > 0.0 {
write!(out, " {:.0}d", d).unwrap();
}
time %= 86400.0;
let h = (time / 3600.0).floor();
if h > 0.0 {
write!(out, " {:.0}h", h).unwrap();
}
time %= 3600.0;
let m = (time / 60.0).floor();
if m > 0.0 {
write!(out, " {:.0}m", m).unwrap();
}
time %= 60.0;
let s = time;
write!(out, " {:.0}s", s).unwrap();
out
}
}
impl GCodeInterceptor for PSSSGCodeInterceptor {
fn post_command(&mut self, command: &GCodeCommand, result: &mut PostProcessEstimationResult) {
self.m73_interceptor.post_command(command, result);
}
fn output_process(
&mut self,
command: &GCodeCommand,
result: &PostProcessEstimationResult,
) -> Option<GCodeCommand> {
lazy_static! {
static ref RE_EST_TIME: Regex =
Regex::new(r"^ estimated printing time \(.*?\) =").unwrap();
}
if let Some(cmd) = self.m73_interceptor.output_process(command, result) {
return Some(cmd);
}
if let Some(com) = &command.comment {
if let Some(c) = RE_EST_TIME.captures(com) {
return Some(GCodeCommand {
op: GCodeOperation::Nop,
comment: Some(format!(
"{}{}",
c.get(0).unwrap().as_str(),
Self::format_dhms(result.total_time)
)),
});
}
}
None
}
}
#[derive(Debug, Default)]
struct IdeaMakerGCodeInterceptor {
time_buffer: VecDeque<f64>,
}
impl GCodeInterceptor for IdeaMakerGCodeInterceptor {
fn post_command(&mut self, command: &GCodeCommand, result: &mut PostProcessEstimationResult) {
if let Some(com) = &command.comment {
if com.starts_with("PRINTING_TIME: ") {
self.time_buffer.push_back(result.total_time);
}
}
}
fn output_process(
&mut self,
command: &GCodeCommand,
result: &PostProcessEstimationResult,
) -> Option<GCodeCommand> {
if let Some(com) = &command.comment {
if com.starts_with("Print Time: ") {
return Some(GCodeCommand {
op: GCodeOperation::Nop,
comment: Some(format!("Print Time: {:.0}", result.total_time.ceil())),
});
} else if com.starts_with("PRINTING_TIME: ") {
if let Some(next) = self.time_buffer.front() {
return Some(GCodeCommand {
op: GCodeOperation::Nop,
comment: Some(format!("PRINTING_TIME: {:.0}", next.ceil())),
});
}
} else if com.starts_with("REMAINING_TIME: ") {
if let Some(next) = self.time_buffer.pop_front() {
return Some(GCodeCommand {
op: GCodeOperation::Nop,
comment: Some(format!(
"REMAINING_TIME: {:.0}",
(result.total_time - next).ceil()
)),
});
}
}
}
None
}
}
#[derive(Debug, Default)]
struct CuraGCodeInterceptor {
time_buffer: VecDeque<f64>,
}
impl GCodeInterceptor for CuraGCodeInterceptor {
fn post_command(&mut self, command: &GCodeCommand, result: &mut PostProcessEstimationResult) {
if let Some(com) = &command.comment {
if com.starts_with("TIME_ELAPSED:") {
self.time_buffer.push_back(result.total_time);
}
}
}
fn output_process(
&mut self,
command: &GCodeCommand,
result: &PostProcessEstimationResult,
) -> Option<GCodeCommand> {
if let Some(com) = &command.comment {
if com.starts_with("TIME:") {
return Some(GCodeCommand {
op: GCodeOperation::Nop,
comment: Some(format!("TIME:{:.0}", result.total_time.ceil())),
});
} else if com.starts_with("PRINT.TIME:") {
return Some(GCodeCommand {
op: GCodeOperation::Nop,
comment: Some(format!("PRINT.TIME:{:.0}", result.total_time.ceil())),
});
} else if com.starts_with("TIME_ELAPSED:") {
if let Some(next) = self.time_buffer.pop_front() {
return Some(GCodeCommand {
op: GCodeOperation::Nop,
comment: Some(format!("TIME_ELAPSED:{:.0}", (next).ceil())),
});
}
}
}
None
}
}
fn metadata_processor(preset: &SlicerPreset) -> Box<dyn GCodeInterceptor> {
match preset {
SlicerPreset::PrusaSlicer { .. } => Box::new(PSSSGCodeInterceptor::default()),
SlicerPreset::SuperSlicer { .. } => Box::new(PSSSGCodeInterceptor::default()),
SlicerPreset::IdeaMaker { .. } => Box::new(IdeaMakerGCodeInterceptor::default()),
SlicerPreset::Cura { .. } => Box::new(CuraGCodeInterceptor::default()),
}
}
#[derive(Debug)]
struct PostProcessEstimationResult {
total_time: f64,
slicer: Option<SlicerPreset>,
}
impl std::default::Default for PostProcessEstimationResult {
fn default() -> Self {
PostProcessEstimationResult {
total_time: 0.0,
slicer: None,
}
}
}
#[derive(Debug)]
struct PostProcessState {
result: PostProcessEstimationResult,
gcode_interceptor: Box<dyn GCodeInterceptor>,
}
impl std::default::Default for PostProcessState {
fn default() -> Self {
PostProcessState {
result: PostProcessEstimationResult::default(),
gcode_interceptor: Box::new(NoopGCodeInterceptor::default()),
}
}
}
#[derive(Debug)]
struct EstimateRunner {
state: PostProcessState,
planner: Planner,
// We use this buffer to synchronize planned moves with input moves
buffer: VecDeque<(usize, GCodeCommand)>,
}
impl EstimateRunner {
fn run<T: BufRead>(&mut self, rdr: &mut GCodeReader<T>) {
for (n, cmd) in rdr.enumerate() {
let cmd = cmd.expect("gcode read");
// If we don't have a slicer figured out yet, and this is a comment, try
if cmd.op.is_nop() && cmd.comment.is_some() && self.state.result.slicer.is_none() {
self.state.result.slicer = SlicerPreset::determine(cmd.comment.as_ref().unwrap());
if let Some(preset) = self.state.result.slicer.as_ref() {
self.state.gcode_interceptor = metadata_processor(preset);
}
}
let x = self.planner.process_cmd(&cmd);
self.buffer.push_back((x, cmd));
if n % 1000 == 0 {
self.flush();
}
}
self.planner.finalize();
self.flush();
}
fn flush(&mut self) {
for c in self.planner.iter() {
let (n, cmd) = self.buffer.front_mut().unwrap();
match c {
PlanningOperation::Delay(d) => {
self.state.result.total_time += d.duration().as_secs_f64()
}
PlanningOperation::Move(m) => self.state.result.total_time += m.total_time(),
PlanningOperation::Fill => {}
}
self.state
.gcode_interceptor
.post_command(cmd, &mut self.state.result);
if *n <= 1 {
let _ = self.buffer.pop_front();
} else {
*n -= 1;
}
}
}
}
impl PostProcessCmd {
fn estimate(&self, opts: &Opts) -> PostProcessState {
let src = File::open(&self.filename).expect("opening gcode file failed");
let mut rdr = GCodeReader::new(BufReader::new(src));
let mut runner = EstimateRunner {
state: PostProcessState::default(),
planner: opts.make_planner(),
buffer: VecDeque::new(),
};
runner.run(&mut rdr);
runner.state
}
fn apply_changes(&self, mut state: PostProcessState) {
let src = File::open(&self.filename).expect("opening gcode file failed");
let rdr = BufReader::new(src);
let mut dst_name = Into::<OsString>::into(".estimate.");
dst_name.push(self.filename.file_name().expect("invalid file name"));
let dst_path = self
.filename
.parent()
.unwrap_or_else(|| Path::new("/"))
.join(dst_name);
let dst = File::create(&dst_path).expect("creating target gcode file failed");
let mut wr = BufWriter::new(dst);
for line in rdr.lines() {
let line = line.expect("IO error");
if let Ok(cmd) = parse_gcode(&line) {
if let Some(cmd) = state.gcode_interceptor.output_process(&cmd, &state.result) {
writeln!(wr, "{}", cmd).expect("IO error");
} else {
writeln!(wr, "{}", line).expect("IO error");
}
} else {
writeln!(wr, "{}", line).expect("IO error");
}
}
write!(
wr,
"; Processed by klipper_estimator {}, {}",
env!("TOOL_VERSION"),
if let Some(slicer) = state.result.slicer {
format!("detected slicer {}", slicer)
} else {
"no slicer detected".into()
}
)
.expect("IO error");
// Flush output file before renaming
wr.flush().expect("IO error");
std::fs::rename(&dst_path, &self.filename).expect("rename failed");
}
pub fn run(&self, opts: &Opts) {
let state = self.estimate(opts);
self.apply_changes(state);
}
}