From dea83da5b7072bab649b31a58d282c504f27623e Mon Sep 17 00:00:00 2001 From: foxox Date: Sat, 7 Dec 2019 19:31:28 -0500 Subject: [PATCH 01/13] Speed legend scales depending on travel visibility. --- src/libslic3r/GCode/Analyzer.cpp | 8 +- src/libslic3r/GCode/PreviewData.cpp | 138 +++++++++---------- src/libslic3r/GCode/PreviewData.hpp | 198 ++++++++++++++++++++++++---- src/slic3r/GUI/GLCanvas3D.cpp | 26 ++-- src/slic3r/GUI/GUI_Preview.cpp | 4 +- 5 files changed, 254 insertions(+), 120 deletions(-) diff --git a/src/libslic3r/GCode/Analyzer.cpp b/src/libslic3r/GCode/Analyzer.cpp index f73f4b4ba1..a9be609be6 100644 --- a/src/libslic3r/GCode/Analyzer.cpp +++ b/src/libslic3r/GCode/Analyzer.cpp @@ -993,7 +993,7 @@ void GCodeAnalyzer::_calc_gcode_preview_extrusion_layers(GCodePreviewData& previ float volumetric_rate = FLT_MAX; GCodePreviewData::Range height_range; GCodePreviewData::Range width_range; - GCodePreviewData::Range feedrate_range; + GCodePreviewData::MultiRange feedrate_range; GCodePreviewData::Range volumetric_rate_range; GCodePreviewData::Range fan_speed_range; @@ -1028,7 +1028,7 @@ void GCodeAnalyzer::_calc_gcode_preview_extrusion_layers(GCodePreviewData& previ volumetric_rate = move.data.feedrate * (float)move.data.mm3_per_mm; height_range.update_from(move.data.height); width_range.update_from(move.data.width); - feedrate_range.update_from(move.data.feedrate); + feedrate_range.update_from(move.data.feedrate, GCodePreviewData::FeedrateKind::EXTRUSION); volumetric_rate_range.update_from(volumetric_rate); fan_speed_range.update_from(move.data.fan_speed); } @@ -1081,7 +1081,7 @@ void GCodeAnalyzer::_calc_gcode_preview_travel(GCodePreviewData& preview_data, s GCodePreviewData::Range height_range; GCodePreviewData::Range width_range; - GCodePreviewData::Range feedrate_range; + GCodePreviewData::MultiRange feedrate_range; // to avoid to call the callback too often unsigned int cancel_callback_threshold = (unsigned int)std::max((int)travel_moves->second.size() / 25, 1); @@ -1121,7 +1121,7 @@ void GCodeAnalyzer::_calc_gcode_preview_travel(GCodePreviewData& preview_data, s extruder_id = move.data.extruder_id; height_range.update_from(move.data.height); width_range.update_from(move.data.width); - feedrate_range.update_from(move.data.feedrate); + feedrate_range.update_from(move.data.feedrate, GCodePreviewData::FeedrateKind::TRAVEL); } // store last polyline diff --git a/src/libslic3r/GCode/PreviewData.cpp b/src/libslic3r/GCode/PreviewData.cpp index e7475089ac..64057ec8d5 100644 --- a/src/libslic3r/GCode/PreviewData.cpp +++ b/src/libslic3r/GCode/PreviewData.cpp @@ -11,9 +11,7 @@ namespace Slic3r { -const GCodePreviewData::Color GCodePreviewData::Color::Dummy(0.0f, 0.0f, 0.0f, 0.0f); - -std::vector GCodePreviewData::Color::as_bytes() const +std::vector Color::as_bytes() const { std::vector ret; for (unsigned int i = 0; i < 4; ++i) @@ -38,20 +36,6 @@ GCodePreviewData::Travel::Polyline::Polyline(EType type, EDirection direction, f { } -const GCodePreviewData::Color GCodePreviewData::Range::Default_Colors[Colors_Count] = -{ - Color(0.043f, 0.173f, 0.478f, 1.0f), - Color(0.075f, 0.349f, 0.522f, 1.0f), - Color(0.110f, 0.533f, 0.569f, 1.0f), - Color(0.016f, 0.839f, 0.059f, 1.0f), - Color(0.667f, 0.949f, 0.000f, 1.0f), - Color(0.988f, 0.975f, 0.012f, 1.0f), - Color(0.961f, 0.808f, 0.039f, 1.0f), - Color(0.890f, 0.533f, 0.125f, 1.0f), - Color(0.820f, 0.408f, 0.188f, 1.0f), - Color(0.761f, 0.322f, 0.235f, 1.0f) -}; - GCodePreviewData::Range::Range() { reset(); @@ -59,54 +43,52 @@ GCodePreviewData::Range::Range() void GCodePreviewData::Range::reset() { - min = FLT_MAX; - max = -FLT_MAX; + min_val = FLT_MAX; + max_val = -FLT_MAX; } bool GCodePreviewData::Range::empty() const { - return min == max; + return min_val >= max_val; } void GCodePreviewData::Range::update_from(float value) { - min = std::min(min, value); - max = std::max(max, value); + min_val = std::min(min_val, value); + max_val = std::max(max_val, value); } -void GCodePreviewData::Range::update_from(const Range& other) +void GCodePreviewData::Range::update_from(const RangeBase& other) { - min = std::min(min, other.min); - max = std::max(max, other.max); + min_val = std::min(min_val, other.min()); + max_val = std::max(max_val, other.max()); } -void GCodePreviewData::Range::set_from(const Range& other) +float GCodePreviewData::RangeBase::step_size() const { - min = other.min; - max = other.max; + return (max() - min()) / static_cast(range_rainbow_colors.size() - 1); } -float GCodePreviewData::Range::step_size() const -{ - return (max - min) / (float)(Colors_Count - 1); -} - -GCodePreviewData::Color GCodePreviewData::Range::get_color_at(float value) const +Color GCodePreviewData::RangeBase::get_color_at(float value) const { if (empty()) - return Color::Dummy; + return Color{}; - float global_t = (value - min) / step_size(); + // Input value scaled to the color range + const float global_t = std::max(0.0f, value - min()) / step_size(); // lower limit of 0.0f + + constexpr std::size_t color_max_idx = range_rainbow_colors.size() - 1; - unsigned int low = (unsigned int)global_t; - unsigned int high = clamp((unsigned int)0, Colors_Count - 1, low + 1); + // Compute the two colors just below (low) and above (high) the input value + const std::size_t color_low_idx = clamp(std::size_t{0}, color_max_idx, static_cast(global_t)); + const std::size_t color_high_idx = clamp(std::size_t{0}, color_max_idx, color_low_idx + 1); + const Color color_low = range_rainbow_colors[color_low_idx]; + const Color color_high = range_rainbow_colors[color_high_idx]; - Color color_low = colors[low]; - Color color_high = colors[high]; + // Compute how far the value is between the low and high colors so that they can be interpolated + const float local_t = std::min(global_t - static_cast(color_low_idx), 1.0f); // upper limit of 1.0f - float local_t = global_t - (float)low; - - // interpolate in RGB space + // Interpolate between the low and high colors in RGB space to find exactly which color the input value should get Color ret; for (unsigned int i = 0; i < 4; ++i) { @@ -115,13 +97,23 @@ GCodePreviewData::Color GCodePreviewData::Range::get_color_at(float value) const return ret; } -GCodePreviewData::LegendItem::LegendItem(const std::string& text, const GCodePreviewData::Color& color) +float GCodePreviewData::Range::min() const +{ + return min_val; +} + +float GCodePreviewData::Range::max() const +{ + return max_val; +} + +GCodePreviewData::LegendItem::LegendItem(const std::string& text, const Color& color) : text(text) , color(color) { } -const GCodePreviewData::Color GCodePreviewData::Extrusion::Default_Extrusion_Role_Colors[erCount] = +const Color GCodePreviewData::Extrusion::Default_Extrusion_Role_Colors[erCount] = { Color(0.0f, 0.0f, 0.0f, 1.0f), // erNone Color(1.0f, 0.0f, 0.0f, 1.0f), // erPerimeter @@ -180,7 +172,7 @@ size_t GCodePreviewData::Extrusion::memory_used() const const float GCodePreviewData::Travel::Default_Width = 0.075f; const float GCodePreviewData::Travel::Default_Height = 0.075f; -const GCodePreviewData::Color GCodePreviewData::Travel::Default_Type_Colors[Num_Types] = +const Color GCodePreviewData::Travel::Default_Type_Colors[Num_Types] = { Color(0.0f, 0.0f, 0.75f, 1.0f), // Move Color(0.0f, 0.75f, 0.0f, 1.0f), // Extrude @@ -206,7 +198,7 @@ size_t GCodePreviewData::Travel::memory_used() const return out; } -const GCodePreviewData::Color GCodePreviewData::Retraction::Default_Color = GCodePreviewData::Color(1.0f, 1.0f, 1.0f, 1.0f); +const Color GCodePreviewData::Retraction::Default_Color = Color(1.0f, 1.0f, 1.0f, 1.0f); GCodePreviewData::Retraction::Position::Position(const Vec3crd& position, float width, float height) : position(position) @@ -238,17 +230,15 @@ GCodePreviewData::GCodePreviewData() void GCodePreviewData::set_default() { - ::memcpy((void*)ranges.height.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color)); - ::memcpy((void*)ranges.width.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color)); - ::memcpy((void*)ranges.feedrate.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color)); - ::memcpy((void*)ranges.fan_speed.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color)); - ::memcpy((void*)ranges.volumetric_rate.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color)); - extrusion.set_default(); travel.set_default(); retraction.set_default(); unretraction.set_default(); shell.set_default(); + + // Configure the color range for feedrate to match the default for travels and to enable extrusions since they are always visible + ranges.feedrate.set_mode(FeedrateKind::TRAVEL, travel.is_visible); + ranges.feedrate.set_mode(FeedrateKind::EXTRUSION, true); } void GCodePreviewData::reset() @@ -268,32 +258,32 @@ bool GCodePreviewData::empty() const return extrusion.layers.empty() && travel.polylines.empty() && retraction.positions.empty() && unretraction.positions.empty(); } -GCodePreviewData::Color GCodePreviewData::get_extrusion_role_color(ExtrusionRole role) const +Color GCodePreviewData::get_extrusion_role_color(ExtrusionRole role) const { return extrusion.role_colors[role]; } -GCodePreviewData::Color GCodePreviewData::get_height_color(float height) const +Color GCodePreviewData::get_height_color(float height) const { return ranges.height.get_color_at(height); } -GCodePreviewData::Color GCodePreviewData::get_width_color(float width) const +Color GCodePreviewData::get_width_color(float width) const { return ranges.width.get_color_at(width); } -GCodePreviewData::Color GCodePreviewData::get_feedrate_color(float feedrate) const +Color GCodePreviewData::get_feedrate_color(float feedrate) const { return ranges.feedrate.get_color_at(feedrate); } -GCodePreviewData::Color GCodePreviewData::get_fan_speed_color(float fan_speed) const +Color GCodePreviewData::get_fan_speed_color(float fan_speed) const { return ranges.fan_speed.get_color_at(fan_speed); } -GCodePreviewData::Color GCodePreviewData::get_volumetric_rate_color(float rate) const +Color GCodePreviewData::get_volumetric_rate_color(float rate) const { return ranges.volumetric_rate.get_color_at(rate); } @@ -312,7 +302,7 @@ void GCodePreviewData::set_extrusion_role_color(const std::string& role_name, fl void GCodePreviewData::set_extrusion_paths_colors(const std::vector& colors) { - unsigned int size = (unsigned int)colors.size(); + unsigned int size = (unsigned int)range_rainbow_colors.size(); if (size % 2 != 0) return; @@ -384,16 +374,16 @@ GCodePreviewData::LegendItemsList GCodePreviewData::get_legend_items(const std:: { struct Helper { - static void FillListFromRange(LegendItemsList& list, const Range& range, unsigned int decimals, float scale_factor) + static void FillListFromRange(LegendItemsList& list, const RangeBase& range, unsigned int decimals, float scale_factor) { - list.reserve(Range::Colors_Count); + list.reserve(range_rainbow_colors.size()); float step = range.step_size(); - for (int i = Range::Colors_Count - 1; i >= 0; --i) + for (int i = static_cast(range_rainbow_colors.size()) - 1; i >= 0; --i) { char buf[1024]; - sprintf(buf, "%.*f", decimals, scale_factor * (range.min + (float)i * step)); - list.emplace_back(buf, range.colors[i]); + sprintf(buf, "%.*f", decimals, scale_factor * (range.min() + (float)i * step)); + list.emplace_back(buf, range_rainbow_colors[i]); } } }; @@ -446,8 +436,8 @@ GCodePreviewData::LegendItemsList GCodePreviewData::get_legend_items(const std:: items.reserve(tools_colors_count); for (unsigned int i = 0; i < tools_colors_count; ++i) { - GCodePreviewData::Color color; - ::memcpy((void*)color.rgba, (const void*)(tool_colors.data() + i * 4), 4 * sizeof(float)); + Color color; + ::memcpy((void*)color.rgba.data(), (const void*)(tool_colors.data() + i * 4), 4 * sizeof(float)); items.emplace_back((boost::format(Slic3r::I18N::translate(L("Extruder %d"))) % (i + 1)).str(), color); } @@ -460,7 +450,7 @@ GCodePreviewData::LegendItemsList GCodePreviewData::get_legend_items(const std:: if (color_print_cnt == 1) // means "Default print color" { Color color; - ::memcpy((void*)color.rgba, (const void*)(tool_colors.data()), 4 * sizeof(float)); + ::memcpy((void*)color.rgba.data(), (const void*)(tool_colors.data()), 4 * sizeof(float)); items.emplace_back(cp_items[0], color); break; @@ -472,7 +462,7 @@ GCodePreviewData::LegendItemsList GCodePreviewData::get_legend_items(const std:: for (int i = 0 ; i < color_print_cnt; ++i) { Color color; - ::memcpy((void*)color.rgba, (const void*)(tool_colors.data() + i * 4), 4 * sizeof(float)); + ::memcpy((void*)color.rgba.data(), (const void*)(tool_colors.data() + i * 4), 4 * sizeof(float)); items.emplace_back(cp_items[i], color); } @@ -496,23 +486,23 @@ size_t GCodePreviewData::memory_used() const sizeof(shell) + sizeof(ranges); } -const std::vector& GCodePreviewData::ColorPrintColors() +const std::vector& ColorPrintColors() { static std::vector color_print = {"#C0392B", "#E67E22", "#F1C40F", "#27AE60", "#1ABC9C", "#2980B9", "#9B59B6"}; return color_print; } -GCodePreviewData::Color operator + (const GCodePreviewData::Color& c1, const GCodePreviewData::Color& c2) +Color operator + (const Color& c1, const Color& c2) { - return GCodePreviewData::Color(clamp(0.0f, 1.0f, c1.rgba[0] + c2.rgba[0]), + return Color(clamp(0.0f, 1.0f, c1.rgba[0] + c2.rgba[0]), clamp(0.0f, 1.0f, c1.rgba[1] + c2.rgba[1]), clamp(0.0f, 1.0f, c1.rgba[2] + c2.rgba[2]), clamp(0.0f, 1.0f, c1.rgba[3] + c2.rgba[3])); } -GCodePreviewData::Color operator * (float f, const GCodePreviewData::Color& color) +Color operator * (float f, const Color& color) { - return GCodePreviewData::Color(clamp(0.0f, 1.0f, f * color.rgba[0]), + return Color(clamp(0.0f, 1.0f, f * color.rgba[0]), clamp(0.0f, 1.0f, f * color.rgba[1]), clamp(0.0f, 1.0f, f * color.rgba[2]), clamp(0.0f, 1.0f, f * color.rgba[3])); diff --git a/src/libslic3r/GCode/PreviewData.hpp b/src/libslic3r/GCode/PreviewData.hpp index 725c0258d9..a023203f1c 100644 --- a/src/libslic3r/GCode/PreviewData.hpp +++ b/src/libslic3r/GCode/PreviewData.hpp @@ -5,43 +5,188 @@ #include "../ExtrusionEntity.hpp" #include "../Point.hpp" +#include +#include +#include +#include +#include +#include +#include + namespace Slic3r { +// Represents an RGBA color +struct Color +{ + std::array rgba; + + Color(const float *argba) + { + memcpy(this->rgba.data(), argba, sizeof(float) * 4); + } + constexpr Color(float r = 1.f, float g = 1.f, float b = 1.f, float a = 1.f) : rgba{r,g,b,a} + { + // Intentionally empty + } + + std::vector as_bytes() const; +}; +Color operator + (const Color& c1, const Color& c2); +Color operator * (float f, const Color& color); + +// Default colors for Ranges +constexpr std::array range_rainbow_colors{ + Color{0.043f, 0.173f, 0.478f, 1.0f}, + Color{0.075f, 0.349f, 0.522f, 1.0f}, + Color{0.110f, 0.533f, 0.569f, 1.0f}, + Color{0.016f, 0.839f, 0.059f, 1.0f}, + Color{0.667f, 0.949f, 0.000f, 1.0f}, + Color{0.988f, 0.975f, 0.012f, 1.0f}, + Color{0.961f, 0.808f, 0.039f, 1.0f}, + Color{0.890f, 0.533f, 0.125f, 1.0f}, + Color{0.820f, 0.408f, 0.188f, 1.0f}, + Color{0.761f, 0.322f, 0.235f, 1.0f}}; + class GCodePreviewData { public: - struct Color + + // Color mapping to convert a float into a smooth rainbow of 10 colors. + class RangeBase { - float rgba[4]; + public: - Color(const float *argba) { memcpy(this->rgba, argba, sizeof(float) * 4); } - Color(float r = 1.f, float g = 1.f, float b = 1.f, float a = 1.f) { rgba[0] = r; rgba[1] = g; rgba[2] = b; rgba[3] = a; } - - std::vector as_bytes() const; - - static const Color Dummy; + virtual void reset() = 0; + virtual bool empty() const = 0; + virtual float min() const = 0; + virtual float max() const = 0; + + // Gets the step size using min(), max() and colors + float step_size() const; + + // Gets the color at a value using colors, min(), and max() + Color get_color_at(float value) const; }; - - // Color mapping from a range into a smooth rainbow of 10 colors. - struct Range + + // Color mapping converting a float in a range between a min and a max into a smooth rainbow of 10 colors. + class Range : public RangeBase { - static const unsigned int Colors_Count = 10; - static const Color Default_Colors[Colors_Count]; - - Color colors[Colors_Count]; - float min; - float max; - + public: Range(); - void reset(); - bool empty() const; + // RangeBase Overrides + void reset() override; + bool empty() const override; + float min() const override; + float max() const override; + + // Range-specific methods void update_from(float value); - void update_from(const Range& other); - void set_from(const Range& other); - float step_size() const; + void update_from(const RangeBase& other); - Color get_color_at(float value) const; + private: + float min_val; + float max_val; + }; + + // Like Range, but stores multiple ranges internally that are used depending on mode. + // Template param EnumRangeType must be an enum with values for each type of range that needs to be tracked in this MultiRange. + // The last enum value should be num_values. The numerical values of all enum values should range from 0 to num_values. + template + class MultiRange : public RangeBase + { + public: + + void reset() override + { + bounds = decltype(bounds){}; + } + + bool empty() const override + { + for (std::size_t i = 0; i < bounds.size(); ++i) + { + if (bounds[i].min != bounds[i].max) + return false; + } + return true; + } + + float min() const override + { + float min = FLT_MAX; + for (std::size_t i = 0; i < bounds.size(); ++i) + { + // Only use bounds[i] if the current mode includes it + if (mode.test(i)) + { + min = std::min(min, bounds[i].min); + } + } + return min; + } + + float max() const override + { + float max = -FLT_MAX; + for (std::size_t i = 0; i < bounds.size(); ++i) + { + // Only use bounds[i] if the current mode includes it + if (mode.test(i)) + { + max = std::max(max, bounds[i].max); + } + } + return max; + } + + void update_from(const float value, EnumRangeType range_type_value) + { + bounds[static_cast(range_type_value)].update_from(value); + } + + void update_from(const MultiRange& other) + { + for (std::size_t i = 0; i < bounds.size(); ++i) + { + bounds[i].update_from(other.bounds[i]); + } + } + + void set_mode(const EnumRangeType range_type_value, const bool enable) + { + mode.set(static_cast(range_type_value), enable); + } + + private: + + // Interval bounds + struct Bounds + { + float min{FLT_MAX}; + float max{-FLT_MAX}; + void update_from(const float value) + { + min = std::min(min, value); + max = std::max(max, value); + } + void update_from(const Bounds other_bounds) + { + min = std::min(min, other_bounds.min); + max = std::max(max, other_bounds.max); + } + }; + + std::array(EnumRangeType::num_values)> bounds; + std::bitset(EnumRangeType::num_values)> mode; + }; + + // Enum distinguishing different kinds of feedrate data + enum class FeedrateKind + { + EXTRUSION = 0, // values must go from 0 up to num_values + TRAVEL, + num_values //must be last in the list of values }; struct Ranges @@ -51,7 +196,7 @@ public: // Color mapping by extrusion width. Range width; // Color mapping by feedrate. - Range feedrate; + MultiRange feedrate; // Color mapping by fan speed. Range fan_speed; // Color mapping by volumetric extrusion rate. @@ -245,9 +390,6 @@ public: static const std::vector& ColorPrintColors(); }; -GCodePreviewData::Color operator + (const GCodePreviewData::Color& c1, const GCodePreviewData::Color& c2); -GCodePreviewData::Color operator * (float f, const GCodePreviewData::Color& color); - } // namespace Slic3r #endif /* slic3r_GCode_PreviewData_hpp_ */ diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index dcf45e91da..24cb6a192a 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -2417,7 +2417,7 @@ static void load_gcode_retractions(const GCodePreviewData::Retraction& retractio volume_index.first_volumes.emplace_back(extrusion_type, 0, (unsigned int)volumes.volumes.size()); - GLVolume *volume = volumes.new_nontoolpath_volume(retractions.color.rgba, VERTEX_BUFFER_RESERVE_SIZE); + GLVolume *volume = volumes.new_nontoolpath_volume(retractions.color.rgba.data(), VERTEX_BUFFER_RESERVE_SIZE); GCodePreviewData::Retraction::PositionsList copy(retractions.positions); std::sort(copy.begin(), copy.end(), [](const GCodePreviewData::Retraction::Position& p1, const GCodePreviewData::Retraction::Position& p2) { return p1.position(2) < p2.position(2); }); @@ -5841,7 +5841,7 @@ void GLCanvas3D::_load_gcode_extrusion_paths(const GCodePreviewData& preview_dat return 0.0f; } - static GCodePreviewData::Color path_color(const GCodePreviewData& data, const std::vector& tool_colors, float value) + static Color path_color(const GCodePreviewData& data, const std::vector& tool_colors, float value) { switch (data.extrusion.view_type) { @@ -5859,8 +5859,8 @@ void GLCanvas3D::_load_gcode_extrusion_paths(const GCodePreviewData& preview_dat return data.get_volumetric_rate_color(value); case GCodePreviewData::Extrusion::Tool: { - GCodePreviewData::Color color; - ::memcpy((void*)color.rgba, (const void*)(tool_colors.data() + (unsigned int)value * 4), 4 * sizeof(float)); + Color color; + ::memcpy((void*)color.rgba.data(), (const void*)(tool_colors.data() + (unsigned int)value * 4), 4 * sizeof(float)); return color; } case GCodePreviewData::Extrusion::ColorPrint: @@ -5868,16 +5868,16 @@ void GLCanvas3D::_load_gcode_extrusion_paths(const GCodePreviewData& preview_dat int color_cnt = (int)tool_colors.size() / 4; int val = value > color_cnt ? color_cnt - 1 : value; - GCodePreviewData::Color color; - ::memcpy((void*)color.rgba, (const void*)(tool_colors.data() + val * 4), 4 * sizeof(float)); + Color color; + ::memcpy((void*)color.rgba.data(), (const void*)(tool_colors.data() + val * 4), 4 * sizeof(float)); return color; } default: - return GCodePreviewData::Color::Dummy; + return Color{}; } - return GCodePreviewData::Color::Dummy; + return Color{}; } }; @@ -5920,7 +5920,7 @@ void GLCanvas3D::_load_gcode_extrusion_paths(const GCodePreviewData& preview_dat if (! values.empty()) { m_gcode_preview_volume_index.first_volumes.emplace_back(GCodePreviewVolumeIndex::Extrusion, role, (unsigned int)m_volumes.volumes.size()); for (const float value : values) - roles_filters.back().emplace_back(value, m_volumes.new_toolpath_volume(Helper::path_color(preview_data, tool_colors, value).rgba, vertex_buffer_prealloc_size)); + roles_filters.back().emplace_back(value, m_volumes.new_toolpath_volume(Helper::path_color(preview_data, tool_colors, value).rgba.data(), vertex_buffer_prealloc_size)); } } } @@ -6003,7 +6003,7 @@ inline void travel_paths_internal( by_type.reserve(values.size()); // creates a new volume for each feedrate for (TYPE type : values) - by_type.emplace_back(type, volumes.new_nontoolpath_volume(func_color(type).rgba, VERTEX_BUFFER_RESERVE_SIZE)); + by_type.emplace_back(type, volumes.new_nontoolpath_volume(func_color(type).rgba.data(), VERTEX_BUFFER_RESERVE_SIZE)); } // populates volumes @@ -6050,19 +6050,19 @@ void GLCanvas3D::_load_gcode_travel_paths(const GCodePreviewData& preview_data, case GCodePreviewData::Extrusion::Feedrate: travel_paths_internal(preview_data, [](const GCodePreviewData::Travel::Polyline &polyline) { return polyline.feedrate; }, - [&preview_data](const float feedrate) -> const GCodePreviewData::Color { return preview_data.get_feedrate_color(feedrate); }, + [&preview_data](const float feedrate) -> const Color { return preview_data.get_feedrate_color(feedrate); }, m_volumes, m_initialized); break; case GCodePreviewData::Extrusion::Tool: travel_paths_internal(preview_data, [](const GCodePreviewData::Travel::Polyline &polyline) { return polyline.extruder_id; }, - [&tool_colors](const unsigned int extruder_id) -> const GCodePreviewData::Color { assert((extruder_id + 1) * 4 <= tool_colors.size()); return GCodePreviewData::Color(tool_colors.data() + extruder_id * 4); }, + [&tool_colors](const unsigned int extruder_id) -> const Color { assert((extruder_id + 1) * 4 <= tool_colors.size()); return Color(tool_colors.data() + extruder_id * 4); }, m_volumes, m_initialized); break; default: travel_paths_internal(preview_data, [](const GCodePreviewData::Travel::Polyline &polyline) { return polyline.type; }, - [&preview_data](const unsigned int type) -> const GCodePreviewData::Color& { return preview_data.travel.type_colors[type]; }, + [&preview_data](const unsigned int type) -> const Color& { return preview_data.travel.type_colors[type]; }, m_volumes, m_initialized); break; } diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index edb244b347..1e4465925d 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -538,7 +538,9 @@ void Preview::on_combochecklist_features(wxCommandEvent& evt) void Preview::on_checkbox_travel(wxCommandEvent& evt) { m_gcode_preview_data->travel.is_visible = m_checkbox_travel->IsChecked(); - refresh_print(); + m_gcode_preview_data->ranges.feedrate.set_mode(GCodePreviewData::FeedrateKind::TRAVEL, m_gcode_preview_data->travel.is_visible); + // Rather than refresh, reload print so that speed color ranges get recomputed (affected by travel visibility) + reload_print(); } void Preview::on_checkbox_retractions(wxCommandEvent& evt) From 07c1c3d1dc9aade0b95fa570314205ee8ee35481 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Wed, 8 Jan 2020 15:23:46 +0100 Subject: [PATCH 02/13] Fixed update of "Cost" field in "Sliced Info" box after a change of any material's options ("bottle_cost", "bottle_volume", "bottle_weight", "material_density") --- src/slic3r/GUI/Plater.cpp | 4 ++-- src/slic3r/GUI/Tab.cpp | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index b0fb3f0e68..2c07425594 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -1192,9 +1192,9 @@ void Sidebar::update_sliced_info_sizer() { double material_cost = cfg->option("bottle_cost")->getFloat() / cfg->option("bottle_volume")->getFloat(); - str_total_cost = wxString::Format("%.2f", material_cost*(ps.objects_used_material + ps.support_used_material) / 1000); + str_total_cost = wxString::Format("%.3f", material_cost*(ps.objects_used_material + ps.support_used_material) / 1000); } - p->sliced_info->SetTextAndShow(siCost, str_total_cost); + p->sliced_info->SetTextAndShow(siCost, str_total_cost, "Cost"); wxString t_est = std::isnan(ps.estimated_print_time) ? "N/A" : get_time_dhms(float(ps.estimated_print_time)); p->sliced_info->SetTextAndShow(siEstimatedTime, t_est, _(L("Estimated printing time")) + " :"); diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index 353e822e3d..c518ede80d 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -3448,7 +3448,7 @@ void TabSLAMaterial::build() new_conf.set_key_value("bottle_weight", new ConfigOptionFloat(new_bottle_weight)); } if (opt_key == "bottle_weight") { - double new_bottle_volume = boost::any_cast(value)/(new_conf.option("material_density")->getFloat() * 1000); + double new_bottle_volume = boost::any_cast(value)/new_conf.option("material_density")->getFloat() * 1000; new_conf.set_key_value("bottle_volume", new ConfigOptionFloat(new_bottle_volume)); } if (opt_key == "material_density") { @@ -3459,12 +3459,10 @@ void TabSLAMaterial::build() load_config(new_conf); update_dirty(); - on_value_change(opt_key, value); - if (opt_key == "bottle_volume" || opt_key == "bottle_cost") { - wxGetApp().sidebar().update_sliced_info_sizer(); - wxGetApp().sidebar().Layout(); - } + // Change of any from those options influences for an update of "Sliced Info" + wxGetApp().sidebar().update_sliced_info_sizer(); + wxGetApp().sidebar().Layout(); }; optgroup = page->new_optgroup(_(L("Layers"))); From fb65e3152f12faf6d515e8e1c184e00c9a42758b Mon Sep 17 00:00:00 2001 From: YuSanka Date: Wed, 8 Jan 2020 15:26:01 +0100 Subject: [PATCH 03/13] DoubleSlider modes(states) are extended and implemented mode detection from model + Some code refactoring --- src/slic3r/GUI/GUI_Preview.cpp | 6 +++- src/slic3r/GUI/Plater.cpp | 32 ++++++++++++++++++++ src/slic3r/GUI/Plater.hpp | 1 + src/slic3r/GUI/wxExtensions.cpp | 53 ++++++++++++++++++++++----------- src/slic3r/GUI/wxExtensions.hpp | 33 +++++--------------- 5 files changed, 82 insertions(+), 43 deletions(-) diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index 1a4d12d316..0c3138c803 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -692,7 +692,11 @@ void Preview::update_double_slider(const std::vector& layers_z, bool kee bool color_print_enable = (wxGetApp().plater()->printer_technology() == ptFFF); m_slider->EnableTickManipulation(color_print_enable); - m_slider->SetManipulationState(wxGetApp().extruders_edited_cnt()); + + // Detect and set manipulation mode for double slider + m_slider->SetManipulationState( wxGetApp().extruders_edited_cnt() == 1 ? DoubleSlider::msSingleExtruder : + wxGetApp().plater()->is_one_extruder_printed_model() ? DoubleSlider::msMultiAsSingle : + DoubleSlider::msMultiExtruder); } void Preview::reset_double_slider() diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 2c07425594..01241bd8f9 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -5358,6 +5358,38 @@ bool Plater::is_export_gcode_scheduled() const return p->background_process.is_export_scheduled(); } +bool Plater::is_one_extruder_printed_model() +{ + if (wxGetApp().extruders_edited_cnt() == 1) + return true; + + // check if model use just one extruder + const ModelObjectPtrs& objects = p->model.objects; + if (!objects.empty()) + { + const size_t extruder = objects[0]->config.has("extruder") ? + objects[0]->config.option("extruder")->getInt() : 0; + for (ModelObject* object : objects) + { + if (object->config.has("extruder") && + object->config.option("extruder")->getInt() != extruder) + return false; + + for (ModelVolume* volume : object->volumes) + if (volume->config.has("extruder") && + volume->config.option("extruder")->getInt() != extruder) + return false; + + for (const auto& range : object->layer_config_ranges) + if (range.second.has("extruder") && + range.second.option("extruder")->getInt() != extruder) + return false; + } + } + + return true; +} + int Plater::get_selected_object_idx() { return p->get_selected_object_idx(); diff --git a/src/slic3r/GUI/Plater.hpp b/src/slic3r/GUI/Plater.hpp index 4793977058..2c7e13e45a 100644 --- a/src/slic3r/GUI/Plater.hpp +++ b/src/slic3r/GUI/Plater.hpp @@ -234,6 +234,7 @@ public: void set_project_filename(const wxString& filename); bool is_export_gcode_scheduled() const; + bool is_one_extruder_printed_model(); int get_selected_object_idx(); bool is_single_full_object_selection() const; diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index 7b36207f52..3f4d43b5b3 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -2861,13 +2861,13 @@ void DoubleSlider::draw_colored_band(wxDC& dc) const wxColour bg_clr = GetParent()->GetBackgroundColour(); - wxColour clr = m_state == msSingleExtruder ? wxColour(colors[0]) : bg_clr; + wxColour clr = m_ticks_.empty() ? bg_clr : wxColour(colors[0]); draw_band(dc, clr, main_band); size_t i = 1; for (auto tick : m_ticks_) { - if ( (m_state == msSingleExtruder && tick.gcode != Slic3r::ColorChangeCode) || + if ( (m_state != msMultiExtruder/*m_state == msSingleExtruder*/ && tick.gcode != Slic3r::ColorChangeCode) || (m_state == msMultiExtruder && tick.gcode != Slic3r::ExtruderChangeCode) ) continue; @@ -3182,31 +3182,50 @@ void DoubleSlider::OnLeftUp(wxMouseEvent& event) if (m_show_context_menu) { - if (m_state == msMultiExtruder) + if (m_state == msSingleExtruder) + add_code(Slic3r::ColorChangeCode); + else { - wxMenu menu; const int extruders_cnt = Slic3r::GUI::wxGetApp().extruders_edited_cnt(); if (extruders_cnt > 1) - { - const int initial_extruder = get_extruder_for_tick(m_selection == ssLower ? m_lower_value : m_higher_value); + { + wxMenu menu; + wxMenu* sub_menu = new wxMenu(); + wxString sub_menu_name = _(L("Change extruder")); + std::string menu_icon_name = "change_extruder"; - wxMenu* change_extruder_menu = new wxMenu(); + if (m_state == msMultiAsSingle) + { + int initial_extruder = get_extruder_for_tick(m_selection == ssLower ? m_lower_value : m_higher_value); + if (initial_extruder == 0) + initial_extruder = 1; - for (int i = 0; i <= extruders_cnt; i++) { - const wxString item_name = i == 0 ? _(L("Default")) : wxString::Format(_(L("Extruder %d")), i); + for (int i = /*0*/1; i <= extruders_cnt; i++) { + const wxString item_name = i == 0 ? _(L("Default")) : wxString::Format(_(L("Extruder %d")), i); - append_menu_radio_item(change_extruder_menu, wxID_ANY, item_name, "", - [this, i](wxCommandEvent&) { change_extruder(i); }, &menu)->Check(i == initial_extruder); + append_menu_radio_item(sub_menu, wxID_ANY, item_name, "", + [this, i](wxCommandEvent&) { change_extruder(i); }, &menu)->Check(i == initial_extruder); + } + } + else + { + for (int i = 1; i <= extruders_cnt; i++) { + const wxString item_name = wxString::Format(_(L("Extruder %d")), i); + + append_menu_item(sub_menu, wxID_ANY, item_name, "", + [this, i](wxCommandEvent&) { add_code(Slic3r::ColorChangeCode, i); }, "", &menu); + } + + sub_menu_name = from_u8((boost::format(_utf8(L("Add color change (%1%) for:"))) % Slic3r::ColorChangeCode).str()); + menu_icon_name = "colorchange_add_m"; } - wxMenuItem* change_extruder_menu_item = menu.AppendSubMenu(change_extruder_menu, _(L("Change extruder")), _(L("Use another extruder"))); - change_extruder_menu_item->SetBitmap(create_scaled_bitmap(nullptr, "change_extruder")); - } + wxMenuItem* sub_menu_item = menu.AppendSubMenu(sub_menu, sub_menu_name, ""); + sub_menu_item->SetBitmap(create_scaled_bitmap(this, menu_icon_name)); - Slic3r::GUI::wxGetApp().plater()->PopupMenu(&menu); + Slic3r::GUI::wxGetApp().plater()->PopupMenu(&menu); + } } - else - add_code(Slic3r::ColorChangeCode); m_show_context_menu = false; } diff --git a/src/slic3r/GUI/wxExtensions.hpp b/src/slic3r/GUI/wxExtensions.hpp index 390dafab9e..31e60d7d69 100644 --- a/src/slic3r/GUI/wxExtensions.hpp +++ b/src/slic3r/GUI/wxExtensions.hpp @@ -823,7 +823,10 @@ public: enum ManipulationState { msSingleExtruder, // single extruder printer preset is selected - msMultiExtruder // multiple extruder printer preset is selected, and "Whole print" is selected + msMultiAsSingle, // multiple extruder printer preset is selected, but + // this mode works just for Single extruder print + // (For all print from objects settings is used just one extruder) + msMultiExtruder // multiple extruder printer preset is selected }; void SetManipulationState(ManipulationState state) { m_state = state; @@ -976,31 +979,11 @@ private: public: struct ExtrudersSequence { - bool is_mm_intervals; - double interval_by_mm; - int interval_by_layers; - std::vector extruders; + bool is_mm_intervals = true; + double interval_by_mm = 3.0; + int interval_by_layers = 10; + std::vector extruders = { 0 }; - ExtrudersSequence() : - is_mm_intervals(true), - interval_by_mm(3.0), - interval_by_layers(10), - extruders({ 0 }) {} - - ExtrudersSequence(const ExtrudersSequence& other) : - is_mm_intervals(other.is_mm_intervals), - interval_by_mm(other.interval_by_mm), - interval_by_layers(other.interval_by_layers), - extruders(other.extruders) {} - - ExtrudersSequence& operator=(const ExtrudersSequence& other) { - this->is_mm_intervals = other.is_mm_intervals; - this->interval_by_mm = other.interval_by_mm; - this->interval_by_layers= other.interval_by_layers; - this->extruders = other.extruders; - - return *this; - } bool operator==(const ExtrudersSequence& other) const { return (other.is_mm_intervals == this->is_mm_intervals ) && From aed277089beaaec1a7a5a9936d0ba309f7057b21 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Thu, 9 Jan 2020 16:38:59 +0100 Subject: [PATCH 04/13] Updated tooltips and context menus for 3 manipulation mode of DoubleSlider + Some code refactoring --- src/slic3r/GUI/GUI_Preview.cpp | 6 +- src/slic3r/GUI/wxExtensions.cpp | 280 ++++++++++++++++++++++---------- src/slic3r/GUI/wxExtensions.hpp | 50 +++--- 3 files changed, 220 insertions(+), 116 deletions(-) diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index 0c3138c803..acb5c89e3d 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -694,9 +694,9 @@ void Preview::update_double_slider(const std::vector& layers_z, bool kee m_slider->EnableTickManipulation(color_print_enable); // Detect and set manipulation mode for double slider - m_slider->SetManipulationState( wxGetApp().extruders_edited_cnt() == 1 ? DoubleSlider::msSingleExtruder : - wxGetApp().plater()->is_one_extruder_printed_model() ? DoubleSlider::msMultiAsSingle : - DoubleSlider::msMultiExtruder); + m_slider->SetManipulationMode( wxGetApp().extruders_edited_cnt() == 1 ? DoubleSlider::mmSingleExtruder : + wxGetApp().plater()->is_one_extruder_printed_model() ? DoubleSlider::mmMultiAsSingle : + DoubleSlider::mmMultiExtruder); } void Preview::reset_double_slider() diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index 3f4d43b5b3..8ccf605f57 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -2561,7 +2561,7 @@ std::vector DoubleSlider::GetTicksValues() const const int val_size = m_values.size(); if (!m_values.empty()) - for (const TICK_CODE& tick : m_ticks_) { + for (const TICK_CODE& tick : m_ticks) { if (tick.tick > val_size) break; values.emplace_back(t_custom_code{m_values[tick.tick], tick.gcode, tick.extruder, tick.color}); @@ -2575,19 +2575,19 @@ void DoubleSlider::SetTicksValues(const std::vector& heights) if (m_values.empty()) return; - const bool was_empty = m_ticks_.empty(); + const bool was_empty = m_ticks.empty(); - m_ticks_.clear(); + m_ticks.clear(); for (auto h : heights) { auto it = std::lower_bound(m_values.begin(), m_values.end(), h.print_z - epsilon()); if (it == m_values.end()) continue; - m_ticks_.emplace(TICK_CODE{int(it-m_values.begin()), h.gcode, h.extruder, h.color}); + m_ticks.emplace(TICK_CODE{int(it-m_values.begin()), h.gcode, h.extruder, h.color}); } - if (!was_empty && m_ticks_.empty()) + if (!was_empty && m_ticks.empty()) // Switch to the "Feature type"/"Tool" from the very beginning of a new object slicing after deleting of the old one wxPostEvent(this->GetParent(), wxCommandEvent(wxCUSTOMEVT_TICKSCHANGED)); @@ -2638,11 +2638,6 @@ void DoubleSlider::render() // draw line draw_scroll_line(dc, lower_pos, higher_pos); -// //lower slider: -// draw_thumb(dc, lower_pos, ssLower); -// //higher slider: -// draw_thumb(dc, higher_pos, ssHigher); - //draw color print ticks draw_ticks(dc); @@ -2668,7 +2663,7 @@ void DoubleSlider::draw_action_icon(wxDC& dc, const wxPoint pt_beg, const wxPoin return; wxBitmap* icon = m_is_action_icon_focesed ? &m_bmp_add_tick_off.bmp() : &m_bmp_add_tick_on.bmp(); - if (m_ticks_.find(TICK_CODE{tick}) != m_ticks_.end()) + if (m_ticks.find(TICK_CODE{tick}) != m_ticks.end()) icon = m_is_action_icon_focesed ? &m_bmp_del_tick_off.bmp() : &m_bmp_del_tick_on.bmp(); wxCoord x_draw, y_draw; @@ -2811,7 +2806,7 @@ void DoubleSlider::draw_ticks(wxDC& dc) int height, width; get_size(&width, &height); const wxCoord mid = is_horizontal() ? 0.5*height : 0.5*width; - for (auto tick : m_ticks_) + for (auto tick : m_ticks) { const wxCoord pos = get_position_from_value(tick.tick); @@ -2834,6 +2829,96 @@ void DoubleSlider::draw_ticks(wxDC& dc) } } +bool DoubleSlider::get_color_for_tick(wxColour& color, std::set::const_iterator tick_it, const std::vector& colors) const +{ + if (m_mode == mmMultiExtruder) + { + color = GetParent()->GetBackgroundColour(); + return true; + } + + const std::string& code = tick_it->gcode; + if ( code == Slic3r::PausePrintCode || + (code != Slic3r::ColorChangeCode && code != Slic3r::ExtruderChangeCode) ) + return false; + + if (m_mode == mmSingleExtruder) { + if (code == Slic3r::ColorChangeCode) { + color = wxColour(tick_it->color); + return true; + } + return false; + } + + const int extruders_cnt = Slic3r::GUI::wxGetApp().extruders_edited_cnt(); + const int colors_cnt = colors.size(); + + auto get_m600_color_idx = [extruders_cnt, colors_cnt](const std::set& ticks, std::set::const_iterator it) + { + int shift = 0; + while (it != ticks.begin()) { + --it; + if (it->gcode == Slic3r::ColorChangeCode) + shift++; + } + + if (extruders_cnt + shift >= colors_cnt) + return 0; + return extruders_cnt + shift; + }; + + auto get_color_idx_for_tool_change = [extruders_cnt, colors_cnt, get_m600_color_idx](const std::set& ticks, std::set::const_iterator it) + { + const int current_extruder = it->extruder == 0 ? 1 : it->extruder; + if (extruders_cnt == colors_cnt) // there is no one "M600" + return std::min(extruders_cnt - 1, std::max(current_extruder - 1, 0)); + + auto it_n = it; + while (it_n != ticks.begin()) { + --it_n; + if (it_n->gcode == Slic3r::ColorChangeCode && it_n->extruder == current_extruder) + return get_m600_color_idx(ticks, it_n); + } + + return std::min(extruders_cnt - 1, std::max(current_extruder - 1, 0)); + }; + + auto get_color_idx_for_color_change = [get_m600_color_idx](const std::set& ticks, std::set::const_iterator it) + { + auto it_n = it; + bool is_tool_change = false; + while (it_n != ticks.begin()) { + --it_n; + if (it_n->gcode == Slic3r::ExtruderChangeCode) { + is_tool_change = true; + if (it_n->extruder == it->extruder) + return get_m600_color_idx(ticks, it); + break; + } + } + if (!is_tool_change && it->extruder == 0) // use correct extruder number instead of 0 + return get_m600_color_idx(ticks, it); + + return -1; + }; + + // change tool (extruder) + if (code == Slic3r::ExtruderChangeCode) + color = wxColour(colors[get_color_idx_for_tool_change(m_ticks, tick_it)]); + + // change color for current extruder + if (code == Slic3r::ColorChangeCode) { + const int color_idx = get_color_idx_for_color_change(m_ticks, tick_it); + if (color_idx < 0) + return false; + if (color_idx >= colors_cnt) + return false; + color = wxColour(colors[color_idx]); + } + + return true; +} + void DoubleSlider::draw_colored_band(wxDC& dc) { if (!m_is_enabled_tick_manipulation) @@ -2856,29 +2941,40 @@ void DoubleSlider::draw_colored_band(wxDC& dc) dc.DrawRectangle(band_rc); }; - const std::vector& colors = Slic3r::GUI::wxGetApp().plater()->get_extruder_colors_from_plater_config(); - int colors_cnt = colors.size(); + const std::vector& colors = Slic3r::GUI::wxGetApp().plater()->get_colors_for_color_print(); const wxColour bg_clr = GetParent()->GetBackgroundColour(); - wxColour clr = m_ticks_.empty() ? bg_clr : wxColour(colors[0]); + wxColour clr = m_ticks.empty() || m_mode == mmMultiExtruder ? bg_clr : wxColour(colors[0]); draw_band(dc, clr, main_band); - size_t i = 1; - for (auto tick : m_ticks_) - { - if ( (m_state != msMultiExtruder/*m_state == msSingleExtruder*/ && tick.gcode != Slic3r::ColorChangeCode) || - (m_state == msMultiExtruder && tick.gcode != Slic3r::ExtruderChangeCode) ) - continue; + if (m_ticks.empty() || m_mode == mmMultiExtruder) + // don't color a band for MultiExtruder mode + return; - const wxCoord pos = get_position_from_value(tick.tick); + std::set::const_iterator tick_it = m_ticks.begin(); + + while (tick_it != m_ticks.end()) + { + if ( (m_mode == mmSingleExtruder && tick_it->gcode != Slic3r::ColorChangeCode ) || + (m_mode == mmMultiAsSingle && !(tick_it->gcode == Slic3r::ExtruderChangeCode || tick_it->gcode == Slic3r::ColorChangeCode)) ) + { + ++tick_it; + continue; + } + + const wxCoord pos = get_position_from_value(tick_it->tick); is_horizontal() ? main_band.SetLeft(SLIDER_MARGIN + pos) : main_band.SetBottom(pos - 1); - clr = (m_state == msMultiExtruder && tick.color.empty()) ? bg_clr : - m_state == msMultiExtruder ? wxColour(colors[std::min(colors_cnt - 1, tick.extruder-1)]) : wxColour(tick.color); + if (!get_color_for_tick(clr, tick_it, colors)) + { + ++tick_it; + continue; + } + draw_band(dc, clr, main_band); - i++; + ++tick_it; } } @@ -2903,7 +2999,7 @@ void DoubleSlider::draw_one_layer_icon(wxDC& dc) void DoubleSlider::draw_revert_icon(wxDC& dc) { - if (m_ticks_.empty() || !m_is_enabled_tick_manipulation) + if (m_ticks.empty() || !m_is_enabled_tick_manipulation) return; int width, height; @@ -2921,7 +3017,7 @@ void DoubleSlider::draw_revert_icon(wxDC& dc) void DoubleSlider::draw_cog_icon(wxDC& dc) { - if (m_state != msMultiExtruder) + if (m_mode != mmMultiExtruder) return; int width, height; @@ -2965,15 +3061,13 @@ void DoubleSlider::detect_selected_slider(const wxPoint& pt) bool DoubleSlider::is_point_in_rect(const wxPoint& pt, const wxRect& rect) { - if (rect.GetLeft() <= pt.x && pt.x <= rect.GetRight() && - rect.GetTop() <= pt.y && pt.y <= rect.GetBottom()) - return true; - return false; + return rect.GetLeft() <= pt.x && pt.x <= rect.GetRight() && + rect.GetTop() <= pt.y && pt.y <= rect.GetBottom(); } int DoubleSlider::is_point_near_tick(const wxPoint& pt) { - for (auto tick : m_ticks_) { + for (auto tick : m_ticks) { const wxCoord pos = get_position_from_value(tick.tick); if (is_horizontal()) { @@ -3034,10 +3128,10 @@ void DoubleSlider::OnLeftDown(wxMouseEvent& event) m_selection == ssLower ? correct_lower_value() : correct_higher_value(); if (!m_selection) m_selection = ssHigher; - m_ticks_.clear(); + m_ticks.clear(); wxPostEvent(this->GetParent(), wxCommandEvent(wxCUSTOMEVT_TICKSCHANGED)); } - else if (is_point_in_rect(pos, m_rect_cog_icon) && m_state == msMultiExtruder) { + else if (is_point_in_rect(pos, m_rect_cog_icon) && m_mode == mmMultiExtruder) { // show dialog for set extruder sequence m_edit_extruder_sequence = true; } @@ -3107,17 +3201,18 @@ wxString DoubleSlider::get_tooltip(IconFocus icon_focus) else if (m_is_action_icon_focesed) { const int tick = m_selection == ssLower ? m_lower_value : m_higher_value; - const auto tick_code_it = m_ticks_.find(TICK_CODE{tick}); - tooltip = tick_code_it == m_ticks_.end() ? (m_state == msSingleExtruder ? - _(L("For add color change use left mouse button click")) : - _(L("For add change extruder use left mouse button click"))) + "\n" + - _(L("For add another code use right mouse button click")) : - tick_code_it->gcode == Slic3r::ColorChangeCode ? ( m_state == msSingleExtruder ? + const auto tick_code_it = m_ticks.find(TICK_CODE{tick}); + tooltip = tick_code_it == m_ticks.end() ? (m_mode == mmMultiAsSingle ? + _(L("For add change extruder use left mouse button click")) : + _(L("For add color change use left mouse button click")) ) + "\n" + + _(L("For add another code use right mouse button click")) : + tick_code_it->gcode == Slic3r::ColorChangeCode ? ( m_mode == mmSingleExtruder ? _(L("For Delete color change use left mouse button click\n" "For Edit color use right mouse button click")) : from_u8((boost::format(_utf8(L("Delete color change for Extruder %1%"))) % tick_code_it->extruder).str()) ): -// tick_code_it->gcode == Slic3r::PausePrintCode ? _(L("Delete pause")) : - tick_code_it->gcode == Slic3r::ExtruderChangeCode ? + tick_code_it->gcode == Slic3r::PausePrintCode ? + _(L("Delete pause")) : + tick_code_it->gcode == Slic3r::ExtruderChangeCode ? from_u8((boost::format(_utf8(L("Delete extruder change to \"%1%\""))) % tick_code_it->extruder).str()) : from_u8((boost::format(_utf8(L("For Delete \"%1%\" code use left mouse button click\n" "For Edit \"%1%\" code use right mouse button click"))) % tick_code_it->gcode ).str()); @@ -3138,7 +3233,7 @@ void DoubleSlider::OnMotion(wxMouseEvent& event) if (!m_is_left_down && !m_is_one_layer) { m_is_action_icon_focesed = is_point_in_rect(pos, m_rect_tick_action); - if (!m_ticks_.empty() && is_point_in_rect(pos, m_rect_revert_icon)) + if (!m_ticks.empty() && is_point_in_rect(pos, m_rect_revert_icon)) icon_focus = ifRevert; else if (is_point_in_rect(pos, m_rect_cog_icon)) icon_focus = ifCog; @@ -3182,7 +3277,7 @@ void DoubleSlider::OnLeftUp(wxMouseEvent& event) if (m_show_context_menu) { - if (m_state == msSingleExtruder) + if (m_mode == mmSingleExtruder) add_code(Slic3r::ColorChangeCode); else { @@ -3194,7 +3289,7 @@ void DoubleSlider::OnLeftUp(wxMouseEvent& event) wxString sub_menu_name = _(L("Change extruder")); std::string menu_icon_name = "change_extruder"; - if (m_state == msMultiAsSingle) + if (m_mode == mmMultiAsSingle) { int initial_extruder = get_extruder_for_tick(m_selection == ssLower ? m_lower_value : m_higher_value); if (initial_extruder == 0) @@ -3213,7 +3308,8 @@ void DoubleSlider::OnLeftUp(wxMouseEvent& event) const wxString item_name = wxString::Format(_(L("Extruder %d")), i); append_menu_item(sub_menu, wxID_ANY, item_name, "", - [this, i](wxCommandEvent&) { add_code(Slic3r::ColorChangeCode, i); }, "", &menu); + [this, i](wxCommandEvent&) { add_code(Slic3r::ColorChangeCode, i); }, "", &menu, + [this]() {return true; }, this); } sub_menu_name = from_u8((boost::format(_utf8(L("Add color change (%1%) for:"))) % Slic3r::ColorChangeCode).str()); @@ -3285,13 +3381,13 @@ void DoubleSlider::action_tick(const TicksAction action) const int tick = m_selection == ssLower ? m_lower_value : m_higher_value; - const auto it = m_ticks_.find(TICK_CODE{tick}); + const auto it = m_ticks.find(TICK_CODE{tick}); - if (it != m_ticks_.end()) // erase this tick + if (it != m_ticks.end()) // erase this tick { if (action == taAdd) return; - m_ticks_.erase(TICK_CODE{tick}); + m_ticks.erase(TICK_CODE{tick}); wxPostEvent(this->GetParent(), wxCommandEvent(wxCUSTOMEVT_TICKSCHANGED)); Refresh(); @@ -3308,7 +3404,7 @@ void DoubleSlider::action_tick(const TicksAction action) if (m_suppress_add_code) return; m_suppress_add_code = true; - if (m_state != msMultiExtruder) + if (m_mode == mmSingleExtruder) // if (m_mode != mmMultiExtruder) add_code(Slic3r::ColorChangeCode); m_suppress_add_code = false; return; @@ -3395,8 +3491,8 @@ void DoubleSlider::OnRightDown(wxMouseEvent& event) { const int tick = m_selection == ssLower ? m_lower_value : m_higher_value; // if on this Z doesn't exist tick - auto it = m_ticks_.find(TICK_CODE{ tick }); - if (it == m_ticks_.end()) + auto it = m_ticks.find(TICK_CODE{ tick }); + if (it == m_ticks.end()) { // show context menu on OnRightUp() m_show_context_menu = true; @@ -3429,11 +3525,11 @@ void DoubleSlider::OnRightDown(wxMouseEvent& event) int DoubleSlider::get_extruder_for_tick(int tick) { - if (m_ticks_.empty()) + if (m_ticks.empty()) return 0; - auto it = m_ticks_.lower_bound(TICK_CODE{tick}); - while (it != m_ticks_.begin()) { + auto it = m_ticks.lower_bound(TICK_CODE{tick}); + while (it != m_ticks.begin()) { --it; if(it->gcode == Slic3r::ExtruderChangeCode) return it->extruder; @@ -3452,45 +3548,57 @@ void DoubleSlider::OnRightUp(wxMouseEvent& event) if (m_show_context_menu) { wxMenu menu; - if (m_state == msMultiExtruder) + if (m_mode == mmSingleExtruder) + append_menu_item(&menu, wxID_ANY, _(L("Add color change")) + " (M600)", "", + [this](wxCommandEvent&) { add_code(Slic3r::ColorChangeCode); }, "colorchange_add_m", &menu, + [](){return true;}, this); + else { const int extruders_cnt = Slic3r::GUI::wxGetApp().extruders_edited_cnt(); if (extruders_cnt > 1) { - const int initial_extruder = get_extruder_for_tick(m_selection == ssLower ? m_lower_value : m_higher_value); + int initial_extruder = get_extruder_for_tick(m_selection == ssLower ? m_lower_value : m_higher_value); + if (initial_extruder == 0) + initial_extruder = 1; wxMenu* change_extruder_menu = new wxMenu(); wxMenu* add_color_change_menu = new wxMenu(); - for (int i = 0; i <= extruders_cnt; i++) { - const wxString item_name = i == 0 ? _(L("Default")) : wxString::Format(_(L("Extruder %d")), i); + for (int i = 1; i <= extruders_cnt; i++) { + const wxString item_name = wxString::Format(_(L("Extruder %d")), i); + if (m_mode == mmMultiAsSingle) append_menu_radio_item(change_extruder_menu, wxID_ANY, item_name, "", [this, i](wxCommandEvent&) { change_extruder(i); }, &menu)->Check(i == initial_extruder); - if (i==0) // don't use M600 for default extruder, if multimaterial print is selected - continue; append_menu_item(add_color_change_menu, wxID_ANY, item_name, "", - [this, i](wxCommandEvent&) { add_code(Slic3r::ColorChangeCode, i); }, "", &menu); + [this, i](wxCommandEvent&) { add_code(Slic3r::ColorChangeCode, i); }, "", &menu, + [](){return true;}, this); } - wxMenuItem* change_extruder_menu_item = menu.AppendSubMenu(change_extruder_menu, _(L("Change extruder")), _(L("Use another extruder"))); + const wxString change_extruder_menu_name = m_mode == mmMultiAsSingle ? _(L("Change extruder")) : _(L("Change extruder (N/A)")); + + wxMenuItem* change_extruder_menu_item = menu.AppendSubMenu(change_extruder_menu, change_extruder_menu_name, _(L("Use another extruder"))); change_extruder_menu_item->SetBitmap(create_scaled_bitmap(nullptr, "change_extruder")); + ManipulationMode mode = m_mode; + Slic3r::GUI::wxGetApp().plater()->Bind(wxEVT_UPDATE_UI, [this, mode, change_extruder_menu_item](wxUpdateUIEvent& evt) { + enable_menu_item(evt, [this](){return m_mode == mmMultiAsSingle;}, change_extruder_menu_item, this); }, + change_extruder_menu_item->GetId()); + const wxString menu_name = from_u8((boost::format(_utf8(L("Add color change (%1%) for:"))) % Slic3r::ColorChangeCode).str()); wxMenuItem* add_color_change_menu_item = menu.AppendSubMenu(add_color_change_menu, menu_name, ""); add_color_change_menu_item->SetBitmap(create_scaled_bitmap(nullptr, "colorchange_add_m")); } } - else - append_menu_item(&menu, wxID_ANY, _(L("Add color change")) + " (M600)", "", - [this](wxCommandEvent&) { add_code(Slic3r::ColorChangeCode); }, "colorchange_add_m", &menu); append_menu_item(&menu, wxID_ANY, _(L("Add pause print")) + " (M601)", "", - [this](wxCommandEvent&) { add_code(Slic3r::PausePrintCode); }, "pause_print", &menu); + [this](wxCommandEvent&) { add_code(Slic3r::PausePrintCode); }, "pause_print", &menu, + []() {return true; }, this); append_menu_item(&menu, wxID_ANY, _(L("Add custom G-code")), "", - [this](wxCommandEvent&) { add_code(""); }, "edit_gcode", &menu); + [this](wxCommandEvent&) { add_code(""); }, "edit_gcode", &menu, + []() {return true; }, this); Slic3r::GUI::wxGetApp().plater()->PopupMenu(&menu); @@ -3499,7 +3607,7 @@ void DoubleSlider::OnRightUp(wxMouseEvent& event) else if (m_show_edit_menu) { wxMenu menu; - std::set::iterator it = m_ticks_.find(TICK_CODE{ m_selection == ssLower ? m_lower_value : m_higher_value }); + std::set::iterator it = m_ticks.find(TICK_CODE{ m_selection == ssLower ? m_lower_value : m_higher_value }); const bool is_color_change = it->gcode == Slic3r::ColorChangeCode; append_menu_item(&menu, wxID_ANY, it->gcode == Slic3r::ColorChangeCode ? _(L("Edit color")) : @@ -3571,17 +3679,17 @@ void DoubleSlider::add_code(std::string code, int selected_extruder/* = -1*/) { const int tick = m_selection == ssLower ? m_lower_value : m_higher_value; // if on this Z doesn't exist tick - auto it = m_ticks_.find(TICK_CODE{ tick }); - if (it == m_ticks_.end()) + auto it = m_ticks.find(TICK_CODE{ tick }); + if (it == m_ticks.end()) { std::string color = ""; if (code == Slic3r::ColorChangeCode) { std::vector colors = Slic3r::GUI::wxGetApp().plater()->get_extruder_colors_from_plater_config(); - if (m_state == msSingleExtruder && !m_ticks_.empty()) { - auto before_tick_it = std::lower_bound(m_ticks_.begin(), m_ticks_.end(), TICK_CODE{ tick }); - while (before_tick_it != m_ticks_.begin()) { + if (m_mode == mmSingleExtruder && !m_ticks.empty()) { + auto before_tick_it = std::lower_bound(m_ticks.begin(), m_ticks.end(), TICK_CODE{ tick }); + while (before_tick_it != m_ticks.begin()) { --before_tick_it; if (before_tick_it->gcode == Slic3r::ColorChangeCode) { color = before_tick_it->color; @@ -3618,14 +3726,14 @@ void DoubleSlider::add_code(std::string code, int selected_extruder/* = -1*/) } int extruder = 1; - if (m_state == msMultiExtruder) { + if (m_mode != mmSingleExtruder) { if (code == Slic3r::ColorChangeCode && selected_extruder >= 0) extruder = selected_extruder; else extruder = get_extruder_for_tick(m_selection == ssLower ? m_lower_value : m_higher_value); } - m_ticks_.emplace(TICK_CODE{tick, code, extruder, color}); + m_ticks.emplace(TICK_CODE{tick, code, extruder, color}); wxPostEvent(this->GetParent(), wxCommandEvent(wxCUSTOMEVT_TICKSCHANGED)); Refresh(); @@ -3637,8 +3745,8 @@ void DoubleSlider::edit_tick() { const int tick = m_selection == ssLower ? m_lower_value : m_higher_value; // if on this Z exists tick - std::set::iterator it = m_ticks_.find(TICK_CODE{ tick }); - if (it != m_ticks_.end()) + std::set::iterator it = m_ticks.find(TICK_CODE{ tick }); + if (it != m_ticks.end()) { std::string edited_value; if (it->gcode == Slic3r::ColorChangeCode) @@ -3663,8 +3771,8 @@ void DoubleSlider::edit_tick() changed_tick.gcode = edited_value; } - m_ticks_.erase(it); - m_ticks_.emplace(changed_tick); + m_ticks.erase(it); + m_ticks.emplace(changed_tick); wxPostEvent(this->GetParent(), wxCommandEvent(wxCUSTOMEVT_TICKSCHANGED)); } @@ -3677,9 +3785,9 @@ void DoubleSlider::change_extruder(int extruder) std::vector colors = Slic3r::GUI::wxGetApp().plater()->get_extruder_colors_from_plater_config(); // if on this Y doesn't exist tick - if (m_ticks_.find(TICK_CODE{tick}) == m_ticks_.end()) + if (m_ticks.find(TICK_CODE{tick}) == m_ticks.end()) { - m_ticks_.emplace(TICK_CODE{tick, Slic3r::ExtruderChangeCode, extruder, extruder == 0 ? "" : colors[extruder-1]}); + m_ticks.emplace(TICK_CODE{tick, Slic3r::ExtruderChangeCode, extruder, extruder == 0 ? "" : colors[extruder-1]}); wxPostEvent(this->GetParent(), wxCommandEvent(wxCUSTOMEVT_TICKSCHANGED)); Refresh(); @@ -3699,10 +3807,10 @@ void DoubleSlider::edit_extruder_sequence() m_extruders_sequence = from_dlg_val; - auto it = m_ticks_.begin(); - while (it != m_ticks_.end()) { + auto it = m_ticks.begin(); + while (it != m_ticks.end()) { if (it->gcode == Slic3r::ExtruderChangeCode) - it = m_ticks_.erase(it); + it = m_ticks.erase(it); else ++it; } @@ -3717,7 +3825,7 @@ void DoubleSlider::edit_extruder_sequence() while (tick <= m_max_value) { int cur_extruder = m_extruders_sequence.extruders[extruder]; - m_ticks_.emplace(TICK_CODE{tick, Slic3r::ExtruderChangeCode, cur_extruder + 1, colors[cur_extruder]}); + m_ticks.emplace(TICK_CODE{tick, Slic3r::ExtruderChangeCode, cur_extruder + 1, colors[cur_extruder]}); extruder++; if (extruder == extr_cnt) diff --git a/src/slic3r/GUI/wxExtensions.hpp b/src/slic3r/GUI/wxExtensions.hpp index 31e60d7d69..87cc922cf7 100644 --- a/src/slic3r/GUI/wxExtensions.hpp +++ b/src/slic3r/GUI/wxExtensions.hpp @@ -821,20 +821,15 @@ public: EnableTickManipulation(false); } - enum ManipulationState { - msSingleExtruder, // single extruder printer preset is selected - msMultiAsSingle, // multiple extruder printer preset is selected, but + enum ManipulationMode { + mmSingleExtruder, // single extruder printer preset is selected + mmMultiAsSingle, // multiple extruder printer preset is selected, but // this mode works just for Single extruder print // (For all print from objects settings is used just one extruder) - msMultiExtruder // multiple extruder printer preset is selected + mmMultiExtruder // multiple extruder printer preset is selected }; - void SetManipulationState(ManipulationState state) { - m_state = state; - } - void SetManipulationState(int extruders_cnt) { - m_state = extruders_cnt ==1 ? msSingleExtruder : msMultiExtruder; - } - ManipulationState GetManipulationState() const { return m_state; } + void SetManipulationMode(ManipulationMode mode) { m_mode = mode; } + ManipulationMode GetManipulationMode() const { return m_mode; } bool is_horizontal() const { return m_style == wxSL_HORIZONTAL; } bool is_one_layer() const { return m_is_one_layer; } @@ -860,6 +855,17 @@ public: void change_extruder(int extruder); void edit_extruder_sequence(); + struct TICK_CODE + { + bool operator<(const TICK_CODE& other) const { return other.tick > this->tick; } + bool operator>(const TICK_CODE& other) const { return other.tick < this->tick; } + + int tick = 0; + std::string gcode = Slic3r::ColorChangeCode; + int extruder = 0; + std::string color; + }; + protected: void render(); @@ -881,7 +887,6 @@ protected: void detect_selected_slider(const wxPoint& pt); void correct_lower_value(); void correct_higher_value(); - wxString get_tooltip(IconFocus icon_focus); void move_current_thumb(const bool condition); void action_tick(const TicksAction action); void enter_window(wxMouseEvent& event, const bool enter); @@ -897,6 +902,10 @@ protected: wxSize get_size(); void get_size(int *w, int *h); double get_double_value(const SelectedSlider& selection); + wxString get_tooltip(IconFocus icon_focus); + bool get_color_for_tick( wxColour& color, + std::set::const_iterator tick_it, + const std::vector& colors) const; private: bool is_osx { false }; @@ -929,7 +938,7 @@ private: bool m_show_edit_menu = false; bool m_edit_extruder_sequence = false; bool m_suppress_add_code = false; - ManipulationState m_state = msSingleExtruder; + ManipulationMode m_mode = mmSingleExtruder; std::string m_custom_gcode = ""; std::string m_pause_print_msg; @@ -960,21 +969,8 @@ private: std::vector m_line_pens; std::vector m_segm_pens; - std::set m_ticks; std::vector m_values; - - struct TICK_CODE - { - bool operator<(const TICK_CODE& other) const { return other.tick > this->tick; } - bool operator>(const TICK_CODE& other) const { return other.tick < this->tick; } - - int tick = 0; - std::string gcode = Slic3r::ColorChangeCode; - int extruder = 0; - std::string color; - }; - - std::set m_ticks_; + std::set m_ticks; public: struct ExtrudersSequence From 02b2e206281e9cfc89c7efbb4d7d294c1332fe89 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Fri, 10 Jan 2020 16:49:07 +0100 Subject: [PATCH 05/13] Context menus improvements. Disabling unused extruders for color_change --- src/slic3r/GUI/GUI_Preview.cpp | 57 +++++++++- src/slic3r/GUI/GUI_Preview.hpp | 3 +- src/slic3r/GUI/Plater.cpp | 32 ------ src/slic3r/GUI/Plater.hpp | 1 - src/slic3r/GUI/wxExtensions.cpp | 190 ++++++++++++++++++-------------- src/slic3r/GUI/wxExtensions.hpp | 18 ++- 6 files changed, 179 insertions(+), 122 deletions(-) diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index acb5c89e3d..fceaeba3bd 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -692,11 +692,60 @@ void Preview::update_double_slider(const std::vector& layers_z, bool kee bool color_print_enable = (wxGetApp().plater()->printer_technology() == ptFFF); m_slider->EnableTickManipulation(color_print_enable); - // Detect and set manipulation mode for double slider - m_slider->SetManipulationMode( wxGetApp().extruders_edited_cnt() == 1 ? DoubleSlider::mmSingleExtruder : - wxGetApp().plater()->is_one_extruder_printed_model() ? DoubleSlider::mmMultiAsSingle : - DoubleSlider::mmMultiExtruder); + update_double_slider_mode(); +} + +void Preview::update_double_slider_mode() +{ + // true -> single-extruder printer profile OR + // multi-extruder printer profile , but whole model is printed by only one extruder + // false -> multi-extruder printer profile , and model is printed by several extruders + bool one_extruder_printed_model = true; + + // extruder used for whole model for multi-extruder printer profile + int only_extruder = -1; + + if (wxGetApp().extruders_edited_cnt() > 1) + { + const ModelObjectPtrs& objects = wxGetApp().plater()->model().objects; + + // check if whole model uses just only one extruder + if (!objects.empty()) + { + const int extruder = objects[0]->config.has("extruder") ? + objects[0]->config.option("extruder")->getInt() : 0; + + auto is_one_extruder_printed_model = [objects, extruder]() + { + for (ModelObject* object : objects) + { + if (object->config.has("extruder") && + object->config.option("extruder")->getInt() != extruder) + return false; + + if (object->volumes.size() > 1) + for (ModelVolume* volume : object->volumes) + if (volume->config.has("extruder") && + volume->config.option("extruder")->getInt() != extruder) + return false; + + for (const auto& range : object->layer_config_ranges) + if (range.second.has("extruder") && + range.second.option("extruder")->getInt() != extruder) + return false; + } + return true; + }; + + if (is_one_extruder_printed_model()) + only_extruder = extruder; + else + one_extruder_printed_model = false; + } + } + + m_slider->SetModeAndOnlyExtruder(one_extruder_printed_model, only_extruder); } void Preview::reset_double_slider() diff --git a/src/slic3r/GUI/GUI_Preview.hpp b/src/slic3r/GUI/GUI_Preview.hpp index 92ec15b22b..ae93e2ff05 100644 --- a/src/slic3r/GUI/GUI_Preview.hpp +++ b/src/slic3r/GUI/GUI_Preview.hpp @@ -157,8 +157,9 @@ private: void create_double_slider(); void check_slider_values(std::vector &ticks_from_model, const std::vector &layers_z); - void update_double_slider(const std::vector& layers_z, bool keep_z_range = false); void reset_double_slider(); + void update_double_slider(const std::vector& layers_z, bool keep_z_range = false); + void update_double_slider_mode(); // update DoubleSlider after keyDown in canvas void update_double_slider_from_canvas(wxKeyEvent& event); diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 01241bd8f9..2c07425594 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -5358,38 +5358,6 @@ bool Plater::is_export_gcode_scheduled() const return p->background_process.is_export_scheduled(); } -bool Plater::is_one_extruder_printed_model() -{ - if (wxGetApp().extruders_edited_cnt() == 1) - return true; - - // check if model use just one extruder - const ModelObjectPtrs& objects = p->model.objects; - if (!objects.empty()) - { - const size_t extruder = objects[0]->config.has("extruder") ? - objects[0]->config.option("extruder")->getInt() : 0; - for (ModelObject* object : objects) - { - if (object->config.has("extruder") && - object->config.option("extruder")->getInt() != extruder) - return false; - - for (ModelVolume* volume : object->volumes) - if (volume->config.has("extruder") && - volume->config.option("extruder")->getInt() != extruder) - return false; - - for (const auto& range : object->layer_config_ranges) - if (range.second.has("extruder") && - range.second.option("extruder")->getInt() != extruder) - return false; - } - } - - return true; -} - int Plater::get_selected_object_idx() { return p->get_selected_object_idx(); diff --git a/src/slic3r/GUI/Plater.hpp b/src/slic3r/GUI/Plater.hpp index 2c7e13e45a..4793977058 100644 --- a/src/slic3r/GUI/Plater.hpp +++ b/src/slic3r/GUI/Plater.hpp @@ -234,7 +234,6 @@ public: void set_project_filename(const wxString& filename); bool is_export_gcode_scheduled() const; - bool is_one_extruder_printed_model(); int get_selected_object_idx(); bool is_single_full_object_selection() const; diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index 8ccf605f57..a831f32bb6 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -2883,7 +2883,9 @@ bool DoubleSlider::get_color_for_tick(wxColour& color, std::set::cons return std::min(extruders_cnt - 1, std::max(current_extruder - 1, 0)); }; - auto get_color_idx_for_color_change = [get_m600_color_idx](const std::set& ticks, std::set::const_iterator it) + const int extruder = std::max(1, m_only_extruder); + + auto get_color_idx_for_color_change = [get_m600_color_idx, extruder](const std::set& ticks, std::set::const_iterator it) { auto it_n = it; bool is_tool_change = false; @@ -2896,7 +2898,7 @@ bool DoubleSlider::get_color_for_tick(wxColour& color, std::set::cons break; } } - if (!is_tool_change && it->extruder == 0) // use correct extruder number instead of 0 + if (!is_tool_change && it->extruder == extruder) // use correct extruder number instead of 0 return get_m600_color_idx(ticks, it); return -1; @@ -2943,9 +2945,9 @@ void DoubleSlider::draw_colored_band(wxDC& dc) const std::vector& colors = Slic3r::GUI::wxGetApp().plater()->get_colors_for_color_print(); - const wxColour bg_clr = GetParent()->GetBackgroundColour(); - - wxColour clr = m_ticks.empty() || m_mode == mmMultiExtruder ? bg_clr : wxColour(colors[0]); + wxColour clr = m_ticks.empty() || m_mode == mmMultiExtruder ? + GetParent()->GetBackgroundColour() : + wxColour(colors[m_mode==mmMultiAsSingle ? std::max(m_only_extruder - 1, 0) : 0]); draw_band(dc, clr, main_band); if (m_ticks.empty() || m_mode == mmMultiExtruder) @@ -2972,7 +2974,7 @@ void DoubleSlider::draw_colored_band(wxDC& dc) ++tick_it; continue; } - + draw_band(dc, clr, main_band); ++tick_it; } @@ -3268,6 +3270,62 @@ void DoubleSlider::OnMotion(wxMouseEvent& event) } } +void DoubleSlider::append_change_extruder_menu_item(wxMenu* menu) +{ + const int extruders_cnt = Slic3r::GUI::wxGetApp().extruders_edited_cnt(); + if (extruders_cnt > 1) + { + const int initial_extruder = std::max(1 , get_extruder_for_tick(m_selection == ssLower ? m_lower_value : m_higher_value)); + + wxMenu* change_extruder_menu = new wxMenu(); + + for (int i = 1; i <= extruders_cnt; i++) + { + const wxString item_name = wxString::Format(_(L("Extruder %d")), i); + + if (m_mode == mmMultiAsSingle) + append_menu_radio_item(change_extruder_menu, wxID_ANY, item_name, "", + [this, i](wxCommandEvent&) { change_extruder(i); }, menu)->Check(i == initial_extruder); + } + + const wxString change_extruder_menu_name = m_mode == mmMultiAsSingle ? _(L("Change extruder")) : _(L("Change extruder (N/A)")); + + wxMenuItem* change_extruder_menu_item = menu->AppendSubMenu(change_extruder_menu, change_extruder_menu_name, _(L("Use another extruder"))); + change_extruder_menu_item->SetBitmap(create_scaled_bitmap(this, "change_extruder")); + + Slic3r::GUI::wxGetApp().plater()->Bind(wxEVT_UPDATE_UI, [this, change_extruder_menu_item](wxUpdateUIEvent& evt) { + enable_menu_item(evt, [this]() {return m_mode == mmMultiAsSingle; }, change_extruder_menu_item, this); }, + change_extruder_menu_item->GetId()); + } +} + +void DoubleSlider::append_add_color_change_menu_item(wxMenu* menu) +{ + const int extruders_cnt = Slic3r::GUI::wxGetApp().extruders_edited_cnt(); + if (extruders_cnt > 1) + { + std::set used_extruders_for_tick = get_used_extruders_for_tick(m_selection == ssLower ? m_lower_value : m_higher_value); + + wxMenu* add_color_change_menu = new wxMenu(); + + for (int i = 1; i <= extruders_cnt; i++) + { + const bool is_used_extruder = used_extruders_for_tick.empty() ? true : // #ys_FIXME till used_extruders_for_tick doesn't filled correct for mmMultiExtruder + used_extruders_for_tick.find(i) != used_extruders_for_tick.end(); + const wxString item_name = wxString::Format(_(L("Extruder %d")), i) + + (is_used_extruder ? "" : " (" + _(L("N/A")) + ")"); + + append_menu_item(add_color_change_menu, wxID_ANY, item_name, "", + [this, i](wxCommandEvent&) { add_code(Slic3r::ColorChangeCode, i); }, "", menu, + [is_used_extruder]() { return is_used_extruder; }, Slic3r::GUI::wxGetApp().plater()); + } + + const wxString menu_name = from_u8((boost::format(_utf8(L("Add color change (%1%) for:"))) % Slic3r::ColorChangeCode).str()); + wxMenuItem* add_color_change_menu_item = menu->AppendSubMenu(add_color_change_menu, menu_name, ""); + add_color_change_menu_item->SetBitmap(create_scaled_bitmap(nullptr, "colorchange_add_m")); + } +} + void DoubleSlider::OnLeftUp(wxMouseEvent& event) { if (!HasCapture()) @@ -3281,46 +3339,14 @@ void DoubleSlider::OnLeftUp(wxMouseEvent& event) add_code(Slic3r::ColorChangeCode); else { - const int extruders_cnt = Slic3r::GUI::wxGetApp().extruders_edited_cnt(); - if (extruders_cnt > 1) - { - wxMenu menu; - wxMenu* sub_menu = new wxMenu(); - wxString sub_menu_name = _(L("Change extruder")); - std::string menu_icon_name = "change_extruder"; + wxMenu menu; - if (m_mode == mmMultiAsSingle) - { - int initial_extruder = get_extruder_for_tick(m_selection == ssLower ? m_lower_value : m_higher_value); - if (initial_extruder == 0) - initial_extruder = 1; + if (m_mode == mmMultiAsSingle) + append_change_extruder_menu_item(&menu); + else + append_add_color_change_menu_item(&menu); - for (int i = /*0*/1; i <= extruders_cnt; i++) { - const wxString item_name = i == 0 ? _(L("Default")) : wxString::Format(_(L("Extruder %d")), i); - - append_menu_radio_item(sub_menu, wxID_ANY, item_name, "", - [this, i](wxCommandEvent&) { change_extruder(i); }, &menu)->Check(i == initial_extruder); - } - } - else - { - for (int i = 1; i <= extruders_cnt; i++) { - const wxString item_name = wxString::Format(_(L("Extruder %d")), i); - - append_menu_item(sub_menu, wxID_ANY, item_name, "", - [this, i](wxCommandEvent&) { add_code(Slic3r::ColorChangeCode, i); }, "", &menu, - [this]() {return true; }, this); - } - - sub_menu_name = from_u8((boost::format(_utf8(L("Add color change (%1%) for:"))) % Slic3r::ColorChangeCode).str()); - menu_icon_name = "colorchange_add_m"; - } - - wxMenuItem* sub_menu_item = menu.AppendSubMenu(sub_menu, sub_menu_name, ""); - sub_menu_item->SetBitmap(create_scaled_bitmap(this, menu_icon_name)); - - Slic3r::GUI::wxGetApp().plater()->PopupMenu(&menu); - } + Slic3r::GUI::wxGetApp().plater()->PopupMenu(&menu); } m_show_context_menu = false; @@ -3525,8 +3551,9 @@ void DoubleSlider::OnRightDown(wxMouseEvent& event) int DoubleSlider::get_extruder_for_tick(int tick) { + int default_initial_extruder = m_mode == mmMultiAsSingle ? m_only_extruder : 0; if (m_ticks.empty()) - return 0; + return default_initial_extruder; auto it = m_ticks.lower_bound(TICK_CODE{tick}); while (it != m_ticks.begin()) { @@ -3535,7 +3562,40 @@ int DoubleSlider::get_extruder_for_tick(int tick) return it->extruder; } - return 0; + return default_initial_extruder; +} + +std::set DoubleSlider::get_used_extruders_for_tick(int tick) +{ + if (m_mode == mmMultiExtruder) + return {}; // #ys_FIXME: correct fill used_extruders_for_tick for mmMultiExtruder + + const int default_initial_extruder = m_mode == mmMultiAsSingle ? std::max(m_only_extruder, 1) : 1; + if (m_ticks.empty()) + return {default_initial_extruder}; + + std::set used_extruders; + + auto it = m_ticks.lower_bound(TICK_CODE{tick}); + while (it != m_ticks.begin()) { + --it; + if(it->gcode == Slic3r::ExtruderChangeCode) + { + used_extruders.emplace(it->extruder); + break; + } + } + if (it == m_ticks.begin()) + used_extruders.emplace(default_initial_extruder); + ++it; + + while (it != m_ticks.end()) { + if(it->gcode == Slic3r::ExtruderChangeCode) + used_extruders.emplace(it->extruder); + ++it; + } + + return used_extruders; } void DoubleSlider::OnRightUp(wxMouseEvent& event) @@ -3554,42 +3614,8 @@ void DoubleSlider::OnRightUp(wxMouseEvent& event) [](){return true;}, this); else { - const int extruders_cnt = Slic3r::GUI::wxGetApp().extruders_edited_cnt(); - if (extruders_cnt > 1) - { - int initial_extruder = get_extruder_for_tick(m_selection == ssLower ? m_lower_value : m_higher_value); - if (initial_extruder == 0) - initial_extruder = 1; - - wxMenu* change_extruder_menu = new wxMenu(); - wxMenu* add_color_change_menu = new wxMenu(); - - for (int i = 1; i <= extruders_cnt; i++) { - const wxString item_name = wxString::Format(_(L("Extruder %d")), i); - - if (m_mode == mmMultiAsSingle) - append_menu_radio_item(change_extruder_menu, wxID_ANY, item_name, "", - [this, i](wxCommandEvent&) { change_extruder(i); }, &menu)->Check(i == initial_extruder); - - append_menu_item(add_color_change_menu, wxID_ANY, item_name, "", - [this, i](wxCommandEvent&) { add_code(Slic3r::ColorChangeCode, i); }, "", &menu, - [](){return true;}, this); - } - - const wxString change_extruder_menu_name = m_mode == mmMultiAsSingle ? _(L("Change extruder")) : _(L("Change extruder (N/A)")); - - wxMenuItem* change_extruder_menu_item = menu.AppendSubMenu(change_extruder_menu, change_extruder_menu_name, _(L("Use another extruder"))); - change_extruder_menu_item->SetBitmap(create_scaled_bitmap(nullptr, "change_extruder")); - - ManipulationMode mode = m_mode; - Slic3r::GUI::wxGetApp().plater()->Bind(wxEVT_UPDATE_UI, [this, mode, change_extruder_menu_item](wxUpdateUIEvent& evt) { - enable_menu_item(evt, [this](){return m_mode == mmMultiAsSingle;}, change_extruder_menu_item, this); }, - change_extruder_menu_item->GetId()); - - const wxString menu_name = from_u8((boost::format(_utf8(L("Add color change (%1%) for:"))) % Slic3r::ColorChangeCode).str()); - wxMenuItem* add_color_change_menu_item = menu.AppendSubMenu(add_color_change_menu, menu_name, ""); - add_color_change_menu_item->SetBitmap(create_scaled_bitmap(nullptr, "colorchange_add_m")); - } + append_change_extruder_menu_item(&menu); + append_add_color_change_menu_item(&menu); } append_menu_item(&menu, wxID_ANY, _(L("Add pause print")) + " (M601)", "", diff --git a/src/slic3r/GUI/wxExtensions.hpp b/src/slic3r/GUI/wxExtensions.hpp index 87cc922cf7..35357cea48 100644 --- a/src/slic3r/GUI/wxExtensions.hpp +++ b/src/slic3r/GUI/wxExtensions.hpp @@ -831,6 +831,14 @@ public: void SetManipulationMode(ManipulationMode mode) { m_mode = mode; } ManipulationMode GetManipulationMode() const { return m_mode; } + void SetModeAndOnlyExtruder(const bool is_one_extruder_printed_model, const int only_extruder) + { + m_mode = !is_one_extruder_printed_model ? mmMultiExtruder : + only_extruder < 0 ? mmSingleExtruder : + mmMultiAsSingle; + m_only_extruder = only_extruder; + } + bool is_horizontal() const { return m_style == wxSL_HORIZONTAL; } bool is_one_layer() const { return m_is_one_layer; } bool is_lower_at_min() const { return m_lower_value == m_min_value; } @@ -848,7 +856,6 @@ public: void OnKeyUp(wxKeyEvent &event); void OnChar(wxKeyEvent &event); void OnRightDown(wxMouseEvent& event); - int get_extruder_for_tick(int tick); void OnRightUp(wxMouseEvent& event); void add_code(std::string code, int selected_extruder = -1); void edit_tick(); @@ -906,6 +913,12 @@ protected: bool get_color_for_tick( wxColour& color, std::set::const_iterator tick_it, const std::vector& colors) const; + int get_extruder_for_tick(int tick); + std::set get_used_extruders_for_tick(int tick); + + + void append_change_extruder_menu_item(wxMenu*); + void append_add_color_change_menu_item(wxMenu*); private: bool is_osx { false }; @@ -926,7 +939,7 @@ private: ScalableBitmap m_bmp_one_layer_unlock_off; ScalableBitmap m_bmp_revert; ScalableBitmap m_bmp_cog; - SelectedSlider m_selection; + SelectedSlider m_selection; bool m_is_left_down = false; bool m_is_right_down = false; bool m_is_one_layer = false; @@ -941,6 +954,7 @@ private: ManipulationMode m_mode = mmSingleExtruder; std::string m_custom_gcode = ""; std::string m_pause_print_msg; + int m_only_extruder = -1; wxRect m_rect_lower_thumb; wxRect m_rect_higher_thumb; From 36de2c5d90df562e0c7b243c942814090b05dd61 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Mon, 13 Jan 2020 17:03:10 +0100 Subject: [PATCH 06/13] Code refactoring for context menus and current extruder color selection for tick --- src/slic3r/GUI/wxExtensions.cpp | 196 ++++++++++++-------------------- src/slic3r/GUI/wxExtensions.hpp | 9 +- 2 files changed, 79 insertions(+), 126 deletions(-) diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index a831f32bb6..43af0ab7fa 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -2829,96 +2829,38 @@ void DoubleSlider::draw_ticks(wxDC& dc) } } -bool DoubleSlider::get_color_for_tick(wxColour& color, std::set::const_iterator tick_it, const std::vector& colors) const +std::string DoubleSlider::get_color_for_tool_change_tick(std::set::const_iterator it) const { - if (m_mode == mmMultiExtruder) - { - color = GetParent()->GetBackgroundColour(); - return true; + const int current_extruder = it->extruder == 0 ? std::max(m_only_extruder, 1) : it->extruder; + + auto it_n = it; + while (it_n != m_ticks.begin()) { + --it_n; + if (it_n->gcode == Slic3r::ColorChangeCode && it_n->extruder == current_extruder) + return it_n->color; } - const std::string& code = tick_it->gcode; - if ( code == Slic3r::PausePrintCode || - (code != Slic3r::ColorChangeCode && code != Slic3r::ExtruderChangeCode) ) - return false; + return it->color; +} - if (m_mode == mmSingleExtruder) { - if (code == Slic3r::ColorChangeCode) { - color = wxColour(tick_it->color); - return true; +std::string DoubleSlider::get_color_for_color_change_tick(std::set::const_iterator it) const +{ + const int def_extruder = std::max(1, m_only_extruder); + auto it_n = it; + bool is_tool_change = false; + while (it_n != m_ticks.begin()) { + --it_n; + if (it_n->gcode == Slic3r::ExtruderChangeCode) { + is_tool_change = true; + if (it_n->extruder == it->extruder) + return it->color; + break; } - return false; } + if (!is_tool_change && it->extruder == def_extruder) + return it->color; - const int extruders_cnt = Slic3r::GUI::wxGetApp().extruders_edited_cnt(); - const int colors_cnt = colors.size(); - - auto get_m600_color_idx = [extruders_cnt, colors_cnt](const std::set& ticks, std::set::const_iterator it) - { - int shift = 0; - while (it != ticks.begin()) { - --it; - if (it->gcode == Slic3r::ColorChangeCode) - shift++; - } - - if (extruders_cnt + shift >= colors_cnt) - return 0; - return extruders_cnt + shift; - }; - - auto get_color_idx_for_tool_change = [extruders_cnt, colors_cnt, get_m600_color_idx](const std::set& ticks, std::set::const_iterator it) - { - const int current_extruder = it->extruder == 0 ? 1 : it->extruder; - if (extruders_cnt == colors_cnt) // there is no one "M600" - return std::min(extruders_cnt - 1, std::max(current_extruder - 1, 0)); - - auto it_n = it; - while (it_n != ticks.begin()) { - --it_n; - if (it_n->gcode == Slic3r::ColorChangeCode && it_n->extruder == current_extruder) - return get_m600_color_idx(ticks, it_n); - } - - return std::min(extruders_cnt - 1, std::max(current_extruder - 1, 0)); - }; - - const int extruder = std::max(1, m_only_extruder); - - auto get_color_idx_for_color_change = [get_m600_color_idx, extruder](const std::set& ticks, std::set::const_iterator it) - { - auto it_n = it; - bool is_tool_change = false; - while (it_n != ticks.begin()) { - --it_n; - if (it_n->gcode == Slic3r::ExtruderChangeCode) { - is_tool_change = true; - if (it_n->extruder == it->extruder) - return get_m600_color_idx(ticks, it); - break; - } - } - if (!is_tool_change && it->extruder == extruder) // use correct extruder number instead of 0 - return get_m600_color_idx(ticks, it); - - return -1; - }; - - // change tool (extruder) - if (code == Slic3r::ExtruderChangeCode) - color = wxColour(colors[get_color_idx_for_tool_change(m_ticks, tick_it)]); - - // change color for current extruder - if (code == Slic3r::ColorChangeCode) { - const int color_idx = get_color_idx_for_color_change(m_ticks, tick_it); - if (color_idx < 0) - return false; - if (color_idx >= colors_cnt) - return false; - color = wxColour(colors[color_idx]); - } - - return true; + return ""; } void DoubleSlider::draw_colored_band(wxDC& dc) @@ -2943,39 +2885,35 @@ void DoubleSlider::draw_colored_band(wxDC& dc) dc.DrawRectangle(band_rc); }; - const std::vector& colors = Slic3r::GUI::wxGetApp().plater()->get_colors_for_color_print(); - - wxColour clr = m_ticks.empty() || m_mode == mmMultiExtruder ? - GetParent()->GetBackgroundColour() : - wxColour(colors[m_mode==mmMultiAsSingle ? std::max(m_only_extruder - 1, 0) : 0]); - draw_band(dc, clr, main_band); - + // don't color a band for MultiExtruder mode if (m_ticks.empty() || m_mode == mmMultiExtruder) - // don't color a band for MultiExtruder mode + { + draw_band(dc, GetParent()->GetBackgroundColour(), main_band); return; + } + + const int default_color_idx = m_mode==mmMultiAsSingle ? std::max(m_only_extruder - 1, 0) : 0; + draw_band(dc, wxColour(Slic3r::GUI::wxGetApp().plater()->get_extruder_colors_from_plater_config()[default_color_idx]), main_band); std::set::const_iterator tick_it = m_ticks.begin(); while (tick_it != m_ticks.end()) { - if ( (m_mode == mmSingleExtruder && tick_it->gcode != Slic3r::ColorChangeCode ) || - (m_mode == mmMultiAsSingle && !(tick_it->gcode == Slic3r::ExtruderChangeCode || tick_it->gcode == Slic3r::ColorChangeCode)) ) - { - ++tick_it; - continue; + if ( (m_mode == mmSingleExtruder && tick_it->gcode == Slic3r::ColorChangeCode ) || + (m_mode == mmMultiAsSingle && (tick_it->gcode == Slic3r::ExtruderChangeCode || tick_it->gcode == Slic3r::ColorChangeCode)) ) + { + const wxCoord pos = get_position_from_value(tick_it->tick); + is_horizontal() ? main_band.SetLeft(SLIDER_MARGIN + pos) : + main_band.SetBottom(pos - 1); + + const std::string clr_str = m_mode == mmSingleExtruder ? tick_it->color : + tick_it->gcode == Slic3r::ExtruderChangeCode ? + get_color_for_tool_change_tick(tick_it) : + get_color_for_color_change_tick(tick_it); + + if (!clr_str.empty()) + draw_band(dc, wxColour(clr_str), main_band); } - - const wxCoord pos = get_position_from_value(tick_it->tick); - is_horizontal() ? main_band.SetLeft(SLIDER_MARGIN + pos) : - main_band.SetBottom(pos - 1); - - if (!get_color_for_tick(clr, tick_it, colors)) - { - ++tick_it; - continue; - } - - draw_band(dc, clr, main_band); ++tick_it; } } @@ -3281,11 +3219,16 @@ void DoubleSlider::append_change_extruder_menu_item(wxMenu* menu) for (int i = 1; i <= extruders_cnt; i++) { - const wxString item_name = wxString::Format(_(L("Extruder %d")), i); + const bool is_active_extruder = i == initial_extruder; + const wxString item_name = wxString::Format(_(L("Extruder %d")), i) + + (is_active_extruder ? " (" + _(L("active")) + ")" : ""); if (m_mode == mmMultiAsSingle) - append_menu_radio_item(change_extruder_menu, wxID_ANY, item_name, "", - [this, i](wxCommandEvent&) { change_extruder(i); }, menu)->Check(i == initial_extruder); + append_menu_item(change_extruder_menu, wxID_ANY, item_name, "", + [this, i](wxCommandEvent&) { change_extruder(i); }, "", menu, + [is_active_extruder]() { return !is_active_extruder; }, Slic3r::GUI::wxGetApp().plater()); +// append_menu_radio_item(change_extruder_menu, wxID_ANY, item_name, "", +// [this, i](wxCommandEvent&) { change_extruder(i); }, menu)->Check(i == initial_extruder); } const wxString change_extruder_menu_name = m_mode == mmMultiAsSingle ? _(L("Change extruder")) : _(L("Change extruder (N/A)")); @@ -3313,7 +3256,7 @@ void DoubleSlider::append_add_color_change_menu_item(wxMenu* menu) const bool is_used_extruder = used_extruders_for_tick.empty() ? true : // #ys_FIXME till used_extruders_for_tick doesn't filled correct for mmMultiExtruder used_extruders_for_tick.find(i) != used_extruders_for_tick.end(); const wxString item_name = wxString::Format(_(L("Extruder %d")), i) + - (is_used_extruder ? "" : " (" + _(L("N/A")) + ")"); + (is_used_extruder ? " (" + _(L("used")) + ")" : ""); append_menu_item(add_color_change_menu, wxID_ANY, item_name, "", [this, i](wxCommandEvent&) { add_code(Slic3r::ColorChangeCode, i); }, "", menu, @@ -3575,8 +3518,15 @@ std::set DoubleSlider::get_used_extruders_for_tick(int tick) return {default_initial_extruder}; std::set used_extruders; - - auto it = m_ticks.lower_bound(TICK_CODE{tick}); + auto it_start = m_ticks.lower_bound(TICK_CODE{tick}); + + auto it = it_start; + if (it == m_ticks.begin() && it->gcode == Slic3r::ExtruderChangeCode) { + used_extruders.emplace(it->extruder); + if (tick < it->tick) + used_extruders.emplace(default_initial_extruder); + } + while (it != m_ticks.begin()) { --it; if(it->gcode == Slic3r::ExtruderChangeCode) @@ -3585,10 +3535,11 @@ std::set DoubleSlider::get_used_extruders_for_tick(int tick) break; } } - if (it == m_ticks.begin()) - used_extruders.emplace(default_initial_extruder); - ++it; + if (it == m_ticks.begin() && used_extruders.empty()) + used_extruders.emplace(default_initial_extruder); + + it = it_start; while (it != m_ticks.end()) { if(it->gcode == Slic3r::ExtruderChangeCode) used_extruders.emplace(it->extruder); @@ -3713,21 +3664,22 @@ void DoubleSlider::add_code(std::string code, int selected_extruder/* = -1*/) { std::vector colors = Slic3r::GUI::wxGetApp().plater()->get_extruder_colors_from_plater_config(); - if (m_mode == mmSingleExtruder && !m_ticks.empty()) { + if (m_ticks.empty()) + color = colors[selected_extruder > 0 ? selected_extruder - 1 : std::max(1, m_only_extruder)-1]; + else + { auto before_tick_it = std::lower_bound(m_ticks.begin(), m_ticks.end(), TICK_CODE{ tick }); while (before_tick_it != m_ticks.begin()) { --before_tick_it; - if (before_tick_it->gcode == Slic3r::ColorChangeCode) { + if (before_tick_it->gcode == Slic3r::ColorChangeCode && before_tick_it->extruder == selected_extruder) { color = before_tick_it->color; break; } } if (color.empty()) - color = colors[0]; + color = colors[selected_extruder > 0 ? selected_extruder - 1 : std::max(1, m_only_extruder) - 1]; } - else - color = colors[selected_extruder > 0 ? selected_extruder-1 : 0]; color = get_new_color(color); if (color.empty()) diff --git a/src/slic3r/GUI/wxExtensions.hpp b/src/slic3r/GUI/wxExtensions.hpp index 35357cea48..81f4d73423 100644 --- a/src/slic3r/GUI/wxExtensions.hpp +++ b/src/slic3r/GUI/wxExtensions.hpp @@ -898,6 +898,8 @@ protected: void action_tick(const TicksAction action); void enter_window(wxMouseEvent& event, const bool enter); +private: + bool is_point_in_rect(const wxPoint& pt, const wxRect& rect); int is_point_near_tick(const wxPoint& pt); @@ -910,9 +912,9 @@ protected: void get_size(int *w, int *h); double get_double_value(const SelectedSlider& selection); wxString get_tooltip(IconFocus icon_focus); - bool get_color_for_tick( wxColour& color, - std::set::const_iterator tick_it, - const std::vector& colors) const; + + std::string get_color_for_tool_change_tick(std::set::const_iterator it) const; + std::string get_color_for_color_change_tick(std::set::const_iterator it) const; int get_extruder_for_tick(int tick); std::set get_used_extruders_for_tick(int tick); @@ -920,7 +922,6 @@ protected: void append_change_extruder_menu_item(wxMenu*); void append_add_color_change_menu_item(wxMenu*); -private: bool is_osx { false }; wxFont m_font; int m_min_value; From 15f873dd74824b1a35c44eb4e9333658f300fc79 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Wed, 15 Jan 2020 15:35:56 +0100 Subject: [PATCH 07/13] DoubleSlider: Implemented code for check of used extruders for MustiAsSingle mode --- src/libslic3r/GCode/ToolOrdering.cpp | 3 ++- src/libslic3r/Print.hpp | 1 + src/slic3r/GUI/wxExtensions.cpp | 21 ++++++++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/libslic3r/GCode/ToolOrdering.cpp b/src/libslic3r/GCode/ToolOrdering.cpp index a76f0fafbf..27abc359a6 100644 --- a/src/libslic3r/GCode/ToolOrdering.cpp +++ b/src/libslic3r/GCode/ToolOrdering.cpp @@ -486,7 +486,8 @@ void ToolOrdering::assign_custom_gcodes(const Print &print) print_z_below = it_lt_below->print_z; if (custom_gcode.print_z > print_z_below + 0.5 * EPSILON) { // The custom G-code applies to the current layer. - if (custom_gcode.gcode != ColorChangeCode || extruder_printing_above[unsigned(custom_gcode.extruder - 1)]) + if ( custom_gcode.gcode != ColorChangeCode || + (custom_gcode.extruder <= num_extruders && extruder_printing_above[unsigned(custom_gcode.extruder - 1)])) // If it is color change, it will actually be useful as the exturder above will print. lt.custom_gcode = &custom_gcode; // Consume that custom G-code event. diff --git a/src/libslic3r/Print.hpp b/src/libslic3r/Print.hpp index 42f8d761e1..3d1f596d19 100644 --- a/src/libslic3r/Print.hpp +++ b/src/libslic3r/Print.hpp @@ -382,6 +382,7 @@ public: // Accessed by SupportMaterial const PrintRegion* get_region(size_t idx) const { return m_regions[idx]; } + const ToolOrdering& get_tool_ordering() const { return m_wipe_tower_data.tool_ordering; } // #ys_FIXME just for testing protected: // methods for handling regions diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index 3872db3fbd..8e1cd0c6f2 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -5,6 +5,7 @@ #include "libslic3r/Utils.hpp" #include "libslic3r/Model.hpp" +#include "libslic3r/Print.hpp" #include #include @@ -3487,7 +3488,25 @@ int DoubleSlider::get_extruder_for_tick(int tick) std::set DoubleSlider::get_used_extruders_for_tick(int tick) { if (m_mode == mmMultiExtruder) - return {}; // #ys_FIXME: correct fill used_extruders_for_tick for mmMultiExtruder + { + // #ys_FIXME: get tool ordering from _correct_ place + const Slic3r::ToolOrdering& tool_ordering = Slic3r::GUI::wxGetApp().plater()->fff_print().get_tool_ordering(); + + if (tool_ordering.empty()) + return {}; + + std::set used_extruders; + + auto it_layer_tools = std::lower_bound(tool_ordering.begin(), tool_ordering.end(), Slic3r::LayerTools(m_values[tick])); + for (; it_layer_tools != tool_ordering.end(); it_layer_tools++) + { + const std::vector& extruders = it_layer_tools->extruders; + for (const auto& extruder : extruders) + used_extruders.emplace(extruder+1); + } + + return used_extruders; + } const int default_initial_extruder = m_mode == mmMultiAsSingle ? std::max(m_only_extruder, 1) : 1; if (m_ticks.empty()) From 8ef29aab78ebbf862914eff14b5e111a4b065e9a Mon Sep 17 00:00:00 2001 From: YuSanka Date: Thu, 16 Jan 2020 10:20:36 +0100 Subject: [PATCH 08/13] Refactoring of DoubleSlider::add_code() --- src/slic3r/GUI/wxExtensions.cpp | 96 ++++++++++++++++----------------- 1 file changed, 45 insertions(+), 51 deletions(-) diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index 8e1cd0c6f2..f2fed3bf76 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -3652,66 +3652,60 @@ void DoubleSlider::add_code(std::string code, int selected_extruder/* = -1*/) const int tick = m_selection == ssLower ? m_lower_value : m_higher_value; // if on this Z doesn't exist tick auto it = m_ticks.find(TICK_CODE{ tick }); - if (it == m_ticks.end()) + if (it != m_ticks.end()) + return; + + std::string color; + const int extruder = selected_extruder > 0 ? selected_extruder : std::max(1, m_only_extruder); + + if (code == Slic3r::ColorChangeCode) { - std::string color = ""; - if (code == Slic3r::ColorChangeCode) + std::vector colors = Slic3r::GUI::wxGetApp().plater()->get_extruder_colors_from_plater_config(); + + if (m_ticks.empty()) + color = colors[extruder-1]; + else { - std::vector colors = Slic3r::GUI::wxGetApp().plater()->get_extruder_colors_from_plater_config(); - - if (m_ticks.empty()) - color = colors[selected_extruder > 0 ? selected_extruder - 1 : std::max(1, m_only_extruder)-1]; - else - { - auto before_tick_it = std::lower_bound(m_ticks.begin(), m_ticks.end(), TICK_CODE{ tick }); - while (before_tick_it != m_ticks.begin()) { - --before_tick_it; - if (before_tick_it->gcode == Slic3r::ColorChangeCode && before_tick_it->extruder == selected_extruder) { - color = before_tick_it->color; - break; - } + auto before_tick_it = std::lower_bound(m_ticks.begin(), m_ticks.end(), TICK_CODE{ tick }); + while (before_tick_it != m_ticks.begin()) { + --before_tick_it; + if (before_tick_it->gcode == Slic3r::ColorChangeCode && before_tick_it->extruder == extruder) { + color = before_tick_it->color; + break; } - - if (color.empty()) - color = colors[selected_extruder > 0 ? selected_extruder - 1 : std::max(1, m_only_extruder) - 1]; } - color = get_new_color(color); if (color.empty()) - return; - } - else if (code == Slic3r::PausePrintCode) - { - /* PausePrintCode doesn't need a color, so - * this field is used for save a short message shown on Printer display - * */ - color = get_pause_print_msg(m_pause_print_msg, m_values[tick]); - if (color.empty()) - return; - m_pause_print_msg = color; - } - else if (code.empty()) - { - code = get_custom_code(m_custom_gcode, m_values[tick]); - if (code.empty()) - return; - m_custom_gcode = code; + color = colors[extruder-1]; } - int extruder = 1; - if (m_mode != mmSingleExtruder) { - if (code == Slic3r::ColorChangeCode && selected_extruder >= 0) - extruder = selected_extruder; - else - extruder = get_extruder_for_tick(m_selection == ssLower ? m_lower_value : m_higher_value); - } - - m_ticks.emplace(TICK_CODE{tick, code, extruder, color}); - - wxPostEvent(this->GetParent(), wxCommandEvent(wxCUSTOMEVT_TICKSCHANGED)); - Refresh(); - Update(); + color = get_new_color(color); + if (color.empty()) + return; } + else if (code == Slic3r::PausePrintCode) + { + /* PausePrintCode doesn't need a color, so + * this field is used for save a short message shown on Printer display + * */ + color = get_pause_print_msg(m_pause_print_msg, m_values[tick]); + if (color.empty()) + return; + m_pause_print_msg = color; + } + else if (code.empty()) + { + code = get_custom_code(m_custom_gcode, m_values[tick]); + if (code.empty()) + return; + m_custom_gcode = code; + } + + m_ticks.emplace(TICK_CODE{tick, code, extruder, color}); + + wxPostEvent(this->GetParent(), wxCommandEvent(wxCUSTOMEVT_TICKSCHANGED)); + Refresh(); + Update(); } void DoubleSlider::edit_tick() From 1b354d8d3c83f2e97a8dc139db64a00ab5628569 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Thu, 16 Jan 2020 13:39:03 +0100 Subject: [PATCH 09/13] WIP: Enhancement of the FDM back end to support Color Change situations, when a project was switched from multi-extruder printer to single extruder printer. --- src/libslic3r/GCode/ToolOrdering.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/libslic3r/GCode/ToolOrdering.cpp b/src/libslic3r/GCode/ToolOrdering.cpp index 2628e19264..634093d721 100644 --- a/src/libslic3r/GCode/ToolOrdering.cpp +++ b/src/libslic3r/GCode/ToolOrdering.cpp @@ -129,8 +129,12 @@ ToolOrdering::ToolOrdering(const Print &print, unsigned int first_extruder, bool // Use the extruder switches from Model::custom_gcode_per_print_z to override the extruder to print the object. // Do it only if all the objects were configured to be printed with a single extruder. std::vector> per_layer_extruder_switches; - if (print.object_extruders().size() == 1) - per_layer_extruder_switches = custom_tool_changes(print.model(), (unsigned int)print.config().nozzle_diameter.size()); + if (auto num_extruders = unsigned(print.config().nozzle_diameter.size()); + num_extruders > 1 && print.object_extruders().size() == 1) { + // Printing a single extruder platter on a printer with more than 1 extruder (or single-extruder multi-material). + // There may be custom per-layer tool changes available at the model. + per_layer_extruder_switches = custom_tool_changes(print.model(), num_extruders); + } // Collect extruders reuqired to print the layers. for (auto object : print.objects()) @@ -470,6 +474,8 @@ void ToolOrdering::assign_custom_gcodes(const Print &print) unsigned int num_extruders = *std::max_element(m_all_printing_extruders.begin(), m_all_printing_extruders.end()) + 1; std::vector extruder_printing_above(num_extruders, false); auto custom_gcode_it = custom_gcode_per_print_z.rbegin(); + // If printing on a single extruder machine, make the tool changes trigger color change (M600) events. + bool tool_changes_as_color_changes = num_extruders == 1; // From the last layer to the first one: for (auto it_lt = m_layer_tools.rbegin(); it_lt != m_layer_tools.rend(); ++ it_lt) { LayerTools < = *it_lt; @@ -489,7 +495,7 @@ void ToolOrdering::assign_custom_gcodes(const Print &print) print_z_below = it_lt_below->print_z; if (custom_gcode.print_z > print_z_below + 0.5 * EPSILON) { // The custom G-code applies to the current layer. - if (custom_gcode.gcode != ColorChangeCode || extruder_printing_above[unsigned(custom_gcode.extruder - 1)]) + if (tool_changes_as_color_changes || custom_gcode.gcode != ColorChangeCode || extruder_printing_above[unsigned(custom_gcode.extruder - 1)]) // If it is color change, it will actually be useful as the exturder above will print. lt.custom_gcode = &custom_gcode; // Consume that custom G-code event. From 3acfae92e12611c3bd76c3623b6d9dcade399458 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Thu, 16 Jan 2020 14:59:16 +0100 Subject: [PATCH 10/13] Fix of recent wipe tower / tool ordering refactoring. --- src/libslic3r/GCode/ToolOrdering.cpp | 5 ----- src/libslic3r/Print.hpp | 11 +++++++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/libslic3r/GCode/ToolOrdering.cpp b/src/libslic3r/GCode/ToolOrdering.cpp index 634093d721..2cc39583b8 100644 --- a/src/libslic3r/GCode/ToolOrdering.cpp +++ b/src/libslic3r/GCode/ToolOrdering.cpp @@ -146,11 +146,6 @@ ToolOrdering::ToolOrdering(const Print &print, unsigned int first_extruder, bool this->fill_wipe_tower_partitions(print.config(), object_bottom_z); this->collect_extruder_statistics(prime_multi_material); - - // Assign custom G-code actions from Model::custom_gcode_per_print_z to their respecive layers, - // ignoring the extruder switches, which were processed above, and ignoring color changes for extruders, - // that do not print above their respective print_z. - this->assign_custom_gcodes(print); } void ToolOrdering::initialize_layers(std::vector &zs) diff --git a/src/libslic3r/Print.hpp b/src/libslic3r/Print.hpp index 4a4ed39648..5e6a26f9d2 100644 --- a/src/libslic3r/Print.hpp +++ b/src/libslic3r/Print.hpp @@ -242,11 +242,10 @@ private: struct WipeTowerData { - WipeTowerData(ToolOrdering &tool_ordering) : tool_ordering(tool_ordering) { clear(); } // Following section will be consumed by the GCodeGenerator. // Tool ordering of a non-sequential print has to be known to calculate the wipe tower. // Cache it here, so it does not need to be recalculated during the G-code generation. - ToolOrdering tool_ordering; + ToolOrdering &tool_ordering; // Cache of tool changes per print layer. std::unique_ptr> priming; std::vector> tool_changes; @@ -267,6 +266,14 @@ struct WipeTowerData depth = 0.f; brim_width = 0.f; } + +private: + // Only allow the WipeTowerData to be instantiated internally by Print, + // as this WipeTowerData shares reference to Print::m_tool_ordering. + friend class Print; + WipeTowerData(ToolOrdering &tool_ordering) : tool_ordering(tool_ordering) { clear(); } + WipeTowerData(const WipeTowerData & /* rhs */) = delete; + WipeTowerData &operator=(const WipeTowerData & /* rhs */) = delete; }; struct PrintStatistics From 89fcd7f95af4a078f86e5545b31fa9ae0fdd5091 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Thu, 16 Jan 2020 16:01:19 +0100 Subject: [PATCH 11/13] Added "mode" variable for custom_gcode_per_print_z --- src/libslic3r/Format/3mf.cpp | 8 ++++---- src/libslic3r/Format/AMF.cpp | 6 +++--- src/libslic3r/GCode/ToolOrdering.cpp | 10 +++++----- src/libslic3r/Model.cpp | 6 +++--- src/libslic3r/Model.hpp | 23 ++++++++++++++++++++++- src/libslic3r/Print.cpp | 2 +- src/slic3r/GUI/GLCanvas3D.cpp | 2 +- src/slic3r/GUI/GUI_Preview.cpp | 8 ++++---- src/slic3r/GUI/Plater.cpp | 6 +++--- src/slic3r/GUI/PresetBundle.cpp | 2 +- 10 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/libslic3r/Format/3mf.cpp b/src/libslic3r/Format/3mf.cpp index cca90b4633..ab4848c9a8 100644 --- a/src/libslic3r/Format/3mf.cpp +++ b/src/libslic3r/Format/3mf.cpp @@ -1088,7 +1088,7 @@ namespace Slic3r { return; pt::ptree code_tree = main_tree.front().second; - m_model->custom_gcode_per_print_z.clear(); + m_model->custom_gcode_per_print_z.gcodes.clear(); for (const auto& code : code_tree) { @@ -1100,7 +1100,7 @@ namespace Slic3r { int extruder = tree.get (".extruder" ); std::string color = tree.get (".color" ); - m_model->custom_gcode_per_print_z.push_back(Model::CustomGCode{print_z, gcode, extruder, color}) ; + m_model->custom_gcode_per_print_z.gcodes.push_back(Model::CustomGCode{print_z, gcode, extruder, color}) ; } } } @@ -2631,12 +2631,12 @@ bool _3MF_Exporter::_add_custom_gcode_per_print_z_file_to_archive( mz_zip_archiv { std::string out = ""; - if (!model.custom_gcode_per_print_z.empty()) + if (!model.custom_gcode_per_print_z.gcodes.empty()) { pt::ptree tree; pt::ptree& main_tree = tree.add("custom_gcodes_per_print_z", ""); - for (const Model::CustomGCode& code : model.custom_gcode_per_print_z) + for (const Model::CustomGCode& code : model.custom_gcode_per_print_z.gcodes) { pt::ptree& code_tree = main_tree.add("code", ""); // store minX and maxZ diff --git a/src/libslic3r/Format/AMF.cpp b/src/libslic3r/Format/AMF.cpp index 947c044581..efb90f5923 100644 --- a/src/libslic3r/Format/AMF.cpp +++ b/src/libslic3r/Format/AMF.cpp @@ -653,7 +653,7 @@ void AMFParserContext::endElement(const char * /* name */) int extruder = atoi(m_value[2].c_str()); const std::string& color = m_value[3]; - m_model.custom_gcode_per_print_z.push_back(Model::CustomGCode{height, gcode, extruder, color}); + m_model.custom_gcode_per_print_z.gcodes.push_back(Model::CustomGCode{height, gcode, extruder, color}); for (std::string& val: m_value) val.clear(); @@ -1250,14 +1250,14 @@ bool store_amf(const char *path, Model *model, const DynamicPrintConfig *config) stream << " \n"; } - if (!model->custom_gcode_per_print_z.empty()) + if (!model->custom_gcode_per_print_z.gcodes.empty()) { std::string out = ""; pt::ptree tree; pt::ptree& main_tree = tree.add("custom_gcodes_per_height", ""); - for (const Model::CustomGCode& code : model->custom_gcode_per_print_z) + for (const Model::CustomGCode& code : model->custom_gcode_per_print_z.gcodes) { pt::ptree& code_tree = main_tree.add("code", ""); // store minX and maxZ diff --git a/src/libslic3r/GCode/ToolOrdering.cpp b/src/libslic3r/GCode/ToolOrdering.cpp index 51093c32a0..fe8c93c9a5 100644 --- a/src/libslic3r/GCode/ToolOrdering.cpp +++ b/src/libslic3r/GCode/ToolOrdering.cpp @@ -462,13 +462,13 @@ void ToolOrdering::assign_custom_gcodes(const Print &print) // Only valid for non-sequential print. assert(! print.config().complete_objects.value); - const std::vector &custom_gcode_per_print_z = print.model().custom_gcode_per_print_z; - if (custom_gcode_per_print_z.empty()) + const Model::CustomGCodeInfo &custom_gcode_per_print_z = print.model().custom_gcode_per_print_z; + if (custom_gcode_per_print_z.gcodes.empty()) return; unsigned int num_extruders = *std::max_element(m_all_printing_extruders.begin(), m_all_printing_extruders.end()) + 1; std::vector extruder_printing_above(num_extruders, false); - auto custom_gcode_it = custom_gcode_per_print_z.rbegin(); + auto custom_gcode_it = custom_gcode_per_print_z.gcodes.rbegin(); // If printing on a single extruder machine, make the tool changes trigger color change (M600) events. bool tool_changes_as_color_changes = num_extruders == 1; // From the last layer to the first one: @@ -478,8 +478,8 @@ void ToolOrdering::assign_custom_gcodes(const Print &print) for (unsigned int i : lt.extruders) extruder_printing_above[i] = true; // Skip all custom G-codes above this layer and skip all extruder switches. - for (; custom_gcode_it != custom_gcode_per_print_z.rend() && (custom_gcode_it->print_z > lt.print_z + EPSILON || custom_gcode_it->gcode == ExtruderChangeCode); ++ custom_gcode_it); - if (custom_gcode_it == custom_gcode_per_print_z.rend()) + for (; custom_gcode_it != custom_gcode_per_print_z.gcodes.rend() && (custom_gcode_it->print_z > lt.print_z + EPSILON || custom_gcode_it->gcode == ExtruderChangeCode); ++ custom_gcode_it); + if (custom_gcode_it == custom_gcode_per_print_z.gcodes.rend()) // Custom G-codes were processed. break; // Some custom G-code is configured for this layer or a layer below. diff --git a/src/libslic3r/Model.cpp b/src/libslic3r/Model.cpp index 812180641b..ead2c95caa 100644 --- a/src/libslic3r/Model.cpp +++ b/src/libslic3r/Model.cpp @@ -126,7 +126,7 @@ Model Model::read_from_file(const std::string& input_file, DynamicPrintConfig* c if (add_default_instances) model.add_default_instances(); - update_custom_gcode_per_print_z_from_config(model.custom_gcode_per_print_z, config); + update_custom_gcode_per_print_z_from_config(model.custom_gcode_per_print_z.gcodes, config); return model; } @@ -163,7 +163,7 @@ Model Model::read_from_archive(const std::string& input_file, DynamicPrintConfig if (add_default_instances) model.add_default_instances(); - update_custom_gcode_per_print_z_from_config(model.custom_gcode_per_print_z, config); + update_custom_gcode_per_print_z_from_config(model.custom_gcode_per_print_z.gcodes, config); return model; } @@ -1846,7 +1846,7 @@ arrangement::ArrangePolygon ModelInstance::get_arrange_polygon() const std::vector> custom_tool_changes(const Model &model, size_t num_extruders) { std::vector> custom_tool_changes; - for (const Model::CustomGCode &custom_gcode : model.custom_gcode_per_print_z) + for (const Model::CustomGCode &custom_gcode : model.custom_gcode_per_print_z.gcodes) if (custom_gcode.gcode == ExtruderChangeCode) { // If extruder count in PrinterSettings was changed, use default (0) extruder for extruders, more than num_extruders custom_tool_changes.emplace_back(custom_gcode.print_z, static_cast(custom_gcode.extruder > num_extruders ? 1 : custom_gcode.extruder)); diff --git a/src/libslic3r/Model.hpp b/src/libslic3r/Model.hpp index 26bb4cb92f..509c70b15f 100644 --- a/src/libslic3r/Model.hpp +++ b/src/libslic3r/Model.hpp @@ -772,7 +772,28 @@ public: std::string color; // if gcode is equal to PausePrintCode, // this field is used for save a short message shown on Printer display }; - std::vector custom_gcode_per_print_z; + + struct CustomGCodeInfo + { + enum MODE + { + SingleExtruder, // single extruder printer preset is selected + MultiAsSingle, // multiple extruder printer preset is selected, but + // this mode works just for Single extruder print + // (For all print from objects settings is used just one extruder) + MultiExtruder // multiple extruder printer preset is selected + } mode; + + std::vector gcodes; + + bool operator==(const CustomGCodeInfo& rhs) const + { + return (rhs.mode == this->mode ) && + (rhs.gcodes == this->gcodes ); + } + bool operator!=(const CustomGCodeInfo& rhs) const { return !(*this == rhs); } + } + custom_gcode_per_print_z; // Default constructor assigns a new ID to the model. Model() { assert(this->id().valid()); } diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index babdd5aa4a..08e43564fa 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -723,7 +723,7 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_ model_object_status.emplace(model_object->id(), ModelObjectStatus::New); } else { if (m_model.custom_gcode_per_print_z != model.custom_gcode_per_print_z) { - update_apply_status(custom_per_printz_gcodes_tool_changes_differ(m_model.custom_gcode_per_print_z, model.custom_gcode_per_print_z) ? + update_apply_status(custom_per_printz_gcodes_tool_changes_differ(m_model.custom_gcode_per_print_z.gcodes, model.custom_gcode_per_print_z.gcodes) ? // The Tool Ordering and the Wipe Tower are no more valid. this->invalidate_steps({ psWipeTower, psGCodeExport }) : // There is no change in Tool Changes stored in custom_gcode_per_print_z, therefore there is no need to update Tool Ordering. diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index d2428cf967..fa34ff9293 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -1011,7 +1011,7 @@ void GLCanvas3D::LegendTexture::fill_color_print_legend_items( const GLCanvas3D std::vector& colors, std::vector& cp_legend_items) { - std::vector custom_gcode_per_print_z = wxGetApp().plater()->model().custom_gcode_per_print_z; + std::vector custom_gcode_per_print_z = wxGetApp().plater()->model().custom_gcode_per_print_z.gcodes; const int extruders_cnt = wxGetApp().extruders_edited_cnt(); if (extruders_cnt == 1) diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index fceaeba3bd..f447127f83 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -564,7 +564,7 @@ void Preview::update_view_type(bool slice_completed) { const DynamicPrintConfig& config = wxGetApp().preset_bundle->project_config; - const wxString& choice = !wxGetApp().plater()->model().custom_gcode_per_print_z.empty() /*&& + const wxString& choice = !wxGetApp().plater()->model().custom_gcode_per_print_z.gcodes.empty() /*&& (wxGetApp().extruders_edited_cnt()==1 || !slice_completed) */? _(L("Color Print")) : config.option("wiping_volumes_matrix")->values.size() > 1 ? @@ -595,7 +595,7 @@ void Preview::create_double_slider() Bind(wxCUSTOMEVT_TICKSCHANGED, [this](wxEvent&) { Model& model = wxGetApp().plater()->model(); - model.custom_gcode_per_print_z = m_slider->GetTicksValues(); + model.custom_gcode_per_print_z.gcodes = m_slider->GetTicksValues(); m_schedule_background_process(); update_view_type(false); @@ -664,7 +664,7 @@ void Preview::update_double_slider(const std::vector& layers_z, bool kee bool snap_to_min = force_sliders_full_range || m_slider->is_lower_at_min(); bool snap_to_max = force_sliders_full_range || m_slider->is_higher_at_max(); - std::vector &ticks_from_model = wxGetApp().plater()->model().custom_gcode_per_print_z; + std::vector &ticks_from_model = wxGetApp().plater()->model().custom_gcode_per_print_z.gcodes; check_slider_values(ticks_from_model, layers_z); m_slider->SetSliderValues(layers_z); @@ -837,7 +837,7 @@ void Preview::load_print_as_fff(bool keep_z_range) colors.push_back("#808080"); // gray color for pause print or custom G-code if (!gcode_preview_data_valid) - color_print_values = wxGetApp().plater()->model().custom_gcode_per_print_z; + color_print_values = wxGetApp().plater()->model().custom_gcode_per_print_z.gcodes; } else if (gcode_preview_data_valid || (m_gcode_preview_data->extrusion.view_type == GCodePreviewData::Extrusion::Tool) ) { diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index dc720ddaee..6f77c1db64 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -2791,7 +2791,7 @@ void Plater::priv::reset() // The hiding of the slicing results, if shown, is not taken care by the background process, so we do it here this->sidebar->show_sliced_info_sizer(false); - model.custom_gcode_per_print_z.clear(); + model.custom_gcode_per_print_z.gcodes.clear(); } void Plater::priv::mirror(Axis axis) @@ -5349,9 +5349,9 @@ std::vector Plater::get_extruder_colors_from_plater_config() const std::vector Plater::get_colors_for_color_print() const { std::vector colors = get_extruder_colors_from_plater_config(); - colors.reserve(colors.size() + p->model.custom_gcode_per_print_z.size()); + colors.reserve(colors.size() + p->model.custom_gcode_per_print_z.gcodes.size()); - for (const Model::CustomGCode& code : p->model.custom_gcode_per_print_z) + for (const Model::CustomGCode& code : p->model.custom_gcode_per_print_z.gcodes) if (code.gcode == ColorChangeCode) colors.emplace_back(code.color); diff --git a/src/slic3r/GUI/PresetBundle.cpp b/src/slic3r/GUI/PresetBundle.cpp index 219d7b89da..8f5d58bc13 100644 --- a/src/slic3r/GUI/PresetBundle.cpp +++ b/src/slic3r/GUI/PresetBundle.cpp @@ -877,7 +877,7 @@ void PresetBundle::load_config_file_config(const std::string &name_or_path, bool // 4) Load the project config values (the per extruder wipe matrix etc). this->project_config.apply_only(config, s_project_options); - update_custom_gcode_per_print_z_from_config(GUI::wxGetApp().plater()->model().custom_gcode_per_print_z, &this->project_config); + update_custom_gcode_per_print_z_from_config(GUI::wxGetApp().plater()->model().custom_gcode_per_print_z.gcodes, &this->project_config); break; } From 75c2b44d239d37ee32f781a769dbe2dc7c2e2191 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Thu, 16 Jan 2020 16:02:40 +0100 Subject: [PATCH 12/13] Follow-up of merge of pull request #3293 -> Fixed link error and toolpaths role colors --- src/libslic3r/GCode/PreviewData.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/libslic3r/GCode/PreviewData.cpp b/src/libslic3r/GCode/PreviewData.cpp index 64057ec8d5..15553c7dca 100644 --- a/src/libslic3r/GCode/PreviewData.cpp +++ b/src/libslic3r/GCode/PreviewData.cpp @@ -80,8 +80,8 @@ Color GCodePreviewData::RangeBase::get_color_at(float value) const constexpr std::size_t color_max_idx = range_rainbow_colors.size() - 1; // Compute the two colors just below (low) and above (high) the input value - const std::size_t color_low_idx = clamp(std::size_t{0}, color_max_idx, static_cast(global_t)); - const std::size_t color_high_idx = clamp(std::size_t{0}, color_max_idx, color_low_idx + 1); + const std::size_t color_low_idx = std::clamp(static_cast(global_t), std::size_t{ 0 }, color_max_idx); + const std::size_t color_high_idx = std::clamp(color_low_idx + 1, std::size_t{ 0 }, color_max_idx); const Color color_low = range_rainbow_colors[color_low_idx]; const Color color_high = range_rainbow_colors[color_high_idx]; @@ -302,7 +302,7 @@ void GCodePreviewData::set_extrusion_role_color(const std::string& role_name, fl void GCodePreviewData::set_extrusion_paths_colors(const std::vector& colors) { - unsigned int size = (unsigned int)range_rainbow_colors.size(); + unsigned int size = (unsigned int)colors.size(); if (size % 2 != 0) return; @@ -486,7 +486,7 @@ size_t GCodePreviewData::memory_used() const sizeof(shell) + sizeof(ranges); } -const std::vector& ColorPrintColors() +const std::vector& GCodePreviewData::ColorPrintColors() { static std::vector color_print = {"#C0392B", "#E67E22", "#F1C40F", "#27AE60", "#1ABC9C", "#2980B9", "#9B59B6"}; return color_print; @@ -494,18 +494,18 @@ const std::vector& ColorPrintColors() Color operator + (const Color& c1, const Color& c2) { - return Color(clamp(0.0f, 1.0f, c1.rgba[0] + c2.rgba[0]), - clamp(0.0f, 1.0f, c1.rgba[1] + c2.rgba[1]), - clamp(0.0f, 1.0f, c1.rgba[2] + c2.rgba[2]), - clamp(0.0f, 1.0f, c1.rgba[3] + c2.rgba[3])); + return Color(std::clamp(c1.rgba[0] + c2.rgba[0], 0.0f, 1.0f), + std::clamp(c1.rgba[1] + c2.rgba[1], 0.0f, 1.0f), + std::clamp(c1.rgba[2] + c2.rgba[2], 0.0f, 1.0f), + std::clamp(c1.rgba[3] + c2.rgba[3], 0.0f, 1.0f)); } Color operator * (float f, const Color& color) { - return Color(clamp(0.0f, 1.0f, f * color.rgba[0]), - clamp(0.0f, 1.0f, f * color.rgba[1]), - clamp(0.0f, 1.0f, f * color.rgba[2]), - clamp(0.0f, 1.0f, f * color.rgba[3])); + return Color(std::clamp(f * color.rgba[0], 0.0f, 1.0f), + std::clamp(f * color.rgba[1], 0.0f, 1.0f), + std::clamp(f * color.rgba[2], 0.0f, 1.0f), + std::clamp(f * color.rgba[3], 0.0f, 1.0f)); } } // namespace Slic3r From 67655743b957fc36f7082133f86bc382f2f1701a Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Thu, 16 Jan 2020 16:18:35 +0100 Subject: [PATCH 13/13] Follow-up of merge of pull request #3293 -> Fixed missing include (for MAC build) --- src/libslic3r/GCode/PreviewData.cpp | 1 - src/libslic3r/GCode/PreviewData.hpp | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libslic3r/GCode/PreviewData.cpp b/src/libslic3r/GCode/PreviewData.cpp index 15553c7dca..bdc4367a3d 100644 --- a/src/libslic3r/GCode/PreviewData.cpp +++ b/src/libslic3r/GCode/PreviewData.cpp @@ -1,6 +1,5 @@ #include "Analyzer.hpp" #include "PreviewData.hpp" -#include #include #include "Utils.hpp" diff --git a/src/libslic3r/GCode/PreviewData.hpp b/src/libslic3r/GCode/PreviewData.hpp index a023203f1c..35bbfa50ac 100644 --- a/src/libslic3r/GCode/PreviewData.hpp +++ b/src/libslic3r/GCode/PreviewData.hpp @@ -13,6 +13,8 @@ #include #include +#include + namespace Slic3r { // Represents an RGBA color