From 8eb9ddc2eb6c3f98cf28125ddbee4d233fa58616 Mon Sep 17 00:00:00 2001
From: Enrico Turri <enricoturri@seznam.cz>
Date: Mon, 7 May 2018 16:13:58 +0200
Subject: [PATCH] Max count of auto assigned extruders when splitting object
 set as dependent of current printer

---
 lib/Slic3r/GUI/Plater.pm                  |  4 ++--
 lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm |  3 ++-
 xs/src/libslic3r/Model.cpp                | 16 ++++++++--------
 xs/src/libslic3r/Model.hpp                | 10 +++++-----
 xs/xsp/Model.xsp                          |  4 ++--
 5 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm
index a3ad064cd..0a5d02b94 100644
--- a/lib/Slic3r/GUI/Plater.pm
+++ b/lib/Slic3r/GUI/Plater.pm
@@ -700,7 +700,7 @@ sub load_files {
                 . "Instead of considering them as multiple objects, should I consider\n"
                 . "this file as a single object having multiple parts?\n"),
                 L('Multi-part object detected'), wxICON_WARNING | wxYES | wxNO);
-            $model->convert_multipart_object if $dialog->ShowModal() == wxID_YES;
+            $model->convert_multipart_object if $dialog->ShowModal(scalar(@$nozzle_dmrs)) == wxID_YES;
         }
         
         if ($one_by_one) {
@@ -717,7 +717,7 @@ sub load_files {
             . "Instead of considering them as multiple objects, should I consider\n"
             . "these files to represent a single object having multiple parts?\n"),
             L('Multi-part object detected'), wxICON_WARNING | wxYES | wxNO);
-        $new_model->convert_multipart_object if $dialog->ShowModal() == wxID_YES;
+        $new_model->convert_multipart_object if $dialog->ShowModal(scalar(@$nozzle_dmrs)) == wxID_YES;
         push @obj_idx, $self->load_model_objects(@{$new_model->objects});
     }
 
diff --git a/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm b/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm
index 28e3bf92b..a632edeea 100644
--- a/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm
+++ b/lib/Slic3r/GUI/Plater/ObjectPartsPanel.pm
@@ -475,7 +475,8 @@ sub on_btn_split {
     my $itemData = $self->get_selection;
     if ($itemData && $itemData->{type} eq 'volume') {
         my $volume = $self->{model_object}->volumes->[$itemData->{volume_id}];
-        $self->{parts_changed} = 1 if $volume->split > 1;
+        my $nozzle_dmrs = $self->GetParent->GetParent->GetParent->{config}->get('nozzle_diameter');
+        $self->{parts_changed} = 1 if $volume->split(scalar(@$nozzle_dmrs)) > 1;
     }
     
     $self->_parts_changed;
diff --git a/xs/src/libslic3r/Model.cpp b/xs/src/libslic3r/Model.cpp
index a43bf0221..755941144 100644
--- a/xs/src/libslic3r/Model.cpp
+++ b/xs/src/libslic3r/Model.cpp
@@ -404,7 +404,7 @@ bool Model::looks_like_multipart_object() const
     return false;
 }
 
-void Model::convert_multipart_object()
+void Model::convert_multipart_object(unsigned int max_extruders)
 {
     if (this->objects.empty())
         return;
@@ -421,7 +421,7 @@ void Model::convert_multipart_object()
             if (new_v != nullptr)
             {
                 new_v->name = o->name;
-                new_v->config.set_deserialize("extruder", get_auto_extruder_id_as_string());
+                new_v->config.set_deserialize("extruder", get_auto_extruder_id_as_string(max_extruders));
             }
         }
 
@@ -481,20 +481,20 @@ bool Model::fits_print_volume(const FullPrintConfig &config) const
     return print_volume.contains(transformed_bounding_box());
 }
 
-unsigned int Model::get_auto_extruder_id()
+unsigned int Model::get_auto_extruder_id(unsigned int max_extruders)
 {
     unsigned int id = s_auto_extruder_id;
 
-    if (++s_auto_extruder_id > 4)
+    if (++s_auto_extruder_id > max_extruders)
         reset_auto_extruder_id();
 
     return id;
 }
 
-std::string Model::get_auto_extruder_id_as_string()
+std::string Model::get_auto_extruder_id_as_string(unsigned int max_extruders)
 {
     char str_extruder[64];
-    sprintf(str_extruder, "%ud", get_auto_extruder_id());
+    sprintf(str_extruder, "%ud", get_auto_extruder_id(max_extruders));
     return str_extruder;
 }
 
@@ -996,7 +996,7 @@ ModelMaterial* ModelVolume::assign_unique_material()
 // Split this volume, append the result to the object owning this volume.
 // Return the number of volumes created from this one.
 // This is useful to assign different materials to different volumes of an object.
-size_t ModelVolume::split()
+size_t ModelVolume::split(unsigned int max_extruders)
 {
     TriangleMeshPtrs meshptrs = this->mesh.split();
     if (meshptrs.size() <= 1) {
@@ -1019,7 +1019,7 @@ size_t ModelVolume::split()
         char str_idx[64];
         sprintf(str_idx, "_%d", idx + 1);
         this->object->volumes[ivolume]->name = name + str_idx;
-        this->object->volumes[ivolume]->config.set_deserialize("extruder", Model::get_auto_extruder_id_as_string());
+        this->object->volumes[ivolume]->config.set_deserialize("extruder", Model::get_auto_extruder_id_as_string(max_extruders));
         delete mesh;
         ++ idx;
     }
diff --git a/xs/src/libslic3r/Model.hpp b/xs/src/libslic3r/Model.hpp
index 0c0ffe776..4835028b9 100644
--- a/xs/src/libslic3r/Model.hpp
+++ b/xs/src/libslic3r/Model.hpp
@@ -173,8 +173,8 @@ public:
     // Split this volume, append the result to the object owning this volume.
     // Return the number of volumes created from this one.
     // This is useful to assign different materials to different volumes of an object.
-    size_t split();
-    
+    size_t split(unsigned int max_extruders);
+
     ModelMaterial* assign_unique_material();
     
 private:
@@ -280,7 +280,7 @@ public:
     void duplicate_objects_grid(size_t x, size_t y, coordf_t dist);
 
     bool looks_like_multipart_object() const;
-    void convert_multipart_object();
+    void convert_multipart_object(unsigned int max_extruders);
 
     // Ensures that the min z of the model is not negative
     void adjust_min_z();
@@ -291,8 +291,8 @@ public:
 
     void print_info() const { for (const ModelObject *o : this->objects) o->print_info(); }
 
-    static unsigned int get_auto_extruder_id();
-    static std::string get_auto_extruder_id_as_string();
+    static unsigned int get_auto_extruder_id(unsigned int max_extruders);
+    static std::string get_auto_extruder_id_as_string(unsigned int max_extruders);
     static void reset_auto_extruder_id();
 };
 
diff --git a/xs/xsp/Model.xsp b/xs/xsp/Model.xsp
index 702839537..5b8318353 100644
--- a/xs/xsp/Model.xsp
+++ b/xs/xsp/Model.xsp
@@ -95,7 +95,7 @@
     void duplicate_objects_grid(unsigned int x, unsigned int y, double dist);
 
     bool looks_like_multipart_object() const;
-    void convert_multipart_object();
+    void convert_multipart_object(unsigned int max_extruders);
 
     void print_info() const;
 
@@ -346,7 +346,7 @@ ModelMaterial::attributes()
     void set_modifier(bool modifier)
         %code%{ THIS->modifier = modifier; %};
 
-    size_t split();
+    size_t split(unsigned int max_extruders);
     
     ModelMaterial* assign_unique_material();
 };