FIX:final step of slicing is to execute post-processing script

JIRA: STUDIO-5828
Change-Id: I8c33e2a66ac5c692244c778586040663b7b54bd7
This commit is contained in:
hu.wang 2024-02-22 15:28:56 +08:00 committed by Lane.Wei
parent e931bd8ab4
commit 6d621816c9
2 changed files with 14 additions and 23 deletions

View File

@ -235,12 +235,13 @@ void BackgroundSlicingProcess::process_fff()
//BBS: add plate index into render params //BBS: add plate index into render params
m_temp_output_path = this->get_current_plate()->get_tmp_gcode_path(); m_temp_output_path = this->get_current_plate()->get_tmp_gcode_path();
m_fff_print->export_gcode(m_temp_output_path, m_gcode_result, [this](const ThumbnailsParams& params) { return this->render_thumbnails(params); }); m_fff_print->export_gcode(m_temp_output_path, m_gcode_result, [this](const ThumbnailsParams& params) { return this->render_thumbnails(params); });
finalize_gcode();
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(": export gcode finished"); BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(": export gcode finished");
} }
if (this->set_step_started(bspsGCodeFinalize)) { if (this->set_step_started(bspsGCodeFinalize)) {
if (! m_export_path.empty()) { if (! m_export_path.empty()) {
wxQueueEvent(GUI::wxGetApp().mainframe->m_plater, new wxCommandEvent(m_event_export_began_id)); wxQueueEvent(GUI::wxGetApp().mainframe->m_plater, new wxCommandEvent(m_event_export_began_id));
finalize_gcode(); export_gcode();
} else if (! m_upload_job.empty()) { } else if (! m_upload_job.empty()) {
wxQueueEvent(GUI::wxGetApp().mainframe->m_plater, new wxCommandEvent(m_event_export_began_id)); wxQueueEvent(GUI::wxGetApp().mainframe->m_plater, new wxCommandEvent(m_event_export_began_id));
prepare_upload(); prepare_upload();
@ -761,44 +762,34 @@ bool BackgroundSlicingProcess::invalidate_all_steps()
return m_step_state.invalidate_all([this](){ this->stop_internal(); }); return m_step_state.invalidate_all([this](){ this->stop_internal(); });
} }
//Call post-processing script for the last step during slicing
void BackgroundSlicingProcess::finalize_gcode()
{
m_print->set_status(95, _utf8(L("Running post-processing scripts")));
run_post_process_scripts(m_temp_output_path, false, "File", m_temp_output_path, m_fff_print->full_print_config());
m_print->set_status(100, _utf8(L("Successfully executed post-processing script")));
}
// G-code is generated in m_temp_output_path. // G-code is generated in m_temp_output_path.
// Optionally run a post-processing script on a copy of m_temp_output_path. // Optionally run a post-processing script on a copy of m_temp_output_path.
// Copy the final G-code to target location (possibly a SD card, if it is a removable media, then verify that the file was written without an error). // Copy the final G-code to target location (possibly a SD card, if it is a removable media, then verify that the file was written without an error).
void BackgroundSlicingProcess::finalize_gcode() void BackgroundSlicingProcess::export_gcode()
{ {
//BBS: don't support running user-defined post-processing scripts
//m_print->set_status(95, _utf8(L("Running post-processing scripts")));
// Perform the final post-processing of the export path by applying the print statistics over the file name. // Perform the final post-processing of the export path by applying the print statistics over the file name.
std::string export_path = m_fff_print->print_statistics().finalize_output_path(m_export_path); std::string export_path = m_fff_print->print_statistics().finalize_output_path(m_export_path);
std::string output_path = m_temp_output_path; std::string output_path = m_temp_output_path;
// Both output_path and export_path ar in-out parameters.
// If post processed, output_path will differ from m_temp_output_path as run_post_process_scripts() will make a copy of the G-code to not
// collide with the G-code viewer memory mapping of the unprocessed G-code. G-code viewer maps unprocessed G-code, because m_gcode_result
// is calculated for the unprocessed G-code and it references lines in the memory mapped G-code file by line numbers.
// export_path may be changed by the post-processing script as well if the post processing script decides so, see GH #6042.
bool post_processed = run_post_process_scripts(output_path, true, "File", export_path, m_fff_print->full_print_config());
auto remove_post_processed_temp_file = [post_processed, &output_path]() {
if (post_processed)
try {
boost::filesystem::remove(output_path);
} catch (const std::exception &ex) {
BOOST_LOG_TRIVIAL(error) << "Failed to remove temp file " << output_path << ": " << ex.what();
}
};
//FIXME localize the messages //FIXME localize the messages
std::string error_message; std::string error_message;
int copy_ret_val = CopyFileResult::SUCCESS; int copy_ret_val = CopyFileResult::SUCCESS;
try try
{ {
copy_ret_val = copy_file(output_path, export_path, error_message, m_export_path_on_removable_media); copy_ret_val = copy_file(output_path, export_path, error_message, m_export_path_on_removable_media);
remove_post_processed_temp_file();
} }
catch (...) catch (...)
{ {
remove_post_processed_temp_file();
throw Slic3r::ExportError(_utf8(L("Unknown error when export G-code."))); throw Slic3r::ExportError(_utf8(L("Unknown error when export G-code.")));
} }
switch (copy_ret_val) { switch (copy_ret_val) {
@ -834,7 +825,6 @@ void BackgroundSlicingProcess::finalize_gcode()
// BBS: to be checked. Whether use export_path or output_path. // BBS: to be checked. Whether use export_path or output_path.
gcode_add_line_number(export_path, m_fff_print->full_print_config()); gcode_add_line_number(export_path, m_fff_print->full_print_config());
m_print->set_status(100, (boost::format(_utf8(L("Succeed to export G-code to %1%"))) % export_path).str());
} }
// A print host upload job has been scheduled, enqueue it to the printhost job queue // A print host upload job has been scheduled, enqueue it to the printhost job queue

View File

@ -287,6 +287,7 @@ private:
// If the background processing stop was requested, throw CanceledException. // If the background processing stop was requested, throw CanceledException.
void throw_if_canceled() const { if (m_print->canceled()) throw CanceledException(); } void throw_if_canceled() const { if (m_print->canceled()) throw CanceledException(); }
void finalize_gcode(); void finalize_gcode();
void export_gcode();
void prepare_upload(); void prepare_upload();
// To be executed at the background thread. // To be executed at the background thread.
ThumbnailsList render_thumbnails(const ThumbnailsParams &params); ThumbnailsList render_thumbnails(const ThumbnailsParams &params);