From b8939ed7df4b4e47d0323d806cbde6a7679ef471 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Wed, 12 Dec 2018 15:09:20 +0100 Subject: [PATCH] Print time in output G-code: fill in the time if the file export dialog is open after the slicing finished. --- src/libslic3r/Print.cpp | 48 +++++++++++++++++++-- src/libslic3r/Print.hpp | 7 +++ src/slic3r/GUI/BackgroundSlicingProcess.cpp | 27 +----------- 3 files changed, 53 insertions(+), 29 deletions(-) diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index 3bbe00805..4d509f112 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -14,6 +14,8 @@ #include "PrintExport.hpp" +#include + //! macro used to mark string used at localization, //! return same string #define L(s) Slic3r::I18N::translate(s) @@ -1864,14 +1866,54 @@ std::string Print::output_filename() const { // Set the placeholders for the data know first after the G-code export is finished. // These values will be just propagated into the output file name. + DynamicConfig config = this->finished() ? this->print_statistics().config() : this->print_statistics().placeholders(); + return this->PrintBase::output_filename(m_config.output_filename_format.value, "gcode", &config); +} + +DynamicConfig PrintStatistics::config() const +{ + DynamicConfig config; + std::string normal_print_time = this->estimated_normal_print_time; + std::string silent_print_time = this->estimated_silent_print_time; + normal_print_time.erase(std::remove_if(normal_print_time.begin(), normal_print_time.end(), std::isspace), normal_print_time.end()); + silent_print_time.erase(std::remove_if(silent_print_time.begin(), silent_print_time.end(), std::isspace), silent_print_time.end()); + config.set_key_value("print_time", new ConfigOptionString(normal_print_time)); + config.set_key_value("normal_print_time", new ConfigOptionString(normal_print_time)); + config.set_key_value("silent_print_time", new ConfigOptionString(silent_print_time)); + config.set_key_value("used_filament", new ConfigOptionFloat (this->total_used_filament)); + config.set_key_value("extruded_volume", new ConfigOptionFloat (this->total_extruded_volume)); + config.set_key_value("total_cost", new ConfigOptionFloat (this->total_cost)); + config.set_key_value("total_weight", new ConfigOptionFloat (this->total_weight)); + config.set_key_value("total_wipe_tower_cost", new ConfigOptionFloat (this->total_wipe_tower_cost)); + config.set_key_value("total_wipe_tower_filament", new ConfigOptionFloat (this->total_wipe_tower_filament)); + return config; +} + +DynamicConfig PrintStatistics::placeholders() +{ DynamicConfig config; for (const std::string &key : { "print_time", "normal_print_time", "silent_print_time", "used_filament", "extruded_volume", "total_cost", "total_weight", "total_wipe_tower_cost", "total_wipe_tower_filament"}) - config.set_key_value(key, new ConfigOptionString(std::string("{") + key + "}")); - return this->PrintBase::output_filename(m_config.output_filename_format.value, "gcode", &config); + config.set_key_value(key, new ConfigOptionString(std::string("{") + key + "}")); + return config; +} + +std::string PrintStatistics::finalize_output_path(const std::string &path_in) const +{ + std::string final_path; + try { + boost::filesystem::path path(path_in); + DynamicConfig cfg = this->config(); + PlaceholderParser pp; + std::string new_stem = pp.process(path.stem().string(), 0, &cfg); + final_path = (path.parent_path() / (new_stem + path.extension().string())).string(); + } catch (const std::exception &ex) { + BOOST_LOG_TRIVIAL(error) << "Failed to apply the print statistics to the export file name: " << ex.what(); + final_path = path_in; + } + return final_path; } } // namespace Slic3r - diff --git a/src/libslic3r/Print.hpp b/src/libslic3r/Print.hpp index 27ee6f3c2..1b79ef295 100644 --- a/src/libslic3r/Print.hpp +++ b/src/libslic3r/Print.hpp @@ -249,6 +249,13 @@ struct PrintStatistics double total_wipe_tower_filament; std::map filament_stats; + // Config with the filled in print statistics. + DynamicConfig config() const; + // Config with the statistics keys populated with placeholder strings. + static DynamicConfig placeholders(); + // Replace the print statistics placeholders in the path. + std::string finalize_output_path(const std::string &path_in) const; + void clear() { estimated_normal_print_time.clear(); estimated_silent_print_time.clear(); diff --git a/src/slic3r/GUI/BackgroundSlicingProcess.cpp b/src/slic3r/GUI/BackgroundSlicingProcess.cpp index b31fa6b5f..b9f146013 100644 --- a/src/slic3r/GUI/BackgroundSlicingProcess.cpp +++ b/src/slic3r/GUI/BackgroundSlicingProcess.cpp @@ -82,32 +82,7 @@ void BackgroundSlicingProcess::process_fff() if (! m_export_path.empty()) { //FIXME localize the messages // Perform the final post-processing of the export path by applying the print statistics over the file name. - std::string export_path; - { - const PrintStatistics &stats = m_fff_print->print_statistics(); - PlaceholderParser pp; - std::string normal_print_time = stats.estimated_normal_print_time; - std::string silent_print_time = stats.estimated_silent_print_time; - normal_print_time.erase(std::remove_if(normal_print_time.begin(), normal_print_time.end(), isspace), normal_print_time.end()); - silent_print_time.erase(std::remove_if(silent_print_time.begin(), silent_print_time.end(), isspace), silent_print_time.end()); - pp.set("print_time", new ConfigOptionString(normal_print_time)); - pp.set("normal_print_time", new ConfigOptionString(normal_print_time)); - pp.set("silent_print_time", new ConfigOptionString(silent_print_time)); - pp.set("used_filament", new ConfigOptionFloat (stats.total_used_filament)); - pp.set("extruded_volume", new ConfigOptionFloat (stats.total_extruded_volume)); - pp.set("total_cost", new ConfigOptionFloat (stats.total_cost)); - pp.set("total_weight", new ConfigOptionFloat (stats.total_weight)); - pp.set("total_wipe_tower_cost", new ConfigOptionFloat (stats.total_wipe_tower_cost)); - pp.set("total_wipe_tower_filament", new ConfigOptionFloat (stats.total_wipe_tower_filament)); - boost::filesystem::path path(m_export_path); - try { - std::string new_stem = pp.process(path.stem().string(), 0); - export_path = (path.parent_path() / (new_stem + path.extension().string())).string(); - } catch (const std::exception &ex) { - BOOST_LOG_TRIVIAL(error) << "Failed to apply the print statistics to the export file name: " << ex.what(); - export_path = m_export_path; - } - } + std::string export_path = m_fff_print->print_statistics().finalize_output_path(m_export_path); if (copy_file(m_temp_output_path, export_path) != 0) throw std::runtime_error("Copying of the temporary G-code to the output G-code failed"); m_print->set_status(95, "Running post-processing scripts");