From db22b2f50413f576745a6bc392c776179661ae05 Mon Sep 17 00:00:00 2001
From: Lukas Matena <lukasmatena@seznam.cz>
Date: Wed, 30 May 2018 11:02:00 +0200
Subject: [PATCH] Bugfix: first color of the wipe tower preview was incorrect
 with some models

---
 xs/src/libslic3r/GCode/WipeTowerPrusaMM.cpp | 11 ++++++++---
 xs/src/libslic3r/GCode/WipeTowerPrusaMM.hpp |  2 +-
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/xs/src/libslic3r/GCode/WipeTowerPrusaMM.cpp b/xs/src/libslic3r/GCode/WipeTowerPrusaMM.cpp
index 399f310da..6486f2917 100644
--- a/xs/src/libslic3r/GCode/WipeTowerPrusaMM.cpp
+++ b/xs/src/libslic3r/GCode/WipeTowerPrusaMM.cpp
@@ -1147,7 +1147,8 @@ void WipeTowerPrusaMM::save_on_last_wipe()
 // Resulting ToolChangeResults are appended into vector "result"
 void WipeTowerPrusaMM::generate(std::vector<std::vector<WipeTower::ToolChangeResult>> &result)
 {
-	if (m_plan.empty())	return;
+	if (m_plan.empty())
+            return;
 
     m_extra_spacing = 1.f;
 
@@ -1161,8 +1162,9 @@ void WipeTowerPrusaMM::generate(std::vector<std::vector<WipeTower::ToolChangeRes
 			make_wipe_tower_square();
 
     m_layer_info = m_plan.begin();
+    m_current_tool = (unsigned int)(-2); // we don't know which extruder to start with - we'll set it according to the first toolchange
 
-	std::vector<WipeTower::ToolChangeResult> layer_result;
+    std::vector<WipeTower::ToolChangeResult> layer_result;
 	for (auto layer : m_plan)
 	{
 		set_layer(layer.z,layer.height,0,layer.z == m_plan.front().z,layer.z == m_plan.back().z);
@@ -1176,8 +1178,11 @@ void WipeTowerPrusaMM::generate(std::vector<std::vector<WipeTower::ToolChangeRes
 		if (!m_peters_wipe_tower && m_layer_info->depth < m_wipe_tower_depth - m_perimeter_width)
 			m_y_shift = (m_wipe_tower_depth-m_layer_info->depth-m_perimeter_width)/2.f;
 
-		for (const auto &toolchange : layer.tool_changes)
+		for (const auto &toolchange : layer.tool_changes) {
+            if (m_current_tool == (unsigned int)(-2))
+                m_current_tool = toolchange.old_tool;
 			layer_result.emplace_back(tool_change(toolchange.new_tool, false));
+        }
 
 		if (! layer_finished()) {
             auto finish_layer_toolchange = finish_layer();
diff --git a/xs/src/libslic3r/GCode/WipeTowerPrusaMM.hpp b/xs/src/libslic3r/GCode/WipeTowerPrusaMM.hpp
index c83c79a04..b7c721128 100644
--- a/xs/src/libslic3r/GCode/WipeTowerPrusaMM.hpp
+++ b/xs/src/libslic3r/GCode/WipeTowerPrusaMM.hpp
@@ -229,7 +229,7 @@ private:
 	bool m_print_brim = true;
 	// A fill-in direction (positive Y, negative Y) alternates with each layer.
 	wipe_shape   	m_current_shape = SHAPE_NORMAL;
-	unsigned int 	m_current_tool  = 0;
+	unsigned int 	m_current_tool;
     std::vector<std::vector<float>> wipe_volumes;
 
 	float           m_depth_traversed = 0.f; // Current y position at the wipe tower.