From 759d78dd2bad7157af8d41570ff24e5f7c4a364f Mon Sep 17 00:00:00 2001 From: "xun.zhang" Date: Mon, 30 Dec 2024 20:59:48 +0800 Subject: [PATCH] ENH: save filament change count in gcode result 1.Save filament change count per filament jira:NONE Signed-off-by: xun.zhang Change-Id: I26e7963c0b5fdcca7c7d0ec5590c3f40c1fc5eed --- src/libslic3r/GCode.cpp | 21 ++++++++++++++++++--- src/libslic3r/GCode/GCodeProcessor.cpp | 2 ++ src/libslic3r/GCode/GCodeProcessor.hpp | 5 +++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 338e66ce6..955a59883 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -2485,15 +2485,30 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato print.throw_if_canceled(); } - +// export info requested for filament change void GCode::export_layer_filaments(GCodeProcessorResult* result) { if (result == nullptr) return; - result->layer_filaments.clear(); + + const std::vectorfilament_map = m_config.filament_map.values; // 1 based + std::vectorprev_filament(m_config.nozzle_diameter.size(), -1); for (size_t idx = 0; idx < m_sorted_layer_filaments.size(); ++idx) { + for (auto f : m_sorted_layer_filaments[idx]) { + int extruder_idx = filament_map[f] - 1; + if (prev_filament[extruder_idx] != -1 && f != prev_filament[extruder_idx]) { + std::pair from_to_pair = { prev_filament[extruder_idx],f }; + auto iter = result->filament_change_count_map.find(from_to_pair); + if (iter == result->filament_change_count_map.end()) + result->filament_change_count_map.emplace(from_to_pair, 1); + else + iter->second += 1; + } + prev_filament[extruder_idx] = f; + } + // now we do not need sorted data, so we sort the filaments in id order - auto& layer_filaments = m_sorted_layer_filaments[idx]; + auto layer_filaments = m_sorted_layer_filaments[idx]; std::sort(layer_filaments.begin(), layer_filaments.end()); auto iter = result->layer_filaments.find(layer_filaments); if (iter == result->layer_filaments.end()) { diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp index d84d1d9aa..a6915f32c 100644 --- a/src/libslic3r/GCode/GCodeProcessor.cpp +++ b/src/libslic3r/GCode/GCodeProcessor.cpp @@ -1285,6 +1285,8 @@ void GCodeProcessorResult::reset() { filament_costs = std::vector(MIN_EXTRUDERS_COUNT, DEFAULT_FILAMENT_COST); custom_gcode_per_print_z = std::vector(); spiral_vase_layers = std::vector>>(); + layer_filaments.clear(); + filament_change_count_map.clear(); warnings.clear(); //BBS: add mutex for protection of gcode result diff --git a/src/libslic3r/GCode/GCodeProcessor.hpp b/src/libslic3r/GCode/GCodeProcessor.hpp index 4549645d3..c85608cfd 100644 --- a/src/libslic3r/GCode/GCodeProcessor.hpp +++ b/src/libslic3r/GCode/GCodeProcessor.hpp @@ -253,6 +253,9 @@ namespace Slic3r { std::vector nozzle_type; // first key stores filaments, second keys stores the layer ranges(enclosed) that use the filaments std::unordered_map, std::vector>,FilamentSequenceHash> layer_filaments; + // first key stores `from` filament, second keys stores the `to` filament + std::map, int > filament_change_count_map; + BedType bed_type = BedType::btCount; #if ENABLE_GCODE_VIEWER_STATISTICS int64_t time{ 0 }; @@ -288,6 +291,8 @@ namespace Slic3r { gcode_check_result = other.gcode_check_result; limit_filament_maps = other.limit_filament_maps; filament_printable_reuslt = other.filament_printable_reuslt; + layer_filaments = other.layer_filaments; + filament_change_count_map = other.filament_change_count_map; #if ENABLE_GCODE_VIEWER_STATISTICS time = other.time; #endif