From f4522cd2fc193d747fbbce566b028ba62ffd0ddd Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Tue, 13 Feb 2018 13:16:23 +0100 Subject: [PATCH] GCode Preview - Customizable extrusion role colors by editing 3DPreview.pm --- lib/Slic3r/GUI/Plater/3DPreview.pm | 18 +++++++++++++ xs/src/libslic3r/GCode/Analyzer.cpp | 12 +++++++++ xs/src/libslic3r/GCode/Analyzer.hpp | 2 ++ xs/src/libslic3r/Print.cpp | 42 +++++++++++++++++++++++++++++ xs/src/libslic3r/Print.hpp | 12 +++++++++ xs/xsp/Print.xsp | 1 + 6 files changed, 87 insertions(+) diff --git a/lib/Slic3r/GUI/Plater/3DPreview.pm b/lib/Slic3r/GUI/Plater/3DPreview.pm index fb1a9bbfc5..f0175f5a4e 100644 --- a/lib/Slic3r/GUI/Plater/3DPreview.pm +++ b/lib/Slic3r/GUI/Plater/3DPreview.pm @@ -233,6 +233,24 @@ sub new { # init canvas $self->print($print); + + # sets colors for gcode preview extrusion roles + my @extrusion_roles_colors = ( + 'Perimeter' => 'FF0000', + 'External perimeter' => '00FF00', + 'Overhang perimeter' => '0000FF', + 'Internal infill' => 'FFFF00', + 'Solid infill' => 'FF00FF', + 'Top solid infill' => '00FFFF', + 'Bridge infill' => '7F7F7F', + 'Gap fill' => 'FFFFFF', + 'Skirt' => '7F0000', + 'Support material' => '007F00', + 'Support material interface' => '00007F', + 'Wipe tower' => 'B3E3AB', + ); + $self->print->set_gcode_extrusion_paths_colors(\@extrusion_roles_colors); + $self->reload_print; return $self; diff --git a/xs/src/libslic3r/GCode/Analyzer.cpp b/xs/src/libslic3r/GCode/Analyzer.cpp index c64857566a..02e999bb4a 100644 --- a/xs/src/libslic3r/GCode/Analyzer.cpp +++ b/xs/src/libslic3r/GCode/Analyzer.cpp @@ -347,6 +347,18 @@ const GCodeAnalyzer::PreviewData::Color& GCodeAnalyzer::PreviewData::get_extrusi return extrusion.ranges.feedrate.get_color_at(feedrate); } +void GCodeAnalyzer::PreviewData::set_extrusion_role_color(const std::string& role_name, float red, float green, float blue, float alpha) +{ + for (unsigned int i = 0; i < Extrusion::Num_Extrusion_Roles; ++i) + { + if (role_name == extrusion.role_names[i]) + { + extrusion.role_colors[i] = Color(red, green, blue, alpha); + break; + } + } +} + std::string GCodeAnalyzer::PreviewData::get_legend_title() const { switch (extrusion.view_type) diff --git a/xs/src/libslic3r/GCode/Analyzer.hpp b/xs/src/libslic3r/GCode/Analyzer.hpp index 6d02134222..121360dbc6 100644 --- a/xs/src/libslic3r/GCode/Analyzer.hpp +++ b/xs/src/libslic3r/GCode/Analyzer.hpp @@ -279,6 +279,8 @@ public: const Color& get_extrusion_width_color(float width) const; const Color& get_extrusion_feedrate_color(float feedrate) const; + void set_extrusion_role_color(const std::string& role_name, float red, float green, float blue, float alpha); + std::string get_legend_title() const; LegendItemsList get_legend_items(const std::vector& tool_colors) const; }; diff --git a/xs/src/libslic3r/Print.cpp b/xs/src/libslic3r/Print.cpp index 172b26cc47..bf0951f115 100644 --- a/xs/src/libslic3r/Print.cpp +++ b/xs/src/libslic3r/Print.cpp @@ -107,6 +107,48 @@ void Print::set_gcode_preview_shells_visible(bool visible) gcode_preview.shell.is_visible = visible; } +void Print::set_gcode_extrusion_paths_colors(const std::vector& colors) +{ + unsigned int size = (unsigned int)colors.size(); + + if (size % 2 != 0) + return; + + for (unsigned int i = 0; i < size; i += 2) + { + const std::string& color_str = colors[i + 1]; + + if (color_str.size() == 6) + { + bool valid = true; + for (int c = 0; c < 6; ++c) + { + if (::isxdigit(color_str[c]) == 0) + { + valid = false; + break; + } + } + + if (valid) + { + unsigned int color; + std::stringstream ss; + ss << std::hex << color_str; + ss >> color; + + float den = 1.0f / 255.0f; + + float r = (float)((color & 0xFF0000) >> 16) * den; + float g = (float)((color & 0x00FF00) >> 8) * den; + float b = (float)(color & 0x0000FF) * den; + + gcode_preview.set_extrusion_role_color(colors[i], r, g, b, 1.0f); + } + } + } +} + PrintRegion* Print::add_region() { regions.push_back(new PrintRegion(this)); diff --git a/xs/src/libslic3r/Print.hpp b/xs/src/libslic3r/Print.hpp index 0a6c143b98..da94917fc4 100644 --- a/xs/src/libslic3r/Print.hpp +++ b/xs/src/libslic3r/Print.hpp @@ -264,6 +264,18 @@ public: void set_gcode_preview_unretractions_visible(bool visible); void set_gcode_preview_shells_visible(bool visible); + // Sets the extrusion path colors from the given strings vector. + // Data in the vector should be formatted as follows: + // std::vector role_colors = + // { , , + // , , + // , , + // ... + // , }; + // where should be a string from GCodeAnalyzer::PreviewData::Extrusion::Default_Extrusion_Role_Names[] + // and an RGB color in hex format (i.e. red = FF0000) + void set_gcode_extrusion_paths_colors(const std::vector& colors); + // methods for handling regions PrintRegion* get_region(size_t idx) { return regions.at(idx); } const PrintRegion* get_region(size_t idx) const { return regions.at(idx); } diff --git a/xs/xsp/Print.xsp b/xs/xsp/Print.xsp index 4f870df686..5749c20db2 100644 --- a/xs/xsp/Print.xsp +++ b/xs/xsp/Print.xsp @@ -172,6 +172,7 @@ _constant() void set_gcode_preview_retractions_visible(bool visible); void set_gcode_preview_unretractions_visible(bool visible); void set_gcode_preview_shells_visible(bool visible); + void set_gcode_extrusion_paths_colors(std::vector colors); PrintRegionPtrs* regions() %code%{ RETVAL = &THIS->regions; %};