Tech ENABLE_GCODE_WINDOW_USE_MAPPED_FILE merged into ENABLE_GCODE_WINDOW

This commit is contained in:
enricoturri1966 2021-03-08 10:20:07 +01:00
parent 5998ee8f2e
commit 14aca210cb
6 changed files with 3 additions and 110 deletions

View File

@ -55,8 +55,6 @@
#define ENABLE_VALIDATE_CUSTOM_GCODE (1 && ENABLE_2_3_1_ALPHA1) #define ENABLE_VALIDATE_CUSTOM_GCODE (1 && ENABLE_2_3_1_ALPHA1)
// Enable showing a imgui window containing gcode in preview // Enable showing a imgui window containing gcode in preview
#define ENABLE_GCODE_WINDOW (1 && ENABLE_2_3_1_ALPHA1) #define ENABLE_GCODE_WINDOW (1 && ENABLE_2_3_1_ALPHA1)
// Enable using file mapping to show a imgui window containing gcode in preview
#define ENABLE_GCODE_WINDOW_USE_MAPPED_FILE (1 && ENABLE_GCODE_WINDOW)
#endif // _prusaslicer_technologies_h_ #endif // _prusaslicer_technologies_h_

View File

@ -303,9 +303,6 @@ void GCodeViewer::SequentialView::Marker::render() const
#if ENABLE_GCODE_WINDOW #if ENABLE_GCODE_WINDOW
void GCodeViewer::SequentialView::GCodeWindow::load_gcode() void GCodeViewer::SequentialView::GCodeWindow::load_gcode()
{ {
#if !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
m_gcode.clear();
#endif // !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
if (m_filename.empty()) if (m_filename.empty())
return; return;
@ -313,19 +310,13 @@ void GCodeViewer::SequentialView::GCodeWindow::load_gcode()
{ {
boost::nowide::ifstream f(m_filename); boost::nowide::ifstream f(m_filename);
std::string line; std::string line;
#if ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
// generate mapping for accessing data in file by line number // generate mapping for accessing data in file by line number
uint64_t offset = 0; uint64_t offset = 0;
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
while (std::getline(f, line)) { while (std::getline(f, line)) {
#if ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
size_t line_length = static_cast<uint64_t>(line.length()); size_t line_length = static_cast<uint64_t>(line.length());
m_lines_map.push_back({ offset, line_length }); m_lines_map.push_back({ offset, line_length });
offset += static_cast<uint64_t>(line_length) + 1; offset += static_cast<uint64_t>(line_length) + 1;
#else }
m_gcode.push_back(line);
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
}
} }
catch (...) catch (...)
{ {
@ -337,7 +328,6 @@ void GCodeViewer::SequentialView::GCodeWindow::load_gcode()
m_selected_line_id = 0; m_selected_line_id = 0;
m_last_lines_size = 0; m_last_lines_size = 0;
#if ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
try try
{ {
m_file.open(boost::filesystem::path(m_filename)); m_file.open(boost::filesystem::path(m_filename));
@ -347,33 +337,16 @@ void GCodeViewer::SequentialView::GCodeWindow::load_gcode()
BOOST_LOG_TRIVIAL(error) << "Unable to map file " << m_filename << ". Cannot show G-code window."; BOOST_LOG_TRIVIAL(error) << "Unable to map file " << m_filename << ". Cannot show G-code window.";
reset(); reset();
} }
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
} }
#if !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
void GCodeViewer::SequentialView::GCodeWindow::start_mapping_file()
{
std::cout << "GCodeViewer::SequentialView::GCodeWindow::start_mapping_file()\n";
}
void GCodeViewer::SequentialView::GCodeWindow::stop_mapping_file()
{
std::cout << "GCodeViewer::SequentialView::GCodeWindow::stop_mapping_file()\n";
}
#endif // !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
void GCodeViewer::SequentialView::GCodeWindow::render(float top, float bottom, uint64_t curr_line_id) const void GCodeViewer::SequentialView::GCodeWindow::render(float top, float bottom, uint64_t curr_line_id) const
{ {
auto update_lines = [this](uint64_t start_id, uint64_t end_id) { auto update_lines = [this](uint64_t start_id, uint64_t end_id) {
std::vector<Line> ret; std::vector<Line> ret;
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) {
#if ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
// 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); std::string gline(m_file.data() + m_lines_map[id - 1].first, m_lines_map[id - 1].second);
#else
const std::string& gline = m_gcode[id - 1];
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
std::string command; std::string command;
std::string parameters; std::string parameters;
@ -407,13 +380,8 @@ 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 ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
if (!m_visible || m_filename.empty() || m_lines_map.empty() || curr_line_id == 0) if (!m_visible || m_filename.empty() || m_lines_map.empty() || curr_line_id == 0)
return; return;
#else
if (!m_visible || m_filename.empty() || m_gcode.empty() || curr_line_id == 0)
return;
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
// window height // window height
const float wnd_height = bottom - top; const float wnd_height = bottom - top;
@ -430,21 +398,13 @@ 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 ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
if (end_id >= static_cast<uint64_t>(m_lines_map.size())) { if (end_id >= static_cast<uint64_t>(m_lines_map.size())) {
end_id = static_cast<uint64_t>(m_lines_map.size()) - 1; end_id = static_cast<uint64_t>(m_lines_map.size()) - 1;
start_id = end_id - lines_count + 1; start_id = end_id - lines_count + 1;
} }
#else
if (end_id >= static_cast<uint64_t>(m_gcode.size())) {
end_id = static_cast<uint64_t>(m_gcode.size()) - 1;
start_id = end_id - lines_count + 1;
}
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
// updates list of lines to show, if needed // updates list of lines to show, if needed
if (m_selected_line_id != curr_line_id || m_last_lines_size != end_id - start_id + 1) { if (m_selected_line_id != curr_line_id || m_last_lines_size != end_id - start_id + 1) {
#if ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
try try
{ {
*const_cast<std::vector<Line>*>(&m_lines) = update_lines(start_id, end_id); *const_cast<std::vector<Line>*>(&m_lines) = update_lines(start_id, end_id);
@ -454,9 +414,6 @@ void GCodeViewer::SequentialView::GCodeWindow::render(float top, float bottom, u
BOOST_LOG_TRIVIAL(error) << "Error while loading from file " << m_filename << ". Cannot show G-code window."; BOOST_LOG_TRIVIAL(error) << "Error while loading from file " << m_filename << ". Cannot show G-code window.";
return; return;
} }
#else
*const_cast<std::vector<Line>*>(&m_lines) = update_lines(start_id, end_id);
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
*const_cast<uint64_t*>(&m_selected_line_id) = curr_line_id; *const_cast<uint64_t*>(&m_selected_line_id) = curr_line_id;
*const_cast<size_t*>(&m_last_lines_size) = m_lines.size(); *const_cast<size_t*>(&m_last_lines_size) = m_lines.size();
} }
@ -531,13 +488,11 @@ void GCodeViewer::SequentialView::GCodeWindow::render(float top, float bottom, u
ImGui::PopStyleVar(); ImGui::PopStyleVar();
} }
#if ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
void GCodeViewer::SequentialView::GCodeWindow::stop_mapping_file() void GCodeViewer::SequentialView::GCodeWindow::stop_mapping_file()
{ {
if (m_file.is_open()) if (m_file.is_open())
m_file.close(); m_file.close();
} }
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
void GCodeViewer::SequentialView::render(float legend_height) const void GCodeViewer::SequentialView::render(float legend_height) const
{ {

View File

@ -5,9 +5,9 @@
#include "libslic3r/GCode/GCodeProcessor.hpp" #include "libslic3r/GCode/GCodeProcessor.hpp"
#include "GLModel.hpp" #include "GLModel.hpp"
#if ENABLE_GCODE_WINDOW_USE_MAPPED_FILE #if ENABLE_GCODE_WINDOW
#include <boost/iostreams/device/mapped_file.hpp> #include <boost/iostreams/device/mapped_file.hpp>
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE #endif // ENABLE_GCODE_WINDOW
#include <cstdint> #include <cstdint>
#include <float.h> #include <float.h>
@ -623,46 +623,30 @@ public:
uint64_t m_selected_line_id{ 0 }; uint64_t m_selected_line_id{ 0 };
size_t m_last_lines_size{ 0 }; size_t m_last_lines_size{ 0 };
std::string m_filename; std::string m_filename;
#if ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
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<std::pair<uint64_t, uint64_t>> m_lines_map;
#else
std::vector<std::string> m_gcode;
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
// current visible lines // current visible lines
std::vector<Line> m_lines; std::vector<Line> m_lines;
public: public:
#if ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
GCodeWindow() = default; GCodeWindow() = default;
~GCodeWindow() { stop_mapping_file(); } ~GCodeWindow() { stop_mapping_file(); }
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
void set_filename(const std::string& filename) { m_filename = filename; } void set_filename(const std::string& filename) { m_filename = filename; }
void load_gcode(); void load_gcode();
#if !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
void start_mapping_file();
void stop_mapping_file();
#endif // !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
#if ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
void reset() { void reset() {
stop_mapping_file(); stop_mapping_file();
m_lines_map.clear(); m_lines_map.clear();
m_lines.clear(); m_lines.clear();
m_filename.clear(); m_filename.clear();
} }
#else
void reset() { m_filename.clear(); }
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
void toggle_visibility() { m_visible = !m_visible; } void toggle_visibility() { m_visible = !m_visible; }
void render(float top, float bottom, uint64_t curr_line_id) const; void render(float top, float bottom, uint64_t curr_line_id) const;
#if ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
private: private:
void stop_mapping_file(); void stop_mapping_file();
#endif // ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
}; };
#endif // ENABLE_GCODE_WINDOW #endif // ENABLE_GCODE_WINDOW
@ -783,10 +767,6 @@ public:
void export_toolpaths_to_obj(const char* filename) const; void export_toolpaths_to_obj(const char* filename) const;
#if ENABLE_GCODE_WINDOW #if ENABLE_GCODE_WINDOW
#if !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
void start_mapping_gcode_file() { m_sequential_view.gcode_window.start_mapping_file(); }
void stop_mapping_gcode_file() { m_sequential_view.gcode_window.stop_mapping_file(); }
#endif // !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
void toggle_gcode_window_visibility() { m_sequential_view.gcode_window.toggle_visibility(); } void toggle_gcode_window_visibility() { m_sequential_view.gcode_window.toggle_visibility(); }
#endif // ENABLE_GCODE_WINDOW #endif // ENABLE_GCODE_WINDOW

View File

@ -1195,11 +1195,6 @@ GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas)
GLCanvas3D::~GLCanvas3D() GLCanvas3D::~GLCanvas3D()
{ {
#if ENABLE_GCODE_WINDOW
#if !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
m_gcode_viewer.stop_mapping_gcode_file();
#endif // !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
#endif // ENABLE_GCODE_WINDOW
reset_volumes(); reset_volumes();
} }
@ -3900,20 +3895,6 @@ void GLCanvas3D::mouse_up_cleanup()
m_canvas->ReleaseMouse(); m_canvas->ReleaseMouse();
} }
#if ENABLE_GCODE_WINDOW
#if !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
void GLCanvas3D::start_mapping_gcode_file()
{
m_gcode_viewer.start_mapping_gcode_file();
}
void GLCanvas3D::stop_mapping_gcode_file()
{
m_gcode_viewer.stop_mapping_gcode_file();
}
#endif // !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
#endif // ENABLE_GCODE_WINDOW
bool GLCanvas3D::_is_shown_on_screen() const bool GLCanvas3D::_is_shown_on_screen() const
{ {
return (m_canvas != nullptr) ? m_canvas->IsShownOnScreen() : false; return (m_canvas != nullptr) ? m_canvas->IsShownOnScreen() : false;

View File

@ -785,13 +785,6 @@ public:
#endif #endif
} }
#if ENABLE_GCODE_WINDOW
#if !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
void start_mapping_gcode_file();
void stop_mapping_gcode_file();
#endif // !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
#endif // ENABLE_GCODE_WINDOW
private: private:
bool _is_shown_on_screen() const; bool _is_shown_on_screen() const;

View File

@ -3368,17 +3368,8 @@ void Plater::priv::set_current_panel(wxPanel* panel)
panel_sizer->Layout(); panel_sizer->Layout();
if (current_panel == view3D) { if (current_panel == view3D) {
#if ENABLE_GCODE_WINDOW
if (old_panel == preview) {
#if !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
preview->get_canvas3d()->stop_mapping_gcode_file();
#endif // !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
preview->get_canvas3d()->unbind_event_handlers();
}
#else
if (old_panel == preview) if (old_panel == preview)
preview->get_canvas3d()->unbind_event_handlers(); preview->get_canvas3d()->unbind_event_handlers();
#endif // ENABLE_GCODE_WINDOW
view3D->get_canvas3d()->bind_event_handlers(); view3D->get_canvas3d()->bind_event_handlers();
@ -3403,11 +3394,6 @@ void Plater::priv::set_current_panel(wxPanel* panel)
view3D->get_canvas3d()->unbind_event_handlers(); view3D->get_canvas3d()->unbind_event_handlers();
preview->get_canvas3d()->bind_event_handlers(); preview->get_canvas3d()->bind_event_handlers();
#if ENABLE_GCODE_WINDOW
#if !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
preview->get_canvas3d()->start_mapping_gcode_file();
#endif // !ENABLE_GCODE_WINDOW_USE_MAPPED_FILE
#endif // ENABLE_GCODE_WINDOW
// see: Plater::priv::object_list_changed() // see: Plater::priv::object_list_changed()
// FIXME: it may be better to have a single function making this check and let it be called wherever needed // FIXME: it may be better to have a single function making this check and let it be called wherever needed