From 0fb8b2ce09458719fd8af7f3b1c2e7dd8e8f468e Mon Sep 17 00:00:00 2001
From: YuSanka <yusanka@gmail.com>
Date: Fri, 15 Nov 2019 16:36:29 +0100
Subject: [PATCH] Delete color changes for unused extruders

---
 src/libslic3r/GCode.cpp                     | 45 ++++++++++++++++++++-
 src/slic3r/GUI/BackgroundSlicingProcess.cpp |  9 +++++
 src/slic3r/GUI/GLCanvas3D.cpp               |  5 ---
 src/slic3r/GUI/GUI_Preview.cpp              |  4 +-
 src/slic3r/GUI/wxExtensions.cpp             |  3 ++
 5 files changed, 58 insertions(+), 8 deletions(-)

diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp
index 311575bce..1e17e3ad9 100644
--- a/src/libslic3r/GCode.cpp
+++ b/src/libslic3r/GCode.cpp
@@ -875,7 +875,7 @@ void GCode::_do_export(Print &print, FILE *file)
     this->set_extruders(print.extruders());
     
     //  #ys_FIXME_COLOR // Initialize colorprint.
-    m_colorprint_heights = cast<float>(print.config().colorprint_heights.values);
+    // m_colorprint_heights = cast<float>(print.config().colorprint_heights.values);
     // Initialize custom gcode
     Model* model = print.get_object(0)->model_object()->get_model();
     m_custom_g_code_heights = model->custom_gcode_per_height;
@@ -1020,6 +1020,49 @@ void GCode::_do_export(Print &print, FILE *file)
     }
     print.throw_if_canceled();
 
+
+    // #ys_FIXME_COLOR
+    /* To avoid change filament for non-used extruder for Multi-material,
+     * check model->custom_gcode_per_height using tool_ordering values
+     * */
+    if (!m_custom_g_code_heights. empty())
+    {
+        bool delete_executed = false;
+        auto it = m_custom_g_code_heights.end();
+        while (it != m_custom_g_code_heights.begin())
+        {
+            --it;
+            if (it->gcode != ColorChangeCode)
+                continue;
+
+            auto it_layer_tools = std::lower_bound(tool_ordering.begin(), tool_ordering.end(), LayerTools(it->height));
+
+            bool used_extruder = false;
+            for (; it_layer_tools != tool_ordering.end(); it_layer_tools++)
+            {
+                const std::vector<unsigned>& extruders = it_layer_tools->extruders;
+                if (std::find(extruders.begin(), extruders.end(), (unsigned)(it->extruder-1)) != extruders.end())
+                {
+                    used_extruder = true;
+                    break;
+                }
+            }
+            if (used_extruder)
+                continue;
+
+            /* If we are there, current extruder wouldn't be used,
+             * so this color change is a redundant move.
+             * Delete this item from m_custom_g_code_heights
+             * */
+            it = m_custom_g_code_heights.erase(it);
+            delete_executed = true;
+        }
+
+        if (delete_executed)
+            model->custom_gcode_per_height = m_custom_g_code_heights;
+    }
+
+
     m_cooling_buffer->set_current_extruder(initial_extruder_id);
 
     // Emit machine envelope limits for the Marlin firmware.
diff --git a/src/slic3r/GUI/BackgroundSlicingProcess.cpp b/src/slic3r/GUI/BackgroundSlicingProcess.cpp
index 6c138d4d0..991bc94a6 100644
--- a/src/slic3r/GUI/BackgroundSlicingProcess.cpp
+++ b/src/slic3r/GUI/BackgroundSlicingProcess.cpp
@@ -29,6 +29,7 @@
 #include <boost/log/trivial.hpp>
 #include <boost/nowide/cstdio.hpp>
 #include "I18N.hpp"
+#include "GUI.hpp"
 
 namespace Slic3r {
 
@@ -84,6 +85,14 @@ void BackgroundSlicingProcess::process_fff()
     m_print->process();
 	wxQueueEvent(GUI::wxGetApp().mainframe->m_plater, new wxCommandEvent(m_event_slicing_completed_id));
 	m_fff_print->export_gcode(m_temp_output_path, m_gcode_preview_data);
+
+    if (m_fff_print->model().custom_gcode_per_height != GUI::wxGetApp().model().custom_gcode_per_height) {
+        GUI::wxGetApp().model().custom_gcode_per_height = m_fff_print->model().custom_gcode_per_height;
+        // #ys_FIXME : controll text
+        GUI::show_info(nullptr, _(L("To except of redundant tool manipulation, \n"
+                                    "Color change(s) for unused extruder(s) was(were) deleted")), _(L("Info")));
+    }
+
 	if (this->set_step_started(bspsGCodeFinalize)) {
 	    if (! m_export_path.empty()) {
 	    	//FIXME localize the messages
diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp
index 97e15bb0a..f5005b798 100644
--- a/src/slic3r/GUI/GLCanvas3D.cpp
+++ b/src/slic3r/GUI/GLCanvas3D.cpp
@@ -5338,11 +5338,6 @@ void GLCanvas3D::_load_gcode_extrusion_paths(const GCodePreviewData& preview_dat
             case GCodePreviewData::Extrusion::ColorPrint:
             {
                 int color_cnt = (int)tool_colors.size() / 4;
-
-                // int val = int(value);
-                // while (val >= color_cnt)
-                //     val -= color_cnt;
-
                 int val = value > color_cnt ? color_cnt - 1 : value;
 
                 GCodePreviewData::Color color;
diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp
index 0ca283d2f..8f4b8a19b 100644
--- a/src/slic3r/GUI/GUI_Preview.cpp
+++ b/src/slic3r/GUI/GUI_Preview.cpp
@@ -592,8 +592,8 @@ void Preview::update_view_type(bool slice_completed)
                                     _(L("Feature type"));
     */
 
-    const wxString& choice = !wxGetApp().plater()->model().custom_gcode_per_height.empty() &&
-                             (wxGetApp().extruders_edited_cnt()==1 || !slice_completed) ? 
+    const wxString& choice = !wxGetApp().plater()->model().custom_gcode_per_height.empty() /*&&
+                             (wxGetApp().extruders_edited_cnt()==1 || !slice_completed) */? 
                                 _(L("Color Print")) :
                                 config.option<ConfigOptionFloats>("wiping_volumes_matrix")->values.size() > 1 ?
                                     _(L("Tool")) : 
diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp
index c2262d949..d62408acc 100644
--- a/src/slic3r/GUI/wxExtensions.cpp
+++ b/src/slic3r/GUI/wxExtensions.cpp
@@ -2560,6 +2560,9 @@ void DoubleSlider::SetTicksValues(const std::vector<t_custom_code>& heights)
     if (!was_empty && m_ticks_.empty() && m_state != msMultiExtruder)
         // 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));
+
+    Refresh();
+    Update();
 }
 
 void DoubleSlider::get_lower_and_higher_position(int& lower_pos, int& higher_pos)