1 #include "libslic3r/libslic3r.h"
2 #include "libslic3r/Utils.hpp"
3 #include "libslic3r/Print.hpp"
4 #include "GCodeProcessor.hpp"
5 
6 #include <boost/log/trivial.hpp>
7 #include <boost/nowide/fstream.hpp>
8 #include <boost/nowide/cstdio.hpp>
9 
10 #include <float.h>
11 #include <assert.h>
12 
13 #if __has_include(<charconv>)
14     #include <charconv>
15     #include <utility>
16 #endif
17 
18 #include <chrono>
19 
20 static const float INCHES_TO_MM = 25.4f;
21 static const float MMMIN_TO_MMSEC = 1.0f / 60.0f;
22 
23 static const float DEFAULT_ACCELERATION = 1500.0f; // Prusa Firmware 1_75mm_MK2
24 
25 namespace Slic3r {
26 
27 const std::string GCodeProcessor::Extrusion_Role_Tag = "TYPE:";
28 const std::string GCodeProcessor::Wipe_Start_Tag     = "WIPE_START";
29 const std::string GCodeProcessor::Wipe_End_Tag       = "WIPE_END";
30 const std::string GCodeProcessor::Height_Tag         = "HEIGHT:";
31 const std::string GCodeProcessor::Layer_Change_Tag   = "LAYER_CHANGE";
32 const std::string GCodeProcessor::Color_Change_Tag   = "COLOR_CHANGE";
33 const std::string GCodeProcessor::Pause_Print_Tag    = "PAUSE_PRINT";
34 const std::string GCodeProcessor::Custom_Code_Tag    = "CUSTOM_GCODE";
35 
36 const std::string GCodeProcessor::First_Line_M73_Placeholder_Tag          = "; _GP_FIRST_LINE_M73_PLACEHOLDER";
37 const std::string GCodeProcessor::Last_Line_M73_Placeholder_Tag           = "; _GP_LAST_LINE_M73_PLACEHOLDER";
38 const std::string GCodeProcessor::Estimated_Printing_Time_Placeholder_Tag = "; _GP_ESTIMATED_PRINTING_TIME_PLACEHOLDER";
39 
40 const float GCodeProcessor::Wipe_Width = 0.05f;
41 const float GCodeProcessor::Wipe_Height = 0.05f;
42 
43 #if ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
44 const std::string GCodeProcessor::Width_Tag = "WIDTH:";
45 #endif // ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
46 #if ENABLE_GCODE_VIEWER_DATA_CHECKING
47 #if !ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
48 const std::string GCodeProcessor::Width_Tag      = "WIDTH:";
49 #endif // !ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
50 const std::string GCodeProcessor::Mm3_Per_Mm_Tag = "MM3_PER_MM:";
51 #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
52 
is_valid_extrusion_role(int value)53 static bool is_valid_extrusion_role(int value)
54 {
55     return (static_cast<int>(erNone) <= value) && (value <= static_cast<int>(erMixed));
56 }
57 
set_option_value(ConfigOptionFloats & option,size_t id,float value)58 static void set_option_value(ConfigOptionFloats& option, size_t id, float value)
59 {
60     if (id < option.values.size())
61         option.values[id] = static_cast<double>(value);
62 };
63 
get_option_value(const ConfigOptionFloats & option,size_t id)64 static float get_option_value(const ConfigOptionFloats& option, size_t id)
65 {
66     return option.values.empty() ? 0.0f :
67         ((id < option.values.size()) ? static_cast<float>(option.values[id]) : static_cast<float>(option.values.back()));
68 }
69 
estimated_acceleration_distance(float initial_rate,float target_rate,float acceleration)70 static float estimated_acceleration_distance(float initial_rate, float target_rate, float acceleration)
71 {
72     return (acceleration == 0.0f) ? 0.0f : (sqr(target_rate) - sqr(initial_rate)) / (2.0f * acceleration);
73 }
74 
intersection_distance(float initial_rate,float final_rate,float acceleration,float distance)75 static float intersection_distance(float initial_rate, float final_rate, float acceleration, float distance)
76 {
77     return (acceleration == 0.0f) ? 0.0f : (2.0f * acceleration * distance - sqr(initial_rate) + sqr(final_rate)) / (4.0f * acceleration);
78 }
79 
speed_from_distance(float initial_feedrate,float distance,float acceleration)80 static float speed_from_distance(float initial_feedrate, float distance, float acceleration)
81 {
82     // to avoid invalid negative numbers due to numerical errors
83     float value = std::max(0.0f, sqr(initial_feedrate) + 2.0f * acceleration * distance);
84     return ::sqrt(value);
85 }
86 
87 // Calculates the maximum allowable speed at this point when you must be able to reach target_velocity using the
88 // acceleration within the allotted distance.
max_allowable_speed(float acceleration,float target_velocity,float distance)89 static float max_allowable_speed(float acceleration, float target_velocity, float distance)
90 {
91     // to avoid invalid negative numbers due to numerical errors
92     float value = std::max(0.0f, sqr(target_velocity) - 2.0f * acceleration * distance);
93     return std::sqrt(value);
94 }
95 
acceleration_time_from_distance(float initial_feedrate,float distance,float acceleration)96 static float acceleration_time_from_distance(float initial_feedrate, float distance, float acceleration)
97 {
98     return (acceleration != 0.0f) ? (speed_from_distance(initial_feedrate, distance, acceleration) - initial_feedrate) / acceleration : 0.0f;
99 }
100 
reset()101 void GCodeProcessor::CachedPosition::reset()
102 {
103     std::fill(position.begin(), position.end(), FLT_MAX);
104     feedrate = FLT_MAX;
105 }
106 
reset()107 void GCodeProcessor::CpColor::reset()
108 {
109     counter = 0;
110     current = 0;
111 }
112 
acceleration_time(float entry_feedrate,float acceleration) const113 float GCodeProcessor::Trapezoid::acceleration_time(float entry_feedrate, float acceleration) const
114 {
115     return acceleration_time_from_distance(entry_feedrate, accelerate_until, acceleration);
116 }
117 
cruise_time() const118 float GCodeProcessor::Trapezoid::cruise_time() const
119 {
120     return (cruise_feedrate != 0.0f) ? cruise_distance() / cruise_feedrate : 0.0f;
121 }
122 
deceleration_time(float distance,float acceleration) const123 float GCodeProcessor::Trapezoid::deceleration_time(float distance, float acceleration) const
124 {
125     return acceleration_time_from_distance(cruise_feedrate, (distance - decelerate_after), -acceleration);
126 }
127 
cruise_distance() const128 float GCodeProcessor::Trapezoid::cruise_distance() const
129 {
130     return decelerate_after - accelerate_until;
131 }
132 
calculate_trapezoid()133 void GCodeProcessor::TimeBlock::calculate_trapezoid()
134 {
135     trapezoid.cruise_feedrate = feedrate_profile.cruise;
136 
137     float accelerate_distance = std::max(0.0f, estimated_acceleration_distance(feedrate_profile.entry, feedrate_profile.cruise, acceleration));
138     float decelerate_distance = std::max(0.0f, estimated_acceleration_distance(feedrate_profile.cruise, feedrate_profile.exit, -acceleration));
139     float cruise_distance = distance - accelerate_distance - decelerate_distance;
140 
141     // Not enough space to reach the nominal feedrate.
142     // This means no cruising, and we'll have to use intersection_distance() to calculate when to abort acceleration
143     // and start braking in order to reach the exit_feedrate exactly at the end of this block.
144     if (cruise_distance < 0.0f) {
145         accelerate_distance = std::clamp(intersection_distance(feedrate_profile.entry, feedrate_profile.exit, acceleration, distance), 0.0f, distance);
146         cruise_distance = 0.0f;
147         trapezoid.cruise_feedrate = speed_from_distance(feedrate_profile.entry, accelerate_distance, acceleration);
148     }
149 
150     trapezoid.accelerate_until = accelerate_distance;
151     trapezoid.decelerate_after = accelerate_distance + cruise_distance;
152 }
153 
time() const154 float GCodeProcessor::TimeBlock::time() const
155 {
156     return trapezoid.acceleration_time(feedrate_profile.entry, acceleration)
157         + trapezoid.cruise_time()
158         + trapezoid.deceleration_time(distance, acceleration);
159 }
160 
reset()161 void GCodeProcessor::TimeMachine::State::reset()
162 {
163     feedrate = 0.0f;
164     safe_feedrate = 0.0f;
165     axis_feedrate = { 0.0f, 0.0f, 0.0f, 0.0f };
166     abs_axis_feedrate = { 0.0f, 0.0f, 0.0f, 0.0f };
167 }
168 
reset()169 void GCodeProcessor::TimeMachine::CustomGCodeTime::reset()
170 {
171     needed = false;
172     cache = 0.0f;
173     times = std::vector<std::pair<CustomGCode::Type, float>>();
174 }
175 
reset()176 void GCodeProcessor::TimeMachine::reset()
177 {
178     enabled = false;
179     acceleration = 0.0f;
180     max_acceleration = 0.0f;
181     extrude_factor_override_percentage = 1.0f;
182     time = 0.0f;
183     curr.reset();
184     prev.reset();
185     gcode_time.reset();
186     blocks = std::vector<TimeBlock>();
187     g1_times_cache = std::vector<G1LinesCacheItem>();
188     std::fill(moves_time.begin(), moves_time.end(), 0.0f);
189     std::fill(roles_time.begin(), roles_time.end(), 0.0f);
190     layers_time = std::vector<float>();
191 }
192 
simulate_st_synchronize(float additional_time)193 void GCodeProcessor::TimeMachine::simulate_st_synchronize(float additional_time)
194 {
195     if (!enabled)
196         return;
197 
198     time += additional_time;
199     gcode_time.cache += additional_time;
200     calculate_time();
201 }
202 
planner_forward_pass_kernel(GCodeProcessor::TimeBlock & prev,GCodeProcessor::TimeBlock & curr)203 static void planner_forward_pass_kernel(GCodeProcessor::TimeBlock& prev, GCodeProcessor::TimeBlock& curr)
204 {
205     // If the previous block is an acceleration block, but it is not long enough to complete the
206     // full speed change within the block, we need to adjust the entry speed accordingly. Entry
207     // speeds have already been reset, maximized, and reverse planned by reverse planner.
208     // If nominal length is true, max junction speed is guaranteed to be reached. No need to recheck.
209     if (!prev.flags.nominal_length) {
210         if (prev.feedrate_profile.entry < curr.feedrate_profile.entry) {
211             float entry_speed = std::min(curr.feedrate_profile.entry, max_allowable_speed(-prev.acceleration, prev.feedrate_profile.entry, prev.distance));
212 
213             // Check for junction speed change
214             if (curr.feedrate_profile.entry != entry_speed) {
215                 curr.feedrate_profile.entry = entry_speed;
216                 curr.flags.recalculate = true;
217             }
218         }
219     }
220 }
221 
planner_reverse_pass_kernel(GCodeProcessor::TimeBlock & curr,GCodeProcessor::TimeBlock & next)222 void planner_reverse_pass_kernel(GCodeProcessor::TimeBlock& curr, GCodeProcessor::TimeBlock& next)
223 {
224     // If entry speed is already at the maximum entry speed, no need to recheck. Block is cruising.
225     // If not, block in state of acceleration or deceleration. Reset entry speed to maximum and
226     // check for maximum allowable speed reductions to ensure maximum possible planned speed.
227     if (curr.feedrate_profile.entry != curr.max_entry_speed) {
228         // If nominal length true, max junction speed is guaranteed to be reached. Only compute
229         // for max allowable speed if block is decelerating and nominal length is false.
230         if (!curr.flags.nominal_length && curr.max_entry_speed > next.feedrate_profile.entry)
231             curr.feedrate_profile.entry = std::min(curr.max_entry_speed, max_allowable_speed(-curr.acceleration, next.feedrate_profile.entry, curr.distance));
232         else
233             curr.feedrate_profile.entry = curr.max_entry_speed;
234 
235         curr.flags.recalculate = true;
236     }
237 }
238 
recalculate_trapezoids(std::vector<GCodeProcessor::TimeBlock> & blocks)239 static void recalculate_trapezoids(std::vector<GCodeProcessor::TimeBlock>& blocks)
240 {
241     GCodeProcessor::TimeBlock* curr = nullptr;
242     GCodeProcessor::TimeBlock* next = nullptr;
243 
244     for (size_t i = 0; i < blocks.size(); ++i) {
245         GCodeProcessor::TimeBlock& b = blocks[i];
246 
247         curr = next;
248         next = &b;
249 
250         if (curr != nullptr) {
251             // Recalculate if current block entry or exit junction speed has changed.
252             if (curr->flags.recalculate || next->flags.recalculate) {
253                 // NOTE: Entry and exit factors always > 0 by all previous logic operations.
254                 GCodeProcessor::TimeBlock block = *curr;
255                 block.feedrate_profile.exit = next->feedrate_profile.entry;
256                 block.calculate_trapezoid();
257                 curr->trapezoid = block.trapezoid;
258                 curr->flags.recalculate = false; // Reset current only to ensure next trapezoid is computed
259             }
260         }
261     }
262 
263     // Last/newest block in buffer. Always recalculated.
264     if (next != nullptr) {
265         GCodeProcessor::TimeBlock block = *next;
266         block.feedrate_profile.exit = next->safe_feedrate;
267         block.calculate_trapezoid();
268         next->trapezoid = block.trapezoid;
269         next->flags.recalculate = false;
270     }
271 }
272 
calculate_time(size_t keep_last_n_blocks)273 void GCodeProcessor::TimeMachine::calculate_time(size_t keep_last_n_blocks)
274 {
275     if (!enabled || blocks.size() < 2)
276         return;
277 
278     assert(keep_last_n_blocks <= blocks.size());
279 
280     // forward_pass
281     for (size_t i = 0; i + 1 < blocks.size(); ++i) {
282         planner_forward_pass_kernel(blocks[i], blocks[i + 1]);
283     }
284 
285     // reverse_pass
286     for (int i = static_cast<int>(blocks.size()) - 1; i > 0; --i)
287         planner_reverse_pass_kernel(blocks[i - 1], blocks[i]);
288 
289     recalculate_trapezoids(blocks);
290 
291     size_t n_blocks_process = blocks.size() - keep_last_n_blocks;
292     for (size_t i = 0; i < n_blocks_process; ++i) {
293         const TimeBlock& block = blocks[i];
294         float block_time = block.time();
295         time += block_time;
296         gcode_time.cache += block_time;
297         moves_time[static_cast<size_t>(block.move_type)] += block_time;
298         roles_time[static_cast<size_t>(block.role)] += block_time;
299         if (block.layer_id > 0) {
300             if (block.layer_id >= layers_time.size()) {
301                 size_t curr_size = layers_time.size();
302                 layers_time.resize(block.layer_id);
303                 for (size_t i = curr_size; i < layers_time.size(); ++i) {
304                     layers_time[i] = 0.0f;
305                 }
306             }
307             layers_time[block.layer_id - 1] += block_time;
308         }
309         g1_times_cache.push_back({ block.g1_line_id, time });
310     }
311 
312     if (keep_last_n_blocks)
313         blocks.erase(blocks.begin(), blocks.begin() + n_blocks_process);
314     else
315         blocks.clear();
316 }
317 
reset()318 void GCodeProcessor::TimeProcessor::reset()
319 {
320     extruder_unloaded = true;
321     export_remaining_time_enabled = false;
322     machine_envelope_processing_enabled = false;
323     machine_limits = MachineEnvelopeConfig();
324     filament_load_times = std::vector<float>();
325     filament_unload_times = std::vector<float>();
326     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
327         machines[i].reset();
328     }
329     machines[static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Normal)].enabled = true;
330 }
331 
post_process(const std::string & filename)332 void GCodeProcessor::TimeProcessor::post_process(const std::string& filename)
333 {
334     boost::nowide::ifstream in(filename);
335     if (!in.good())
336         throw Slic3r::RuntimeError(std::string("Time estimator post process export failed.\nCannot open file for reading.\n"));
337 
338     // temporary file to contain modified gcode
339     std::string out_path = filename + ".postprocess";
340     FILE* out = boost::nowide::fopen(out_path.c_str(), "wb");
341     if (out == nullptr)
342         throw Slic3r::RuntimeError(std::string("Time estimator post process export failed.\nCannot open file for writing.\n"));
343 
344     auto time_in_minutes = [](float time_in_seconds) {
345         return int(::roundf(time_in_seconds / 60.0f));
346     };
347 
348     auto format_line_M73 = [](const std::string& mask, int percent, int time) {
349         char line_M73[64];
350         sprintf(line_M73, mask.c_str(),
351             std::to_string(percent).c_str(),
352             std::to_string(time).c_str());
353         return std::string(line_M73);
354     };
355 
356     GCodeReader parser;
357     std::string gcode_line;
358     size_t g1_lines_counter = 0;
359     // keeps track of last exported pair <percent, remaining time>
360     std::array<std::pair<int, int>, static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count)> last_exported;
361     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
362         last_exported[i] = { 0, time_in_minutes(machines[i].time) };
363     }
364 
365     // buffer line to export only when greater than 64K to reduce writing calls
366     std::string export_line;
367 
368     // replace placeholder lines with the proper final value
369     auto process_placeholders = [&](const std::string& gcode_line) {
370         // remove trailing '\n'
371         std::string line = gcode_line.substr(0, gcode_line.length() - 1);
372 
373         std::string ret;
374 
375         if (export_remaining_time_enabled && (line == First_Line_M73_Placeholder_Tag || line == Last_Line_M73_Placeholder_Tag)) {
376             for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
377                 const TimeMachine& machine = machines[i];
378                 if (machine.enabled) {
379                     ret += format_line_M73(machine.line_m73_mask.c_str(),
380                         (line == First_Line_M73_Placeholder_Tag) ? 0 : 100,
381                         (line == First_Line_M73_Placeholder_Tag) ? time_in_minutes(machines[i].time) : 0);
382                 }
383             }
384         }
385         else if (line == Estimated_Printing_Time_Placeholder_Tag) {
386             for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
387                 const TimeMachine& machine = machines[i];
388                 PrintEstimatedTimeStatistics::ETimeMode mode = static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i);
389                 if (mode == PrintEstimatedTimeStatistics::ETimeMode::Normal || machine.enabled) {
390                     char buf[128];
391                     sprintf(buf, "; estimated printing time (%s mode) = %s\n",
392                         (mode == PrintEstimatedTimeStatistics::ETimeMode::Normal) ? "normal" : "silent",
393                         get_time_dhms(machine.time).c_str());
394                     ret += buf;
395                 }
396             }
397         }
398 
399         return std::make_pair(!ret.empty(), ret.empty() ? gcode_line : ret);
400     };
401 
402     // check for temporary lines
403     auto is_temporary_decoration = [](const std::string_view gcode_line) {
404         // remove trailing '\n'
405         assert(! gcode_line.empty());
406         assert(gcode_line.back() == '\n');
407 
408         // return true for decorations which are used in processing the gcode but that should not be exported into the final gcode
409         // i.e.:
410         // bool ret = gcode_line.substr(0, gcode_line.length() - 1) == ";" + Layer_Change_Tag;
411         // ...
412         // return ret;
413         return false;
414     };
415 
416     // Iterators for the normal and silent cached time estimate entry recently processed, used by process_line_G1.
417     auto g1_times_cache_it = Slic3r::reserve_vector<std::vector<TimeMachine::G1LinesCacheItem>::const_iterator>(machines.size());
418     for (const auto& machine : machines)
419         g1_times_cache_it.emplace_back(machine.g1_times_cache.begin());
420     // add lines M73 to exported gcode
421     auto process_line_G1 = [&]() {
422         if (export_remaining_time_enabled) {
423             for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
424                 const TimeMachine& machine = machines[i];
425                 if (machine.enabled) {
426                     // Skip all machine.g1_times_cache below g1_lines_counter.
427                     auto& it = g1_times_cache_it[i];
428                     while (it != machine.g1_times_cache.end() && it->id < g1_lines_counter)
429                         ++it;
430                     if (it != machine.g1_times_cache.end() && it->id == g1_lines_counter) {
431                         float elapsed_time = it->elapsed_time;
432                         std::pair<int, int> to_export = { int(100.0f * elapsed_time / machine.time),
433                                                           time_in_minutes(machine.time - elapsed_time) };
434                         if (last_exported[i] != to_export) {
435                             export_line += format_line_M73(machine.line_m73_mask.c_str(),
436                                 to_export.first, to_export.second);
437                             last_exported[i] = to_export;
438                         }
439                     }
440                 }
441             }
442         }
443     };
444 
445     // helper function to write to disk
446     auto write_string = [&](const std::string& str) {
447         fwrite((const void*)export_line.c_str(), 1, export_line.length(), out);
448         if (ferror(out)) {
449             in.close();
450             fclose(out);
451             boost::nowide::remove(out_path.c_str());
452             throw Slic3r::RuntimeError(std::string("Time estimator post process export failed.\nIs the disk full?\n"));
453         }
454         export_line.clear();
455     };
456 
457     while (std::getline(in, gcode_line)) {
458         if (!in.good()) {
459             fclose(out);
460             throw Slic3r::RuntimeError(std::string("Time estimator post process export failed.\nError while reading from file.\n"));
461         }
462 
463         gcode_line += "\n";
464         // replace placeholder lines
465         auto [processed, result] = process_placeholders(gcode_line);
466         gcode_line = result;
467         if (!processed) {
468             // remove temporary lines
469             if (is_temporary_decoration(gcode_line))
470                 continue;
471 
472             // add lines M73 where needed
473             parser.parse_line(gcode_line,
474                 [&](GCodeReader& reader, const GCodeReader::GCodeLine& line) {
475                     if (line.cmd_is("G1")) {
476                         process_line_G1();
477                         ++g1_lines_counter;
478                     }
479                 });
480         }
481 
482         export_line += gcode_line;
483         if (export_line.length() > 65535)
484             write_string(export_line);
485     }
486 
487     if (!export_line.empty())
488         write_string(export_line);
489 
490     fclose(out);
491     in.close();
492 
493     if (rename_file(out_path, filename))
494         throw Slic3r::RuntimeError(std::string("Failed to rename the output G-code file from ") + out_path + " to " + filename + '\n' +
495             "Is " + out_path + " locked?" + '\n');
496 }
497 
498 const std::vector<std::pair<GCodeProcessor::EProducer, std::string>> GCodeProcessor::Producers = {
499     { EProducer::PrusaSlicer, "PrusaSlicer" },
500     { EProducer::Slic3rPE,    "Slic3r Prusa Edition" },
501     { EProducer::Slic3r,      "Slic3r" },
502     { EProducer::Cura,        "Cura_SteamEngine" },
503     { EProducer::Simplify3D,  "Simplify3D" },
504     { EProducer::CraftWare,   "CraftWare" },
505     { EProducer::ideaMaker,   "ideaMaker" },
506     { EProducer::KissSlicer,  "KISSlicer" }
507 };
508 
509 unsigned int GCodeProcessor::s_result_id = 0;
510 
GCodeProcessor()511 GCodeProcessor::GCodeProcessor()
512 {
513     reset();
514     m_time_processor.machines[static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Normal)].line_m73_mask = "M73 P%s R%s\n";
515     m_time_processor.machines[static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Stealth)].line_m73_mask = "M73 Q%s S%s\n";
516 }
517 
apply_config(const PrintConfig & config)518 void GCodeProcessor::apply_config(const PrintConfig& config)
519 {
520     m_parser.apply_config(config);
521 
522     m_flavor = config.gcode_flavor;
523 
524     size_t extruders_count = config.nozzle_diameter.values.size();
525     m_result.extruders_count = extruders_count;
526 
527     m_extruder_offsets.resize(extruders_count);
528     for (size_t i = 0; i < extruders_count; ++i) {
529         Vec2f offset = config.extruder_offset.get_at(i).cast<float>();
530         m_extruder_offsets[i] = { offset(0), offset(1), 0.0f };
531     }
532 
533     m_extruder_colors.resize(extruders_count);
534     for (size_t i = 0; i < extruders_count; ++i) {
535         m_extruder_colors[i] = static_cast<unsigned char>(i);
536     }
537 
538     m_filament_diameters.resize(config.filament_diameter.values.size());
539     for (size_t i = 0; i < config.filament_diameter.values.size(); ++i) {
540         m_filament_diameters[i] = static_cast<float>(config.filament_diameter.values[i]);
541     }
542 
543     if (m_flavor == gcfMarlin && config.machine_limits_usage.value != MachineLimitsUsage::Ignore)
544         m_time_processor.machine_limits = reinterpret_cast<const MachineEnvelopeConfig&>(config);
545 
546     // Filament load / unload times are not specific to a firmware flavor. Let anybody use it if they find it useful.
547     // As of now the fields are shown at the UI dialog in the same combo box as the ramming values, so they
548     // are considered to be active for the single extruder multi-material printers only.
549     m_time_processor.filament_load_times.resize(config.filament_load_time.values.size());
550     for (size_t i = 0; i < config.filament_load_time.values.size(); ++i) {
551         m_time_processor.filament_load_times[i] = static_cast<float>(config.filament_load_time.values[i]);
552     }
553     m_time_processor.filament_unload_times.resize(config.filament_unload_time.values.size());
554     for (size_t i = 0; i < config.filament_unload_time.values.size(); ++i) {
555         m_time_processor.filament_unload_times[i] = static_cast<float>(config.filament_unload_time.values[i]);
556     }
557 
558     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
559         float max_acceleration = get_option_value(m_time_processor.machine_limits.machine_max_acceleration_extruding, i);
560         m_time_processor.machines[i].max_acceleration = max_acceleration;
561         m_time_processor.machines[i].acceleration = (max_acceleration > 0.0f) ? max_acceleration : DEFAULT_ACCELERATION;
562     }
563 
564     m_time_processor.export_remaining_time_enabled = config.remaining_times.value;
565 
566 #if ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING
567     m_use_volumetric_e = config.use_volumetric_e;
568 #endif // ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING
569 }
570 
apply_config(const DynamicPrintConfig & config)571 void GCodeProcessor::apply_config(const DynamicPrintConfig& config)
572 {
573     m_parser.apply_config(config);
574 
575     const ConfigOptionEnum<GCodeFlavor>* gcode_flavor = config.option<ConfigOptionEnum<GCodeFlavor>>("gcode_flavor");
576     if (gcode_flavor != nullptr)
577         m_flavor = gcode_flavor->value;
578 
579     const ConfigOptionPoints* bed_shape = config.option<ConfigOptionPoints>("bed_shape");
580     if (bed_shape != nullptr)
581         m_result.bed_shape = bed_shape->values;
582 
583     const ConfigOptionString* print_settings_id = config.option<ConfigOptionString>("print_settings_id");
584     if (print_settings_id != nullptr)
585         m_result.settings_ids.print = print_settings_id->value;
586 
587     const ConfigOptionStrings* filament_settings_id = config.option<ConfigOptionStrings>("filament_settings_id");
588     if (filament_settings_id != nullptr)
589         m_result.settings_ids.filament = filament_settings_id->values;
590 
591     const ConfigOptionString* printer_settings_id = config.option<ConfigOptionString>("printer_settings_id");
592     if (printer_settings_id != nullptr)
593         m_result.settings_ids.printer = printer_settings_id->value;
594 
595     const ConfigOptionFloats* filament_diameters = config.option<ConfigOptionFloats>("filament_diameter");
596     if (filament_diameters != nullptr) {
597         for (double diam : filament_diameters->values) {
598             m_filament_diameters.push_back(static_cast<float>(diam));
599         }
600     }
601 
602     m_result.extruders_count = config.option<ConfigOptionFloats>("nozzle_diameter")->values.size();
603 
604     const ConfigOptionPoints* extruder_offset = config.option<ConfigOptionPoints>("extruder_offset");
605     if (extruder_offset != nullptr) {
606         m_extruder_offsets.resize(extruder_offset->values.size());
607         for (size_t i = 0; i < extruder_offset->values.size(); ++i) {
608             Vec2f offset = extruder_offset->values[i].cast<float>();
609             m_extruder_offsets[i] = { offset(0), offset(1), 0.0f };
610         }
611     }
612 
613     const ConfigOptionStrings* extruder_colour = config.option<ConfigOptionStrings>("extruder_colour");
614     if (extruder_colour != nullptr) {
615         // takes colors from config
616         m_result.extruder_colors = extruder_colour->values;
617         // try to replace missing values with filament colors
618         const ConfigOptionStrings* filament_colour = config.option<ConfigOptionStrings>("filament_colour");
619         if (filament_colour != nullptr && filament_colour->values.size() == m_result.extruder_colors.size()) {
620             for (size_t i = 0; i < m_result.extruder_colors.size(); ++i) {
621                 if (m_result.extruder_colors[i].empty())
622                     m_result.extruder_colors[i] = filament_colour->values[i];
623             }
624         }
625     }
626 
627     // replace missing values with default
628     std::string default_color = "#FF8000";
629     for (size_t i = 0; i < m_result.extruder_colors.size(); ++i) {
630         if (m_result.extruder_colors[i].empty())
631             m_result.extruder_colors[i] = default_color;
632     }
633 
634     m_extruder_colors.resize(m_result.extruder_colors.size());
635     for (size_t i = 0; i < m_result.extruder_colors.size(); ++i) {
636         m_extruder_colors[i] = static_cast<unsigned char>(i);
637     }
638 
639     const ConfigOptionFloats* filament_load_time = config.option<ConfigOptionFloats>("filament_load_time");
640     if (filament_load_time != nullptr) {
641         m_time_processor.filament_load_times.resize(filament_load_time->values.size());
642         for (size_t i = 0; i < filament_load_time->values.size(); ++i) {
643             m_time_processor.filament_load_times[i] = static_cast<float>(filament_load_time->values[i]);
644         }
645     }
646 
647     const ConfigOptionFloats* filament_unload_time = config.option<ConfigOptionFloats>("filament_unload_time");
648     if (filament_unload_time != nullptr) {
649         m_time_processor.filament_unload_times.resize(filament_unload_time->values.size());
650         for (size_t i = 0; i < filament_unload_time->values.size(); ++i) {
651             m_time_processor.filament_unload_times[i] = static_cast<float>(filament_unload_time->values[i]);
652         }
653     }
654 
655     if (m_flavor == gcfMarlin) {
656         const ConfigOptionFloats* machine_max_acceleration_x = config.option<ConfigOptionFloats>("machine_max_acceleration_x");
657         if (machine_max_acceleration_x != nullptr)
658             m_time_processor.machine_limits.machine_max_acceleration_x.values = machine_max_acceleration_x->values;
659 
660         const ConfigOptionFloats* machine_max_acceleration_y = config.option<ConfigOptionFloats>("machine_max_acceleration_y");
661         if (machine_max_acceleration_y != nullptr)
662             m_time_processor.machine_limits.machine_max_acceleration_y.values = machine_max_acceleration_y->values;
663 
664         const ConfigOptionFloats* machine_max_acceleration_z = config.option<ConfigOptionFloats>("machine_max_acceleration_z");
665         if (machine_max_acceleration_z != nullptr)
666             m_time_processor.machine_limits.machine_max_acceleration_z.values = machine_max_acceleration_z->values;
667 
668         const ConfigOptionFloats* machine_max_acceleration_e = config.option<ConfigOptionFloats>("machine_max_acceleration_e");
669         if (machine_max_acceleration_e != nullptr)
670             m_time_processor.machine_limits.machine_max_acceleration_e.values = machine_max_acceleration_e->values;
671 
672         const ConfigOptionFloats* machine_max_feedrate_x = config.option<ConfigOptionFloats>("machine_max_feedrate_x");
673         if (machine_max_feedrate_x != nullptr)
674             m_time_processor.machine_limits.machine_max_feedrate_x.values = machine_max_feedrate_x->values;
675 
676         const ConfigOptionFloats* machine_max_feedrate_y = config.option<ConfigOptionFloats>("machine_max_feedrate_y");
677         if (machine_max_feedrate_y != nullptr)
678             m_time_processor.machine_limits.machine_max_feedrate_y.values = machine_max_feedrate_y->values;
679 
680         const ConfigOptionFloats* machine_max_feedrate_z = config.option<ConfigOptionFloats>("machine_max_feedrate_z");
681         if (machine_max_feedrate_z != nullptr)
682             m_time_processor.machine_limits.machine_max_feedrate_z.values = machine_max_feedrate_z->values;
683 
684         const ConfigOptionFloats* machine_max_feedrate_e = config.option<ConfigOptionFloats>("machine_max_feedrate_e");
685         if (machine_max_feedrate_e != nullptr)
686             m_time_processor.machine_limits.machine_max_feedrate_e.values = machine_max_feedrate_e->values;
687 
688         const ConfigOptionFloats* machine_max_jerk_x = config.option<ConfigOptionFloats>("machine_max_jerk_x");
689         if (machine_max_jerk_x != nullptr)
690             m_time_processor.machine_limits.machine_max_jerk_x.values = machine_max_jerk_x->values;
691 
692         const ConfigOptionFloats* machine_max_jerk_y = config.option<ConfigOptionFloats>("machine_max_jerk_y");
693         if (machine_max_jerk_y != nullptr)
694             m_time_processor.machine_limits.machine_max_jerk_y.values = machine_max_jerk_y->values;
695 
696         const ConfigOptionFloats* machine_max_jerk_z = config.option<ConfigOptionFloats>("machine_max_jerkz");
697         if (machine_max_jerk_z != nullptr)
698             m_time_processor.machine_limits.machine_max_jerk_z.values = machine_max_jerk_z->values;
699 
700         const ConfigOptionFloats* machine_max_jerk_e = config.option<ConfigOptionFloats>("machine_max_jerk_e");
701         if (machine_max_jerk_e != nullptr)
702             m_time_processor.machine_limits.machine_max_jerk_e.values = machine_max_jerk_e->values;
703 
704         const ConfigOptionFloats* machine_max_acceleration_extruding = config.option<ConfigOptionFloats>("machine_max_acceleration_extruding");
705         if (machine_max_acceleration_extruding != nullptr)
706             m_time_processor.machine_limits.machine_max_acceleration_extruding.values = machine_max_acceleration_extruding->values;
707 
708         const ConfigOptionFloats* machine_max_acceleration_retracting = config.option<ConfigOptionFloats>("machine_max_acceleration_retracting");
709         if (machine_max_acceleration_retracting != nullptr)
710             m_time_processor.machine_limits.machine_max_acceleration_retracting.values = machine_max_acceleration_retracting->values;
711 
712         const ConfigOptionFloats* machine_min_extruding_rate = config.option<ConfigOptionFloats>("machine_min_extruding_rate");
713         if (machine_min_extruding_rate != nullptr)
714             m_time_processor.machine_limits.machine_min_extruding_rate.values = machine_min_extruding_rate->values;
715 
716         const ConfigOptionFloats* machine_min_travel_rate = config.option<ConfigOptionFloats>("machine_min_travel_rate");
717         if (machine_min_travel_rate != nullptr)
718             m_time_processor.machine_limits.machine_min_travel_rate.values = machine_min_travel_rate->values;
719     }
720 
721     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
722         float max_acceleration = get_option_value(m_time_processor.machine_limits.machine_max_acceleration_extruding, i);
723         m_time_processor.machines[i].max_acceleration = max_acceleration;
724         m_time_processor.machines[i].acceleration = (max_acceleration > 0.0f) ? max_acceleration : DEFAULT_ACCELERATION;
725     }
726 
727     if (m_time_processor.machine_limits.machine_max_acceleration_x.values.size() > 1)
728         enable_stealth_time_estimator(true);
729 
730 #if ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING
731     const ConfigOptionBool* use_volumetric_e = config.option<ConfigOptionBool>("use_volumetric_e");
732     if (use_volumetric_e != nullptr)
733         m_use_volumetric_e = use_volumetric_e->value;
734 #endif // ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING
735 }
736 
enable_stealth_time_estimator(bool enabled)737 void GCodeProcessor::enable_stealth_time_estimator(bool enabled)
738 {
739     m_time_processor.machines[static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Stealth)].enabled = enabled;
740 }
741 
reset()742 void GCodeProcessor::reset()
743 {
744     static const size_t Min_Extruder_Count = 5;
745 
746     m_units = EUnits::Millimeters;
747     m_global_positioning_type = EPositioningType::Absolute;
748     m_e_local_positioning_type = EPositioningType::Absolute;
749     m_extruder_offsets = std::vector<Vec3f>(Min_Extruder_Count, Vec3f::Zero());
750     m_flavor = gcfRepRapSprinter;
751 
752     m_start_position = { 0.0f, 0.0f, 0.0f, 0.0f };
753     m_end_position = { 0.0f, 0.0f, 0.0f, 0.0f };
754     m_origin = { 0.0f, 0.0f, 0.0f, 0.0f };
755     m_cached_position.reset();
756     m_wiping = false;
757 
758     m_feedrate = 0.0f;
759     m_width = 0.0f;
760     m_height = 0.0f;
761 #if ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
762     m_forced_width = 0.0f;
763     m_forced_height = 0.0f;
764 #endif // ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
765     m_mm3_per_mm = 0.0f;
766     m_fan_speed = 0.0f;
767 
768     m_extrusion_role = erNone;
769     m_extruder_id = 0;
770     m_extruder_colors.resize(Min_Extruder_Count);
771     for (size_t i = 0; i < Min_Extruder_Count; ++i) {
772         m_extruder_colors[i] = static_cast<unsigned char>(i);
773     }
774 
775     m_filament_diameters = std::vector<float>(Min_Extruder_Count, 1.75f);
776     m_extruded_last_z = 0.0f;
777     m_g1_line_id = 0;
778     m_layer_id = 0;
779     m_cp_color.reset();
780 
781     m_producer = EProducer::Unknown;
782     m_producers_enabled = false;
783 
784     m_time_processor.reset();
785 
786     m_result.reset();
787     m_result.id = ++s_result_id;
788 
789 #if ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING
790     m_use_volumetric_e = false;
791 #endif // ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING
792 
793 #if ENABLE_GCODE_VIEWER_DATA_CHECKING
794     m_mm3_per_mm_compare.reset();
795     m_height_compare.reset();
796     m_width_compare.reset();
797 #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
798 }
799 
process_file(const std::string & filename,bool apply_postprocess,std::function<void ()> cancel_callback)800 void GCodeProcessor::process_file(const std::string& filename, bool apply_postprocess, std::function<void()> cancel_callback)
801 {
802     auto last_cancel_callback_time = std::chrono::high_resolution_clock::now();
803 
804 #if ENABLE_GCODE_VIEWER_STATISTICS
805     auto start_time = std::chrono::high_resolution_clock::now();
806 #endif // ENABLE_GCODE_VIEWER_STATISTICS
807 
808     // pre-processing
809     // parse the gcode file to detect its producer
810     if (m_producers_enabled) {
811         m_parser.parse_file(filename, [this](GCodeReader& reader, const GCodeReader::GCodeLine& line) {
812             const std::string_view cmd = line.cmd();
813             if (cmd.length() == 0) {
814                 const std::string_view comment = line.comment();
815                 if (comment.length() > 1 && detect_producer(comment))
816                     m_parser.quit_parsing_file();
817             }
818             });
819 
820         // if the gcode was produced by PrusaSlicer,
821         // extract the config from it
822         if (m_producer == EProducer::PrusaSlicer || m_producer == EProducer::Slic3rPE || m_producer == EProducer::Slic3r) {
823             DynamicPrintConfig config;
824             config.apply(FullPrintConfig::defaults());
825             // Silently substitute unknown values by new ones for loading configurations from PrusaSlicer's own G-code.
826             // Showing substitution log or errors may make sense, but we are not really reading many values from the G-code config,
827             // thus a probability of incorrect substitution is low and the G-code viewer is a consumer-only anyways.
828             config.load_from_gcode_file(filename, ForwardCompatibilitySubstitutionRule::EnableSilent);
829             apply_config(config);
830         }
831     }
832 
833     // process gcode
834     m_result.id = ++s_result_id;
835     // 1st move must be a dummy move
836     m_result.moves.emplace_back(MoveVertex());
837     m_parser.parse_file(filename, [this, cancel_callback, &last_cancel_callback_time](GCodeReader& reader, const GCodeReader::GCodeLine& line) {
838         if (cancel_callback != nullptr) {
839             // call the cancel callback every 100 ms
840             auto curr_time = std::chrono::high_resolution_clock::now();
841             if (std::chrono::duration_cast<std::chrono::milliseconds>(curr_time - last_cancel_callback_time).count() > 100) {
842                 cancel_callback();
843                 last_cancel_callback_time = curr_time;
844             }
845         }
846         process_gcode_line(line);
847         });
848 
849     // update width/height of wipe moves
850     for (MoveVertex& move : m_result.moves) {
851         if (move.type == EMoveType::Wipe) {
852             move.width = Wipe_Width;
853             move.height = Wipe_Height;
854         }
855     }
856 
857     // process the time blocks
858     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
859         TimeMachine& machine = m_time_processor.machines[i];
860         TimeMachine::CustomGCodeTime& gcode_time = machine.gcode_time;
861         machine.calculate_time();
862         if (gcode_time.needed && gcode_time.cache != 0.0f)
863             gcode_time.times.push_back({ CustomGCode::ColorChange, gcode_time.cache });
864     }
865 
866     update_estimated_times_stats();
867 
868     // post-process to add M73 lines into the gcode
869     if (apply_postprocess)
870         m_time_processor.post_process(filename);
871 
872 #if ENABLE_GCODE_VIEWER_DATA_CHECKING
873     std::cout << "\n";
874     m_mm3_per_mm_compare.output();
875     m_height_compare.output();
876     m_width_compare.output();
877 #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
878 
879 #if ENABLE_GCODE_VIEWER_STATISTICS
880     m_result.time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start_time).count();
881 #endif // ENABLE_GCODE_VIEWER_STATISTICS
882 }
883 
get_time(PrintEstimatedTimeStatistics::ETimeMode mode) const884 float GCodeProcessor::get_time(PrintEstimatedTimeStatistics::ETimeMode mode) const
885 {
886     return (mode < PrintEstimatedTimeStatistics::ETimeMode::Count) ? m_time_processor.machines[static_cast<size_t>(mode)].time : 0.0f;
887 }
888 
get_time_dhm(PrintEstimatedTimeStatistics::ETimeMode mode) const889 std::string GCodeProcessor::get_time_dhm(PrintEstimatedTimeStatistics::ETimeMode mode) const
890 {
891     return (mode < PrintEstimatedTimeStatistics::ETimeMode::Count) ? short_time(get_time_dhms(m_time_processor.machines[static_cast<size_t>(mode)].time)) : std::string("N/A");
892 }
893 
get_custom_gcode_times(PrintEstimatedTimeStatistics::ETimeMode mode,bool include_remaining) const894 std::vector<std::pair<CustomGCode::Type, std::pair<float, float>>> GCodeProcessor::get_custom_gcode_times(PrintEstimatedTimeStatistics::ETimeMode mode, bool include_remaining) const
895 {
896     std::vector<std::pair<CustomGCode::Type, std::pair<float, float>>> ret;
897     if (mode < PrintEstimatedTimeStatistics::ETimeMode::Count) {
898         const TimeMachine& machine = m_time_processor.machines[static_cast<size_t>(mode)];
899         float total_time = 0.0f;
900         for (const auto& [type, time] : machine.gcode_time.times) {
901             float remaining = include_remaining ? machine.time - total_time : 0.0f;
902             ret.push_back({ type, { time, remaining } });
903             total_time += time;
904         }
905     }
906     return ret;
907 }
908 
get_moves_time(PrintEstimatedTimeStatistics::ETimeMode mode) const909 std::vector<std::pair<EMoveType, float>> GCodeProcessor::get_moves_time(PrintEstimatedTimeStatistics::ETimeMode mode) const
910 {
911     std::vector<std::pair<EMoveType, float>> ret;
912     if (mode < PrintEstimatedTimeStatistics::ETimeMode::Count) {
913         for (size_t i = 0; i < m_time_processor.machines[static_cast<size_t>(mode)].moves_time.size(); ++i) {
914             float time = m_time_processor.machines[static_cast<size_t>(mode)].moves_time[i];
915             if (time > 0.0f)
916                 ret.push_back({ static_cast<EMoveType>(i), time });
917         }
918     }
919     return ret;
920 }
921 
get_roles_time(PrintEstimatedTimeStatistics::ETimeMode mode) const922 std::vector<std::pair<ExtrusionRole, float>> GCodeProcessor::get_roles_time(PrintEstimatedTimeStatistics::ETimeMode mode) const
923 {
924     std::vector<std::pair<ExtrusionRole, float>> ret;
925     if (mode < PrintEstimatedTimeStatistics::ETimeMode::Count) {
926         for (size_t i = 0; i < m_time_processor.machines[static_cast<size_t>(mode)].roles_time.size(); ++i) {
927             float time = m_time_processor.machines[static_cast<size_t>(mode)].roles_time[i];
928             if (time > 0.0f)
929                 ret.push_back({ static_cast<ExtrusionRole>(i), time });
930         }
931     }
932     return ret;
933 }
934 
get_layers_time(PrintEstimatedTimeStatistics::ETimeMode mode) const935 std::vector<float> GCodeProcessor::get_layers_time(PrintEstimatedTimeStatistics::ETimeMode mode) const
936 {
937     return (mode < PrintEstimatedTimeStatistics::ETimeMode::Count) ?
938         m_time_processor.machines[static_cast<size_t>(mode)].layers_time :
939         std::vector<float>();
940 }
941 
process_gcode_line(const GCodeReader::GCodeLine & line)942 void GCodeProcessor::process_gcode_line(const GCodeReader::GCodeLine& line)
943 {
944 /* std::cout << line.raw() << std::endl; */
945 
946     // update start position
947     m_start_position = m_end_position;
948 
949     const std::string_view cmd = line.cmd();
950     if (cmd.length() > 1) {
951         // process command lines
952         switch (::toupper(cmd[0]))
953         {
954         case 'G':
955             {
956                 switch (::atoi(&cmd[1]))
957                 {
958                 case 0:  { process_G0(line); break; }  // Move
959                 case 1:  { process_G1(line); break; }  // Move
960                 case 10: { process_G10(line); break; } // Retract
961                 case 11: { process_G11(line); break; } // Unretract
962                 case 20: { process_G20(line); break; } // Set Units to Inches
963                 case 21: { process_G21(line); break; } // Set Units to Millimeters
964                 case 22: { process_G22(line); break; } // Firmware controlled retract
965                 case 23: { process_G23(line); break; } // Firmware controlled unretract
966                 case 90: { process_G90(line); break; } // Set to Absolute Positioning
967                 case 91: { process_G91(line); break; } // Set to Relative Positioning
968                 case 92: { process_G92(line); break; } // Set Position
969                 default: { break; }
970                 }
971                 break;
972             }
973         case 'M':
974             {
975                 switch (::atoi(&cmd[1]))
976                 {
977                 case 1:   { process_M1(line); break; }   // Sleep or Conditional stop
978                 case 82:  { process_M82(line); break; }  // Set extruder to absolute mode
979                 case 83:  { process_M83(line); break; }  // Set extruder to relative mode
980                 case 106: { process_M106(line); break; } // Set fan speed
981                 case 107: { process_M107(line); break; } // Disable fan
982                 case 108: { process_M108(line); break; } // Set tool (Sailfish)
983                 case 132: { process_M132(line); break; } // Recall stored home offsets
984                 case 135: { process_M135(line); break; } // Set tool (MakerWare)
985                 case 201: { process_M201(line); break; } // Set max printing acceleration
986                 case 203: { process_M203(line); break; } // Set maximum feedrate
987                 case 204: { process_M204(line); break; } // Set default acceleration
988                 case 205: { process_M205(line); break; } // Advanced settings
989                 case 221: { process_M221(line); break; } // Set extrude factor override percentage
990                 case 401: { process_M401(line); break; } // Repetier: Store x, y and z position
991                 case 402: { process_M402(line); break; } // Repetier: Go to stored position
992                 case 566: { process_M566(line); break; } // Set allowable instantaneous speed change
993                 case 702: { process_M702(line); break; } // Unload the current filament into the MK3 MMU2 unit at the end of print.
994                 default: { break; }
995                 }
996                 break;
997             }
998         case 'T':
999             {
1000                 process_T(line); // Select Tool
1001                 break;
1002             }
1003         default: { break; }
1004         }
1005     }
1006     else {
1007         const std::string &comment = line.raw();
1008         if (comment.length() > 2 && comment.front() == ';')
1009             // Process tags embedded into comments. Tag comments always start at the start of a line
1010             // with a comment and continue with a tag without any whitespace separator.
1011             process_tags(comment.substr(1));
1012     }
1013 }
1014 
starts_with(const std::string_view comment,const std::string_view tag)1015 static inline bool starts_with(const std::string_view comment, const std::string_view tag)
1016 {
1017     size_t tag_len = tag.size();
1018     return comment.size() >= tag_len && comment.substr(0, tag_len) == tag;
1019 }
1020 
1021 #if __has_include(<charconv>)
1022     template <typename T, typename = void>
1023     struct is_from_chars_convertible : std::false_type {};
1024     template <typename T>
1025     struct is_from_chars_convertible<T, std::void_t<decltype(std::from_chars(std::declval<const char*>(), std::declval<const char*>(), std::declval<T&>()))>> : std::true_type {};
1026 #endif
1027 
1028 // Returns true if the number was parsed correctly into out and the number spanned the whole input string.
1029 template<typename T>
parse_number(const std::string_view sv,T & out)1030 [[nodiscard]] static inline bool parse_number(const std::string_view sv, T &out)
1031 {
1032     // https://www.bfilipek.com/2019/07/detect-overload-from-chars.html#example-stdfromchars
1033 #if __has_include(<charconv>)
1034     // Visual Studio 19 supports from_chars all right.
1035     // OSX compiler that we use only implements std::from_chars just for ints.
1036     // GCC that we compile on does not provide <charconv> at all.
1037     if constexpr (is_from_chars_convertible<T>::value) {
1038         auto str_end = sv.data() + sv.size();
1039         auto [end_ptr, error_code] = std::from_chars(sv.data(), str_end, out);
1040         return error_code == std::errc() && end_ptr == str_end;
1041     }
1042     else
1043 #endif
1044     {
1045         // Legacy conversion, which is costly due to having to make a copy of the string before conversion.
1046         try {
1047             assert(sv.size() < 1024);
1048 	    assert(sv.data() != nullptr);
1049             std::string str { sv };
1050             size_t read = 0;
1051             if constexpr (std::is_same_v<T, int>)
1052                 out = std::stoi(str, &read);
1053             else if constexpr (std::is_same_v<T, long>)
1054                 out = std::stol(str, &read);
1055             else if constexpr (std::is_same_v<T, float>)
1056                 out = std::stof(str, &read);
1057             else if constexpr (std::is_same_v<T, double>)
1058                 out = std::stod(str, &read);
1059             return str.size() == read;
1060         } catch (...) {
1061             return false;
1062         }
1063     }
1064 }
1065 
process_tags(const std::string_view comment)1066 void GCodeProcessor::process_tags(const std::string_view comment)
1067 {
1068     // producers tags
1069     if (m_producers_enabled && process_producers_tags(comment))
1070         return;
1071 
1072     // extrusion role tag
1073     if (starts_with(comment, Extrusion_Role_Tag)) {
1074         m_extrusion_role = ExtrusionEntity::string_to_role(comment.substr(Extrusion_Role_Tag.length()));
1075         return;
1076     }
1077 
1078     // wipe start tag
1079     if (starts_with(comment, Wipe_Start_Tag)) {
1080         m_wiping = true;
1081         return;
1082     }
1083 
1084     // wipe end tag
1085     if (starts_with(comment, Wipe_End_Tag)) {
1086         m_wiping = false;
1087         return;
1088     }
1089 
1090 #if ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
1091     if (!m_producers_enabled || m_producer == EProducer::PrusaSlicer) {
1092         // height tag
1093         if (starts_with(comment, Height_Tag)) {
1094             if (!parse_number(comment.substr(Height_Tag.size()), m_forced_height))
1095                 BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Height (" << comment << ").";
1096             return;
1097         }
1098         // width tag
1099         if (starts_with(comment, Width_Tag)) {
1100             if (!parse_number(comment.substr(Width_Tag.size()), m_forced_width))
1101                 BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Width (" << comment << ").";
1102             return;
1103         }
1104     }
1105 #else
1106     if ((!m_producers_enabled || m_producer == EProducer::PrusaSlicer) &&
1107         starts_with(comment, Height_Tag)) {
1108         // height tag
1109         if (!parse_number(comment.substr(Height_Tag.size()), m_height))
1110             BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Height (" << comment << ").";
1111         return;
1112     }
1113 
1114 #if ENABLE_GCODE_VIEWER_DATA_CHECKING
1115     // width tag
1116     if (starts_with(comment, Width_Tag)) {
1117         if (! parse_number(comment.substr(Width_Tag.size()), m_width_compare.last_tag_value))
1118             BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Width (" << comment << ").";
1119         return;
1120     }
1121 #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
1122 #endif // ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
1123 
1124     // color change tag
1125     if (starts_with(comment, Color_Change_Tag)) {
1126         unsigned char extruder_id = 0;
1127         if (starts_with(comment.substr(Color_Change_Tag.size()), ",T")) {
1128             int eid;
1129             if (! parse_number(comment.substr(Color_Change_Tag.size() + 2), eid) || eid < 0 || eid > 255) {
1130                 BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Color_Change (" << comment << ").";
1131                 return;
1132             }
1133             extruder_id = static_cast<unsigned char>(eid);
1134         }
1135 
1136         m_extruder_colors[extruder_id] = static_cast<unsigned char>(m_extruder_offsets.size()) + m_cp_color.counter; // color_change position in list of color for preview
1137         ++m_cp_color.counter;
1138         if (m_cp_color.counter == UCHAR_MAX)
1139             m_cp_color.counter = 0;
1140 
1141         if (m_extruder_id == extruder_id) {
1142             m_cp_color.current = m_extruder_colors[extruder_id];
1143             store_move_vertex(EMoveType::Color_change);
1144         }
1145 
1146         process_custom_gcode_time(CustomGCode::ColorChange);
1147 
1148         return;
1149     }
1150 
1151     // pause print tag
1152     if (comment == Pause_Print_Tag) {
1153         store_move_vertex(EMoveType::Pause_Print);
1154         process_custom_gcode_time(CustomGCode::PausePrint);
1155         return;
1156     }
1157 
1158     // custom code tag
1159     if (comment == Custom_Code_Tag) {
1160         store_move_vertex(EMoveType::Custom_GCode);
1161         return;
1162     }
1163 
1164 #if ENABLE_GCODE_VIEWER_DATA_CHECKING
1165     // mm3_per_mm print tag
1166     if (starts_with(comment, Mm3_Per_Mm_Tag)) {
1167         if (! parse_number(comment.substr(Mm3_Per_Mm_Tag.size()), m_mm3_per_mm_compare.last_tag_value))
1168             BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Mm3_Per_Mm (" << comment << ").";
1169         return;
1170     }
1171 #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
1172 
1173     // layer change tag
1174     if (comment == Layer_Change_Tag) {
1175         ++m_layer_id;
1176         return;
1177     }
1178 }
1179 
process_producers_tags(const std::string_view comment)1180 bool GCodeProcessor::process_producers_tags(const std::string_view comment)
1181 {
1182     switch (m_producer)
1183     {
1184     case EProducer::Slic3rPE:
1185     case EProducer::Slic3r:
1186     case EProducer::PrusaSlicer: { return process_prusaslicer_tags(comment); }
1187     case EProducer::Cura:        { return process_cura_tags(comment); }
1188     case EProducer::Simplify3D:  { return process_simplify3d_tags(comment); }
1189     case EProducer::CraftWare:   { return process_craftware_tags(comment); }
1190     case EProducer::ideaMaker:   { return process_ideamaker_tags(comment); }
1191     case EProducer::KissSlicer:  { return process_kissslicer_tags(comment); }
1192     default:                     { return false; }
1193     }
1194 }
1195 
process_prusaslicer_tags(const std::string_view comment)1196 bool GCodeProcessor::process_prusaslicer_tags(const std::string_view comment)
1197 {
1198     return false;
1199 }
1200 
process_cura_tags(const std::string_view comment)1201 bool GCodeProcessor::process_cura_tags(const std::string_view comment)
1202 {
1203     // TYPE -> extrusion role
1204     std::string tag = "TYPE:";
1205     size_t pos = comment.find(tag);
1206     if (pos != comment.npos) {
1207         const std::string_view type = comment.substr(pos + tag.length());
1208         if (type == "SKIRT")
1209             m_extrusion_role = erSkirt;
1210         else if (type == "WALL-OUTER")
1211             m_extrusion_role = erExternalPerimeter;
1212         else if (type == "WALL-INNER")
1213             m_extrusion_role = erPerimeter;
1214         else if (type == "SKIN")
1215             m_extrusion_role = erSolidInfill;
1216         else if (type == "FILL")
1217             m_extrusion_role = erInternalInfill;
1218         else if (type == "SUPPORT")
1219             m_extrusion_role = erSupportMaterial;
1220         else if (type == "SUPPORT-INTERFACE")
1221             m_extrusion_role = erSupportMaterialInterface;
1222         else if (type == "PRIME-TOWER")
1223             m_extrusion_role = erWipeTower;
1224         else {
1225             m_extrusion_role = erNone;
1226             BOOST_LOG_TRIVIAL(warning) << "GCodeProcessor found unknown extrusion role: " << type;
1227         }
1228 
1229         return true;
1230     }
1231 
1232     // flavor
1233     tag = "FLAVOR:";
1234     pos = comment.find(tag);
1235     if (pos != comment.npos) {
1236         const std::string_view flavor = comment.substr(pos + tag.length());
1237         if (flavor == "BFB")
1238             m_flavor = gcfMarlin; // << ???????????????????????
1239         else if (flavor == "Mach3")
1240             m_flavor = gcfMach3;
1241         else if (flavor == "Makerbot")
1242             m_flavor = gcfMakerWare;
1243         else if (flavor == "UltiGCode")
1244             m_flavor = gcfMarlin; // << ???????????????????????
1245         else if (flavor == "Marlin(Volumetric)")
1246             m_flavor = gcfMarlin; // << ???????????????????????
1247         else if (flavor == "Griffin")
1248             m_flavor = gcfMarlin; // << ???????????????????????
1249         else if (flavor == "Repetier")
1250             m_flavor = gcfRepetier;
1251         else if (flavor == "RepRap")
1252             m_flavor = gcfRepRapFirmware;
1253         else if (flavor == "Marlin")
1254             m_flavor = gcfMarlin;
1255         else
1256             BOOST_LOG_TRIVIAL(warning) << "GCodeProcessor found unknown flavor: " << flavor;
1257 
1258         return true;
1259     }
1260 
1261     // layer
1262     tag = "LAYER:";
1263     pos = comment.find(tag);
1264     if (pos != comment.npos) {
1265         ++m_layer_id;
1266         return true;
1267     }
1268 
1269     return false;
1270 }
1271 
process_simplify3d_tags(const std::string_view comment)1272 bool GCodeProcessor::process_simplify3d_tags(const std::string_view comment)
1273 {
1274     // extrusion roles
1275 
1276     // ; skirt
1277     size_t pos = comment.find(" skirt");
1278     if (pos == 0) {
1279         m_extrusion_role = erSkirt;
1280         return true;
1281     }
1282 
1283     // ; outer perimeter
1284     pos = comment.find(" outer perimeter");
1285     if (pos == 0) {
1286         m_extrusion_role = erExternalPerimeter;
1287         return true;
1288     }
1289 
1290     // ; inner perimeter
1291     pos = comment.find(" inner perimeter");
1292     if (pos == 0) {
1293         m_extrusion_role = erPerimeter;
1294         return true;
1295     }
1296 
1297     // ; gap fill
1298     pos = comment.find(" gap fill");
1299     if (pos == 0) {
1300         m_extrusion_role = erGapFill;
1301         return true;
1302     }
1303 
1304     // ; infill
1305     pos = comment.find(" infill");
1306     if (pos == 0) {
1307         m_extrusion_role = erInternalInfill;
1308         return true;
1309     }
1310 
1311     // ; solid layer
1312     pos = comment.find(" solid layer");
1313     if (pos == 0) {
1314         m_extrusion_role = erNone; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1315         return true;
1316     }
1317 
1318     // ; bridge
1319     pos = comment.find(" bridge");
1320     if (pos == 0) {
1321         m_extrusion_role = erBridgeInfill;
1322         return true;
1323     }
1324 
1325     // ; support
1326     pos = comment.find(" support");
1327     if (pos == 0) {
1328         m_extrusion_role = erSupportMaterial;
1329         return true;
1330     }
1331 
1332     // ; prime pillar
1333     pos = comment.find(" prime pillar");
1334     if (pos == 0) {
1335         m_extrusion_role = erWipeTower;
1336         return true;
1337     }
1338 
1339     // ; ooze shield
1340     pos = comment.find(" ooze shield");
1341     if (pos == 0) {
1342         m_extrusion_role = erNone; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1343         return true;
1344     }
1345 
1346     // ; raft
1347     pos = comment.find(" raft");
1348     if (pos == 0) {
1349         m_extrusion_role = erSkirt;
1350         return true;
1351     }
1352 
1353     // geometry
1354 #if ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
1355     // ; tool
1356     std::string tag = " tool";
1357     pos = comment.find(tag);
1358     if (pos == 0) {
1359         const std::string_view data = comment.substr(pos + tag.length());
1360         std::string h_tag = "H";
1361         size_t h_start = data.find(h_tag);
1362         size_t h_end = data.find_first_of(' ', h_start);
1363         std::string w_tag = "W";
1364         size_t w_start = data.find(w_tag);
1365         size_t w_end = data.find_first_of(' ', w_start);
1366         if (h_start != data.npos) {
1367             if (!parse_number(data.substr(h_start + 1, (h_end != data.npos) ? h_end - h_start - 1 : h_end), m_forced_height))
1368                 BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Height (" << comment << ").";
1369         }
1370         if (w_start != data.npos) {
1371             if (!parse_number(data.substr(w_start + 1, (w_end != data.npos) ? w_end - w_start - 1 : w_end), m_forced_width))
1372                 BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Width (" << comment << ").";
1373         }
1374 
1375         return true;
1376     }
1377 
1378     // ; layer
1379     tag = " layer";
1380 #else
1381 #if ENABLE_GCODE_VIEWER_DATA_CHECKING
1382     // ; tool
1383     std::string tag = " tool";
1384     pos = comment.find(tag);
1385     if (pos == 0) {
1386         const std::string_view data = comment.substr(pos + tag.length());
1387         std::string h_tag = "H";
1388         size_t h_start = data.find(h_tag);
1389         size_t h_end = data.find_first_of(' ', h_start);
1390         std::string w_tag = "W";
1391         size_t w_start = data.find(w_tag);
1392         size_t w_end = data.find_first_of(' ', w_start);
1393         if (h_start != data.npos) {
1394             if (! parse_number(data.substr(h_start + 1, (h_end != data.npos) ? h_end - h_start - 1 : h_end), m_height_compare.last_tag_value))
1395                 BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Height (" << comment << ").";
1396         }
1397         if (w_start != data.npos) {
1398             if (! parse_number(data.substr(w_start + 1, (w_end != data.npos) ? w_end - w_start - 1 : w_end), m_width_compare.last_tag_value))
1399                 BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Width (" << comment << ").";
1400         }
1401 
1402         return true;
1403     }
1404 #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
1405 
1406     // ; layer
1407     std::string tag = " layer";
1408 #endif // ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
1409     pos = comment.find(tag);
1410     if (pos == 0) {
1411         // skip lines "; layer end"
1412         const std::string_view data = comment.substr(pos + tag.length());
1413         size_t end_start = data.find("end");
1414         if (end_start == data.npos)
1415             ++m_layer_id;
1416 
1417         return true;
1418     }
1419 
1420     return false;
1421 }
1422 
process_craftware_tags(const std::string_view comment)1423 bool GCodeProcessor::process_craftware_tags(const std::string_view comment)
1424 {
1425     // segType -> extrusion role
1426     std::string tag = "segType:";
1427     size_t pos = comment.find(tag);
1428     if (pos != comment.npos) {
1429         const std::string_view type = comment.substr(pos + tag.length());
1430         if (type == "Skirt")
1431             m_extrusion_role = erSkirt;
1432         else if (type == "Perimeter")
1433             m_extrusion_role = erExternalPerimeter;
1434         else if (type == "HShell")
1435             m_extrusion_role = erNone; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1436         else if (type == "InnerHair")
1437             m_extrusion_role = erNone; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1438         else if (type == "Loop")
1439             m_extrusion_role = erNone; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1440         else if (type == "Infill")
1441             m_extrusion_role = erInternalInfill;
1442         else if (type == "Raft")
1443             m_extrusion_role = erSkirt;
1444         else if (type == "Support")
1445             m_extrusion_role = erSupportMaterial;
1446         else if (type == "SupportTouch")
1447             m_extrusion_role = erSupportMaterial;
1448         else if (type == "SoftSupport")
1449             m_extrusion_role = erSupportMaterialInterface;
1450         else if (type == "Pillar")
1451             m_extrusion_role = erWipeTower;
1452         else {
1453             m_extrusion_role = erNone;
1454             BOOST_LOG_TRIVIAL(warning) << "GCodeProcessor found unknown extrusion role: " << type;
1455         }
1456 
1457         return true;
1458     }
1459 
1460     // layer
1461     pos = comment.find(" Layer #");
1462     if (pos == 0) {
1463         ++m_layer_id;
1464         return true;
1465     }
1466 
1467     return false;
1468 }
1469 
process_ideamaker_tags(const std::string_view comment)1470 bool GCodeProcessor::process_ideamaker_tags(const std::string_view comment)
1471 {
1472     // TYPE -> extrusion role
1473     std::string tag = "TYPE:";
1474     size_t pos = comment.find(tag);
1475     if (pos != comment.npos) {
1476         const std::string_view type = comment.substr(pos + tag.length());
1477         if (type == "RAFT")
1478             m_extrusion_role = erSkirt;
1479         else if (type == "WALL-OUTER")
1480             m_extrusion_role = erExternalPerimeter;
1481         else if (type == "WALL-INNER")
1482             m_extrusion_role = erPerimeter;
1483         else if (type == "SOLID-FILL")
1484             m_extrusion_role = erSolidInfill;
1485         else if (type == "FILL")
1486             m_extrusion_role = erInternalInfill;
1487         else if (type == "BRIDGE")
1488             m_extrusion_role = erBridgeInfill;
1489         else if (type == "SUPPORT")
1490             m_extrusion_role = erSupportMaterial;
1491         else {
1492             m_extrusion_role = erNone;
1493             BOOST_LOG_TRIVIAL(warning) << "GCodeProcessor found unknown extrusion role: " << type;
1494         }
1495         return true;
1496     }
1497 
1498     // geometry
1499 #if ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
1500     // width
1501     tag = "WIDTH:";
1502     pos = comment.find(tag);
1503     if (pos != comment.npos) {
1504         if (!parse_number(comment.substr(pos + tag.length()), m_forced_width))
1505             BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Width (" << comment << ").";
1506         return true;
1507     }
1508 
1509     // height
1510     tag = "HEIGHT:";
1511     pos = comment.find(tag);
1512     if (pos != comment.npos) {
1513         if (!parse_number(comment.substr(pos + tag.length()), m_forced_height))
1514             BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Height (" << comment << ").";
1515         return true;
1516     }
1517 #else
1518 #if ENABLE_GCODE_VIEWER_DATA_CHECKING
1519     // width
1520     tag = "WIDTH:";
1521     pos = comment.find(tag);
1522     if (pos != comment.npos) {
1523         if (! parse_number(comment.substr(pos + tag.length()), m_width_compare.last_tag_value))
1524             BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Width (" << comment << ").";
1525         return true;
1526     }
1527 
1528     // height
1529     tag = "HEIGHT:";
1530     pos = comment.find(tag);
1531     if (pos != comment.npos) {
1532         if (! parse_number(comment.substr(pos + tag.length()), m_height_compare.last_tag_value))
1533             BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Height (" << comment << ").";
1534         return true;
1535     }
1536 #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
1537 #endif // ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
1538 
1539     // layer
1540     pos = comment.find("LAYER:");
1541     if (pos == 0) {
1542         ++m_layer_id;
1543         return true;
1544     }
1545 
1546     return false;
1547 }
1548 
process_kissslicer_tags(const std::string_view comment)1549 bool GCodeProcessor::process_kissslicer_tags(const std::string_view comment)
1550 {
1551     // extrusion roles
1552 
1553     // ; 'Raft Path'
1554     size_t pos = comment.find(" 'Raft Path'");
1555     if (pos == 0) {
1556         m_extrusion_role = erSkirt;
1557         return true;
1558     }
1559 
1560     // ; 'Support Interface Path'
1561     pos = comment.find(" 'Support Interface Path'");
1562     if (pos == 0) {
1563         m_extrusion_role = erSupportMaterialInterface;
1564         return true;
1565     }
1566 
1567     // ; 'Travel/Ironing Path'
1568     pos = comment.find(" 'Travel/Ironing Path'");
1569     if (pos == 0) {
1570         m_extrusion_role = erIroning;
1571         return true;
1572     }
1573 
1574     // ; 'Support (may Stack) Path'
1575     pos = comment.find(" 'Support (may Stack) Path'");
1576     if (pos == 0) {
1577         m_extrusion_role = erSupportMaterial;
1578         return true;
1579     }
1580 
1581     // ; 'Perimeter Path'
1582     pos = comment.find(" 'Perimeter Path'");
1583     if (pos == 0) {
1584         m_extrusion_role = erExternalPerimeter;
1585         return true;
1586     }
1587 
1588     // ; 'Pillar Path'
1589     pos = comment.find(" 'Pillar Path'");
1590     if (pos == 0) {
1591         m_extrusion_role = erNone; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1592         return true;
1593     }
1594 
1595     // ; 'Destring/Wipe/Jump Path'
1596     pos = comment.find(" 'Destring/Wipe/Jump Path'");
1597     if (pos == 0) {
1598         m_extrusion_role = erNone; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1599         return true;
1600     }
1601 
1602     // ; 'Prime Pillar Path'
1603     pos = comment.find(" 'Prime Pillar Path'");
1604     if (pos == 0) {
1605         m_extrusion_role = erNone; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1606         return true;
1607     }
1608 
1609     // ; 'Loop Path'
1610     pos = comment.find(" 'Loop Path'");
1611     if (pos == 0) {
1612         m_extrusion_role = erNone; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1613         return true;
1614     }
1615 
1616     // ; 'Crown Path'
1617     pos = comment.find(" 'Crown Path'");
1618     if (pos == 0) {
1619         m_extrusion_role = erNone; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1620         return true;
1621     }
1622 
1623     // ; 'Solid Path'
1624     pos = comment.find(" 'Solid Path'");
1625     if (pos == 0) {
1626         m_extrusion_role = erNone;
1627         return true;
1628     }
1629 
1630     // ; 'Stacked Sparse Infill Path'
1631     pos = comment.find(" 'Stacked Sparse Infill Path'");
1632     if (pos == 0) {
1633         m_extrusion_role = erInternalInfill;
1634         return true;
1635     }
1636 
1637     // ; 'Sparse Infill Path'
1638     pos = comment.find(" 'Sparse Infill Path'");
1639     if (pos == 0) {
1640         m_extrusion_role = erSolidInfill;
1641         return true;
1642     }
1643 
1644     // geometry
1645 
1646     // layer
1647     pos = comment.find(" BEGIN_LAYER_");
1648     if (pos == 0) {
1649         ++m_layer_id;
1650         return true;
1651     }
1652 
1653     return false;
1654 }
1655 
detect_producer(const std::string_view comment)1656 bool GCodeProcessor::detect_producer(const std::string_view comment)
1657 {
1658     for (const auto& [id, search_string] : Producers) {
1659         size_t pos = comment.find(search_string);
1660         if (pos != comment.npos) {
1661             m_producer = id;
1662             BOOST_LOG_TRIVIAL(info) << "Detected gcode producer: " << search_string;
1663             return true;
1664         }
1665     }
1666     return false;
1667 }
1668 
process_G0(const GCodeReader::GCodeLine & line)1669 void GCodeProcessor::process_G0(const GCodeReader::GCodeLine& line)
1670 {
1671     process_G1(line);
1672 }
1673 
process_G1(const GCodeReader::GCodeLine & line)1674 void GCodeProcessor::process_G1(const GCodeReader::GCodeLine& line)
1675 {
1676 #if ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING
1677     float filament_diameter = (static_cast<size_t>(m_extruder_id) < m_filament_diameters.size()) ? m_filament_diameters[m_extruder_id] : m_filament_diameters.back();
1678     float filament_radius = 0.5f * filament_diameter;
1679     float area_filament_cross_section = static_cast<float>(M_PI) * sqr(filament_radius);
1680     auto absolute_position = [this, area_filament_cross_section](Axis axis, const GCodeReader::GCodeLine& lineG1) {
1681 #else
1682     auto absolute_position = [this](Axis axis, const GCodeReader::GCodeLine& lineG1) {
1683 #endif // ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING
1684         bool is_relative = (m_global_positioning_type == EPositioningType::Relative);
1685         if (axis == E)
1686             is_relative |= (m_e_local_positioning_type == EPositioningType::Relative);
1687 
1688         if (lineG1.has(Slic3r::Axis(axis))) {
1689             float lengthsScaleFactor = (m_units == EUnits::Inches) ? INCHES_TO_MM : 1.0f;
1690             float ret = lineG1.value(Slic3r::Axis(axis)) * lengthsScaleFactor;
1691 #if ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING
1692             if (axis == E && m_use_volumetric_e)
1693                 ret /= area_filament_cross_section;
1694 #endif // ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING
1695             return is_relative ? m_start_position[axis] + ret : m_origin[axis] + ret;
1696         }
1697         else
1698             return m_start_position[axis];
1699     };
1700 
1701     auto move_type = [this](const AxisCoords& delta_pos) {
1702         EMoveType type = EMoveType::Noop;
1703 
1704         if (m_wiping)
1705             type = EMoveType::Wipe;
1706         else if (delta_pos[E] < 0.0f)
1707             type = (delta_pos[X] != 0.0f || delta_pos[Y] != 0.0f || delta_pos[Z] != 0.0f) ? EMoveType::Travel : EMoveType::Retract;
1708         else if (delta_pos[E] > 0.0f) {
1709             if (delta_pos[X] == 0.0f && delta_pos[Y] == 0.0f)
1710                 type = (delta_pos[Z] == 0.0f) ? EMoveType::Unretract : EMoveType::Travel;
1711             else if (delta_pos[X] != 0.0f || delta_pos[Y] != 0.0f)
1712                 type = EMoveType::Extrude;
1713         }
1714         else if (delta_pos[X] != 0.0f || delta_pos[Y] != 0.0f || delta_pos[Z] != 0.0f)
1715             type = EMoveType::Travel;
1716 
1717         return type;
1718     };
1719 
1720     ++m_g1_line_id;
1721 
1722     // enable processing of lines M201/M203/M204/M205
1723     m_time_processor.machine_envelope_processing_enabled = true;
1724 
1725     // updates axes positions from line
1726     for (unsigned char a = X; a <= E; ++a) {
1727         m_end_position[a] = absolute_position((Axis)a, line);
1728     }
1729 
1730     // updates feedrate from line, if present
1731     if (line.has_f())
1732         m_feedrate = line.f() * MMMIN_TO_MMSEC;
1733 
1734     // calculates movement deltas
1735     float max_abs_delta = 0.0f;
1736     AxisCoords delta_pos;
1737     for (unsigned char a = X; a <= E; ++a) {
1738         delta_pos[a] = m_end_position[a] - m_start_position[a];
1739         max_abs_delta = std::max(max_abs_delta, std::abs(delta_pos[a]));
1740     }
1741 
1742     // no displacement, return
1743     if (max_abs_delta == 0.0f)
1744         return;
1745 
1746     EMoveType type = move_type(delta_pos);
1747     if (type == EMoveType::Extrude && m_end_position[Z] == 0.0f)
1748         type = EMoveType::Travel;
1749 
1750     if (type == EMoveType::Extrude) {
1751         float delta_xyz = std::sqrt(sqr(delta_pos[X]) + sqr(delta_pos[Y]) + sqr(delta_pos[Z]));
1752 #if !ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING
1753         float filament_diameter = (static_cast<size_t>(m_extruder_id) < m_filament_diameters.size()) ? m_filament_diameters[m_extruder_id] : m_filament_diameters.back();
1754         float filament_radius = 0.5f * filament_diameter;
1755         float area_filament_cross_section = static_cast<float>(M_PI) * sqr(filament_radius);
1756 #endif // !ENABLE_VOLUMETRIC_EXTRUSION_PROCESSING
1757         float volume_extruded_filament = area_filament_cross_section * delta_pos[E];
1758         float area_toolpath_cross_section = volume_extruded_filament / delta_xyz;
1759 
1760         // volume extruded filament / tool displacement = area toolpath cross section
1761         m_mm3_per_mm = area_toolpath_cross_section;
1762 #if ENABLE_GCODE_VIEWER_DATA_CHECKING
1763         m_mm3_per_mm_compare.update(area_toolpath_cross_section, m_extrusion_role);
1764 #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
1765 
1766 #if ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
1767         if (m_forced_height > 0.0f)
1768             m_height = m_forced_height;
1769         else {
1770             if (m_end_position[Z] > m_extruded_last_z + EPSILON) {
1771                 m_height = m_end_position[Z] - m_extruded_last_z;
1772                 m_extruded_last_z = m_end_position[Z];
1773             }
1774         }
1775 
1776 #if ENABLE_GCODE_VIEWER_DATA_CHECKING
1777         m_height_compare.update(m_height, m_extrusion_role);
1778 #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
1779 #else
1780         if ((m_producers_enabled && m_producer != EProducer::PrusaSlicer) || m_height == 0.0f) {
1781             if (m_end_position[Z] > m_extruded_last_z + EPSILON) {
1782                 m_height = m_end_position[Z] - m_extruded_last_z;
1783 #if ENABLE_GCODE_VIEWER_DATA_CHECKING
1784                 m_height_compare.update(m_height, m_extrusion_role);
1785 #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
1786                 m_extruded_last_z = m_end_position[Z];
1787             }
1788         }
1789 #endif // ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
1790 
1791 #if ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
1792         if (m_forced_width > 0.0f)
1793             m_width = m_forced_width;
1794         else if (m_extrusion_role == erExternalPerimeter)
1795 #else
1796         if (m_extrusion_role == erExternalPerimeter)
1797 #endif // ENABLE_TOOLPATHS_WIDTH_HEIGHT_FROM_GCODE
1798             // cross section: rectangle
1799             m_width = delta_pos[E] * static_cast<float>(M_PI * sqr(1.05f * filament_radius)) / (delta_xyz * m_height);
1800         else if (m_extrusion_role == erBridgeInfill || m_extrusion_role == erNone)
1801             // cross section: circle
1802             m_width = static_cast<float>(m_filament_diameters[m_extruder_id]) * std::sqrt(delta_pos[E] / delta_xyz);
1803         else
1804             // cross section: rectangle + 2 semicircles
1805             m_width = delta_pos[E] * static_cast<float>(M_PI * sqr(filament_radius)) / (delta_xyz * m_height) + static_cast<float>(1.0 - 0.25 * M_PI) * m_height;
1806 
1807         // clamp width to avoid artifacts which may arise from wrong values of m_height
1808         m_width = std::min(m_width, std::max(1.0f, 4.0f * m_height));
1809 
1810 #if ENABLE_GCODE_VIEWER_DATA_CHECKING
1811         m_width_compare.update(m_width, m_extrusion_role);
1812 #endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
1813     }
1814 
1815     if (type == EMoveType::Extrude && (m_extrusion_role == erCustom || m_width == 0.0f || m_height == 0.0f))
1816         type = EMoveType::Travel;
1817 
1818     // time estimate section
1819     auto move_length = [](const AxisCoords& delta_pos) {
1820         float sq_xyz_length = sqr(delta_pos[X]) + sqr(delta_pos[Y]) + sqr(delta_pos[Z]);
1821         return (sq_xyz_length > 0.0f) ? std::sqrt(sq_xyz_length) : std::abs(delta_pos[E]);
1822     };
1823 
1824     auto is_extrusion_only_move = [](const AxisCoords& delta_pos) {
1825         return delta_pos[X] == 0.0f && delta_pos[Y] == 0.0f && delta_pos[Z] == 0.0f && delta_pos[E] != 0.0f;
1826     };
1827 
1828     float distance = move_length(delta_pos);
1829     assert(distance != 0.0f);
1830     float inv_distance = 1.0f / distance;
1831 
1832     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
1833         TimeMachine& machine = m_time_processor.machines[i];
1834         if (!machine.enabled)
1835             continue;
1836 
1837         TimeMachine::State& curr = machine.curr;
1838         TimeMachine::State& prev = machine.prev;
1839         std::vector<TimeBlock>& blocks = machine.blocks;
1840 
1841         curr.feedrate = (delta_pos[E] == 0.0f) ?
1842             minimum_travel_feedrate(static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i), m_feedrate) :
1843             minimum_feedrate(static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i), m_feedrate);
1844 
1845         TimeBlock block;
1846         block.move_type = type;
1847         block.role = m_extrusion_role;
1848         block.distance = distance;
1849         block.g1_line_id = m_g1_line_id;
1850         block.layer_id = m_layer_id;
1851 
1852         // calculates block cruise feedrate
1853         float min_feedrate_factor = 1.0f;
1854         for (unsigned char a = X; a <= E; ++a) {
1855             curr.axis_feedrate[a] = curr.feedrate * delta_pos[a] * inv_distance;
1856             if (a == E)
1857                 curr.axis_feedrate[a] *= machine.extrude_factor_override_percentage;
1858 
1859             curr.abs_axis_feedrate[a] = std::abs(curr.axis_feedrate[a]);
1860             if (curr.abs_axis_feedrate[a] != 0.0f) {
1861                 float axis_max_feedrate = get_axis_max_feedrate(static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i), static_cast<Axis>(a));
1862                 if (axis_max_feedrate != 0.0f)
1863                     min_feedrate_factor = std::min(min_feedrate_factor, axis_max_feedrate / curr.abs_axis_feedrate[a]);
1864             }
1865         }
1866 
1867         block.feedrate_profile.cruise = min_feedrate_factor * curr.feedrate;
1868 
1869         if (min_feedrate_factor < 1.0f) {
1870             for (unsigned char a = X; a <= E; ++a) {
1871                 curr.axis_feedrate[a] *= min_feedrate_factor;
1872                 curr.abs_axis_feedrate[a] *= min_feedrate_factor;
1873             }
1874         }
1875 
1876         // calculates block acceleration
1877         float acceleration = is_extrusion_only_move(delta_pos) ?
1878             get_retract_acceleration(static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i)) :
1879             get_acceleration(static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i));
1880 
1881         for (unsigned char a = X; a <= E; ++a) {
1882             float axis_max_acceleration = get_axis_max_acceleration(static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i), static_cast<Axis>(a));
1883             if (acceleration * std::abs(delta_pos[a]) * inv_distance > axis_max_acceleration)
1884                 acceleration = axis_max_acceleration;
1885         }
1886 
1887         block.acceleration = acceleration;
1888 
1889         // calculates block exit feedrate
1890         curr.safe_feedrate = block.feedrate_profile.cruise;
1891 
1892         for (unsigned char a = X; a <= E; ++a) {
1893             float axis_max_jerk = get_axis_max_jerk(static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i), static_cast<Axis>(a));
1894             if (curr.abs_axis_feedrate[a] > axis_max_jerk)
1895                 curr.safe_feedrate = std::min(curr.safe_feedrate, axis_max_jerk);
1896         }
1897 
1898         block.feedrate_profile.exit = curr.safe_feedrate;
1899 
1900         static const float PREVIOUS_FEEDRATE_THRESHOLD = 0.0001f;
1901 
1902         // calculates block entry feedrate
1903         float vmax_junction = curr.safe_feedrate;
1904         if (!blocks.empty() && prev.feedrate > PREVIOUS_FEEDRATE_THRESHOLD) {
1905             bool prev_speed_larger = prev.feedrate > block.feedrate_profile.cruise;
1906             float smaller_speed_factor = prev_speed_larger ? (block.feedrate_profile.cruise / prev.feedrate) : (prev.feedrate / block.feedrate_profile.cruise);
1907             // Pick the smaller of the nominal speeds. Higher speed shall not be achieved at the junction during coasting.
1908             vmax_junction = prev_speed_larger ? block.feedrate_profile.cruise : prev.feedrate;
1909 
1910             float v_factor = 1.0f;
1911             bool limited = false;
1912 
1913             for (unsigned char a = X; a <= E; ++a) {
1914                 // Limit an axis. We have to differentiate coasting from the reversal of an axis movement, or a full stop.
1915                 float v_exit = prev.axis_feedrate[a];
1916                 float v_entry = curr.axis_feedrate[a];
1917 
1918                 if (prev_speed_larger)
1919                     v_exit *= smaller_speed_factor;
1920 
1921                 if (limited) {
1922                     v_exit *= v_factor;
1923                     v_entry *= v_factor;
1924                 }
1925 
1926                 // Calculate the jerk depending on whether the axis is coasting in the same direction or reversing a direction.
1927                 float jerk =
1928                     (v_exit > v_entry) ?
1929                     (((v_entry > 0.0f) || (v_exit < 0.0f)) ?
1930                         // coasting
1931                         (v_exit - v_entry) :
1932                         // axis reversal
1933                         std::max(v_exit, -v_entry)) :
1934                     // v_exit <= v_entry
1935                     (((v_entry < 0.0f) || (v_exit > 0.0f)) ?
1936                         // coasting
1937                         (v_entry - v_exit) :
1938                         // axis reversal
1939                         std::max(-v_exit, v_entry));
1940 
1941                 float axis_max_jerk = get_axis_max_jerk(static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i), static_cast<Axis>(a));
1942                 if (jerk > axis_max_jerk) {
1943                     v_factor *= axis_max_jerk / jerk;
1944                     limited = true;
1945                 }
1946             }
1947 
1948             if (limited)
1949                 vmax_junction *= v_factor;
1950 
1951             // Now the transition velocity is known, which maximizes the shared exit / entry velocity while
1952             // respecting the jerk factors, it may be possible, that applying separate safe exit / entry velocities will achieve faster prints.
1953             float vmax_junction_threshold = vmax_junction * 0.99f;
1954 
1955             // Not coasting. The machine will stop and start the movements anyway, better to start the segment from start.
1956             if ((prev.safe_feedrate > vmax_junction_threshold) && (curr.safe_feedrate > vmax_junction_threshold))
1957                 vmax_junction = curr.safe_feedrate;
1958         }
1959 
1960         float v_allowable = max_allowable_speed(-acceleration, curr.safe_feedrate, block.distance);
1961         block.feedrate_profile.entry = std::min(vmax_junction, v_allowable);
1962 
1963         block.max_entry_speed = vmax_junction;
1964         block.flags.nominal_length = (block.feedrate_profile.cruise <= v_allowable);
1965         block.flags.recalculate = true;
1966         block.safe_feedrate = curr.safe_feedrate;
1967 
1968         // calculates block trapezoid
1969         block.calculate_trapezoid();
1970 
1971         // updates previous
1972         prev = curr;
1973 
1974         blocks.push_back(block);
1975 
1976         if (blocks.size() > TimeProcessor::Planner::refresh_threshold)
1977             machine.calculate_time(TimeProcessor::Planner::queue_size);
1978     }
1979 
1980     // store move
1981     store_move_vertex(type);
1982 }
1983 
1984 void GCodeProcessor::process_G10(const GCodeReader::GCodeLine& line)
1985 {
1986     // stores retract move
1987     store_move_vertex(EMoveType::Retract);
1988 }
1989 
1990 void GCodeProcessor::process_G11(const GCodeReader::GCodeLine& line)
1991 {
1992     // stores unretract move
1993     store_move_vertex(EMoveType::Unretract);
1994 }
1995 
1996 void GCodeProcessor::process_G20(const GCodeReader::GCodeLine& line)
1997 {
1998     m_units = EUnits::Inches;
1999 }
2000 
2001 void GCodeProcessor::process_G21(const GCodeReader::GCodeLine& line)
2002 {
2003     m_units = EUnits::Millimeters;
2004 }
2005 
2006 void GCodeProcessor::process_G22(const GCodeReader::GCodeLine& line)
2007 {
2008     // stores retract move
2009     store_move_vertex(EMoveType::Retract);
2010 }
2011 
2012 void GCodeProcessor::process_G23(const GCodeReader::GCodeLine& line)
2013 {
2014     // stores unretract move
2015     store_move_vertex(EMoveType::Unretract);
2016 }
2017 
2018 void GCodeProcessor::process_G90(const GCodeReader::GCodeLine& line)
2019 {
2020     m_global_positioning_type = EPositioningType::Absolute;
2021 }
2022 
2023 void GCodeProcessor::process_G91(const GCodeReader::GCodeLine& line)
2024 {
2025     m_global_positioning_type = EPositioningType::Relative;
2026 }
2027 
2028 void GCodeProcessor::process_G92(const GCodeReader::GCodeLine& line)
2029 {
2030     float lengths_scale_factor = (m_units == EUnits::Inches) ? INCHES_TO_MM : 1.0f;
2031     bool any_found = false;
2032 
2033     if (line.has_x()) {
2034         m_origin[X] = m_end_position[X] - line.x() * lengths_scale_factor;
2035         any_found = true;
2036     }
2037 
2038     if (line.has_y()) {
2039         m_origin[Y] = m_end_position[Y] - line.y() * lengths_scale_factor;
2040         any_found = true;
2041     }
2042 
2043     if (line.has_z()) {
2044         m_origin[Z] = m_end_position[Z] - line.z() * lengths_scale_factor;
2045         any_found = true;
2046     }
2047 
2048     if (line.has_e()) {
2049         // extruder coordinate can grow to the point where its float representation does not allow for proper addition with small increments,
2050         // we set the value taken from the G92 line as the new current position for it
2051         m_end_position[E] = line.e() * lengths_scale_factor;
2052         any_found = true;
2053     }
2054     else
2055         simulate_st_synchronize();
2056 
2057     if (!any_found && !line.has_unknown_axis()) {
2058         // The G92 may be called for axes that PrusaSlicer does not recognize, for example see GH issue #3510,
2059         // where G92 A0 B0 is called although the extruder axis is till E.
2060         for (unsigned char a = X; a <= E; ++a) {
2061             m_origin[a] = m_end_position[a];
2062         }
2063     }
2064 }
2065 
2066 void GCodeProcessor::process_M1(const GCodeReader::GCodeLine& line)
2067 {
2068     simulate_st_synchronize();
2069 }
2070 
2071 void GCodeProcessor::process_M82(const GCodeReader::GCodeLine& line)
2072 {
2073     m_e_local_positioning_type = EPositioningType::Absolute;
2074 }
2075 
2076 void GCodeProcessor::process_M83(const GCodeReader::GCodeLine& line)
2077 {
2078     m_e_local_positioning_type = EPositioningType::Relative;
2079 }
2080 
2081 void GCodeProcessor::process_M106(const GCodeReader::GCodeLine& line)
2082 {
2083     if (!line.has('P')) {
2084         // The absence of P means the print cooling fan, so ignore anything else.
2085         float new_fan_speed;
2086         if (line.has_value('S', new_fan_speed))
2087             m_fan_speed = (100.0f / 255.0f) * new_fan_speed;
2088         else
2089             m_fan_speed = 100.0f;
2090     }
2091 }
2092 
2093 void GCodeProcessor::process_M107(const GCodeReader::GCodeLine& line)
2094 {
2095     m_fan_speed = 0.0f;
2096 }
2097 
2098 void GCodeProcessor::process_M108(const GCodeReader::GCodeLine& line)
2099 {
2100     // These M-codes are used by Sailfish to change active tool.
2101     // They have to be processed otherwise toolchanges will be unrecognised
2102     // by the analyzer - see https://github.com/prusa3d/PrusaSlicer/issues/2566
2103 
2104     if (m_flavor != gcfSailfish)
2105         return;
2106 
2107     std::string cmd = line.raw();
2108     size_t pos = cmd.find("T");
2109     if (pos != std::string::npos)
2110         process_T(cmd.substr(pos));
2111 }
2112 
2113 void GCodeProcessor::process_M132(const GCodeReader::GCodeLine& line)
2114 {
2115     // This command is used by Makerbot to load the current home position from EEPROM
2116     // see: https://github.com/makerbot/s3g/blob/master/doc/GCodeProtocol.md
2117     // Using this command to reset the axis origin to zero helps in fixing: https://github.com/prusa3d/PrusaSlicer/issues/3082
2118 
2119     if (line.has_x())
2120         m_origin[X] = 0.0f;
2121 
2122     if (line.has_y())
2123         m_origin[Y] = 0.0f;
2124 
2125     if (line.has_z())
2126         m_origin[Z] = 0.0f;
2127 
2128     if (line.has_e())
2129         m_origin[E] = 0.0f;
2130 }
2131 
2132 void GCodeProcessor::process_M135(const GCodeReader::GCodeLine& line)
2133 {
2134     // These M-codes are used by MakerWare to change active tool.
2135     // They have to be processed otherwise toolchanges will be unrecognised
2136     // by the analyzer - see https://github.com/prusa3d/PrusaSlicer/issues/2566
2137 
2138     if (m_flavor != gcfMakerWare)
2139         return;
2140 
2141     std::string cmd = line.raw();
2142     size_t pos = cmd.find("T");
2143     if (pos != std::string::npos)
2144         process_T(cmd.substr(pos));
2145 }
2146 
2147 void GCodeProcessor::process_M201(const GCodeReader::GCodeLine& line)
2148 {
2149     // see http://reprap.org/wiki/G-code#M201:_Set_max_printing_acceleration
2150     float factor = ((m_flavor != gcfRepRapSprinter && m_flavor != gcfRepRapFirmware) && m_units == EUnits::Inches) ? INCHES_TO_MM : 1.0f;
2151 
2152     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
2153         if (static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i) == PrintEstimatedTimeStatistics::ETimeMode::Normal ||
2154             m_time_processor.machine_envelope_processing_enabled) {
2155             if (line.has_x())
2156                 set_option_value(m_time_processor.machine_limits.machine_max_acceleration_x, i, line.x() * factor);
2157 
2158             if (line.has_y())
2159                 set_option_value(m_time_processor.machine_limits.machine_max_acceleration_y, i, line.y() * factor);
2160 
2161             if (line.has_z())
2162                 set_option_value(m_time_processor.machine_limits.machine_max_acceleration_z, i, line.z() * factor);
2163 
2164             if (line.has_e())
2165                 set_option_value(m_time_processor.machine_limits.machine_max_acceleration_e, i, line.e() * factor);
2166         }
2167     }
2168 }
2169 
2170 void GCodeProcessor::process_M203(const GCodeReader::GCodeLine& line)
2171 {
2172     // see http://reprap.org/wiki/G-code#M203:_Set_maximum_feedrate
2173     if (m_flavor == gcfRepetier)
2174         return;
2175 
2176     // see http://reprap.org/wiki/G-code#M203:_Set_maximum_feedrate
2177     // http://smoothieware.org/supported-g-codes
2178     float factor = (m_flavor == gcfMarlin || m_flavor == gcfSmoothie) ? 1.0f : MMMIN_TO_MMSEC;
2179 
2180     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
2181         if (static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i) == PrintEstimatedTimeStatistics::ETimeMode::Normal ||
2182             m_time_processor.machine_envelope_processing_enabled) {
2183             if (line.has_x())
2184                 set_option_value(m_time_processor.machine_limits.machine_max_feedrate_x, i, line.x() * factor);
2185 
2186             if (line.has_y())
2187                 set_option_value(m_time_processor.machine_limits.machine_max_feedrate_y, i, line.y() * factor);
2188 
2189             if (line.has_z())
2190                 set_option_value(m_time_processor.machine_limits.machine_max_feedrate_z, i, line.z() * factor);
2191 
2192             if (line.has_e())
2193                 set_option_value(m_time_processor.machine_limits.machine_max_feedrate_e, i, line.e() * factor);
2194         }
2195     }
2196 }
2197 
2198 void GCodeProcessor::process_M204(const GCodeReader::GCodeLine& line)
2199 {
2200     float value;
2201     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
2202         if (static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i) == PrintEstimatedTimeStatistics::ETimeMode::Normal ||
2203             m_time_processor.machine_envelope_processing_enabled) {
2204             if (line.has_value('S', value)) {
2205                 // Legacy acceleration format. This format is used by the legacy Marlin, MK2 or MK3 firmware,
2206                 // and it is also generated by Slic3r to control acceleration per extrusion type
2207                 // (there is a separate acceleration settings in Slicer for perimeter, first layer etc).
2208                 set_acceleration(static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i), value);
2209                 if (line.has_value('T', value))
2210                     set_option_value(m_time_processor.machine_limits.machine_max_acceleration_retracting, i, value);
2211             }
2212             else {
2213                 // New acceleration format, compatible with the upstream Marlin.
2214                 if (line.has_value('P', value))
2215                     set_acceleration(static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i), value);
2216                 if (line.has_value('R', value))
2217                     set_option_value(m_time_processor.machine_limits.machine_max_acceleration_retracting, i, value);
2218                 if (line.has_value('T', value)) {
2219                     // Interpret the T value as the travel acceleration in the new Marlin format.
2220                     //FIXME Prusa3D firmware currently does not support travel acceleration value independent from the extruding acceleration value.
2221                     // set_travel_acceleration(value);
2222                 }
2223             }
2224         }
2225     }
2226 }
2227 
2228 void GCodeProcessor::process_M205(const GCodeReader::GCodeLine& line)
2229 {
2230     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
2231         if (static_cast<PrintEstimatedTimeStatistics::ETimeMode>(i) == PrintEstimatedTimeStatistics::ETimeMode::Normal ||
2232             m_time_processor.machine_envelope_processing_enabled) {
2233             if (line.has_x()) {
2234                 float max_jerk = line.x();
2235                 set_option_value(m_time_processor.machine_limits.machine_max_jerk_x, i, max_jerk);
2236                 set_option_value(m_time_processor.machine_limits.machine_max_jerk_y, i, max_jerk);
2237             }
2238 
2239             if (line.has_y())
2240                 set_option_value(m_time_processor.machine_limits.machine_max_jerk_y, i, line.y());
2241 
2242             if (line.has_z())
2243                 set_option_value(m_time_processor.machine_limits.machine_max_jerk_z, i, line.z());
2244 
2245             if (line.has_e())
2246                 set_option_value(m_time_processor.machine_limits.machine_max_jerk_e, i, line.e());
2247 
2248             float value;
2249             if (line.has_value('S', value))
2250                 set_option_value(m_time_processor.machine_limits.machine_min_extruding_rate, i, value);
2251 
2252             if (line.has_value('T', value))
2253                 set_option_value(m_time_processor.machine_limits.machine_min_travel_rate, i, value);
2254         }
2255     }
2256 }
2257 
2258 void GCodeProcessor::process_M221(const GCodeReader::GCodeLine& line)
2259 {
2260     float value_s;
2261     float value_t;
2262     if (line.has_value('S', value_s) && !line.has_value('T', value_t)) {
2263         value_s *= 0.01f;
2264         for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
2265             m_time_processor.machines[i].extrude_factor_override_percentage = value_s;
2266         }
2267     }
2268 }
2269 
2270 void GCodeProcessor::process_M401(const GCodeReader::GCodeLine& line)
2271 {
2272     if (m_flavor != gcfRepetier)
2273         return;
2274 
2275     for (unsigned char a = 0; a <= 3; ++a) {
2276         m_cached_position.position[a] = m_start_position[a];
2277     }
2278     m_cached_position.feedrate = m_feedrate;
2279 }
2280 
2281 void GCodeProcessor::process_M402(const GCodeReader::GCodeLine& line)
2282 {
2283     if (m_flavor != gcfRepetier)
2284         return;
2285 
2286     // see for reference:
2287     // https://github.com/repetier/Repetier-Firmware/blob/master/src/ArduinoAVR/Repetier/Printer.cpp
2288     // void Printer::GoToMemoryPosition(bool x, bool y, bool z, bool e, float feed)
2289 
2290     bool has_xyz = !(line.has_x() || line.has_y() || line.has_z());
2291 
2292     float p = FLT_MAX;
2293     for (unsigned char a = X; a <= Z; ++a) {
2294         if (has_xyz || line.has(a)) {
2295             p = m_cached_position.position[a];
2296             if (p != FLT_MAX)
2297                 m_start_position[a] = p;
2298         }
2299     }
2300 
2301     p = m_cached_position.position[E];
2302     if (p != FLT_MAX)
2303         m_start_position[E] = p;
2304 
2305     p = FLT_MAX;
2306     if (!line.has_value(4, p))
2307         p = m_cached_position.feedrate;
2308 
2309     if (p != FLT_MAX)
2310         m_feedrate = p;
2311 }
2312 
2313 void GCodeProcessor::process_M566(const GCodeReader::GCodeLine& line)
2314 {
2315     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
2316         if (line.has_x())
2317             set_option_value(m_time_processor.machine_limits.machine_max_jerk_x, i, line.x() * MMMIN_TO_MMSEC);
2318 
2319         if (line.has_y())
2320             set_option_value(m_time_processor.machine_limits.machine_max_jerk_y, i, line.y() * MMMIN_TO_MMSEC);
2321 
2322         if (line.has_z())
2323             set_option_value(m_time_processor.machine_limits.machine_max_jerk_z, i, line.z() * MMMIN_TO_MMSEC);
2324 
2325         if (line.has_e())
2326             set_option_value(m_time_processor.machine_limits.machine_max_jerk_e, i, line.e() * MMMIN_TO_MMSEC);
2327     }
2328 }
2329 
2330 void GCodeProcessor::process_M702(const GCodeReader::GCodeLine& line)
2331 {
2332     if (line.has('C')) {
2333         // MK3 MMU2 specific M code:
2334         // M702 C is expected to be sent by the custom end G-code when finalizing a print.
2335         // The MK3 unit shall unload and park the active filament into the MMU2 unit.
2336         m_time_processor.extruder_unloaded = true;
2337         simulate_st_synchronize(get_filament_unload_time(m_extruder_id));
2338     }
2339 }
2340 
2341 void GCodeProcessor::process_T(const GCodeReader::GCodeLine& line)
2342 {
2343     process_T(line.cmd());
2344 }
2345 
2346 void GCodeProcessor::process_T(const std::string_view command)
2347 {
2348     if (command.length() > 1) {
2349         int eid;
2350         if (! parse_number(command.substr(1), eid) || eid < 0 || eid > 255) {
2351             // T-1 is a valid gcode line for RepRap Firmwares (used to deselects all tools) see https://github.com/prusa3d/PrusaSlicer/issues/5677
2352             if ((m_flavor != gcfRepRapFirmware && m_flavor != gcfRepRapSprinter) || eid != -1)
2353                 BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid toolchange (" << command << ").";
2354         } else {
2355             unsigned char id = static_cast<unsigned char>(eid);
2356             if (m_extruder_id != id) {
2357                 unsigned char extruders_count = static_cast<unsigned char>(m_extruder_offsets.size());
2358                 if (id >= extruders_count)
2359                     BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid toolchange, maybe from a custom gcode.";
2360                 else {
2361                     unsigned char old_extruder_id = m_extruder_id;
2362                     m_extruder_id = id;
2363                     m_cp_color.current = m_extruder_colors[id];
2364                     // Specific to the MK3 MMU2:
2365                     // The initial value of extruder_unloaded is set to true indicating
2366                     // that the filament is parked in the MMU2 unit and there is nothing to be unloaded yet.
2367                     float extra_time = get_filament_unload_time(static_cast<size_t>(old_extruder_id));
2368                     m_time_processor.extruder_unloaded = false;
2369                     extra_time += get_filament_load_time(static_cast<size_t>(m_extruder_id));
2370                     simulate_st_synchronize(extra_time);
2371                 }
2372 
2373                 // store tool change move
2374                 store_move_vertex(EMoveType::Tool_change);
2375             }
2376         }
2377     }
2378 }
2379 
2380 void GCodeProcessor::store_move_vertex(EMoveType type)
2381 {
2382     MoveVertex vertex = {
2383         type,
2384         m_extrusion_role,
2385         m_extruder_id,
2386         m_cp_color.current,
2387         Vec3f(m_end_position[X], m_end_position[Y], m_end_position[Z]) + m_extruder_offsets[m_extruder_id],
2388         m_end_position[E] - m_start_position[E],
2389         m_feedrate,
2390         m_width,
2391         m_height,
2392         m_mm3_per_mm,
2393         m_fan_speed,
2394         static_cast<float>(m_result.moves.size())
2395     };
2396     m_result.moves.emplace_back(vertex);
2397 }
2398 
2399 float GCodeProcessor::minimum_feedrate(PrintEstimatedTimeStatistics::ETimeMode mode, float feedrate) const
2400 {
2401     if (m_time_processor.machine_limits.machine_min_extruding_rate.empty())
2402         return feedrate;
2403 
2404     return std::max(feedrate, get_option_value(m_time_processor.machine_limits.machine_min_extruding_rate, static_cast<size_t>(mode)));
2405 }
2406 
2407 float GCodeProcessor::minimum_travel_feedrate(PrintEstimatedTimeStatistics::ETimeMode mode, float feedrate) const
2408 {
2409     if (m_time_processor.machine_limits.machine_min_travel_rate.empty())
2410         return feedrate;
2411 
2412     return std::max(feedrate, get_option_value(m_time_processor.machine_limits.machine_min_travel_rate, static_cast<size_t>(mode)));
2413 }
2414 
2415 float GCodeProcessor::get_axis_max_feedrate(PrintEstimatedTimeStatistics::ETimeMode mode, Axis axis) const
2416 {
2417     switch (axis)
2418     {
2419     case X: { return get_option_value(m_time_processor.machine_limits.machine_max_feedrate_x, static_cast<size_t>(mode)); }
2420     case Y: { return get_option_value(m_time_processor.machine_limits.machine_max_feedrate_y, static_cast<size_t>(mode)); }
2421     case Z: { return get_option_value(m_time_processor.machine_limits.machine_max_feedrate_z, static_cast<size_t>(mode)); }
2422     case E: { return get_option_value(m_time_processor.machine_limits.machine_max_feedrate_e, static_cast<size_t>(mode)); }
2423     default: { return 0.0f; }
2424     }
2425 }
2426 
2427 float GCodeProcessor::get_axis_max_acceleration(PrintEstimatedTimeStatistics::ETimeMode mode, Axis axis) const
2428 {
2429     switch (axis)
2430     {
2431     case X: { return get_option_value(m_time_processor.machine_limits.machine_max_acceleration_x, static_cast<size_t>(mode)); }
2432     case Y: { return get_option_value(m_time_processor.machine_limits.machine_max_acceleration_y, static_cast<size_t>(mode)); }
2433     case Z: { return get_option_value(m_time_processor.machine_limits.machine_max_acceleration_z, static_cast<size_t>(mode)); }
2434     case E: { return get_option_value(m_time_processor.machine_limits.machine_max_acceleration_e, static_cast<size_t>(mode)); }
2435     default: { return 0.0f; }
2436     }
2437 }
2438 
2439 float GCodeProcessor::get_axis_max_jerk(PrintEstimatedTimeStatistics::ETimeMode mode, Axis axis) const
2440 {
2441     switch (axis)
2442     {
2443     case X: { return get_option_value(m_time_processor.machine_limits.machine_max_jerk_x, static_cast<size_t>(mode)); }
2444     case Y: { return get_option_value(m_time_processor.machine_limits.machine_max_jerk_y, static_cast<size_t>(mode)); }
2445     case Z: { return get_option_value(m_time_processor.machine_limits.machine_max_jerk_z, static_cast<size_t>(mode)); }
2446     case E: { return get_option_value(m_time_processor.machine_limits.machine_max_jerk_e, static_cast<size_t>(mode)); }
2447     default: { return 0.0f; }
2448     }
2449 }
2450 
2451 float GCodeProcessor::get_retract_acceleration(PrintEstimatedTimeStatistics::ETimeMode mode) const
2452 {
2453     return get_option_value(m_time_processor.machine_limits.machine_max_acceleration_retracting, static_cast<size_t>(mode));
2454 }
2455 
2456 float GCodeProcessor::get_acceleration(PrintEstimatedTimeStatistics::ETimeMode mode) const
2457 {
2458     size_t id = static_cast<size_t>(mode);
2459     return (id < m_time_processor.machines.size()) ? m_time_processor.machines[id].acceleration : DEFAULT_ACCELERATION;
2460 }
2461 
2462 void GCodeProcessor::set_acceleration(PrintEstimatedTimeStatistics::ETimeMode mode, float value)
2463 {
2464     size_t id = static_cast<size_t>(mode);
2465     if (id < m_time_processor.machines.size()) {
2466         m_time_processor.machines[id].acceleration = (m_time_processor.machines[id].max_acceleration == 0.0f) ? value :
2467             // Clamp the acceleration with the maximum.
2468             std::min(value, m_time_processor.machines[id].max_acceleration);
2469     }
2470 }
2471 
2472 float GCodeProcessor::get_filament_load_time(size_t extruder_id)
2473 {
2474     return (m_time_processor.filament_load_times.empty() || m_time_processor.extruder_unloaded) ?
2475         0.0f :
2476         ((extruder_id < m_time_processor.filament_load_times.size()) ?
2477             m_time_processor.filament_load_times[extruder_id] : m_time_processor.filament_load_times.front());
2478 }
2479 
2480 float GCodeProcessor::get_filament_unload_time(size_t extruder_id)
2481 {
2482     return (m_time_processor.filament_unload_times.empty() || m_time_processor.extruder_unloaded) ?
2483         0.0f :
2484         ((extruder_id < m_time_processor.filament_unload_times.size()) ?
2485             m_time_processor.filament_unload_times[extruder_id] : m_time_processor.filament_unload_times.front());
2486 }
2487 
2488 void GCodeProcessor::process_custom_gcode_time(CustomGCode::Type code)
2489 {
2490     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
2491         TimeMachine& machine = m_time_processor.machines[i];
2492         if (!machine.enabled)
2493             continue;
2494 
2495         TimeMachine::CustomGCodeTime& gcode_time = machine.gcode_time;
2496         gcode_time.needed = true;
2497         //FIXME this simulates st_synchronize! is it correct?
2498         // The estimated time may be longer than the real print time.
2499         machine.simulate_st_synchronize();
2500         if (gcode_time.cache != 0.0f) {
2501             gcode_time.times.push_back({ code, gcode_time.cache });
2502             gcode_time.cache = 0.0f;
2503         }
2504     }
2505 }
2506 
2507 void GCodeProcessor::simulate_st_synchronize(float additional_time)
2508 {
2509     for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {
2510         m_time_processor.machines[i].simulate_st_synchronize(additional_time);
2511     }
2512 }
2513 
2514 void GCodeProcessor::update_estimated_times_stats()
2515 {
2516     auto update_mode = [this](PrintEstimatedTimeStatistics::ETimeMode mode) {
2517         PrintEstimatedTimeStatistics::Mode& data = m_result.time_statistics.modes[static_cast<size_t>(mode)];
2518         data.time = get_time(mode);
2519         data.custom_gcode_times = get_custom_gcode_times(mode, true);
2520         data.moves_times = get_moves_time(mode);
2521         data.roles_times = get_roles_time(mode);
2522         data.layers_times = get_layers_time(mode);
2523     };
2524 
2525     update_mode(PrintEstimatedTimeStatistics::ETimeMode::Normal);
2526     if (m_time_processor.machines[static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Stealth)].enabled)
2527         update_mode(PrintEstimatedTimeStatistics::ETimeMode::Stealth);
2528     else
2529         m_result.time_statistics.modes[static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Stealth)].reset();
2530 }
2531 
2532 } /* namespace Slic3r */
2533 
2534