SPE-1649 - Fixed crash in G-code post processor calculating the preheat / cooldown positions

This commit is contained in:
enricoturri1966 2023-04-06 15:35:14 +02:00
parent 83a2dc3b5a
commit 99f3a3d54f
4 changed files with 23 additions and 2 deletions

View File

@ -836,6 +836,7 @@ void GCode::do_export(Print* print, const char* path, GCodeProcessorResult* resu
path_tmp += ".tmp"; path_tmp += ".tmp";
m_processor.initialize(path_tmp); m_processor.initialize(path_tmp);
m_processor.set_print(print);
GCodeOutputStream file(boost::nowide::fopen(path_tmp.c_str(), "wb"), m_processor); GCodeOutputStream file(boost::nowide::fopen(path_tmp.c_str(), "wb"), m_processor);
if (! file.is_open()) if (! file.is_open())
throw Slic3r::RuntimeError(std::string("G-code export to ") + path + " failed.\nCannot open the file for writing.\n"); throw Slic3r::RuntimeError(std::string("G-code export to ") + path + " failed.\nCannot open the file for writing.\n");

View File

@ -3384,7 +3384,7 @@ void GCodeProcessor::process_T(const std::string_view command)
if (m_extruder_id != id) { if (m_extruder_id != id) {
if (((m_producer == EProducer::PrusaSlicer || m_producer == EProducer::Slic3rPE || m_producer == EProducer::Slic3r) && id >= m_result.extruders_count) || if (((m_producer == EProducer::PrusaSlicer || m_producer == EProducer::Slic3rPE || m_producer == EProducer::Slic3r) && id >= m_result.extruders_count) ||
((m_producer != EProducer::PrusaSlicer && m_producer != EProducer::Slic3rPE && m_producer != EProducer::Slic3r) && id >= m_result.extruder_colors.size())) ((m_producer != EProducer::PrusaSlicer && m_producer != EProducer::Slic3rPE && m_producer != EProducer::Slic3r) && id >= m_result.extruder_colors.size()))
BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid toolchange, maybe from a custom gcode."; BOOST_LOG_TRIVIAL(error) << "GCodeProcessor encountered an invalid toolchange, maybe from a custom gcode (" << command << ").";
else { else {
unsigned char old_extruder_id = m_extruder_id; unsigned char old_extruder_id = m_extruder_id;
process_filaments(CustomGCode::ToolChange); process_filaments(CustomGCode::ToolChange);
@ -3991,6 +3991,18 @@ void GCodeProcessor::post_process()
int tool_number = -1; int tool_number = -1;
ss >> tool_number; ss >> tool_number;
if (tool_number != -1) if (tool_number != -1)
if (tool_number < 0 || (int)m_extruder_temps_config.size() <= tool_number) {
// found an invalid value, clamp it to a valid one
tool_number = std::clamp<int>(0, m_extruder_temps_config.size() - 1, tool_number);
// emit warning
std::string warning = _u8L("GCode Post-Processor encountered an invalid toolchange, maybe from a custom gcode:");
warning += "\n> ";
warning += gcode_line;
warning += _u8L("Generated M104 lines may be incorrect.");
BOOST_LOG_TRIVIAL(error) << warning;
if (m_print != nullptr)
m_print->active_step_add_warning(PrintStateBase::WarningLevel::CRITICAL, warning);
}
export_lines.insert_lines(backtrace, cmd, export_lines.insert_lines(backtrace, cmd,
// line inserter // line inserter
[tool_number, this](unsigned int id, float time, float time_diff) { [tool_number, this](unsigned int id, float time, float time_diff) {

View File

@ -16,6 +16,8 @@
namespace Slic3r { namespace Slic3r {
class Print;
enum class EMoveType : unsigned char enum class EMoveType : unsigned char
{ {
Noop, Noop,
@ -588,6 +590,8 @@ namespace Slic3r {
TimeProcessor m_time_processor; TimeProcessor m_time_processor;
UsedFilaments m_used_filaments; UsedFilaments m_used_filaments;
Print* m_print{ nullptr };
GCodeProcessorResult m_result; GCodeProcessorResult m_result;
static unsigned int s_result_id; static unsigned int s_result_id;
@ -601,6 +605,8 @@ namespace Slic3r {
GCodeProcessor(); GCodeProcessor();
void apply_config(const PrintConfig& config); void apply_config(const PrintConfig& config);
void set_print(Print* print) { m_print = print; }
void enable_stealth_time_estimator(bool enabled); void enable_stealth_time_estimator(bool enabled);
bool is_stealth_time_estimator_enabled() const { bool is_stealth_time_estimator_enabled() const {
return m_time_processor.machines[static_cast<size_t>(PrintEstimatedStatistics::ETimeMode::Stealth)].enabled; return m_time_processor.machines[static_cast<size_t>(PrintEstimatedStatistics::ETimeMode::Stealth)].enabled;

View File

@ -660,6 +660,8 @@ private:
// To allow GCode to set the Print's GCodeExport step status. // To allow GCode to set the Print's GCodeExport step status.
friend class GCode; friend class GCode;
// To allow GCodeProcessor to emit warnings.
friend class GCodeProcessor;
// Allow PrintObject to access m_mutex and m_cancel_callback. // Allow PrintObject to access m_mutex and m_cancel_callback.
friend class PrintObject; friend class PrintObject;
}; };