mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-07-15 23:12:08 +00:00
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:
@@ -1,6 +1,10 @@
|
||||
#include <catch2/catch_all.hpp>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "libslic3r/GCodeWriter.hpp"
|
||||
#include "libslic3r/GCode.hpp"
|
||||
@@ -417,7 +421,7 @@ SCENARIO("Extruder reads the injected config column", "[GCodeWriter][H2C]") {
|
||||
// Filament-indexed arrays keep one entry per filament.
|
||||
writer.config.filament_diameter.values = {1.75, 1.75};
|
||||
writer.set_extruders({0, 1});
|
||||
writer.toolchange(1);
|
||||
writer.toolchange(1, 1);
|
||||
Extruder *fil = writer.filament();
|
||||
REQUIRE(fil != nullptr);
|
||||
REQUIRE(fil->id() == 1);
|
||||
@@ -456,3 +460,214 @@ SCENARIO("Extruder reads the injected config column", "[GCodeWriter][H2C]") {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Numeric argument of every line starting with `prefix`, in file order.
|
||||
static std::vector<int> collect_line_args(const std::string &gcode, const std::string &prefix)
|
||||
{
|
||||
std::vector<int> values;
|
||||
std::istringstream stream(gcode);
|
||||
std::string line;
|
||||
while (std::getline(stream, line))
|
||||
if (line.compare(0, prefix.size(), prefix) == 0)
|
||||
values.push_back(std::atoi(line.c_str() + int(prefix.size())));
|
||||
return values;
|
||||
}
|
||||
|
||||
static int count_lines_with_prefix(const std::string &gcode, const std::string &prefix)
|
||||
{
|
||||
return (int) collect_line_args(gcode, prefix).size();
|
||||
}
|
||||
|
||||
// A toolchange ordinal sequence is healthy when it advances by exactly one per
|
||||
// change block; a change-less prime-tower visit must not consume an ordinal.
|
||||
static bool ordinals_consecutive(const std::vector<int> &values)
|
||||
{
|
||||
for (size_t i = 1; i < values.size(); ++i)
|
||||
if (values[i] != values[i - 1] + 1)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
SCENARIO("Toolchange emission and prefix per printer kind", "[GCodeWriter][H2C]") {
|
||||
GIVEN("A dual-extruder writer with two filaments") {
|
||||
GCodeWriter writer;
|
||||
writer.config.filament_diameter.values = {1.75, 1.75};
|
||||
writer.set_extruders({0, 1});
|
||||
|
||||
WHEN("the printer is a BBL machine") {
|
||||
writer.set_is_bbl_machine(true);
|
||||
THEN("the toolchange prefix is the plain T command") {
|
||||
REQUIRE_THAT(writer.toolchange_prefix(), Catch::Matchers::Equals("T"));
|
||||
}
|
||||
THEN("toolchange emits a single M1020 with the nozzle id") {
|
||||
const std::string gcode = writer.toolchange(1, 0);
|
||||
REQUIRE_THAT(gcode, Catch::Matchers::ContainsSubstring("M1020 S1 H0"));
|
||||
REQUIRE_THAT(gcode, !Catch::Matchers::StartsWith("T1"));
|
||||
}
|
||||
THEN("the other filament and nozzle emit their own ids") {
|
||||
REQUIRE_THAT(writer.toolchange(0, 1), Catch::Matchers::ContainsSubstring("M1020 S0 H1"));
|
||||
}
|
||||
THEN("an unresolved nozzle keeps the literal -1 convention") {
|
||||
REQUIRE_THAT(writer.toolchange(1, -1), Catch::Matchers::ContainsSubstring("M1020 S1 H-1"));
|
||||
}
|
||||
}
|
||||
WHEN("the printer is a BBL machine with manual filament change") {
|
||||
writer.set_is_bbl_machine(true);
|
||||
writer.config.manual_filament_change.value = true;
|
||||
THEN("the manual tag wins over the M1020 form") {
|
||||
REQUIRE_THAT(writer.toolchange_prefix(), Catch::Matchers::StartsWith(";"));
|
||||
const std::string gcode = writer.toolchange(1, 0);
|
||||
REQUIRE_THAT(gcode, Catch::Matchers::ContainsSubstring(writer.toolchange_prefix() + "1"));
|
||||
REQUIRE_THAT(gcode, !Catch::Matchers::ContainsSubstring("M1020"));
|
||||
}
|
||||
}
|
||||
WHEN("the printer is not a BBL machine") {
|
||||
THEN("toolchange keeps the plain T command") {
|
||||
REQUIRE_THAT(writer.toolchange_prefix(), Catch::Matchers::Equals("T"));
|
||||
const std::string gcode = writer.toolchange(1, 0);
|
||||
REQUIRE_THAT(gcode, Catch::Matchers::StartsWith("T1"));
|
||||
REQUIRE_THAT(gcode, !Catch::Matchers::ContainsSubstring("M1020"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Shared dual-extruder printer config for the toolchange-count scenarios below.
|
||||
static DynamicPrintConfig dual_extruder_toolchange_config()
|
||||
{
|
||||
DynamicPrintConfig config = DynamicPrintConfig::full_print_config();
|
||||
config.set_key_value("gcode_flavor", new ConfigOptionEnum<GCodeFlavor>(gcfMarlinFirmware));
|
||||
config.set_key_value("emit_machine_limits_to_gcode", new ConfigOptionBool(false));
|
||||
config.set_key_value("machine_start_gcode", new ConfigOptionString(""));
|
||||
config.set_key_value("layer_height", new ConfigOptionFloat(0.2));
|
||||
config.set_key_value("initial_layer_print_height", new ConfigOptionFloat(0.2));
|
||||
config.set_key_value("initial_layer_line_width", new ConfigOptionFloatOrPercent(0, false));
|
||||
config.set_key_value("z_hop", new ConfigOptionFloats({0., 0.}));
|
||||
// The change block carries both a real toolchange command and the ordinal
|
||||
// placeholder the stock profiles feed to the firmware.
|
||||
config.set_key_value("change_filament_gcode",
|
||||
new ConfigOptionString("T[next_filament_id]\nM620 O{toolchange_count + 1}\n"));
|
||||
|
||||
// 2 extruders, one filament each (manual map so nothing regroups them).
|
||||
config.set_key_value("nozzle_diameter", new ConfigOptionFloats({0.4, 0.4}));
|
||||
config.set_key_value("printer_extruder_id", new ConfigOptionInts({1, 2}));
|
||||
config.set_key_value("printer_extruder_variant", new ConfigOptionStrings({"Direct Drive Standard", "Direct Drive Standard"}));
|
||||
config.set_key_value("filament_diameter", new ConfigOptionFloats({1.75, 1.75}));
|
||||
config.set_key_value("filament_colour", new ConfigOptionStrings({"#FF0000", "#00FF00"}));
|
||||
config.set_key_value("default_filament_colour", new ConfigOptionStrings({"#FF0000", "#00FF00"}));
|
||||
config.set_key_value("filament_type", new ConfigOptionStrings({"PLA", "PLA"}));
|
||||
config.option<ConfigOptionEnum<FilamentMapMode>>("filament_map_mode", true)->value = fmmManual;
|
||||
config.set_key_value("filament_map", new ConfigOptionInts({1, 2}));
|
||||
config.set_key_value("nozzle_temperature", new ConfigOptionInts({210, 210}));
|
||||
config.set_key_value("nozzle_temperature_range_low", new ConfigOptionInts({190, 190}));
|
||||
config.set_key_value("nozzle_temperature_range_high", new ConfigOptionInts({240, 240}));
|
||||
config.set_key_value("flush_multiplier", new ConfigOptionFloats({1}));
|
||||
config.set_key_value("flush_volumes_matrix", new ConfigOptionFloats({0, 140, 140, 0}));
|
||||
return config;
|
||||
}
|
||||
|
||||
SCENARIO("Change blocks carry consecutive toolchange ordinals without a duplicate command", "[GCodeWriter][H2C]") {
|
||||
GIVEN("Two sequentially printed objects on different extruders of a BBL machine") {
|
||||
DynamicPrintConfig config = dual_extruder_toolchange_config();
|
||||
config.set_key_value("print_sequence", new ConfigOptionEnum<PrintSequence>(PrintSequence::ByObject));
|
||||
|
||||
Model model;
|
||||
auto *obj1 = model.add_object();
|
||||
obj1->add_volume(cube(20));
|
||||
obj1->add_instance();
|
||||
auto *obj2 = model.add_object();
|
||||
obj2->add_volume(cube(20));
|
||||
obj2->add_instance();
|
||||
obj2->config.set_key_value("extruder", new ConfigOptionInt(2));
|
||||
|
||||
auto slice_to_gcode = [&]() {
|
||||
Print print;
|
||||
print.is_BBL_printer() = true;
|
||||
arrange_objects(model, InfiniteBed{}, ArrangeParams{scaled(min_object_distance(config))});
|
||||
for (auto *mo : model.objects) {
|
||||
mo->ensure_on_bed();
|
||||
print.auto_assign_extruders(mo);
|
||||
}
|
||||
print.apply(model, config);
|
||||
print.validate();
|
||||
print.set_status_silent();
|
||||
print.process();
|
||||
return Slic3r::Test::gcode(print);
|
||||
};
|
||||
|
||||
WHEN("the change block already changes the tool") {
|
||||
const std::string gcode = slice_to_gcode();
|
||||
const std::vector<int> ordinals = collect_line_args(gcode, "M620 O");
|
||||
THEN("each change block advances the ordinal by exactly one, without inflation") {
|
||||
REQUIRE(!ordinals.empty());
|
||||
REQUIRE(ordinals_consecutive(ordinals));
|
||||
REQUIRE(ordinals.front() <= 3);
|
||||
}
|
||||
THEN("the writer's own command is suppressed as a duplicate") {
|
||||
REQUIRE(count_lines_with_prefix(gcode, "M1020") == 0);
|
||||
REQUIRE(count_lines_with_prefix(gcode, "T1") >= 1);
|
||||
}
|
||||
}
|
||||
WHEN("the change block does not change the tool itself") {
|
||||
config.set_key_value("change_filament_gcode",
|
||||
new ConfigOptionString("M620 O{toolchange_count + 1}\n"));
|
||||
const std::string gcode = slice_to_gcode();
|
||||
const std::vector<int> ordinals = collect_line_args(gcode, "M620 O");
|
||||
THEN("the writer's toolchange survives and carries a nozzle id") {
|
||||
REQUIRE(count_lines_with_prefix(gcode, "M1020 S1 H") >= 1);
|
||||
}
|
||||
THEN("the ordinal sequence stays consecutive") {
|
||||
REQUIRE(!ordinals.empty());
|
||||
REQUIRE(ordinals_consecutive(ordinals));
|
||||
REQUIRE(ordinals.front() <= 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SCENARIO("Prime-tower visits without a filament change do not advance the toolchange ordinal", "[GCodeWriter][H2C]") {
|
||||
GIVEN("A print whose only filament change happens far above the bed") {
|
||||
DynamicPrintConfig config = dual_extruder_toolchange_config();
|
||||
config.set_key_value("enable_prime_tower", new ConfigOptionBool(true));
|
||||
|
||||
// Filament 2 is used only above z=6, so every tower layer below it is a
|
||||
// change-less visit — the exact geometry that used to inflate the ordinal.
|
||||
Model model;
|
||||
auto *obj = model.add_object();
|
||||
obj->add_volume(cube(10));
|
||||
obj->add_instance();
|
||||
DynamicPrintConfig range_config;
|
||||
range_config.set_key_value("extruder", new ConfigOptionInt(2));
|
||||
// Every layer range must carry a layer_height (see layer_height_profile_from_ranges).
|
||||
range_config.set_key_value("layer_height", new ConfigOptionFloat(0.2));
|
||||
obj->layer_config_ranges[{6.0, 10.0}].assign_config(std::move(range_config));
|
||||
|
||||
Print print;
|
||||
print.is_BBL_printer() = true;
|
||||
arrange_objects(model, InfiniteBed{}, ArrangeParams{scaled(min_object_distance(config))});
|
||||
for (auto *mo : model.objects) {
|
||||
mo->ensure_on_bed();
|
||||
print.auto_assign_extruders(mo);
|
||||
}
|
||||
print.apply(model, config);
|
||||
print.validate();
|
||||
print.set_status_silent();
|
||||
print.process();
|
||||
const std::string gcode = Slic3r::Test::gcode(print);
|
||||
|
||||
WHEN("the print is exported") {
|
||||
const std::vector<int> ordinals = collect_line_args(gcode, "M620 O");
|
||||
THEN("the prime-tower toolchange path was exercised") {
|
||||
REQUIRE_THAT(gcode, Catch::Matchers::ContainsSubstring("CP TOOLCHANGE START"));
|
||||
}
|
||||
THEN("dozens of change-less tower layers consume no ordinal") {
|
||||
REQUIRE(!ordinals.empty());
|
||||
REQUIRE(ordinals_consecutive(ordinals));
|
||||
REQUIRE(ordinals.front() <= 3);
|
||||
}
|
||||
THEN("no duplicate toolchange command follows the change block") {
|
||||
REQUIRE(count_lines_with_prefix(gcode, "M1020") == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user