From 42c5c19f1c9c3454cbecc94e71772cc1995a8c88 Mon Sep 17 00:00:00 2001
From: Lukas Matena <lukasmatena@seznam.cz>
Date: Tue, 27 Aug 2019 12:41:00 +0200
Subject: [PATCH] GCodeWriter.cpp: Fixed skipped z-lifts when its height was
 equal to layer height (https://github.com/prusa3d/PrusaSlicer/issues/2154)

---
 src/libslic3r/GCodeWriter.cpp | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/libslic3r/GCodeWriter.cpp b/src/libslic3r/GCodeWriter.cpp
index 6ef17f4f4..51fca58f6 100644
--- a/src/libslic3r/GCodeWriter.cpp
+++ b/src/libslic3r/GCodeWriter.cpp
@@ -298,7 +298,11 @@ std::string GCodeWriter::travel_to_xyz(const Vec3d &point, const std::string &co
         used for unlift. */
     if (!this->will_move_z(point(2))) {
         double nominal_z = m_pos(2) - m_lifted;
-        m_lifted = m_lifted - (point(2) - nominal_z);
+        m_lifted -= (point(2) - nominal_z);
+        // In case that retract_lift == layer_height we could end up with almost zero in_m_lifted
+        // and a retract could be skipped (https://github.com/prusa3d/PrusaSlicer/issues/2154
+        if (std::abs(m_lifted) < EPSILON)
+            m_lifted = 0.;
         return this->travel_to_xy(to_2d(point));
     }
     
@@ -324,7 +328,9 @@ std::string GCodeWriter::travel_to_z(double z, const std::string &comment)
         reducing the lift amount that will be used for unlift. */
     if (!this->will_move_z(z)) {
         double nominal_z = m_pos(2) - m_lifted;
-        m_lifted = m_lifted - (z - nominal_z);
+        m_lifted -= (z - nominal_z);
+        if (std::abs(m_lifted) < EPSILON)
+            m_lifted = 0.;
         return "";
     }