From edfdf236da68a01c43ee0a39c6993e44b4621005 Mon Sep 17 00:00:00 2001
From: Alessandro Ranellucci <aar@cpan.org>
Date: Wed, 22 Aug 2012 19:47:59 +0200
Subject: [PATCH] Fix for the recent commit about the new retraction settings

---
 lib/Slic3r/GCode.pm | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm
index 867d5a27c..88f10eb90 100644
--- a/lib/Slic3r/GCode.pm
+++ b/lib/Slic3r/GCode.pm
@@ -378,13 +378,25 @@ sub set_tool {
     my $self = shift;
     my ($tool) = @_;
     
+    # return nothing if this tool was already selected
     return "" if (defined $self->extruder_idx) && ($self->extruder_idx == $tool);
-    $self->extruder_idx($tool);
-    return "" if @{$Slic3r::extruders} == 1;
     
-    return $self->retract(toolchange => 1)
-        . (sprintf "T%d%s\n", $tool, ($Slic3r::Config->gcode_comments ? ' ; change tool' : ''))
-        . $self->reset_e;
+    # if we are running a single-extruder setup, just set the extruder and return nothing
+    if (@{$Slic3r::extruders} == 1) {
+        $self->extruder_idx($tool);
+        return "";
+    }
+    
+    # trigger retraction on the current tool (if any) 
+    my $gcode = "";
+    $gcode .= $self->retract(toolchange => 1) if defined $self->extruder_idx;
+    
+    # set the new tool
+    $self->extruder_idx($tool);
+    $gcode .= sprintf "T%d%s\n", $tool, ($Slic3r::Config->gcode_comments ? ' ; change tool' : '');
+    $gcode .= $self->reset_e;
+    
+    return $gcode;
 }
 
 sub set_fan {