From a7a01bfc7284efb09aa4c2d9cee7e0fd0a532874 Mon Sep 17 00:00:00 2001
From: Enrico Turri <enricoturri@seznam.cz>
Date: Fri, 6 Sep 2019 13:11:20 +0200
Subject: [PATCH 1/6] Keep selection after reload from disk

---
 src/slic3r/GUI/Plater.cpp | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp
index 141b33d01..4442c65e4 100644
--- a/src/slic3r/GUI/Plater.cpp
+++ b/src/slic3r/GUI/Plater.cpp
@@ -3080,7 +3080,7 @@ void Plater::priv::reload_from_disk()
 {
     Plater::TakeSnapshot snapshot(q, _(L("Reload from Disk")));
 
-    const auto &selection = get_selection();
+    auto& selection = get_selection();
     const auto obj_orig_idx = selection.get_object_idx();
     if (selection.is_wipe_tower() || obj_orig_idx == -1) { return; }
 
@@ -3107,6 +3107,14 @@ void Plater::priv::reload_from_disk()
     }
 
     remove(obj_orig_idx);
+
+    // the previous call to remove() clears the selection
+    // select newly added objects
+    selection.clear();
+    for (const auto idx : new_idxs)
+    {
+        selection.add_object((unsigned int)idx - 1, false);
+    }
 }
 
 void Plater::priv::fix_through_netfabb(const int obj_idx, const int vol_idx/* = -1*/)

From fe07bde3852f699176570cefc73dfd12b6815020 Mon Sep 17 00:00:00 2001
From: Enrico Turri <enricoturri@seznam.cz>
Date: Fri, 6 Sep 2019 13:49:15 +0200
Subject: [PATCH 2/6] Do not render intermediate states while reload from disk
 action is taking place

---
 src/slic3r/GUI/GLCanvas3D.cpp | 3 ++-
 src/slic3r/GUI/GLCanvas3D.hpp | 4 ++++
 src/slic3r/GUI/Plater.cpp     | 6 ++++++
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp
index 0494af3d5..a6abc83fd 100644
--- a/src/slic3r/GUI/GLCanvas3D.cpp
+++ b/src/slic3r/GUI/GLCanvas3D.cpp
@@ -1127,6 +1127,7 @@ GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas, Bed3D& bed, Camera& camera, GLToolbar
     , m_retina_helper(nullptr)
 #endif
     , m_in_render(false)
+    , m_render_enabled(true)
     , m_bed(bed)
     , m_camera(camera)
     , m_view_toolbar(view_toolbar)
@@ -1513,7 +1514,7 @@ void GLCanvas3D::update_volumes_colors_by_extruder()
 
 void GLCanvas3D::render()
 {
-    if (m_in_render)
+    if (!m_render_enabled || m_in_render)
     {
         // if called recursively, return
         m_dirty = true;
diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp
index 879584528..d805cd60f 100644
--- a/src/slic3r/GUI/GLCanvas3D.hpp
+++ b/src/slic3r/GUI/GLCanvas3D.hpp
@@ -403,6 +403,7 @@ private:
     std::unique_ptr<RetinaHelper> m_retina_helper;
 #endif
     bool m_in_render;
+    bool m_render_enabled;
     LegendTexture m_legend_texture;
     WarningTexture m_warning_texture;
     wxTimer m_timer;
@@ -533,6 +534,9 @@ public:
     void enable_dynamic_background(bool enable);
     void allow_multisample(bool allow);
 
+    void enable_render(bool enable) { m_render_enabled = enable; }
+    bool is_render_enabled() const { return m_render_enabled; }
+
     void zoom_to_bed();
     void zoom_to_volumes();
     void zoom_to_selection();
diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp
index 4442c65e4..6e2d3b1b5 100644
--- a/src/slic3r/GUI/Plater.cpp
+++ b/src/slic3r/GUI/Plater.cpp
@@ -3087,6 +3087,9 @@ void Plater::priv::reload_from_disk()
     auto *object_orig = model.objects[obj_orig_idx];
     std::vector<fs::path> input_paths(1, object_orig->input_file);
 
+    // disable render to avoid to show intermediate states
+    view3D->get_canvas3d()->enable_render(false);
+
     const auto new_idxs = load_files(input_paths, true, false);
 
     for (const auto idx : new_idxs) {
@@ -3108,6 +3111,9 @@ void Plater::priv::reload_from_disk()
 
     remove(obj_orig_idx);
 
+    // re-enable render 
+    view3D->get_canvas3d()->enable_render(true);
+
     // the previous call to remove() clears the selection
     // select newly added objects
     selection.clear();

From 4f1747d5f6d2d302092a941e244ea4f961f8bde7 Mon Sep 17 00:00:00 2001
From: Enrico Turri <enricoturri@seznam.cz>
Date: Fri, 6 Sep 2019 14:36:47 +0200
Subject: [PATCH 3/6] Keep correct instance selection after reload from disk

---
 src/slic3r/GUI/Plater.cpp | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp
index 6e2d3b1b5..a7a09b50a 100644
--- a/src/slic3r/GUI/Plater.cpp
+++ b/src/slic3r/GUI/Plater.cpp
@@ -3083,6 +3083,7 @@ void Plater::priv::reload_from_disk()
     auto& selection = get_selection();
     const auto obj_orig_idx = selection.get_object_idx();
     if (selection.is_wipe_tower() || obj_orig_idx == -1) { return; }
+    int instance_idx = selection.get_instance_idx();
 
     auto *object_orig = model.objects[obj_orig_idx];
     std::vector<fs::path> input_paths(1, object_orig->input_file);
@@ -3091,6 +3092,12 @@ void Plater::priv::reload_from_disk()
     view3D->get_canvas3d()->enable_render(false);
 
     const auto new_idxs = load_files(input_paths, true, false);
+    if (new_idxs.empty())
+    {
+        // error while loading
+        view3D->get_canvas3d()->enable_render(true);
+        return;
+    }
 
     for (const auto idx : new_idxs) {
         ModelObject *object = model.objects[idx];
@@ -3119,7 +3126,7 @@ void Plater::priv::reload_from_disk()
     selection.clear();
     for (const auto idx : new_idxs)
     {
-        selection.add_object((unsigned int)idx - 1, false);
+        selection.add_instance((unsigned int)idx - 1, instance_idx, false);
     }
 }
 

From 6c3aae8f897df96e3899dcd0c55ff67ff28a7369 Mon Sep 17 00:00:00 2001
From: Enrico Turri <enricoturri@seznam.cz>
Date: Fri, 6 Sep 2019 15:10:07 +0200
Subject: [PATCH 4/6] Fixed update of instances in objects list after reload
 from disk

---
 src/slic3r/GUI/Plater.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp
index a7a09b50a..0f2ac417d 100644
--- a/src/slic3r/GUI/Plater.cpp
+++ b/src/slic3r/GUI/Plater.cpp
@@ -3113,6 +3113,8 @@ void Plater::priv::reload_from_disk()
             }
         }
 
+        sidebar->obj_list()->increase_object_instances(idx, object_orig->instances.size());
+
         // XXX: Restore more: layer_height_ranges, layer_height_profile (?)
     }
 

From abd170fbf471a61e8d68eba9e63a432f847f2c66 Mon Sep 17 00:00:00 2001
From: Enrico Turri <enricoturri@seznam.cz>
Date: Fri, 6 Sep 2019 15:50:54 +0200
Subject: [PATCH 5/6] Follow-up of 6c3aae8f897df96e3899dcd0c55ff67ff28a7369 ->
 Fixed update of objects list when object has a unique instance

---
 src/slic3r/GUI/Plater.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp
index 0f2ac417d..dd060ecb9 100644
--- a/src/slic3r/GUI/Plater.cpp
+++ b/src/slic3r/GUI/Plater.cpp
@@ -3113,7 +3113,8 @@ void Plater::priv::reload_from_disk()
             }
         }
 
-        sidebar->obj_list()->increase_object_instances(idx, object_orig->instances.size());
+        if (object_orig->instances.size() > 1)
+            sidebar->obj_list()->increase_object_instances(idx, object_orig->instances.size());
 
         // XXX: Restore more: layer_height_ranges, layer_height_profile (?)
     }

From e6366894daf1f710a90113af2bd786e2ddbd279d Mon Sep 17 00:00:00 2001
From: Enrico Turri <enricoturri@seznam.cz>
Date: Fri, 6 Sep 2019 17:26:08 +0200
Subject: [PATCH 6/6] Fixed object settings after reload from disk

---
 src/slic3r/GUI/Plater.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp
index dd060ecb9..838e79e3d 100644
--- a/src/slic3r/GUI/Plater.cpp
+++ b/src/slic3r/GUI/Plater.cpp
@@ -3101,7 +3101,7 @@ void Plater::priv::reload_from_disk()
 
     for (const auto idx : new_idxs) {
         ModelObject *object = model.objects[idx];
-
+        object->config.apply(object_orig->config);
         object->clear_instances();
         for (const ModelInstance *instance : object_orig->instances) {
             object->add_instance(*instance);
@@ -3131,6 +3131,8 @@ void Plater::priv::reload_from_disk()
     {
         selection.add_instance((unsigned int)idx - 1, instance_idx, false);
     }
+
+    wxGetApp().obj_list()->update_and_show_object_settings_item();
 }
 
 void Plater::priv::fix_through_netfabb(const int obj_idx, const int vol_idx/* = -1*/)