From 45559f87f3d64527b7eb88fd986f0450d74ab467 Mon Sep 17 00:00:00 2001
From: Alessandro Ranellucci <aar@cpan.org>
Date: Wed, 2 Apr 2014 20:23:26 +0200
Subject: [PATCH] Save the vector used for aligning object volumes to origin
 and apply it to additional parts added from the object part panel in order to
 make them coincide

---
 lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm | 3 +++
 lib/Slic3r/Model.pm                       | 2 ++
 lib/Slic3r/Print/Object.pm                | 4 ++--
 3 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm b/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm
index b911c8141..7b62bea52 100644
--- a/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm
+++ b/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm
@@ -207,6 +207,9 @@ sub on_btn_load {
                     $new_volume->material_id($material_name);
                 }
                 
+                # apply the same translation we applied to the object
+                $new_volume->mesh->translate(@{$self->{model_object}->origin_translation}, 0);
+                
                 # set a default extruder value, since user can't add it manually
                 my $material = $self->{model_object}->model->materials->{$new_volume->material_id};
                 $material->config->set_ifndef('extruder', 1);
diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm
index c074d7908..efd331f1f 100644
--- a/lib/Slic3r/Model.pm
+++ b/lib/Slic3r/Model.pm
@@ -309,6 +309,7 @@ has 'instances'             => (is => 'rw');
 has 'config'                => (is => 'rw', default => sub { Slic3r::Config->new });
 has 'layer_height_ranges'   => (is => 'rw', default => sub { [] }); # [ z_min, z_max, layer_height ]
 has '_bounding_box'         => (is => 'rw');
+has 'origin_translation'    => (is => 'ro', default => sub { Slic3r::Point->new });  # translation vector applied by center_around_origin() 
 
 sub add_volume {
     my $self = shift;
@@ -446,6 +447,7 @@ sub center_around_origin {
     $shift[Y] -= $size->y/2;  #//
     
     $self->translate(@shift);
+    $self->origin_translation->translate(@shift[X,Y]);
     
     if (defined $self->instances) {
         foreach my $instance (@{ $self->instances }) {
diff --git a/lib/Slic3r/Print/Object.pm b/lib/Slic3r/Print/Object.pm
index 981999641..5bf9c4c97 100644
--- a/lib/Slic3r/Print/Object.pm
+++ b/lib/Slic3r/Print/Object.pm
@@ -42,8 +42,8 @@ sub BUILD {
  	    
  	    # Translate meshes so that our toolpath generation algorithms work with smaller
  	    # XY coordinates; this translation is an optimization and not strictly required.
- 	    # However, this also aligns object to Z = 0, which on the contrary is required
- 	    # since we don't assume input is already aligned.
+ 	    # A cloned mesh will be aligned to 0 before slicing in _slice_region() since we
+ 	    # don't assume it's already aligned and we don't alter the original position in model.
  	    # We store the XY translation so that we can place copies correctly in the output G-code
  	    # (copies are expressed in G-code coordinates and this translation is not publicly exposed).
  	    $self->_copies_shift(Slic3r::Point->new_scale($bb->x_min, $bb->y_min));