fix(engine): real-change-only toolchange ordinals and a single M1020 toolchange command

A dual-nozzle H2C print with support filament hangs at its first nozzle
switch. The emitted file shows the change-filament block's M620 O ordinal
jumping from O1 straight to O230, plus a duplicate "M1020 S<n>" toolchange
command right after every change block. Two causes, fixed together because
they interlock (the ordinal check keys off the same toolchange detection
that suppresses the duplicate):

- append_tcr incremented m_toolchange_count once per prime-tower visit
  (roughly once per layer), while the change-filament template only emits
  its M620 O{toolchange_count + 1} line on real filament changes. With 229
  change-less sparse tower layers below the first support layer, the first
  real change reported ordinal 230. The counter now advances only when the
  expanded change block really contains a toolchange command, and the
  placeholder exposes the upcoming change's ordinal (count + 1). The
  set_extruder path already counted per real change and is unchanged.

- toolchange_prefix() returned "M1020 S" for BBL printers, so the
  custom_gcode_changes_tool() dedup could never match the stock profiles'
  line-leading "T[next_filament_id] ..." commands and the writer's own
  toolchange was appended after every change block on dual-extruder
  machines. The prefix is now the plain "T" (the manual-filament-change tag
  branch stays first), and the M1020 form moved into GCodeWriter::toolchange()
  as an explicit branch that also carries the nozzle:
  "M1020 S<filament> H<nozzle>". The nozzle parameter is signed on purpose:
  the null-safe nozzle lookup legitimately yields -1, matching the stock
  templates' own H-1 convention.

The prefix change also lets the CoolingBuffer recognize the change blocks'
T commands as tool boundaries on BBL printers (its per-filament attribution
previously keyed off the duplicate M1020, or nothing at all on
single-extruder models); its existing out-of-range guard ignores
T1000-class machine commands.

Verification: full suites green (libslic3r 48998 assertions / 169 cases;
fff_print 692 / 65 including three new scenarios - writer emission per
printer kind, dedup + ordinal progression on sequential prints, and a
prime-tower regression scenario verified to fail against the old per-visit
counting). Byte gate: 18 of 20 fixtures bit-identical; the sequential repro
differs by exactly its 3 removed duplicate M1020 lines, deterministic
across two runs. Reslicing the field project that exposed the hang yields
M620 O1 followed by a gapless O2..O59 and zero duplicate M1020 lines.

Co-authored-by: songwei.li <songwei.li@bambulab.com>
This commit is contained in:
SoftFever
2026-07-12 21:04:19 +08:00
parent 3ebd68d959
commit e98750e713
4 changed files with 246 additions and 27 deletions

View File

@@ -927,9 +927,6 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
}
}
//BBS: increase toolchange count
gcodegen.m_toolchange_count++;
std::string toolchange_gcode_str;
ZHopType z_hope_type = ZHopType(gcodegen.config().z_hop_types.get_at(gcodegen.get_filament_config_index((int)gcodegen.writer().filament()->id())));
@@ -1058,7 +1055,7 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
config.set_key_value("max_layer_z", new ConfigOptionFloat(gcodegen.m_max_layer_z));
config.set_key_value("relative_e_axis", new ConfigOptionBool(full_config.use_relative_e_distances));
config.set_key_value("toolchange_count", new ConfigOptionInt((int) gcodegen.m_toolchange_count));
config.set_key_value("toolchange_count", new ConfigOptionInt((int) gcodegen.m_toolchange_count + 1));
// BBS: fan speed is useless placeholer now, but we don't remove it to avoid
// slicing error in old change_filament_gcode in old 3MF
config.set_key_value("fan_speed", new ConfigOptionInt((int) 0));
@@ -1180,7 +1177,10 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
std::string toolchange_command;
if (tcr.priming || (new_filament_id >= 0 && gcodegen.writer().need_toolchange(new_filament_id)))
toolchange_command = gcodegen.writer().toolchange(new_filament_id);
// Orca: null-safe, layer-aware nozzle lookup — group_result may be null on
// non-multi-nozzle paths (the helper falls back to the extruder id).
toolchange_command = gcodegen.writer().toolchange(new_filament_id,
nozzle_id_for_gcode_placeholder(group_result, new_filament_id, new_extruder_id, m_layer_idx));
if (!custom_gcode_changes_tool(toolchange_gcode_str, gcodegen.writer().toolchange_prefix(), new_filament_id))
toolchange_gcode_str += toolchange_command;
else {
@@ -1302,6 +1302,10 @@ static std::vector<Vec2d> get_path_of_change_filament(const Print& print)
std::string tcr_gcode, tcr_escaped_gcode = gcodegen.placeholder_parser_process("tcr_rotated_gcode", tcr_rotated_gcode, new_filament_id, &config);
unescape_string_cstyle(tcr_escaped_gcode, tcr_gcode);
gcode += tcr_gcode;
// Count the toolchange only when the emitted block really changed the tool —
// tower visits without a filament change must not advance the ordinal.
if (custom_gcode_changes_tool(tcr_gcode, gcodegen.writer().toolchange_prefix(), new_filament_id))
gcodegen.m_toolchange_count++;
check_add_eol(toolchange_gcode_str);
// SoftFever: set new PA for new filament
@@ -8648,7 +8652,7 @@ std::string GCode::set_extruder(unsigned int new_filament_id, double print_z, bo
m_pa_processor->resetPreviousPA(m_config.pressure_advance.get_at(new_filament_id));
}
gcode += m_writer.toolchange(new_filament_id);
gcode += m_writer.toolchange(new_filament_id, new_extruder_id);
if (Extruder *fil = m_writer.filament())
fil->set_config_index((int)get_filament_config_index((int)fil->id()));
return gcode;
@@ -8933,7 +8937,7 @@ std::string GCode::set_extruder(unsigned int new_filament_id, double print_z, bo
//BBS: don't add T[next extruder] if there is no T cmd on filament change
//We inform the writer about what is happening, but we may not use the resulting gcode.
std::string toolchange_command = m_writer.toolchange(new_filament_id);
std::string toolchange_command = m_writer.toolchange(new_filament_id, next_nozzle_id);
if (Extruder *fil = m_writer.filament())
fil->set_config_index((int)get_filament_config_index((int)fil->id()));
if (!custom_gcode_changes_tool(toolchange_gcode_parsed, m_writer.toolchange_prefix(), new_filament_id))

View File

@@ -594,23 +594,15 @@ std::string GCodeWriter::update_progress(unsigned int num, unsigned int tot, boo
std::string GCodeWriter::toolchange_prefix() const
{
std::string gcode = "T";
// Orca: the manual-filament-change tag must stay ahead of the flavor selection so
// MMU manual-change handling keeps working.
if (config.manual_filament_change)
gcode = ";" + GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Manual_Tool_Change) + "T";
else {
if (m_is_bbl_printers)
gcode = "M1020 S";
else {
if (FLAVOR_IS(gcfMakerWare))
gcode = "M135 T";
else if (FLAVOR_IS(gcfSailfish))
gcode = "M108 T";
}
}
return gcode;
return ";" + GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Manual_Tool_Change) + "T";
return FLAVOR_IS(gcfMakerWare) ? "M135 T" :
FLAVOR_IS(gcfSailfish) ? "M108 T" : "T";
}
std::string GCodeWriter::toolchange(unsigned int filament_id)
std::string GCodeWriter::toolchange(unsigned int filament_id, int nozzle_id)
{
// set the new extruder
auto filament_extruder_iter = Slic3r::lower_bound_by_predicate(m_filament_extruders.begin(), m_filament_extruders.end(), [filament_id](const Extruder &e) { return e.id() < filament_id; });
@@ -621,9 +613,16 @@ std::string GCodeWriter::toolchange(unsigned int filament_id)
// return the toolchange command
// if we are running a single-extruder setup, just set the extruder and return nothing
std::ostringstream gcode;
// Orca: also emit for non-BBL single-extruder multi-filament setups (MMU-style).
if (this->multiple_extruders || (this->config.filament_diameter.values.size() > 1 && !is_bbl_printers())) {
// Orca: call toolchange_prefix() to get the correct command prefix based on the configuration and flavor.
gcode << this->toolchange_prefix() << filament_id;
// Orca: manual filament change keeps its tag line even on BBL machines, so the
// M1020 form must not shadow it. nozzle_id is signed: the null-safe nozzle
// lookup legitimately yields -1 ("no specific nozzle"), matching the literal
// H-1 the stock change templates emit; an unsigned would wrap.
if (m_is_bbl_printers && !config.manual_filament_change)
gcode << "M1020 S" << filament_id << " H" << nozzle_id;
else
gcode << this->toolchange_prefix() << filament_id;
if (GCodeWriter::full_gcode_comment)
gcode << " ; change extruder";
gcode << "\n";
@@ -1271,8 +1270,9 @@ std::string GCodeWriter::set_extruder(unsigned int filament_id)
auto filament_ext_it = Slic3r::lower_bound_by_predicate(m_filament_extruders.begin(), m_filament_extruders.end(), [filament_id](const Extruder &e) { return e.id() < filament_id; });
unsigned int extruder_id = filament_ext_it->extruder_id();
assert(filament_ext_it != m_filament_extruders.end() && filament_ext_it->id() == filament_id);
//TODO: optmize here, pass extruder_id to toolchange
return this->need_toolchange(filament_id) ? this->toolchange(filament_id) : "";
// Orca: writer-only context (calibration paths) has no nozzle grouping; the
// filament's own extruder id is the correct degenerate nozzle value.
return this->need_toolchange(filament_id) ? this->toolchange(filament_id, (int) extruder_id) : "";
}
void GCodeWriter::init_extruder(unsigned int filament_id)

View File

@@ -72,7 +72,7 @@ public:
// Prefix of the toolchange G-code line, to be used by the CoolingBuffer to separate sections of the G-code
// printed with the same extruder.
std::string toolchange_prefix() const;
std::string toolchange(unsigned int filament_id);
std::string toolchange(unsigned int filament_id, int nozzle_id);
std::string set_speed(double F, const std::string &comment = std::string(), const std::string &cooling_marker = std::string());
// SoftFever NOTE: the returned speed is mm/minute
double get_current_speed() const { return m_current_speed;}