diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 1f4120a0bb..77a05ed171 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -2504,7 +2504,8 @@ void GCode::do_export(Print* print, const char* path, GCodeProcessorResult* resu { LifecycleEventContext ctx; - ctx.name = std::to_string(print->model().id().id); + ctx.id = std::to_string(print->model().id().id); + ctx.name = print->get_model_name(); ctx.code = LifecycleEvtCode::Ok; ctx.msg = path; ctx.cancellation_check = [print]() { return print->canceled(); }; @@ -2556,7 +2557,8 @@ void GCode::do_export(Print* print, const char* path, GCodeProcessorResult* resu } { LifecycleEventContext ctx; - ctx.name = std::to_string(print->model().id().id); + ctx.id = std::to_string(print->model().id().id); + ctx.name = print->get_model_name(); ctx.code = LifecycleEvtCode::Error; ctx.msg = std::string(path) + "\n" + err_msg; ctx.cancellation_check = [print]() { return print->canceled(); }; @@ -2580,7 +2582,8 @@ void GCode::do_export(Print* print, const char* path, GCodeProcessorResult* resu boost::nowide::remove(path_tmp.c_str()); { LifecycleEventContext ctx; - ctx.name = std::to_string(print->model().id().id); + ctx.id = std::to_string(print->model().id().id); + ctx.name = print->get_model_name(); ctx.code = LifecycleEvtCode::Error; ctx.msg = std::string(path) + "\n" + ex.what(); ctx.cancellation_check = [print]() { return print->canceled(); }; @@ -2693,7 +2696,8 @@ void GCode::do_export(Print* print, const char* path, GCodeProcessorResult* resu if (ret) { { LifecycleEventContext ctx; - ctx.name = std::to_string(print->model().id().id); + ctx.id = std::to_string(print->model().id().id); + ctx.name = print->get_model_name(); ctx.code = LifecycleEvtCode::Error; ctx.msg = std::string(path) + "\nFailed to rename the output G-code file: " + ret.message(); ctx.cancellation_check = [print]() { return print->canceled(); }; @@ -2712,7 +2716,8 @@ void GCode::do_export(Print* print, const char* path, GCodeProcessorResult* resu { LifecycleEventContext ctx; - ctx.name = std::to_string(print->model().id().id); + ctx.id = std::to_string(print->model().id().id); + ctx.name = print->get_model_name(); ctx.code = LifecycleEvtCode::Ok; ctx.msg = path; ctx.cancellation_check = [print]() { return print->canceled(); }; diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index 00398f9ebb..ed3ac6de88 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -2717,7 +2717,8 @@ void Print::process(long long *time_cost_with_cache, bool use_cache) { LifecycleEventContext ctx; - ctx.name = std::to_string(m_model.id().id); + ctx.id = std::to_string(m_model.id().id); + ctx.name = get_model_name(); ctx.code = LifecycleEvtCode::Ok; ctx.cancellation_check = [this]() { return canceled(); }; fire_lifecycle_event(LifecycleEvent::SliceStarted, ctx); @@ -3344,7 +3345,8 @@ void Print::process(long long *time_cost_with_cache, bool use_cache) { LifecycleEventContext ctx; - ctx.name = std::to_string(m_model.id().id); + ctx.id = std::to_string(m_model.id().id); + ctx.name = get_model_name(); ctx.code = LifecycleEvtCode::Ok; ctx.cancellation_check = [this]() { return canceled(); }; fire_lifecycle_event(LifecycleEvent::SliceGeometryFinished, ctx); @@ -4950,7 +4952,8 @@ void Print::export_gcode_from_previous_file(const std::string& file, GCodeProces { { LifecycleEventContext ctx; - ctx.name = std::to_string(m_model.id().id); + ctx.id = std::to_string(m_model.id().id); + ctx.name = get_model_name(); ctx.code = LifecycleEvtCode::Ok; ctx.msg = file; ctx.cancellation_check = [this]() { return canceled(); }; @@ -4980,7 +4983,8 @@ void Print::export_gcode_from_previous_file(const std::string& file, GCodeProces BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << boost::format(": found errors when process gcode file %1%") %file.c_str(); { LifecycleEventContext ctx; - ctx.name = std::to_string(m_model.id().id); + ctx.id = std::to_string(m_model.id().id); + ctx.name = get_model_name(); ctx.code = LifecycleEvtCode::Error; ctx.msg = file + "\n" + ex.what(); ctx.cancellation_check = [this]() { return canceled(); }; @@ -4994,7 +4998,8 @@ void Print::export_gcode_from_previous_file(const std::string& file, GCodeProces { LifecycleEventContext ctx; - ctx.name = std::to_string(m_model.id().id); + ctx.id = std::to_string(m_model.id().id); + ctx.name = get_model_name(); ctx.code = LifecycleEvtCode::Ok; ctx.msg = file; ctx.cancellation_check = [this]() { return canceled(); }; diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index bdb8340edc..951739e213 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -12800,8 +12800,6 @@ void Plater::priv::on_process_completed(SlicingProcessCompletedEvent &evt) notification_manager->set_slicing_progress_export_possible(); // Reset the "export G-code path" name, so that the automatic background processing will be enabled again. - const std::string lifecycle_job_name = this->background_process.fff_print() ? - this->background_process.fff_print()->output_filename() : std::string(); this->background_process.reset_export(); // This bool stops showing export finished notification even when process_completed_with_error is false bool has_error = false; @@ -12848,8 +12846,18 @@ void Plater::priv::on_process_completed(SlicingProcessCompletedEvent &evt) { Slic3r::LifecycleEventContext ctx; - ctx.name = lifecycle_job_name; - ctx.code = evt.cancelled() ? Slic3r::LifecycleEvtCode::Warn : (has_error ? Slic3r::LifecycleEvtCode::Error : Slic3r::LifecycleEvtCode::Ok); + if (const PrintBase* print = this->background_process.current_print()) { + const Model& model = print->model(); + ctx.id = std::to_string(model.id().id); + if (model.model_info) + ctx.name = model.model_info->model_name; + } else { + // Realistically Printbase* print will never be null because select_technology already asserts an active print + // and the worker thread asserts it before processing. + BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << ": slicing completed without an active print; lifecycle event has no model ID"; + } + ctx.code = evt.cancelled() ? Slic3r::LifecycleEvtCode::Warn : + (has_error ? Slic3r::LifecycleEvtCode::Error : Slic3r::LifecycleEvtCode::Ok); ctx.msg = evt.cancelled() ? "cancelled" : (has_error ? lifecycle_error_msg : std::string()); Slic3r::fire_lifecycle_event(Slic3r::LifecycleEvent::SlicingJobComplete, ctx); } diff --git a/tests/fff_print/test_print.cpp b/tests/fff_print/test_print.cpp index 383a7165d5..32cd608b43 100644 --- a/tests/fff_print/test_print.cpp +++ b/tests/fff_print/test_print.cpp @@ -15,6 +15,9 @@ #include "libslic3r/Layer.hpp" #include "libslic3r/Model.hpp" #include "libslic3r/GCodeReader.hpp" +#include "libslic3r/GCode/GCodeProcessor.hpp" +#include "libslic3r/Exception.hpp" +#include "libslic3r/LifecycleEvents.hpp" #include "test_helpers.hpp" #include "test_utils.hpp" @@ -22,7 +25,10 @@ #include #include #include +#include #include +#include +#include using namespace Slic3r; using namespace Slic3r::Test; @@ -224,8 +230,88 @@ std::string resolved_output_name(Model& model, const std::string& format, const return print.output_filename(filename_base); } +struct ScopedLifecycleHook +{ + explicit ScopedLifecycleHook(LifecycleHookFn hook) { set_lifecycle_hook_fn(std::move(hook)); } + ~ScopedLifecycleHook() { set_lifecycle_hook_fn(nullptr); } +}; + } // namespace +TEST_CASE("Slicing lifecycle events identify the model", "[Print][LifecycleEvents]") +{ + struct ObservedEvent { + LifecycleEvent event; + std::string id; + std::string name; + }; + std::vector events; + ScopedLifecycleHook hook([&](LifecycleEvent event, const LifecycleEventContext& ctx) { + events.push_back({ event, ctx.id, ctx.name }); + }); + + Print print; + Model model; + ModelInfo info; + info.model_name = "Lifecycle test model"; + model.model_info = std::make_shared(std::move(info)); + init_print({cube(20)}, print, model); + + print.process(); + ScopedTemporaryFile temp(".gcode"); + print.export_gcode(temp.string(), nullptr, nullptr); + GCodeProcessorResult result; + print.export_gcode_from_previous_file(temp.string(), &result); + + const std::string expected_id = std::to_string(print.model().id().id); + const std::vector expected_events = { + LifecycleEvent::SliceStarted, + LifecycleEvent::SliceGeometryFinished, + LifecycleEvent::GCodeExportStarted, + LifecycleEvent::GCodeExportFinished, + LifecycleEvent::GCodeExportStarted, + LifecycleEvent::GCodeExportFinished, + }; + REQUIRE(events.size() == expected_events.size()); + for (size_t i = 0; i < expected_events.size(); ++i) { + CHECK(events[i].event == expected_events[i]); + CHECK(events[i].id == expected_id); + CHECK(events[i].name == "Lifecycle test model"); + } +} + +TEST_CASE("Slicing lifecycle event name is empty without model metadata", "[Print][LifecycleEvents]") +{ + std::string event_id; + std::string event_name = "unset"; + ScopedLifecycleHook hook([&](LifecycleEvent event, const LifecycleEventContext& ctx) { + if (event == LifecycleEvent::SliceStarted) { + event_id = ctx.id; + event_name = ctx.name; + } + }); + + Print print; + Model model; + init_print({cube(20)}, print, model); + print.process(); + + CHECK(event_id == std::to_string(print.model().id().id)); + CHECK(event_name.empty()); +} + +TEST_CASE("Output filenames with numeric statistics fail before slicing finishes", "[Print][Regression]") +{ + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.set_key_value("filename_format", new ConfigOptionString("{int(total_weight*10) / 10.0}")); + + Print print; + Model model; + init_print({cube(20)}, print, model, config); + + CHECK_THROWS_AS(print.output_filename(), PlaceholderParserError); +} + TEST_CASE("Print: {first_object_name} names the first printable object on the plate", "[Print]") { Model model;