From a7989e382cdb5da923f336032bd6fc039f4a4be4 Mon Sep 17 00:00:00 2001
From: Alessandro Ranellucci <aar@cpan.org>
Date: Tue, 17 Sep 2013 23:51:30 +0200
Subject: [PATCH] Bugfix: enabling raft layers generated support for the whole
 model. Includes regression test. #1375

---
 lib/Slic3r/Print/Object.pm          |  3 ++-
 lib/Slic3r/Print/SupportMaterial.pm |  8 +++++---
 t/support.t                         | 14 ++++++++++----
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/lib/Slic3r/Print/Object.pm b/lib/Slic3r/Print/Object.pm
index 7c5e95b31..9aab3850c 100644
--- a/lib/Slic3r/Print/Object.pm
+++ b/lib/Slic3r/Print/Object.pm
@@ -787,7 +787,8 @@ sub combine_infill {
 
 sub generate_support_material {
     my $self = shift;
-    return unless $self->config->support_material && $self->layer_count >= 2;
+    return unless ($self->config->support_material || $self->config->raft_layers > 0)
+        && $self->layer_count >= 2;
     
     Slic3r::Print::SupportMaterial->new(object => $self)->generate;
 }
diff --git a/lib/Slic3r/Print/SupportMaterial.pm b/lib/Slic3r/Print/SupportMaterial.pm
index 5fc860037..f5683fbda 100644
--- a/lib/Slic3r/Print/SupportMaterial.pm
+++ b/lib/Slic3r/Print/SupportMaterial.pm
@@ -16,7 +16,6 @@ sub flow {
 
 sub generate {
     my $self = shift;
-    return unless $self->object->config->support_material && $self->object->layer_count >= 2;
     
     my $flow = $self->flow;
     
@@ -44,6 +43,7 @@ sub generate {
     my %contact  = ();  # contact_z => [ polygons ]
     my %overhang = ();  # contact_z => [ expolygons ] - this stores the actual overhang supported by each contact layer
     for my $layer_id (1 .. $#{$self->object->layers}) {
+        last if $layer_id > $self->object->config->raft_layers && !$self->object->config->support_material;
         my $layer = $self->object->layers->[$layer_id];
         my $lower_layer = $self->object->layers->[$layer_id-1];
         
@@ -54,7 +54,9 @@ sub generate {
             my $diff;
             
             # If a threshold angle was specified, use a different logic for detecting overhangs.
-            if (defined $threshold_rad || $layer_id <= $self->object->config->support_material_enforce_layers) {
+            if (defined $threshold_rad
+                || $layer_id <= $self->object->config->support_material_enforce_layers
+                || $layer_id <= $self->object->config->raft_layers) {
                 my $d = defined $threshold_rad
                     ? scale $lower_layer->height * ((cos $threshold_rad) / (sin $threshold_rad))
                     : 0;
@@ -326,7 +328,7 @@ sub generate {
                 [ @$interface, @$support, @$contact_infill ],
                 1,
             );
-            $support{$layer_id} = diff(
+            $support = diff(
                 $support,
                 $interface,
             );
diff --git a/t/support.t b/t/support.t
index 2bae77620..baeb97eb6 100644
--- a/t/support.t
+++ b/t/support.t
@@ -66,7 +66,7 @@ use Slic3r::Test;
     $config->set('support_material_interface_extruder', 2);
     $config->set('layer_height', 0.4);
     $config->set('first_layer_height', '100%');
-    my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
+    my $print = Slic3r::Test::init_print('overhang', config => $config);
     ok my $gcode = Slic3r::Test::gcode($print), 'no conflict between raft/support and brim';
     
     my $tool = 0;
@@ -75,9 +75,15 @@ use Slic3r::Test;
         
         if ($cmd =~ /^T(\d+)/) {
             $tool = $1;
-        } elsif ($info->{extruding} && $self->Z <= ($config->raft_layers * $config->layer_height)) {
-            fail 'not extruding raft/brim with support material extruder'
-                if $tool != ($config->support_material_extruder-1);
+        } elsif ($info->{extruding}) {
+            if ($self->Z <= ($config->raft_layers * $config->layer_height)) {
+                fail 'not extruding raft/brim with support material extruder'
+                    if $tool != ($config->support_material_extruder-1);
+            } else {
+                fail 'support material exceeds raft layers'
+                    if $tool == $config->support_material_extruder-1;
+                # TODO: we should test that full support is generated when we use raft too
+            }
         }
     });
 }