GCodeProcessor -> Added cancel callback

This commit is contained in:
enricoturri1966 2020-09-03 08:32:06 +02:00
parent 0cfa64e245
commit 573194e059
3 changed files with 16 additions and 7 deletions

View File

@ -787,7 +787,7 @@ void GCode::do_export(Print* print, const char* path, GCodePreviewData* preview_
}
#if ENABLE_GCODE_VIEWER
m_processor.process_file(path_tmp);
m_processor.process_file(path_tmp, [print]() { print->throw_if_canceled(); });
DoExport::update_print_estimated_times_stats(m_processor, print->m_print_statistics);
if (result != nullptr)
*result = std::move(m_processor.extract_result());

View File

@ -11,10 +11,7 @@
#include <assert.h>
#if ENABLE_GCODE_VIEWER
#if ENABLE_GCODE_VIEWER_STATISTICS
#include <chrono>
#endif // ENABLE_GCODE_VIEWER_STATISTICS
static const float INCHES_TO_MM = 25.4f;
static const float MMMIN_TO_MMSEC = 1.0f / 60.0f;
@ -730,8 +727,10 @@ void GCodeProcessor::reset()
#endif // ENABLE_GCODE_VIEWER_DATA_CHECKING
}
void GCodeProcessor::process_file(const std::string& filename)
void GCodeProcessor::process_file(const std::string& filename, std::function<void()> cancel_callback)
{
auto last_cancel_callback_time = std::chrono::high_resolution_clock::now();
#if ENABLE_GCODE_VIEWER_STATISTICS
auto start_time = std::chrono::high_resolution_clock::now();
#endif // ENABLE_GCODE_VIEWER_STATISTICS
@ -758,9 +757,18 @@ void GCodeProcessor::process_file(const std::string& filename)
}
}
// process gcode
m_result.id = ++s_result_id;
m_result.moves.emplace_back(MoveVertex());
m_parser.parse_file(filename, [this](GCodeReader& reader, const GCodeReader::GCodeLine& line) { process_gcode_line(line); });
m_parser.parse_file(filename, [this, cancel_callback, &last_cancel_callback_time](GCodeReader& reader, const GCodeReader::GCodeLine& line) {
auto curr_time = std::chrono::high_resolution_clock::now();
// call the cancel callback every 100 ms
if (std::chrono::duration_cast<std::chrono::milliseconds>(curr_time - last_cancel_callback_time).count() > 100) {
cancel_callback();
last_cancel_callback_time = curr_time;
}
process_gcode_line(line);
});
// process the time blocks
for (size_t i = 0; i < static_cast<size_t>(PrintEstimatedTimeStatistics::ETimeMode::Count); ++i) {

View File

@ -419,7 +419,8 @@ namespace Slic3r {
Result&& extract_result() { return std::move(m_result); }
// Process the gcode contained in the file with the given filename
void process_file(const std::string& filename);
// throws CanceledException through print->throw_if_canceled() (sent by the caller as callback).
void process_file(const std::string& filename, std::function<void()> cancel_callback = std::function<void()>());
float get_time(PrintEstimatedTimeStatistics::ETimeMode mode) const;
std::string get_time_dhm(PrintEstimatedTimeStatistics::ETimeMode mode) const;