GCodeProcessor collects positions of line ends for GCodeViewer,

GCodeViewer no more parses G-code just to extract line end positions.
Removed start_mapping_gcode_window(), void stop_mapping_gcode_window(),
they are no more needed.
This commit is contained in:
Vojtech Bubnik 2021-09-07 15:42:50 +02:00
parent 719c91514b
commit 32733b7db9
9 changed files with 30 additions and 92 deletions

View file

@ -354,7 +354,7 @@ struct FilePtr {
FILE* f = nullptr; FILE* f = nullptr;
}; };
void GCodeProcessor::TimeProcessor::post_process(const std::string& filename, std::vector<MoveVertex>& moves) void GCodeProcessor::TimeProcessor::post_process(const std::string& filename, std::vector<MoveVertex>& moves, std::vector<size_t>& lines_ends)
{ {
FilePtr in{ boost::nowide::fopen(filename.c_str(), "rb") }; FilePtr in{ boost::nowide::fopen(filename.c_str(), "rb") };
if (in.f == nullptr) if (in.f == nullptr)
@ -567,13 +567,19 @@ void GCodeProcessor::TimeProcessor::post_process(const std::string& filename, st
}; };
// helper function to write to disk // helper function to write to disk
auto write_string = [&export_line, &out, &out_path](const std::string& str) { size_t out_file_pos = 0;
lines_ends.clear();
auto write_string = [&export_line, &out, &out_path, &out_file_pos, &lines_ends](const std::string& str) {
fwrite((const void*)export_line.c_str(), 1, export_line.length(), out.f); fwrite((const void*)export_line.c_str(), 1, export_line.length(), out.f);
if (ferror(out.f)) { if (ferror(out.f)) {
out.close(); out.close();
boost::nowide::remove(out_path.c_str()); boost::nowide::remove(out_path.c_str());
throw Slic3r::RuntimeError(std::string("Time estimator post process export failed.\nIs the disk full?\n")); throw Slic3r::RuntimeError(std::string("Time estimator post process export failed.\nIs the disk full?\n"));
} }
for (size_t i = 0; i < export_line.size(); ++ i)
if (export_line[i] == '\n')
lines_ends.emplace_back(out_file_pos + i + 1);
out_file_pos += export_line.size();
export_line.clear(); export_line.clear();
}; };
@ -736,7 +742,9 @@ void GCodeProcessor::Result::reset() {
} }
#else #else
void GCodeProcessor::Result::reset() { void GCodeProcessor::Result::reset() {
moves = std::vector<GCodeProcessor::MoveVertex>();
moves.clear();
lines_ends.clear();
bed_shape = Pointfs(); bed_shape = Pointfs();
settings_ids.reset(); settings_ids.reset();
extruders_count = 0; extruders_count = 0;
@ -1270,7 +1278,7 @@ void GCodeProcessor::process_file(const std::string& filename, bool apply_postpr
// post-process to add M73 lines into the gcode // post-process to add M73 lines into the gcode
if (apply_postprocess) if (apply_postprocess)
m_time_processor.post_process(filename, m_result.moves); m_time_processor.post_process(filename, m_result.moves, m_result.lines_ends);
#if ENABLE_GCODE_VIEWER_DATA_CHECKING #if ENABLE_GCODE_VIEWER_DATA_CHECKING
std::cout << "\n"; std::cout << "\n";

View file

@ -306,7 +306,7 @@ namespace Slic3r {
// post process the file with the given filename to add remaining time lines M73 // post process the file with the given filename to add remaining time lines M73
// and updates moves' gcode ids accordingly // and updates moves' gcode ids accordingly
void post_process(const std::string& filename, std::vector<MoveVertex>& moves); void post_process(const std::string& filename, std::vector<MoveVertex>& moves, std::vector<size_t>& lines_ends);
}; };
struct UsedFilaments // filaments per ColorChange struct UsedFilaments // filaments per ColorChange
@ -350,6 +350,8 @@ namespace Slic3r {
std::string filename; std::string filename;
unsigned int id; unsigned int id;
std::vector<MoveVertex> moves; std::vector<MoveVertex> moves;
// Positions of ends of lines of the final G-code this->filename after TimeProcessor::post_process() finalizes the G-code.
std::vector<size_t> lines_ends;
Pointfs bed_shape; Pointfs bed_shape;
SettingsIds settings_ids; SettingsIds settings_ids;
size_t extruders_count; size_t extruders_count;

View file

@ -574,11 +574,8 @@ Print::ApplyStatus BackgroundSlicingProcess::apply(const Model &model, const Dyn
// Some FFF status was invalidated, and the G-code was not exported yet. // Some FFF status was invalidated, and the G-code was not exported yet.
// Let the G-code preview UI know that the final G-code preview is not valid. // Let the G-code preview UI know that the final G-code preview is not valid.
// In addition, this early memory deallocation reduces memory footprint. // In addition, this early memory deallocation reduces memory footprint.
if (m_gcode_result != nullptr) { if (m_gcode_result != nullptr)
//FIXME calling platter from here is not a staple of a good architecture.
GUI::wxGetApp().plater()->stop_mapping_gcode_window();
m_gcode_result->reset(); m_gcode_result->reset();
}
} }
return invalidated; return invalidated;
} }

View file

@ -284,45 +284,14 @@ void GCodeViewer::SequentialView::Marker::render() const
ImGui::PopStyleVar(); ImGui::PopStyleVar();
} }
void GCodeViewer::SequentialView::GCodeWindow::load_gcode() void GCodeViewer::SequentialView::GCodeWindow::load_gcode(const std::string& filename, const std::vector<size_t> &lines_ends)
{ {
if (m_filename.empty()) assert(! m_file.is_open());
return;
if (m_file.is_open()) if (m_file.is_open())
return; return;
try m_filename = filename;
{ m_lines_ends = std::move(lines_ends);
// generate mapping for accessing data in file by line number
boost::nowide::ifstream f(m_filename);
f.seekg(0, f.end);
uint64_t file_length = static_cast<uint64_t>(f.tellg());
f.seekg(0, f.beg);
std::string line;
uint64_t offset = 0;
while (std::getline(f, line)) {
uint64_t line_length = static_cast<uint64_t>(line.length());
m_lines_map.push_back({ offset, line_length });
offset += static_cast<uint64_t>(line_length) + 1;
}
if (offset != file_length) {
// if the final offset does not match with file length, lines are terminated with CR+LF
// so update all offsets accordingly
for (size_t i = 0; i < m_lines_map.size(); ++i) {
m_lines_map[i].first += static_cast<uint64_t>(i);
}
}
}
catch (...)
{
BOOST_LOG_TRIVIAL(error) << "Unable to load data from " << m_filename << ". Cannot show G-code window.";
reset();
return;
}
m_selected_line_id = 0; m_selected_line_id = 0;
m_last_lines_size = 0; m_last_lines_size = 0;
@ -345,7 +314,9 @@ void GCodeViewer::SequentialView::GCodeWindow::render(float top, float bottom, u
ret.reserve(end_id - start_id + 1); ret.reserve(end_id - start_id + 1);
for (uint64_t id = start_id; id <= end_id; ++id) { for (uint64_t id = start_id; id <= end_id; ++id) {
// read line from file // read line from file
std::string gline(m_file.data() + m_lines_map[id - 1].first, m_lines_map[id - 1].second); const size_t start = id == 1 ? 0 : m_lines_ends[id - 2];
const size_t len = m_lines_ends[id - 1] - start;
std::string gline(m_file.data() + start, len);
std::string command; std::string command;
std::string parameters; std::string parameters;
@ -379,7 +350,7 @@ void GCodeViewer::SequentialView::GCodeWindow::render(float top, float bottom, u
static const ImVec4 PARAMETERS_COLOR = { 1.0f, 1.0f, 1.0f, 1.0f }; static const ImVec4 PARAMETERS_COLOR = { 1.0f, 1.0f, 1.0f, 1.0f };
static const ImVec4 COMMENT_COLOR = { 0.7f, 0.7f, 0.7f, 1.0f }; static const ImVec4 COMMENT_COLOR = { 0.7f, 0.7f, 0.7f, 1.0f };
if (!m_visible || m_filename.empty() || m_lines_map.empty() || curr_line_id == 0) if (!m_visible || m_filename.empty() || m_lines_ends.empty() || curr_line_id == 0)
return; return;
// window height // window height
@ -397,8 +368,8 @@ void GCodeViewer::SequentialView::GCodeWindow::render(float top, float bottom, u
const uint64_t half_lines_count = lines_count / 2; const uint64_t half_lines_count = lines_count / 2;
uint64_t start_id = (curr_line_id >= half_lines_count) ? curr_line_id - half_lines_count : 0; uint64_t start_id = (curr_line_id >= half_lines_count) ? curr_line_id - half_lines_count : 0;
uint64_t end_id = start_id + lines_count - 1; uint64_t end_id = start_id + lines_count - 1;
if (end_id >= static_cast<uint64_t>(m_lines_map.size())) { if (end_id >= static_cast<uint64_t>(m_lines_ends.size())) {
end_id = static_cast<uint64_t>(m_lines_map.size()) - 1; end_id = static_cast<uint64_t>(m_lines_ends.size()) - 1;
start_id = end_id - lines_count + 1; start_id = end_id - lines_count + 1;
} }
@ -606,8 +577,7 @@ void GCodeViewer::load(const GCodeProcessor::Result& gcode_result, const Print&
// release gpu memory, if used // release gpu memory, if used
reset(); reset();
m_sequential_view.gcode_window.set_filename(gcode_result.filename); m_sequential_view.gcode_window.load_gcode(gcode_result.filename, gcode_result.lines_ends);
m_sequential_view.gcode_window.load_gcode();
#if ENABLE_FIX_IMPORTING_COLOR_PRINT_VIEW_INTO_GCODEVIEWER #if ENABLE_FIX_IMPORTING_COLOR_PRINT_VIEW_INTO_GCODEVIEWER
if (wxGetApp().is_gcode_viewer()) if (wxGetApp().is_gcode_viewer())
@ -1146,16 +1116,6 @@ void GCodeViewer::export_toolpaths_to_obj(const char* filename) const
fclose(fp); fclose(fp);
} }
void GCodeViewer::start_mapping_gcode_window()
{
m_sequential_view.gcode_window.load_gcode();
}
void GCodeViewer::stop_mapping_gcode_window()
{
m_sequential_view.gcode_window.stop_mapping_file();
}
void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result) void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result)
{ {
// max index buffer size, in bytes // max index buffer size, in bytes

View file

@ -639,18 +639,17 @@ public:
std::string m_filename; std::string m_filename;
boost::iostreams::mapped_file_source m_file; boost::iostreams::mapped_file_source m_file;
// map for accessing data in file by line number // map for accessing data in file by line number
std::vector<std::pair<uint64_t, uint64_t>> m_lines_map; std::vector<size_t> m_lines_ends;
// current visible lines // current visible lines
std::vector<Line> m_lines; std::vector<Line> m_lines;
public: public:
GCodeWindow() = default; GCodeWindow() = default;
~GCodeWindow() { stop_mapping_file(); } ~GCodeWindow() { stop_mapping_file(); }
void set_filename(const std::string& filename) { m_filename = filename; } void load_gcode(const std::string& filename, const std::vector<size_t> &lines_ends);
void load_gcode();
void reset() { void reset() {
stop_mapping_file(); stop_mapping_file();
m_lines_map.clear(); m_lines_ends.clear();
m_lines.clear(); m_lines.clear();
m_filename.clear(); m_filename.clear();
} }
@ -777,8 +776,6 @@ public:
void export_toolpaths_to_obj(const char* filename) const; void export_toolpaths_to_obj(const char* filename) const;
void start_mapping_gcode_window();
void stop_mapping_gcode_window();
void toggle_gcode_window_visibility() { m_sequential_view.gcode_window.toggle_visibility(); } void toggle_gcode_window_visibility() { m_sequential_view.gcode_window.toggle_visibility(); }
#if ENABLE_FIX_IMPORTING_COLOR_PRINT_VIEW_INTO_GCODEVIEWER #if ENABLE_FIX_IMPORTING_COLOR_PRINT_VIEW_INTO_GCODEVIEWER

View file

@ -1114,16 +1114,6 @@ int GLCanvas3D::check_volumes_outside_state() const
return (int)state; return (int)state;
} }
void GLCanvas3D::start_mapping_gcode_window()
{
m_gcode_viewer.start_mapping_gcode_window();
}
void GLCanvas3D::stop_mapping_gcode_window()
{
m_gcode_viewer.stop_mapping_gcode_window();
}
void GLCanvas3D::toggle_sla_auxiliaries_visibility(bool visible, const ModelObject* mo, int instance_idx) void GLCanvas3D::toggle_sla_auxiliaries_visibility(bool visible, const ModelObject* mo, int instance_idx)
{ {
m_render_sla_auxiliaries = visible; m_render_sla_auxiliaries = visible;

View file

@ -621,9 +621,6 @@ public:
const GCodeViewer::SequentialView& get_gcode_sequential_view() const { return m_gcode_viewer.get_sequential_view(); } const GCodeViewer::SequentialView& get_gcode_sequential_view() const { return m_gcode_viewer.get_sequential_view(); }
void update_gcode_sequential_view_current(unsigned int first, unsigned int last) { m_gcode_viewer.update_sequential_view_current(first, last); } void update_gcode_sequential_view_current(unsigned int first, unsigned int last) { m_gcode_viewer.update_sequential_view_current(first, last); }
void start_mapping_gcode_window();
void stop_mapping_gcode_window();
void toggle_sla_auxiliaries_visibility(bool visible, const ModelObject* mo = nullptr, int instance_idx = -1); void toggle_sla_auxiliaries_visibility(bool visible, const ModelObject* mo = nullptr, int instance_idx = -1);
void toggle_model_objects_visibility(bool visible, const ModelObject* mo = nullptr, int instance_idx = -1); void toggle_model_objects_visibility(bool visible, const ModelObject* mo = nullptr, int instance_idx = -1);
void update_instance_printable_state_for_object(size_t obj_idx); void update_instance_printable_state_for_object(size_t obj_idx);

View file

@ -6143,16 +6143,6 @@ BoundingBoxf Plater::bed_shape_bb() const
return p->bed_shape_bb(); return p->bed_shape_bb();
} }
void Plater::start_mapping_gcode_window()
{
p->preview->get_canvas3d()->start_mapping_gcode_window();
}
void Plater::stop_mapping_gcode_window()
{
p->preview->get_canvas3d()->stop_mapping_gcode_window();
}
void Plater::arrange() void Plater::arrange()
{ {
p->m_ui_jobs.arrange(); p->m_ui_jobs.arrange();

View file

@ -282,9 +282,6 @@ public:
GLCanvas3D* get_current_canvas3D(); GLCanvas3D* get_current_canvas3D();
BoundingBoxf bed_shape_bb() const; BoundingBoxf bed_shape_bb() const;
void start_mapping_gcode_window();
void stop_mapping_gcode_window();
void arrange(); void arrange();
void find_new_position(const ModelInstancePtrs &instances); void find_new_position(const ModelInstancePtrs &instances);