From 11cf9a87f143dcc18c82779ce63a7cdf768521e4 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Wed, 29 Jul 2020 10:04:10 +0200 Subject: [PATCH] GCodeProcessor -> Calculate mm3 per mm on the fly --- src/libslic3r/GCode.cpp | 8 ++------ src/libslic3r/GCode.hpp | 7 ++++++- src/libslic3r/GCode/GCodeProcessor.cpp | 28 ++++++++++++-------------- src/libslic3r/GCode/GCodeProcessor.hpp | 2 +- src/libslic3r/GCode/WipeTower.cpp | 26 ++++++++++++------------ 5 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 742498baf..ced52207d 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1178,7 +1178,6 @@ void GCode::_do_export(Print& print, FILE* file, ThumbnailsGeneratorCallback thu // resets analyzer's tracking data #if ENABLE_GCODE_VIEWER - m_last_mm3_per_mm = 0.0f; m_last_width = 0.0f; m_last_height = 0.0f; #else @@ -3237,16 +3236,13 @@ std::string GCode::_extrude(const ExtrusionPath &path, std::string description, } #endif // ENABLE_GCODE_VIEWER +#if !ENABLE_GCODE_VIEWER if (last_was_wipe_tower || (m_last_mm3_per_mm != path.mm3_per_mm)) { m_last_mm3_per_mm = path.mm3_per_mm; -#if ENABLE_GCODE_VIEWER - sprintf(buf, ";%s%f\n", GCodeProcessor::Mm3_Per_Mm_Tag.c_str(), m_last_mm3_per_mm); - gcode += buf; -#else sprintf(buf, ";%s%f\n", GCodeAnalyzer::Mm3_Per_Mm_Tag.c_str(), m_last_mm3_per_mm); gcode += buf; -#endif // ENABLE_GCODE_VIEWER } +#endif // !ENABLE_GCODE_VIEWER if (last_was_wipe_tower || (m_last_width != path.width)) { m_last_width = path.width; diff --git a/src/libslic3r/GCode.hpp b/src/libslic3r/GCode.hpp index 273242762..443c25bb2 100644 --- a/src/libslic3r/GCode.hpp +++ b/src/libslic3r/GCode.hpp @@ -173,7 +173,6 @@ public: m_last_pos_defined(false), m_last_extrusion_role(erNone), #if ENABLE_GCODE_VIEWER - m_last_mm3_per_mm(0.0f), m_last_width(0.0f), m_last_height(0.0f), #else @@ -378,10 +377,16 @@ private: double m_volumetric_speed; // Support for the extrusion role markers. Which marker is active? ExtrusionRole m_last_extrusion_role; +#if ENABLE_GCODE_VIEWER + // Support for G-Code Processor + float m_last_width; + float m_last_height; +#else // Support for G-Code Analyzer double m_last_mm3_per_mm; float m_last_width; float m_last_height; +#endif // ENABLE_GCODE_VIEWER Point m_last_pos; bool m_last_pos_defined; diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp index 44598d240..ef9c64880 100644 --- a/src/libslic3r/GCode/GCodeProcessor.cpp +++ b/src/libslic3r/GCode/GCodeProcessor.cpp @@ -24,7 +24,6 @@ namespace Slic3r { const std::string GCodeProcessor::Extrusion_Role_Tag = "ExtrType:"; const std::string GCodeProcessor::Width_Tag = "PrusaSlicer__WIDTH:"; const std::string GCodeProcessor::Height_Tag = "PrusaSlicer__HEIGHT:"; -const std::string GCodeProcessor::Mm3_Per_Mm_Tag = "PrusaSlicer__MM3_PER_MM:"; const std::string GCodeProcessor::Color_Change_Tag = "COLOR_CHANGE"; const std::string GCodeProcessor::Pause_Print_Tag = "PAUSE_PRINT"; const std::string GCodeProcessor::Custom_Code_Tag = "CUSTOM_CODE"; @@ -325,6 +324,10 @@ void GCodeProcessor::apply_config(const PrintConfig& config) m_extruders_color[id] = static_cast(id); } + for (double diam : config.filament_diameter.values) { + m_filament_diameters.push_back(static_cast(diam)); + } + m_time_processor.machine_limits = reinterpret_cast(config); // Filament load / unload times are not specific to a firmware flavor. Let anybody use it if they find it useful. // As of now the fields are shown at the UI dialog in the same combo box as the ramming values, so they @@ -370,6 +373,7 @@ void GCodeProcessor::reset() m_extrusion_role = erNone; m_extruder_id = 0; m_extruders_color = ExtrudersColor(); + m_filament_diameters = std::vector(); m_cp_color.reset(); m_producer = EProducer::Unknown; @@ -592,20 +596,6 @@ void GCodeProcessor::process_tags(const std::string& comment) return; } - // mm3 per mm tag - pos = comment.find(Mm3_Per_Mm_Tag); - if (pos != comment.npos) { - try - { - m_mm3_per_mm = std::stof(comment.substr(pos + Mm3_Per_Mm_Tag.length())); - } - catch (...) - { - BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid value for Mm3_Per_Mm (" << comment << ")."; - } - return; - } - // color change tag pos = comment.find(Color_Change_Tag); if (pos != comment.npos) { @@ -1019,6 +1009,14 @@ void GCodeProcessor::process_G1(const GCodeReader::GCodeLine& line) EMoveType type = move_type(delta_pos); + if (type == EMoveType::Extrude) { + if (delta_pos[E] > 0.0f) { + float ds = std::sqrt(sqr(delta_pos[X]) + sqr(delta_pos[Y]) + sqr(delta_pos[Z])); + if (ds > 0.0f && static_cast(m_extruder_id) < m_filament_diameters.size()) + m_mm3_per_mm = round_nearest(delta_pos[E] * static_cast(M_PI) * sqr(static_cast(m_filament_diameters[m_extruder_id])) / (4.0f * ds), 3); + } + } + // time estimate section auto move_length = [](const AxisCoords& delta_pos) { float sq_xyz_length = sqr(delta_pos[X]) + sqr(delta_pos[Y]) + sqr(delta_pos[Z]); diff --git a/src/libslic3r/GCode/GCodeProcessor.hpp b/src/libslic3r/GCode/GCodeProcessor.hpp index 9507adf4d..d59fc7bb9 100644 --- a/src/libslic3r/GCode/GCodeProcessor.hpp +++ b/src/libslic3r/GCode/GCodeProcessor.hpp @@ -21,7 +21,6 @@ namespace Slic3r { static const std::string Extrusion_Role_Tag; static const std::string Width_Tag; static const std::string Height_Tag; - static const std::string Mm3_Per_Mm_Tag; static const std::string Color_Change_Tag; static const std::string Pause_Print_Tag; static const std::string Custom_Code_Tag; @@ -241,6 +240,7 @@ namespace Slic3r { ExtrusionRole m_extrusion_role; unsigned char m_extruder_id; ExtrudersColor m_extruders_color; + std::vector m_filament_diameters; CpColor m_cp_color; enum class EProducer diff --git a/src/libslic3r/GCode/WipeTower.cpp b/src/libslic3r/GCode/WipeTower.cpp index e4f995aba..c20009a48 100644 --- a/src/libslic3r/GCode/WipeTower.cpp +++ b/src/libslic3r/GCode/WipeTower.cpp @@ -84,19 +84,17 @@ public: return *this; } - WipeTowerWriter& change_analyzer_mm3_per_mm(float len, float e) { - static const float area = float(M_PI) * 1.75f * 1.75f / 4.f; - float mm3_per_mm = (len == 0.f ? 0.f : area * e / len); - // adds tag for analyzer: - char buf[64]; -#if ENABLE_GCODE_VIEWER - sprintf(buf, ";%s%f\n", GCodeProcessor::Mm3_Per_Mm_Tag.c_str(), mm3_per_mm); -#else - sprintf(buf, ";%s%f\n", GCodeAnalyzer::Mm3_Per_Mm_Tag.c_str(), mm3_per_mm); -#endif // ENABLE_GCODE_VIEWER - m_gcode += buf; - return *this; +#if !ENABLE_GCODE_VIEWER + WipeTowerWriter& change_analyzer_mm3_per_mm(float len, float e) { + static const float area = float(M_PI) * 1.75f * 1.75f / 4.f; + float mm3_per_mm = (len == 0.f ? 0.f : area * e / len); + // adds tag for analyzer: + char buf[64]; + sprintf(buf, ";%s%f\n", GCodeAnalyzer::Mm3_Per_Mm_Tag.c_str(), mm3_per_mm); + m_gcode += buf; + return *this; } +#endif // !ENABLE_GCODE_VIEWER WipeTowerWriter& set_initial_position(const Vec2f &pos, float width = 0.f, float depth = 0.f, float internal_angle = 0.f) { m_wipe_tower_width = width; @@ -169,8 +167,10 @@ public: Vec2f rot(this->rotate(Vec2f(x,y))); // this is where we want to go if (! m_preview_suppressed && e > 0.f && len > 0.f) { +#if !ENABLE_GCODE_VIEWER change_analyzer_mm3_per_mm(len, e); - // Width of a squished extrusion, corrected for the roundings of the squished extrusions. +#endif // !ENABLE_GCODE_VIEWER + // Width of a squished extrusion, corrected for the roundings of the squished extrusions. // This is left zero if it is a travel move. float width = e * m_filpar[0].filament_area / (len * m_layer_height); // Correct for the roundings of a squished extrusion.