From cf146f4fed41768c47c6f8ab86a03735b9964320 Mon Sep 17 00:00:00 2001 From: "xun.zhang" Date: Tue, 16 Apr 2024 14:57:04 +0800 Subject: [PATCH] FIX: can't use support filament in gcode.3mf 1. Add total_filament_volumes, directly access it to get used filaments github:#3865 Signed-off-by: xun.zhang Change-Id: I4fae4f1947b4ebd16e394e0f3cf5fb0e9f979717 --- src/libslic3r/Format/bbs_3mf.cpp | 13 ++---- src/libslic3r/GCode.cpp | 48 +------------------- src/libslic3r/GCode/GCodeProcessor.cpp | 61 +++++++++++++++++++------- src/libslic3r/GCode/GCodeProcessor.hpp | 17 ++++--- src/slic3r/GUI/GCodeViewer.cpp | 12 ++--- src/slic3r/GUI/PartPlate.cpp | 9 +--- src/slic3r/GUI/Plater.cpp | 14 +++--- 7 files changed, 74 insertions(+), 100 deletions(-) diff --git a/src/libslic3r/Format/bbs_3mf.cpp b/src/libslic3r/Format/bbs_3mf.cpp index 6cf951b36..4fd7cc0af 100644 --- a/src/libslic3r/Format/bbs_3mf.cpp +++ b/src/libslic3r/Format/bbs_3mf.cpp @@ -537,21 +537,14 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result) return ret; }; - for (auto it = ps.volumes_per_extruder.begin(); it != ps.volumes_per_extruder.end(); it++) { + for (auto it = ps.total_volumes_per_extruder.begin(); it != ps.total_volumes_per_extruder.end(); it++) { double volume = it->second; auto [used_filament_m, used_filament_g] = get_used_filament_from_volume(volume, it->first); FilamentInfo info; info.id = it->first; - if (ps.flush_per_filament.find(it->first) != ps.flush_per_filament.end()) { - volume = ps.flush_per_filament.at(it->first); - auto [flushed_filament_m, flushed_filament_g] = get_used_filament_from_volume(volume, it->first); - info.used_m = used_filament_m + flushed_filament_m; - info.used_g = used_filament_g + flushed_filament_g; - } else { - info.used_m = used_filament_m; - info.used_g = used_filament_g; - } + info.used_g = used_filament_g; + info.used_m = used_filament_m; slice_filaments_info.push_back(info); } diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 0bc7268d7..21fdada5d 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -976,52 +976,8 @@ namespace DoExport { double total_used_filament = 0.0; double total_weight = 0.0; double total_cost = 0.0; - for (auto volume : result.print_statistics.volumes_per_extruder) { - total_extruded_volume += volume.second; - size_t extruder_id = volume.first; - auto extruder = std::find_if(extruders.begin(), extruders.end(), [extruder_id](const Extruder& extr) { return extr.id() == extruder_id; }); - if (extruder == extruders.end()) - continue; - - double s = PI * sqr(0.5* extruder->filament_diameter()); - double weight = volume.second * extruder->filament_density() * 0.001; - total_used_filament += volume.second/s; - total_weight += weight; - total_cost += weight * extruder->filament_cost() * 0.001; - } - //BBS: add flush volume - for (auto volume : result.print_statistics.flush_per_filament) { - total_extruded_volume += volume.second; - - size_t extruder_id = volume.first; - auto extruder = std::find_if(extruders.begin(), extruders.end(), [extruder_id](const Extruder& extr) { return extr.id() == extruder_id; }); - if (extruder == extruders.end()) - continue; - - double s = PI * sqr(0.5* extruder->filament_diameter()); - double weight = volume.second * extruder->filament_density() * 0.001; - total_used_filament += volume.second/s; - total_weight += weight; - total_cost += weight * extruder->filament_cost() * 0.001; - } - - for (auto volume : result.print_statistics.wipe_tower_volumes_per_extruder) { - total_extruded_volume += volume.second; - - size_t extruder_id = volume.first; - auto extruder = std::find_if(extruders.begin(), extruders.end(), [extruder_id](const Extruder& extr) {return extr.id() == extruder_id; }); - if (extruder == extruders.end()) - continue; - - double s = PI * sqr(0.5* extruder->filament_diameter()); - double weight = volume.second * extruder->filament_density() * 0.001; - total_used_filament += volume.second/s; - total_weight += weight; - total_cost += weight * extruder->filament_cost() * 0.001; - } - - for (auto volume : result.print_statistics.support_volumes_per_extruder) { + for (auto volume : result.print_statistics.total_volumes_per_extruder) { total_extruded_volume += volume.second; size_t extruder_id = volume.first; @@ -1041,7 +997,7 @@ namespace DoExport { print_statistics.total_weight = total_weight; print_statistics.total_cost = total_cost; - print_statistics.filament_stats = result.print_statistics.volumes_per_extruder; + print_statistics.filament_stats = result.print_statistics.model_volumes_per_extruder; } // if any reserved keyword is found, returns a std::vector containing the first MAX_COUNT keywords found diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp index 918d6d549..eb704142c 100644 --- a/src/libslic3r/GCode/GCodeProcessor.cpp +++ b/src/libslic3r/GCode/GCodeProcessor.cpp @@ -709,7 +709,7 @@ void GCodeProcessor::UsedFilaments::reset() volumes_per_color_change = std::vector(); model_extrude_cache = 0.0f; - volumes_per_extruder.clear(); + model_volumes_per_extruder.clear(); flush_per_filament.clear(); @@ -717,16 +717,20 @@ void GCodeProcessor::UsedFilaments::reset() filaments_per_role.clear(); wipe_tower_cache = 0.0f; - wipe_tower_volume_per_extruder.clear(); + wipe_tower_volumes_per_extruder.clear(); support_volume_cache = 0.0f; - support_volume_per_extruder.clear(); + support_volumes_per_extruder.clear(); + + total_volume_cache = 0.0f; + total_volumes_per_extruder.clear(); } void GCodeProcessor::UsedFilaments::increase_support_caches(double extruded_volume) { support_volume_cache += extruded_volume; role_cache += extruded_volume; + total_volume_cache += extruded_volume; } void GCodeProcessor::UsedFilaments::increase_model_caches(double extruded_volume) @@ -734,12 +738,14 @@ void GCodeProcessor::UsedFilaments::increase_model_caches(double extruded_volume color_change_cache += extruded_volume; model_extrude_cache += extruded_volume; role_cache += extruded_volume; + total_volume_cache += extruded_volume; } void GCodeProcessor::UsedFilaments::increase_wipe_tower_caches(double extruded_volume) { wipe_tower_cache += extruded_volume; role_cache += extruded_volume; + total_volume_cache += extruded_volume; } void GCodeProcessor::UsedFilaments::process_color_change_cache() @@ -750,14 +756,27 @@ void GCodeProcessor::UsedFilaments::process_color_change_cache() } } + +void GCodeProcessor::UsedFilaments::process_total_volume_cache(GCodeProcessor* processor) +{ + size_t active_extruder_id = processor->m_extruder_id; + if (total_volume_cache!= 0.0f) { + if (total_volumes_per_extruder.find(active_extruder_id) != total_volumes_per_extruder.end()) + total_volumes_per_extruder[active_extruder_id] += total_volume_cache; + else + total_volumes_per_extruder[active_extruder_id] = total_volume_cache; + total_volume_cache = 0.0f; + } +} + void GCodeProcessor::UsedFilaments::process_model_cache(GCodeProcessor* processor) { size_t active_extruder_id = processor->m_extruder_id; if (model_extrude_cache != 0.0f) { - if (volumes_per_extruder.find(active_extruder_id) != volumes_per_extruder.end()) - volumes_per_extruder[active_extruder_id] += model_extrude_cache; + if (model_volumes_per_extruder.find(active_extruder_id) != model_volumes_per_extruder.end()) + model_volumes_per_extruder[active_extruder_id] += model_extrude_cache; else - volumes_per_extruder[active_extruder_id] = model_extrude_cache; + model_volumes_per_extruder[active_extruder_id] = model_extrude_cache; model_extrude_cache = 0.0f; } } @@ -766,10 +785,10 @@ void GCodeProcessor::UsedFilaments::process_wipe_tower_cache(GCodeProcessor* pro { size_t active_extruder_id = processor->m_extruder_id; if (wipe_tower_cache != 0.0f) { - if (wipe_tower_volume_per_extruder.find(active_extruder_id) != wipe_tower_volume_per_extruder.end()) - wipe_tower_volume_per_extruder[active_extruder_id] += wipe_tower_cache; + if (wipe_tower_volumes_per_extruder.find(active_extruder_id) != wipe_tower_volumes_per_extruder.end()) + wipe_tower_volumes_per_extruder[active_extruder_id] += wipe_tower_cache; else - wipe_tower_volume_per_extruder[active_extruder_id] = wipe_tower_cache; + wipe_tower_volumes_per_extruder[active_extruder_id] = wipe_tower_cache; wipe_tower_cache = 0.0f; } } @@ -778,10 +797,10 @@ void GCodeProcessor::UsedFilaments::process_support_cache(GCodeProcessor* proces { size_t active_extruder_id = processor->m_extruder_id; if (support_volume_cache != 0.0f){ - if (support_volume_per_extruder.find(active_extruder_id) != support_volume_per_extruder.end()) - support_volume_per_extruder[active_extruder_id] += support_volume_cache; + if (support_volumes_per_extruder.find(active_extruder_id) != support_volumes_per_extruder.end()) + support_volumes_per_extruder[active_extruder_id] += support_volume_cache; else - support_volume_per_extruder[active_extruder_id] = support_volume_cache; + support_volumes_per_extruder[active_extruder_id] = support_volume_cache; support_volume_cache = 0.0f; } } @@ -792,6 +811,11 @@ void GCodeProcessor::UsedFilaments::update_flush_per_filament(size_t extrude_id, flush_per_filament[extrude_id] += flush_volume; else flush_per_filament[extrude_id] = flush_volume; + + if (total_volumes_per_extruder.find(extrude_id) != total_volumes_per_extruder.end()) + total_volumes_per_extruder[extrude_id] += flush_volume; + else + total_volumes_per_extruder[extrude_id] = flush_volume; } void GCodeProcessor::UsedFilaments::process_role_cache(GCodeProcessor* processor) @@ -821,6 +845,7 @@ void GCodeProcessor::UsedFilaments::process_caches(GCodeProcessor* processor) process_role_cache(processor); process_wipe_tower_cache(processor); process_support_cache(processor); + process_total_volume_cache(processor); } #if ENABLE_GCODE_VIEWER_STATISTICS @@ -4363,6 +4388,7 @@ void GCodeProcessor::process_filaments(CustomGCode::Type code) if (code == CustomGCode::ToolChange) { m_used_filaments.process_model_cache(this); m_used_filaments.process_support_cache(this); + m_used_filaments.process_total_volume_cache(this); //BBS: reset remaining filament m_remaining_volume = m_nozzle_volume; } @@ -4394,11 +4420,12 @@ void GCodeProcessor::update_estimated_times_stats() m_result.print_statistics.modes[static_cast(PrintEstimatedStatistics::ETimeMode::Stealth)].reset(); m_result.print_statistics.volumes_per_color_change = m_used_filaments.volumes_per_color_change; - m_result.print_statistics.volumes_per_extruder = m_used_filaments.volumes_per_extruder; - m_result.print_statistics.wipe_tower_volumes_per_extruder = m_used_filaments.wipe_tower_volume_per_extruder; - m_result.print_statistics.support_volumes_per_extruder = m_used_filaments.support_volume_per_extruder; + m_result.print_statistics.model_volumes_per_extruder = m_used_filaments.model_volumes_per_extruder; + m_result.print_statistics.wipe_tower_volumes_per_extruder = m_used_filaments.wipe_tower_volumes_per_extruder; + m_result.print_statistics.support_volumes_per_extruder = m_used_filaments.support_volumes_per_extruder; m_result.print_statistics.flush_per_filament = m_used_filaments.flush_per_filament; m_result.print_statistics.used_filaments_per_role = m_used_filaments.filaments_per_role; + m_result.print_statistics.total_volumes_per_extruder = m_used_filaments.total_volumes_per_extruder; } //BBS: ugly code... @@ -4408,8 +4435,8 @@ void GCodeProcessor::update_slice_warnings() auto get_used_extruders = [this]() { std::vector used_extruders; - used_extruders.reserve(m_used_filaments.volumes_per_extruder.size()); - for (auto item : m_used_filaments.volumes_per_extruder) { + used_extruders.reserve(m_used_filaments.total_volumes_per_extruder.size()); + for (auto item : m_used_filaments.total_volumes_per_extruder) { used_extruders.push_back(item.first); } return used_extruders; diff --git a/src/libslic3r/GCode/GCodeProcessor.hpp b/src/libslic3r/GCode/GCodeProcessor.hpp index a0a6c4c68..f521d5304 100644 --- a/src/libslic3r/GCode/GCodeProcessor.hpp +++ b/src/libslic3r/GCode/GCodeProcessor.hpp @@ -73,9 +73,10 @@ namespace Slic3r { }; std::vector volumes_per_color_change; - std::map volumes_per_extruder; + std::map model_volumes_per_extruder; std::map wipe_tower_volumes_per_extruder; std::map support_volumes_per_extruder; + std::map total_volumes_per_extruder; //BBS: the flush amount of every filament std::map flush_per_filament; std::map> used_filaments_per_role; @@ -92,7 +93,9 @@ namespace Slic3r { volumes_per_color_change.clear(); volumes_per_color_change.shrink_to_fit(); wipe_tower_volumes_per_extruder.clear(); - volumes_per_extruder.clear(); + model_volumes_per_extruder.clear(); + support_volumes_per_extruder.clear(); + total_volumes_per_extruder.clear(); flush_per_filament.clear(); used_filaments_per_role.clear(); total_filamentchanges = 0; @@ -495,17 +498,20 @@ namespace Slic3r { std::vector volumes_per_color_change; double model_extrude_cache; - std::map volumes_per_extruder; + std::map model_volumes_per_extruder; double wipe_tower_cache; - std::mapwipe_tower_volume_per_extruder; + std::mapwipe_tower_volumes_per_extruder; double support_volume_cache; - std::mapsupport_volume_per_extruder; + std::mapsupport_volumes_per_extruder; //BBS: the flush amount of every filament std::map flush_per_filament; + double total_volume_cache; + std::maptotal_volumes_per_extruder; + double role_cache; std::map> filaments_per_role; @@ -519,6 +525,7 @@ namespace Slic3r { void process_model_cache(GCodeProcessor* processor); void process_wipe_tower_cache(GCodeProcessor* processor); void process_support_cache(GCodeProcessor* processor); + void process_total_volume_cache(GCodeProcessor* processor); void update_flush_per_filament(size_t extrude_id, float flush_length); void process_role_cache(GCodeProcessor* processor); diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index 848fed57f..13ce703d4 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -4289,10 +4289,10 @@ void GCodeViewer::render_all_plates_stats(const std::vectorget_extruders(true); for (size_t extruder_id : plate_extruders) { extruder_id -= 1; - if (plate_print_statistics.volumes_per_extruder.find(extruder_id) == plate_print_statistics.volumes_per_extruder.end()) + if (plate_print_statistics.model_volumes_per_extruder.find(extruder_id) == plate_print_statistics.model_volumes_per_extruder.end()) flushed_volume_of_extruders_all_plates[extruder_id] += 0; else { - double model_volume = plate_print_statistics.volumes_per_extruder.at(extruder_id); + double model_volume = plate_print_statistics.model_volumes_per_extruder.at(extruder_id); model_volume_of_extruders_all_plates[extruder_id] += model_volume; } if (plate_print_statistics.flush_per_filament.find(extruder_id) == plate_print_statistics.flush_per_filament.end()) @@ -4850,12 +4850,12 @@ void GCodeViewer::render_legend(float &legend_height, int canvas_width, int canv // used filament statistics for (size_t extruder_id : m_extruder_ids) { - if (m_print_statistics.volumes_per_extruder.find(extruder_id) == m_print_statistics.volumes_per_extruder.end()) { + if (m_print_statistics.model_volumes_per_extruder.find(extruder_id) == m_print_statistics.model_volumes_per_extruder.end()) { model_used_filaments_m.push_back(0.0); model_used_filaments_g.push_back(0.0); } else { - double volume = m_print_statistics.volumes_per_extruder.at(extruder_id); + double volume = m_print_statistics.model_volumes_per_extruder.at(extruder_id); auto [model_used_filament_m, model_used_filament_g] = get_used_filament_from_volume(volume, extruder_id); model_used_filaments_m.push_back(model_used_filament_m); model_used_filaments_g.push_back(model_used_filament_g); @@ -4979,9 +4979,9 @@ void GCodeViewer::render_legend(float &legend_height, int canvas_width, int canv { // calculate used filaments data for (size_t extruder_id : m_extruder_ids) { - if (m_print_statistics.volumes_per_extruder.find(extruder_id) == m_print_statistics.volumes_per_extruder.end()) + if (m_print_statistics.model_volumes_per_extruder.find(extruder_id) == m_print_statistics.model_volumes_per_extruder.end()) continue; - double volume = m_print_statistics.volumes_per_extruder.at(extruder_id); + double volume = m_print_statistics.model_volumes_per_extruder.at(extruder_id); auto [model_used_filament_m, model_used_filament_g] = get_used_filament_from_volume(volume, extruder_id); model_used_filaments_m.push_back(model_used_filament_m); diff --git a/src/slic3r/GUI/PartPlate.cpp b/src/slic3r/GUI/PartPlate.cpp index e4dbba07e..9e9d70ef1 100644 --- a/src/slic3r/GUI/PartPlate.cpp +++ b/src/slic3r/GUI/PartPlate.cpp @@ -1638,14 +1638,7 @@ std::vector PartPlate::get_used_extruders() std::set used_extruders_set; PrintEstimatedStatistics& ps = result->print_statistics; - // model usage - for (const auto&item:ps.volumes_per_extruder) - used_extruders_set.emplace(item.first + 1); - // support usage - for (const auto&item:ps.support_volumes_per_extruder) - used_extruders_set.emplace(item.first + 1); - // wipe tower usage - for (const auto&item:ps.wipe_tower_volumes_per_extruder) + for (const auto& item : ps.total_volumes_per_extruder) used_extruders_set.emplace(item.first + 1); return std::vector(used_extruders_set.begin(), used_extruders_set.end()); diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index dff2ed312..efeb14f7a 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -9763,14 +9763,12 @@ void Plater::load_gcode(const wxString& filename) //BBS: add cost info when drag in gcode auto& ps = current_result->print_statistics; double total_cost = 0.0; - for (auto& volumes_map : { ps.volumes_per_extruder,ps.flush_per_filament ,ps.wipe_tower_volumes_per_extruder }) { - for (auto volume : volumes_map) { - size_t extruder_id = volume.first; - double density = current_result->filament_densities.at(extruder_id); - double cost = current_result->filament_costs.at(extruder_id); - double weight = volume.second * density * 0.001; - total_cost += weight * cost * 0.001; - } + for (auto volume : ps.total_volumes_per_extruder) { + size_t extruder_id = volume.first; + double density = current_result->filament_densities.at(extruder_id); + double cost = current_result->filament_costs.at(extruder_id); + double weight = volume.second * density * 0.001; + total_cost += weight * cost * 0.001; } current_print.print_statistics().total_cost = total_cost;