From cb43f19a90c05b5b8830b5594bc8e2f933d48dfc Mon Sep 17 00:00:00 2001
From: bubnikv <bubnikv@gmail.com>
Date: Thu, 15 Jun 2017 20:15:53 +0200
Subject: [PATCH] Fixed a variable layer height profile update issue.
 https://github.com/prusa3d/Slic3r/issues/358

---
 xs/src/libslic3r/Print.cpp       | 12 ++++++------
 xs/src/libslic3r/PrintObject.cpp |  1 +
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/xs/src/libslic3r/Print.cpp b/xs/src/libslic3r/Print.cpp
index a7153c181..712570ff9 100644
--- a/xs/src/libslic3r/Print.cpp
+++ b/xs/src/libslic3r/Print.cpp
@@ -401,8 +401,6 @@ bool Print::apply_config(DynamicPrintConfig config)
             // The layer_height_profile is not valid for some reason (updated by the user or invalidated due to some option change).
             // Invalidate the slicing step, which in turn invalidates everything.
             object->invalidate_step(posSlice);
-            // Following line sets the layer_height_profile_valid flag.
-            object->update_layer_height_profile();
             // Trigger recalculation.
             invalidated = true;
         }
@@ -478,13 +476,15 @@ exit_for_rearrange_regions:
         for (PrintObject *object : this->objects)
             model_objects.push_back(object->model_object());
         this->clear_objects();
-        for (ModelObject *mo : model_objects) {
+        for (ModelObject *mo : model_objects)
             this->add_model_object(mo);
-            // Update layer_height_profile from the main thread as it may pull the data from the associated ModelObject.
-            this->objects.back()->update_layer_height_profile();
-        }
         invalidated = true;
     }
+
+    // Always make sure that the layer_height_profiles are set, as they should not be modified from the worker threads.
+    for (PrintObject *object : this->objects)
+        if (! object->layer_height_profile_valid)
+            object->update_layer_height_profile();
     
     return invalidated;
 }
diff --git a/xs/src/libslic3r/PrintObject.cpp b/xs/src/libslic3r/PrintObject.cpp
index 30da8b4ae..588e46ef6 100644
--- a/xs/src/libslic3r/PrintObject.cpp
+++ b/xs/src/libslic3r/PrintObject.cpp
@@ -1468,6 +1468,7 @@ void PrintObject::reset_layer_height_profile()
 {
     // Reset the layer_heigth_profile.
     this->layer_height_profile.clear();
+	this->layer_height_profile_valid = false;
     // Reset the source layer_height_profile if it exists at the ModelObject.
     this->model_object()->layer_height_profile.clear();
     this->model_object()->layer_height_profile_valid = false;