From 956f7a4593887237f68150eb9da855d50a030e5f Mon Sep 17 00:00:00 2001
From: enricoturri1966 <enricoturri@seznam.cz>
Date: Thu, 2 Apr 2020 12:03:18 +0200
Subject: [PATCH] GCodeProcessor additions:

process G90 lines

process G91 lines

process G92 lines

process M82 lines

process M83 lines

process T lines

process extrusion role/width/height comment tags

debug output
---
 src/libslic3r/GCode.cpp                |  43 +++--
 src/libslic3r/GCode/Analyzer.cpp       |  29 ++-
 src/libslic3r/GCode/Analyzer.hpp       |  12 ++
 src/libslic3r/GCode/GCodeProcessor.cpp | 251 +++++++++++++++++++------
 src/libslic3r/GCode/GCodeProcessor.hpp |  66 +++++--
 src/libslic3r/GCode/WipeTower.cpp      |  15 ++
 src/libslic3r/Technologies.hpp         |   7 +-
 src/slic3r/GUI/GLCanvas3D.cpp          |  10 +
 8 files changed, 349 insertions(+), 84 deletions(-)

diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp
index 28fa2ca28..3ea365b3c 100644
--- a/src/libslic3r/GCode.cpp
+++ b/src/libslic3r/GCode.cpp
@@ -780,6 +780,10 @@ void GCode::do_export(Print* print, const char* path, GCodePreviewData* preview_
 
     // starts analyzer calculations
     if (m_enable_analyzer) {
+#if ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
+        m_analyzer.close_debug_output_file();
+#endif // ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
+
         BOOST_LOG_TRIVIAL(debug) << "Preparing G-code preview data" << log_memory_info();
         m_analyzer.calc_gcode_preview_data(*preview_data, [print]() { print->throw_if_canceled(); });
         m_analyzer.reset();
@@ -897,24 +901,17 @@ namespace DoExport {
 
 	    // tell analyzer about the gcode flavor
 	    analyzer.set_gcode_flavor(config.gcode_flavor);
-	}
+
+#if ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
+        analyzer.open_debug_output_file();
+#endif // ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
+    }
 
 #if ENABLE_GCODE_VIEWER
     static void init_gcode_processor(const PrintConfig& config, GCodeProcessor& processor)
     {
         processor.reset();
         processor.apply_config(config);
-
-        // send extruder offset data to processor
-        GCodeProcessor::ExtruderOffsetsMap extruder_offsets;
-        const size_t num_extruders = config.nozzle_diameter.values.size();
-        for (size_t id = 0; id < num_extruders; ++id)
-        {
-            const Vec2d& offset = config.extruder_offset.get_at(id);
-            if (!offset.isApprox(Vec2d::Zero()))
-                extruder_offsets[static_cast<unsigned int>(id)] = offset;
-        }
-        processor.set_extruder_offsets(extruder_offsets);
     }
 #endif // ENABLE_GCODE_VIEWER
 
@@ -1340,6 +1337,11 @@ void GCode::_do_export(Print& print, FILE* file, ThumbnailsGeneratorCallback thu
         // adds tag for analyzer
         _write_format(file, ";%s%d\n", GCodeAnalyzer::Extrusion_Role_Tag.c_str(), erCustom);
 
+#if ENABLE_GCODE_VIEWER
+    // adds tag for processor
+    _write_format(file, ";%s%d\n", GCodeProcessor::Extrusion_Role_Tag.c_str(), erCustom);
+#endif // ENABLE_GCODE_VIEWER
+
     // Write the custom start G-code
     _writeln(file, start_gcode);
 
@@ -1493,6 +1495,11 @@ void GCode::_do_export(Print& print, FILE* file, ThumbnailsGeneratorCallback thu
         // adds tag for analyzer
         _write_format(file, ";%s%d\n", GCodeAnalyzer::Extrusion_Role_Tag.c_str(), erCustom);
 
+#if ENABLE_GCODE_VIEWER
+    // adds tag for processor
+    _write_format(file, ";%s%d\n", GCodeProcessor::Extrusion_Role_Tag.c_str(), erCustom);
+#endif // ENABLE_GCODE_VIEWER
+
     // Process filament-specific gcode in extruder order.
     {
         DynamicConfig config;
@@ -3112,6 +3119,10 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description,
         {
             m_last_analyzer_extrusion_role = path.role();
             sprintf(buf, ";%s%d\n", GCodeAnalyzer::Extrusion_Role_Tag.c_str(), int(m_last_analyzer_extrusion_role));
+#if ENABLE_GCODE_VIEWER
+            gcode += buf;
+            sprintf(buf, ";%s%d\n", GCodeProcessor::Extrusion_Role_Tag.c_str(), int(m_last_analyzer_extrusion_role));
+#endif // ENABLE_GCODE_VIEWER
             gcode += buf;
         }
 
@@ -3127,6 +3138,10 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description,
             m_last_width = path.width;
             sprintf(buf, ";%s%f\n", GCodeAnalyzer::Width_Tag.c_str(), m_last_width);
             gcode += buf;
+#if ENABLE_GCODE_VIEWER
+            sprintf(buf, ";%s%f\n", GCodeProcessor::Width_Tag.c_str(), m_last_width);
+            gcode += buf;
+#endif // ENABLE_GCODE_VIEWER
         }
 
         if (last_was_wipe_tower || (m_last_height != path.height))
@@ -3134,6 +3149,10 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description,
             m_last_height = path.height;
             sprintf(buf, ";%s%f\n", GCodeAnalyzer::Height_Tag.c_str(), m_last_height);
             gcode += buf;
+#if ENABLE_GCODE_VIEWER
+            sprintf(buf, ";%s%f\n", GCodeProcessor::Height_Tag.c_str(), m_last_height);
+            gcode += buf;
+#endif // ENABLE_GCODE_VIEWER
         }
     }
 
diff --git a/src/libslic3r/GCode/Analyzer.cpp b/src/libslic3r/GCode/Analyzer.cpp
index 442d5ec83..8a2ba7804 100644
--- a/src/libslic3r/GCode/Analyzer.cpp
+++ b/src/libslic3r/GCode/Analyzer.cpp
@@ -174,6 +174,19 @@ bool GCodeAnalyzer::is_valid_extrusion_role(ExtrusionRole role)
     return ((erPerimeter <= role) && (role < erMixed));
 }
 
+#if ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
+void GCodeAnalyzer::open_debug_output_file()
+{
+    boost::filesystem::path path("d:/analyzer.output");
+    m_debug_output.open(path.string());
+}
+
+void GCodeAnalyzer::close_debug_output_file()
+{
+    m_debug_output.close();
+}
+#endif // ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
+
 void GCodeAnalyzer::_process_gcode_line(GCodeReader&, const GCodeReader::GCodeLine& line)
 {
     // processes 'special' comments contained in line
@@ -350,7 +363,7 @@ void GCodeAnalyzer::_processG1(const GCodeReader::GCodeLine& line)
     if (delta_pos[E] < 0.0f)
     {
         if ((delta_pos[X] != 0.0f) || (delta_pos[Y] != 0.0f) || (delta_pos[Z] != 0.0f))
-        type = GCodeMove::Move;
+            type = GCodeMove::Move;
         else
             type = GCodeMove::Retract;
     }
@@ -922,6 +935,20 @@ void GCodeAnalyzer::_store_move(GCodeAnalyzer::GCodeMove::EType type)
     Vec3d start_position = _get_start_position() + extruder_offset;
     Vec3d end_position = _get_end_position() + extruder_offset;
     it->second.emplace_back(type, _get_extrusion_role(), extruder_id, _get_mm3_per_mm(), _get_width(), _get_height(), _get_feedrate(), start_position, end_position, _get_delta_extrusion(), _get_fan_speed(), _get_cp_color_id());
+
+#if ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
+    if (m_debug_output.good())
+    {
+        m_debug_output << std::to_string((int)type);
+        m_debug_output << ", " << std::to_string((int)_get_extrusion_role());
+        m_debug_output << ", " << Slic3r::to_string(_get_end_position());
+        m_debug_output << ", " << std::to_string(extruder_id);
+        m_debug_output << ", " << std::to_string(_get_feedrate());
+        m_debug_output << ", " << std::to_string(_get_width());
+        m_debug_output << ", " << std::to_string(_get_height());
+        m_debug_output << "\n";
+    }
+#endif // ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
 }
 
 bool GCodeAnalyzer::_is_valid_extrusion_role(int value) const
diff --git a/src/libslic3r/GCode/Analyzer.hpp b/src/libslic3r/GCode/Analyzer.hpp
index cd5654a74..fb5ef3b4c 100644
--- a/src/libslic3r/GCode/Analyzer.hpp
+++ b/src/libslic3r/GCode/Analyzer.hpp
@@ -8,6 +8,10 @@
 #include "../Point.hpp"
 #include "../GCodeReader.hpp"
 
+#if ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
+#include <boost/nowide/fstream.hpp>
+#endif // ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
+
 namespace Slic3r {
 
 class GCodePreviewData;
@@ -147,6 +151,14 @@ public:
 
     static bool is_valid_extrusion_role(ExtrusionRole role);
 
+#if ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
+private:
+    boost::nowide::ofstream m_debug_output;
+public:
+    void open_debug_output_file();
+    void close_debug_output_file();
+#endif // ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
+
 private:
     // Processes the given gcode line
     void _process_gcode_line(GCodeReader& reader, const GCodeReader::GCodeLine& line);
diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp
index 493321a6e..ffb4e1714 100644
--- a/src/libslic3r/GCode/GCodeProcessor.cpp
+++ b/src/libslic3r/GCode/GCodeProcessor.cpp
@@ -6,19 +6,47 @@
 static const float INCHES_TO_MM = 25.4f;
 static const float MMMIN_TO_MMSEC = 1.0f / 60.0f;
 
+static bool is_valid_extrusion_role(int value)
+{
+    return ((int)Slic3r::erNone <= value) && (value <= (int)Slic3r::erMixed);
+}
+
 namespace Slic3r {
 
+const std::string GCodeProcessor::Extrusion_Role_Tag = "_PROCESSOR_EXTRUSION_ROLE:";
+const std::string GCodeProcessor::Width_Tag          = "_PROCESSOR_WIDTH:";
+const std::string GCodeProcessor::Height_Tag         = "_PROCESSOR_HEIGHT:";
+
+void GCodeProcessor::apply_config(const PrintConfig& config)
+{
+    m_parser.apply_config(config);
+
+    size_t extruders_count = config.nozzle_diameter.values.size();
+    if (m_extruder_offsets.size() != extruders_count)
+        m_extruder_offsets.resize(extruders_count);
+
+    for (size_t id = 0; id < extruders_count; ++id)
+    {
+        Vec2f offset = config.extruder_offset.get_at(id).cast<float>();
+        m_extruder_offsets[id] = Vec3f(offset(0), offset(1), 0.0f);
+    }
+}
+
 void GCodeProcessor::reset()
 {
     m_units = EUnits::Millimeters;
     m_global_positioning_type = EPositioningType::Absolute;
     m_e_local_positioning_type = EPositioningType::Absolute;
-    
+    m_extruder_offsets = std::vector<Vec3f>(1, Vec3f::Zero());
+
     std::fill(m_start_position.begin(), m_start_position.end(), 0.0f);
     std::fill(m_end_position.begin(), m_end_position.end(), 0.0f);
     std::fill(m_origin.begin(), m_origin.end(), 0.0f);
 
     m_feedrate = 0.0f;
+    m_width = 0.0f;
+    m_height = 0.0f;
+    m_extrusion_role = erNone;
     m_extruder_id = 0;
 
     m_result.reset();
@@ -26,10 +54,8 @@ void GCodeProcessor::reset()
 
 void GCodeProcessor::process_file(const std::string& filename)
 {
-    MoveVertex start_vertex {};
-    m_result.moves.emplace_back(start_vertex);
+    m_result.moves.emplace_back(MoveVertex());
     m_parser.parse_file(filename, [this](GCodeReader& reader, const GCodeReader::GCodeLine& line) { process_gcode_line(line); });
-    int a = 0;
 }
 
 void GCodeProcessor::process_gcode_line(const GCodeReader::GCodeLine& line)
@@ -42,8 +68,6 @@ void GCodeProcessor::process_gcode_line(const GCodeReader::GCodeLine& line)
     m_start_position = m_end_position;
 
     std::string cmd = line.cmd();
-    std::string comment = line.comment();
-
     if (cmd.length() > 1)
     {
         // process command lines
@@ -54,7 +78,13 @@ void GCodeProcessor::process_gcode_line(const GCodeReader::GCodeLine& line)
                 switch (::atoi(&cmd[1]))
                 {
                 // Move
-                case 1: { process_G1(line); }
+                case 1: { process_G1(line); break; }
+                // Set to Absolute Positioning
+                case 90: { processG90(line); break; }
+                // Set to Relative Positioning
+                case 91: { processG91(line); break; }
+                // Set Position
+                case 92: { processG92(line); break; }
                 default: { break; }
                 }
                 break;
@@ -63,6 +93,10 @@ void GCodeProcessor::process_gcode_line(const GCodeReader::GCodeLine& line)
             {
                 switch (::atoi(&cmd[1]))
                 {
+                // Set extruder to absolute mode
+                case 82: { processM82(line); break; }
+                // Set extruder to relative mode
+                case 83: { processM83(line); break; }
                 default: { break; }
                 }
                 break;
@@ -74,20 +108,52 @@ void GCodeProcessor::process_gcode_line(const GCodeReader::GCodeLine& line)
         default: { break; }
         }
     }
-    else if (comment.length() > 1)
+    else
     {
-        // process tags embedded into comments
-        process_comment(line);
+        std::string comment = line.comment();
+        if (comment.length() > 1)
+            // process tags embedded into comments
+            process_tags(comment);
     }
 }
 
-void GCodeProcessor::process_comment(const GCodeReader::GCodeLine& line)
+void GCodeProcessor::process_tags(const std::string& comment)
 {
+    // extrusion role tag
+    size_t pos = comment.find(Extrusion_Role_Tag);
+    if (pos != comment.npos)
+    {
+        int role = std::stoi(comment.substr(pos + Extrusion_Role_Tag.length()));
+        if (is_valid_extrusion_role(role))
+            m_extrusion_role = static_cast<ExtrusionRole>(role);
+        else
+        {
+            // todo: show some error ?
+        }
+
+        return;
+    }
+
+    // width tag
+    pos = comment.find(Width_Tag);
+    if (pos != comment.npos)
+    {
+        m_width = std::stof(comment.substr(pos + Width_Tag.length()));
+        return;
+    }
+
+    // height tag
+    pos = comment.find(Height_Tag);
+    if (pos != comment.npos)
+    {
+        m_height = std::stof(comment.substr(pos + Height_Tag.length()));
+        return;
+    }
 }
 
 void GCodeProcessor::process_G1(const GCodeReader::GCodeLine& line)
 {
-    auto axis_absolute_position = [this](Axis axis, const GCodeReader::GCodeLine& lineG1)
+    auto absolute_position = [this](Axis axis, const GCodeReader::GCodeLine& lineG1)
     {
         bool is_relative = (m_global_positioning_type == EPositioningType::Relative);
         if (axis == E)
@@ -103,10 +169,36 @@ void GCodeProcessor::process_G1(const GCodeReader::GCodeLine& line)
             return m_start_position[axis];
     };
 
+    auto move_type = [this](const AxisCoords& delta_pos) {
+        EMoveType type = EMoveType::Noop;
+
+        if (delta_pos[E] < 0.0f)
+        {
+            if (delta_pos[X] != 0.0f || delta_pos[Y] != 0.0f || delta_pos[Z] != 0.0f)
+                type = EMoveType::Travel;
+            else
+                type = EMoveType::Retract;
+        }
+        else if (delta_pos[E] > 0.0f)
+        {
+            if (delta_pos[X] == 0.0f && delta_pos[Y] == 0.0f && delta_pos[Z] == 0.0f)
+                type = EMoveType::Unretract;
+            else if ((delta_pos[X] != 0.0f) || (delta_pos[Y] != 0.0f))
+                type = EMoveType::Extrude;
+        }
+        else if (delta_pos[X] != 0.0f || delta_pos[Y] != 0.0f || delta_pos[Z] != 0.0f)
+            type = EMoveType::Travel;
+
+        if (type == EMoveType::Extrude && (m_width == 0.0f || m_height == 0.0f || !is_valid_extrusion_role(m_extrusion_role)))
+            type = EMoveType::Travel;
+
+        return type;
+    };
+
     // updates axes positions from line
     for (unsigned char a = X; a <= E; ++a)
     {
-        m_end_position[a] = axis_absolute_position((Axis)a, line);
+        m_end_position[a] = absolute_position((Axis)a, line);
     }
 
     // updates feedrate from line, if present
@@ -124,56 +216,109 @@ void GCodeProcessor::process_G1(const GCodeReader::GCodeLine& line)
 
     // no displacement, return
     if (max_abs_delta == 0.0f)
-        return; // <<<<<<<<<<<<<<<<< is this correct for time estimate ?
+        return;
 
-    // detect move type
-    EMoveType move_type = EMoveType::Noop;
-    if (delta_pos[E] < 0.0f)
+    // store g1 move
+    store_move_vertex(move_type(delta_pos));
+}
+
+void GCodeProcessor::processG90(const GCodeReader::GCodeLine& line)
+{
+    m_global_positioning_type = EPositioningType::Absolute;
+}
+
+void GCodeProcessor::processG91(const GCodeReader::GCodeLine& line)
+{
+    m_global_positioning_type = EPositioningType::Relative;
+}
+
+void GCodeProcessor::processG92(const GCodeReader::GCodeLine& line)
+{
+    float lengthsScaleFactor = (m_units == EUnits::Inches) ? INCHES_TO_MM : 1.0f;
+    bool anyFound = false;
+
+    if (line.has_x())
     {
-        if ((delta_pos[X] != 0.0f) || (delta_pos[Y] != 0.0f) || (delta_pos[Z] != 0.0f))
-            move_type = EMoveType::Travel;
-        else
-            move_type = EMoveType::Retract;
+        m_origin[X] = m_end_position[X] - line.x() * lengthsScaleFactor;
+        anyFound = true;
     }
-    else if (delta_pos[E] > 0.0f)
+
+    if (line.has_y())
     {
-        if ((delta_pos[X] == 0.0f) && (delta_pos[Y] == 0.0f) && (delta_pos[Z] == 0.0f))
-            move_type = EMoveType::Unretract;
-        else if ((delta_pos[X] != 0.0f) || (delta_pos[Y] != 0.0f))
-            move_type = EMoveType::Extrude;
+        m_origin[Y] = m_end_position[Y] - line.y() * lengthsScaleFactor;
+        anyFound = true;
     }
-    else if ((delta_pos[X] != 0.0f) || (delta_pos[Y] != 0.0f) || (delta_pos[Z] != 0.0f))
-        move_type = EMoveType::Travel;
 
-    // correct position by extruder offset
-    Vec3d extruder_offset = Vec3d::Zero();
-    auto it = m_extruder_offsets.find(m_extruder_id);
-    if (it != m_extruder_offsets.end())
-        extruder_offset = Vec3d(it->second(0), it->second(1), 0.0);
+    if (line.has_z())
+    {
+        m_origin[Z] = m_end_position[Z] - line.z() * lengthsScaleFactor;
+        anyFound = true;
+    }
 
+    if (line.has_e())
+    {
+        // extruder coordinate can grow to the point where its float representation does not allow for proper addition with small increments,
+        // we set the value taken from the G92 line as the new current position for it
+        m_end_position[E] = line.e() * lengthsScaleFactor;
+        anyFound = true;
+    }
+
+    if (!anyFound && !line.has_unknown_axis())
+    {
+        // The G92 may be called for axes that PrusaSlicer does not recognize, for example see GH issue #3510, 
+        // where G92 A0 B0 is called although the extruder axis is till E.
+        for (unsigned char a = X; a <= E; ++a)
+        {
+            m_origin[a] = m_end_position[a];
+        }
+    }
+}
+
+void GCodeProcessor::processM82(const GCodeReader::GCodeLine& line)
+{
+    m_e_local_positioning_type = EPositioningType::Absolute;
+}
+
+void GCodeProcessor::processM83(const GCodeReader::GCodeLine& line)
+{
+    m_e_local_positioning_type = EPositioningType::Relative;
+}
+
+void GCodeProcessor::processT(const GCodeReader::GCodeLine& line)
+{
+    const std::string& cmd = line.cmd();
+    if (cmd.length() > 1)
+    {
+        unsigned int id = (unsigned int)std::stoi(cmd.substr(1));
+        if (m_extruder_id != id)
+        {
+            unsigned int extruders_count = (unsigned int)m_extruder_offsets.size();
+            if (id >= extruders_count)
+                BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid toolchange, maybe from a custom gcode.";
+            else
+            {
+                m_extruder_id = id;
+//                if (_get_cp_color_id() != INT_MAX) <<<<<<<<<<<<<<<<<<< TODO
+//                    _set_cp_color_id(m_extruder_color[id]);
+            }
+
+            // store tool change move
+            store_move_vertex(EMoveType::Tool_change);
+        }
+    }
+}
+
+void GCodeProcessor::store_move_vertex(EMoveType type)
+{
     MoveVertex vertex;
-    vertex.position = Vec3d(m_end_position[0], m_end_position[1], m_end_position[2]) + extruder_offset;
+    vertex.type = type;
+    vertex.extrusion_role = m_extrusion_role;
+    vertex.position = Vec3f(m_end_position[0], m_end_position[1], m_end_position[2]) + m_extruder_offsets[m_extruder_id];
     vertex.feedrate = m_feedrate;
-    vertex.type = move_type;
+    vertex.width = m_width;
+    vertex.height = m_height;
+    vertex.extruder_id = m_extruder_id;
     m_result.moves.emplace_back(vertex);
-
-/*
-    std::cout << "start: ";
-    for (unsigned char a = X; a <= E; ++a)
-    {
-        std::cout << m_start_position[a];
-        if (a != E)
-            std::cout << ", ";
-    }
-    std::cout << " - end: ";
-    for (unsigned char a = X; a <= E; ++a)
-    {
-        std::cout << m_end_position[a];
-        if (a != E)
-            std::cout << ", ";
-    }
-    std::cout << "\n";
-*/
 }
 
 } /* namespace Slic3r */
diff --git a/src/libslic3r/GCode/GCodeProcessor.hpp b/src/libslic3r/GCode/GCodeProcessor.hpp
index 7fa1733b7..537617485 100644
--- a/src/libslic3r/GCode/GCodeProcessor.hpp
+++ b/src/libslic3r/GCode/GCodeProcessor.hpp
@@ -3,11 +3,19 @@
 
 #if ENABLE_GCODE_VIEWER
 #include "../GCodeReader.hpp"
+#include "../Point.hpp"
+#include "../ExtrusionEntity.hpp"
 
 namespace Slic3r {
 
     class GCodeProcessor
     {
+    public:
+        static const std::string Extrusion_Role_Tag;
+        static const std::string Width_Tag;
+        static const std::string Height_Tag;
+
+    private:
         using AxisCoords = std::array<float, 4>;
 
         enum class EUnits : unsigned char
@@ -33,16 +41,29 @@ namespace Slic3r {
             Num_Types
         };
 
+    public:
         struct MoveVertex
         {
-            Vec3d position{ Vec3d::Zero() }; // mm
-            float feedrate{ 0.0f }; // mm/s
-            // type of the move terminating at this vertex
             EMoveType type{ EMoveType::Noop };
-        };
+            ExtrusionRole extrusion_role{ erNone };
+            Vec3f position{ Vec3f::Zero() }; // mm
+            float feedrate{ 0.0f }; // mm/s
+            float width{ 0.0f }; // mm
+            float height{ 0.0f }; // mm
+            unsigned int extruder_id{ 0 };
 
-    public:
-        typedef std::map<unsigned int, Vec2d> ExtruderOffsetsMap;
+            std::string to_string() const
+            {
+                std::string str = std::to_string((int)type);
+                str += ", " + std::to_string((int)extrusion_role);
+                str += ", " + Slic3r::to_string((Vec3d)position.cast<double>());
+                str += ", " + std::to_string(extruder_id);
+                str += ", " + std::to_string(feedrate);
+                str += ", " + std::to_string(width);
+                str += ", " + std::to_string(height);
+                return str;
+            }
+        };
 
         struct Result
         {
@@ -56,25 +77,26 @@ namespace Slic3r {
         EUnits m_units;
         EPositioningType m_global_positioning_type;
         EPositioningType m_e_local_positioning_type;
+        std::vector<Vec3f> m_extruder_offsets;
 
         AxisCoords m_start_position; // mm
         AxisCoords m_end_position;   // mm
         AxisCoords m_origin;         // mm
 
         float m_feedrate; // mm/s
+        float m_width;    // mm
+        float m_height;   // mm
+        ExtrusionRole m_extrusion_role;
         unsigned int m_extruder_id;
-        ExtruderOffsetsMap m_extruder_offsets;
 
         Result m_result;
 
     public:
         GCodeProcessor() { reset(); }
 
-        void apply_config(const PrintConfig& config) { m_parser.apply_config(config); }
+        void apply_config(const PrintConfig& config);
         void reset();
 
-        void set_extruder_offsets(const ExtruderOffsetsMap& extruder_offsets) { m_extruder_offsets = extruder_offsets; }
-
         const Result& get_result() const { return m_result; }
         Result&& extract_result() { return std::move(m_result); }
 
@@ -86,11 +108,31 @@ namespace Slic3r {
         void process_gcode_line(const GCodeReader::GCodeLine& line);
 
         // Process tags embedded into comments
-        void process_comment(const GCodeReader::GCodeLine& line);
+        void process_tags(const std::string& comment);
 
         // Move
         void process_G1(const GCodeReader::GCodeLine& line);
-    };
+
+        // Set to Absolute Positioning
+        void processG90(const GCodeReader::GCodeLine& line);
+
+        // Set to Relative Positioning
+        void processG91(const GCodeReader::GCodeLine& line);
+
+        // Set Position
+        void processG92(const GCodeReader::GCodeLine& line);
+
+        // Set extruder to absolute mode
+        void processM82(const GCodeReader::GCodeLine& line);
+
+        // Set extruder to relative mode
+        void processM83(const GCodeReader::GCodeLine& line);
+
+        // Processes T line (Select Tool)
+        void processT(const GCodeReader::GCodeLine& line);
+
+        void store_move_vertex(EMoveType type);
+   };
 
 } /* namespace Slic3r */
 
diff --git a/src/libslic3r/GCode/WipeTower.cpp b/src/libslic3r/GCode/WipeTower.cpp
index d31adbd8f..339012d0b 100644
--- a/src/libslic3r/GCode/WipeTower.cpp
+++ b/src/libslic3r/GCode/WipeTower.cpp
@@ -22,6 +22,9 @@ TODO LIST
 #include <numeric>
 
 #include "Analyzer.hpp"
+#if ENABLE_GCODE_VIEWER
+#include "GCodeProcessor.hpp"
+#endif // ENABLE_GCODE_VIEWER
 #include "BoundingBox.hpp"
 
 #if defined(__linux) || defined(__GNUC__ )
@@ -55,7 +58,15 @@ public:
             char buf[64];
             sprintf(buf, ";%s%f\n", GCodeAnalyzer::Height_Tag.c_str(), m_layer_height); // don't rely on GCodeAnalyzer knowing the layer height - it knows nothing at priming
             m_gcode += buf;
+#if ENABLE_GCODE_VIEWER
+            sprintf(buf, ";%s%f\n", GCodeProcessor::Height_Tag.c_str(), m_layer_height); // don't rely on GCodeAnalyzer knowing the layer height - it knows nothing at priming
+            m_gcode += buf;
+#endif // ENABLE_GCODE_VIEWER
             sprintf(buf, ";%s%d\n", GCodeAnalyzer::Extrusion_Role_Tag.c_str(), erWipeTower);
+#if ENABLE_GCODE_VIEWER
+            m_gcode += buf;
+            sprintf(buf, ";%s%d\n", GCodeProcessor::Extrusion_Role_Tag.c_str(), erWipeTower);
+#endif // ENABLE_GCODE_VIEWER
             m_gcode += buf;
             change_analyzer_line_width(line_width);
         }
@@ -65,6 +76,10 @@ public:
             char buf[64];
             sprintf(buf, ";%s%f\n", GCodeAnalyzer::Width_Tag.c_str(), line_width);
             m_gcode += buf;
+#if ENABLE_GCODE_VIEWER
+            sprintf(buf, ";%s%f\n", GCodeProcessor::Width_Tag.c_str(), line_width);
+            m_gcode += buf;
+#endif // ENABLE_GCODE_VIEWER
             return *this;
     }
 
diff --git a/src/libslic3r/Technologies.hpp b/src/libslic3r/Technologies.hpp
index 44c10fa54..85ce8a2cb 100644
--- a/src/libslic3r/Technologies.hpp
+++ b/src/libslic3r/Technologies.hpp
@@ -60,14 +60,9 @@
 // Moves GLCanvas3DManager from being a static member of _3DScene to be a normal member of GUI_App
 #define ENABLE_NON_STATIC_CANVAS_MANAGER (1 && ENABLE_2_3_0_ALPHA1)
 
-
-//==================
-// 2.3.0.alpha1 techs
-//==================
-#define ENABLE_2_3_0_ALPHA1 1
-
 // Enable G-Code viewer
 #define ENABLE_GCODE_VIEWER (1 && ENABLE_2_3_0_ALPHA1)
+#define ENABLE_GCODE_VIEWER_DEBUG_OUTPUT (1 && ENABLE_GCODE_VIEWER)
 
 
 #endif // _prusaslicer_technologies_h_
diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp
index d8b102cee..de85929ea 100644
--- a/src/slic3r/GUI/GLCanvas3D.cpp
+++ b/src/slic3r/GUI/GLCanvas3D.cpp
@@ -2772,6 +2772,16 @@ static void load_gcode_retractions(const GCodePreviewData::Retraction& retractio
 #if ENABLE_GCODE_VIEWER
 void GLCanvas3D::load_gcode_preview_2(const GCodeProcessor::Result& gcode_result)
 {
+#if ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
+    boost::filesystem::path path("d:/processor.output");
+    boost::nowide::ofstream out;
+    out.open(path.string());
+    for (const GCodeProcessor::MoveVertex& v : gcode_result.moves)
+    {
+        out << v.to_string() << "\n";
+    }
+    out.close();
+#endif // ENABLE_GCODE_VIEWER_DEBUG_OUTPUT
 }
 #endif // ENABLE_GCODE_VIEWER