Merge upstream main (Bambu H2C/A2L multi-nozzle support) into IMEX branch

Conflicts were all co-located additions rather than design collisions:
- GCode.cpp: adopt upstream's toolchange(filament_id, nozzle_id) signature and
  per-variant set_config_index() while keeping the IMEX bare-T<n> suppression;
  rebase the second-layer temperature loop's non-IMEX branch onto upstream's
  get_filament_config_index() resolution.
- Preset.cpp / PresetBundle.cpp / PrintConfig.cpp: keep both sides' option-list
  and enum-map entries.
- GLCanvas3D.cpp: upstream's printable_heights argument plus the IMEX ghost pass.
- PartPlate.cpp: keep <set> (still used).
- test_gcodewriter.cpp / test_3mf.cpp: keep both sides' test cases.
This commit is contained in:
Clifford Garwood committed 2026-07-13 16:22:22 -04:00
commit fdb0e20f36
691 files changed
+84512 -2016

No files matched your search

+95
View File
@@ -2,12 +2,14 @@
#include "libslic3r/libslic3r.h"
#include "libslic3r/GCode/GCodeProcessor.hpp"
#include "libslic3r/MultiNozzleUtils.hpp"
#include "libslic3r/PrintConfig.hpp"
#include "test_utils.hpp"
#include <fstream>
#include <map>
#include <memory>
using namespace Slic3r;
using Catch::Matchers::WithinAbs;
@@ -323,3 +325,96 @@ TEST_CASE("Carried-forward tool-change delay reaches the total without polluting
REQUIRE_THAT(rd.at(role), WithinAbs(zero_time, 1e-2));
}
}
TEST_CASE("Per-slot machine limits follow the active nozzle", "[GCodeTiming][MultiNozzle]")
{
// Single physical extruder carrying two nozzle variants: machine slot 0 (Standard) caps X/Y
// speed at 200 mm/s, slot 1 (High Flow) at 50 mm/s. The estimator must clamp each move by the
// slot of the nozzle the active filament occupies -- resolved from the grouping context handed
// over before the replay plus the occupancy recorder, i.e. the exact in-slicer streaming path.
FullPrintConfig config = make_config(0.0, 0.0, 0.0);
config.extruder_type.values = {static_cast<int>(etDirectDrive)};
config.printer_extruder_id.values = {1, 1};
config.printer_extruder_variant.values = {"Direct Drive Standard", "Direct Drive High Flow"};
// Slot-major layout: [slot0-Normal, slot0-Stealth, slot1-Normal, slot1-Stealth].
config.machine_max_speed_x.values = {200., 200., 50., 50.};
config.machine_max_speed_y.values = {200., 200., 50., 50.};
config.machine_max_speed_z.values = {200., 200., 50., 50.};
config.machine_max_speed_e.values = {200., 200., 50., 50.};
// Keep acceleration and jerk far from limiting so move times are speed-dominated.
for (auto *accel : {&config.machine_max_acceleration_x, &config.machine_max_acceleration_y,
&config.machine_max_acceleration_z, &config.machine_max_acceleration_e})
accel->values = {100000., 100000., 100000., 100000.};
config.machine_max_acceleration_travel.values = {100000., 100000.};
config.machine_max_acceleration_extruding.values = {100000., 100000.};
config.machine_max_jerk_x.values = {10000., 10000.};
config.machine_max_jerk_y.values = {10000., 10000.};
config.machine_max_jerk_z.values = {10000., 10000.};
config.machine_max_jerk_e.values = {10000., 10000.};
// Grouping stub: filament 0 lives on the Standard nozzle (slot 0), filament 1 on the
// High Flow nozzle (slot 1), both mounted on extruder 0.
std::vector<MultiNozzleUtils::NozzleInfo> nozzles;
{
MultiNozzleUtils::NozzleInfo n;
n.diameter = "0.4";
n.volume_type = nvtStandard; n.extruder_id = 0; n.group_id = 0; nozzles.push_back(n);
n.volume_type = nvtHighFlow; n.extruder_id = 0; n.group_id = 1; nozzles.push_back(n);
}
std::vector<int> filament_nozzle_map = {0, 1};
std::vector<unsigned int> used_filaments = {0, 1};
auto group = MultiNozzleUtils::LayeredNozzleGroupResult::create(filament_nozzle_map, nozzles, used_filaments);
REQUIRE(group.has_value());
auto context = std::make_shared<MultiNozzleUtils::LayeredNozzleGroupResult>(*group);
// Two identical 100 mm X travels, one per filament; T..H.. carries the target nozzle id.
// The trailing 1 mm move keeps two blocks queued at finalize, so the measured move's time is
// flushed (a lone final block is never attributed); it adds 1 mm to the second bucket.
const char* gcode =
"M83\n"
"T0 H0\n"
"G1 X100 F30000\n"
"T1 H1\n"
"G1 X0 F30000\n"
"G1 X1 F30000\n";
// Travel time accumulated after each tool-change move (bucket 0 = before any T).
auto travel_times_by_tool = [](const GCodeProcessorResult& r) {
std::vector<double> out(1, 0.0);
for (const auto& mv : r.moves) {
if (mv.type == EMoveType::Tool_change)
out.push_back(0.0);
else if (mv.type == EMoveType::Travel)
out.back() += mv.time[NORMAL];
}
return out;
};
SECTION("the move on the High Flow nozzle is clamped by its own slot") {
GCodeProcessor proc;
proc.initialize_from_context(context);
run_processor(proc, config, gcode);
auto times = travel_times_by_tool(proc.get_result());
REQUIRE(times.size() == 3);
REQUIRE_THAT(times[1], Catch::Matchers::WithinRel(100.0 / 200.0, 0.10));
REQUIRE_THAT(times[2], Catch::Matchers::WithinRel(101.0 / 50.0, 0.10));
}
SECTION("an emitted envelope line reaches every slot") {
const std::string enveloped = std::string("M201 X20000\nM203 X80\n") + gcode;
GCodeProcessor proc;
proc.initialize_from_context(context);
run_processor(proc, config, enveloped.c_str());
auto times = travel_times_by_tool(proc.get_result());
REQUIRE(times.size() == 3);
REQUIRE_THAT(times[1], Catch::Matchers::WithinRel(100.0 / 80.0, 0.10));
REQUIRE_THAT(times[2], Catch::Matchers::WithinRel(101.0 / 80.0, 0.10));
}
SECTION("no grouping context degrades to slot 0") {
GCodeProcessor proc;
run_processor(proc, config, gcode);
auto times = travel_times_by_tool(proc.get_result());
REQUIRE(times.size() == 3);
REQUIRE_THAT(times[1], Catch::Matchers::WithinRel(100.0 / 200.0, 0.10));
REQUIRE_THAT(times[2], Catch::Matchers::WithinRel(101.0 / 200.0, 0.10));
}
}
+279 -3
View File
@@ -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"
@@ -13,6 +17,16 @@
using namespace Slic3r;
using namespace Slic3r::Test;
// Arrange on a finite bed, not an unbounded InfiniteBed: the latter places items
// near INT64_MIN/4 (~2.3e18), which reaches ClipperLib's coordinate limit and throws
// "Coordinate outside allowed range" on Windows/arm64. A 500x500 bed keeps coordinates
// small while still covering large printers.
static void arrange_objects_on_test_bed(Model &model, const DynamicPrintConfig &config)
{
const BoundingBox bed{Point::new_scale(0., 0.), Point::new_scale(500., 500.)};
arrange_objects(model, bed, ArrangeParams{scaled(min_object_distance(config))});
}
SCENARIO("set_speed emits values with fixed-point output.", "[GCodeWriter]") {
GIVEN("GCodeWriter instance") {
@@ -241,8 +255,7 @@ TEST_CASE("Machine envelope emits max limit among used extruders", "[GCodeWriter
obj2->config.set_key_value("extruder", new ConfigOptionInt(2)); // 0-based index 1
Print print;
arrange_objects(model, InfiniteBed{},
ArrangeParams{scaled(min_object_distance(config))});
arrange_objects_on_test_bed(model, config);
for (auto* mo : model.objects) {
mo->ensure_on_bed();
print.auto_assign_extruders(mo);
@@ -354,7 +367,7 @@ TEST_CASE("EXTRUDER_LIMIT per-extruder clamping and max fallback", "[GCodeWriter
obj2->config.set_key_value("extruder", new ConfigOptionInt(2)); // 0-based index 1
Print print;
arrange_objects(model, InfiniteBed{}, ArrangeParams{scaled(min_object_distance(config))});
arrange_objects_on_test_bed(model, config);
for (auto* mo : model.objects) {
mo->ensure_on_bed();
print.auto_assign_extruders(mo);
@@ -405,6 +418,269 @@ TEST_CASE("EXTRUDER_LIMIT per-extruder clamping and max fallback", "[GCodeWriter
}
}
SCENARIO("Extruder reads the injected config column", "[GCodeWriter][H2C]") {
GIVEN("A writer whose per-variant arrays hold three columns for two filaments") {
GCodeWriter writer;
// Column layout after a migrating regroup: filament 0 -> column 0, filament 1 ->
// columns 1 (its first variant) and 2 (its second variant).
writer.config.retraction_length.values = {0.8, 0.5, 1.2};
writer.config.z_hop.values = {0.4, 0.6, 0.9};
writer.config.retraction_speed.values = {30., 40., 50.};
writer.config.filament_flow_ratio.values = {0.98, 1.0, 1.02};
// 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, 1);
Extruder *fil = writer.filament();
REQUIRE(fil != nullptr);
REQUIRE(fil->id() == 1);
const double crossection = 1.75 * 1.75 * 0.25 * PI;
WHEN("no column has been injected") {
THEN("the getters read the filament id's column") {
REQUIRE(fil->config_index() == 1);
REQUIRE_THAT(fil->retraction_length(), Catch::Matchers::WithinAbs(0.5, 1e-9));
REQUIRE_THAT(fil->retract_lift(), Catch::Matchers::WithinAbs(0.6, 1e-9));
REQUIRE(fil->retract_speed() == 40);
REQUIRE_THAT(fil->e_per_mm3(), Catch::Matchers::WithinRel(1.0 / crossection, 1e-9));
}
}
WHEN("the second variant column is injected") {
fil->set_config_index(2);
THEN("the getters follow the column and the flow cache is rescaled") {
REQUIRE(fil->config_index() == 2);
REQUIRE_THAT(fil->retraction_length(), Catch::Matchers::WithinAbs(1.2, 1e-9));
REQUIRE_THAT(fil->retract_lift(), Catch::Matchers::WithinAbs(0.9, 1e-9));
REQUIRE(fil->retract_speed() == 50);
REQUIRE_THAT(fil->e_per_mm3(), Catch::Matchers::WithinRel(1.02 / crossection, 1e-9));
}
THEN("filament-indexed reads keep using the filament id") {
REQUIRE_THAT(fil->filament_diameter(), Catch::Matchers::WithinAbs(1.75, 1e-9));
}
}
WHEN("a negative index is injected") {
fil->set_config_index(2);
fil->set_config_index(-1);
THEN("resolution resets to the filament id") {
REQUIRE(fil->config_index() == 1);
REQUIRE_THAT(fil->retraction_length(), Catch::Matchers::WithinAbs(0.5, 1e-9));
REQUIRE_THAT(fil->e_per_mm3(), Catch::Matchers::WithinRel(1.0 / crossection, 1e-9));
}
}
}
}
// 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_on_test_bed(model, 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_on_test_bed(model, 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);
}
}
}
}
SCENARIO("set_pressure_advance emits nothing for negative PA", "[GCodeWriter][PressureAdvance]") {
GIVEN("A default GCodeWriter") {
GCodeWriter writer;
+22
View File
@@ -418,3 +418,25 @@ TEST_CASE("Sequential printing follows model order", "[Print]")
REQUIRE_THAT(first_object_peak_z, Catch::Matchers::WithinAbs(20.0, 0.3));
}
// A sequential (by-object) print must publish the print-level nozzle group result just
// like a by-layer print, so custom g-code can index the per-nozzle placeholder tables
// (e.g. nozzle_diameter_at_nozzle_id[]) instead of failing on an empty vector.
TEST_CASE("Sequential printing publishes the nozzle group result", "[Print][MultiNozzle]")
{
SECTION("process() publishes the result") {
Print print;
Model model;
place_two_cubes_apart(60.0, { { "print_sequence", "by object" } }, print, model);
print.process();
REQUIRE(print.get_layered_nozzle_group_result() != nullptr);
}
SECTION("start g-code can index the per-nozzle diameter table") {
const std::string gcode = slice_two_cubes_arranged({
{ "print_sequence", "by object" },
{ "machine_start_gcode", "{if nozzle_diameter_at_nozzle_id[0] > 0}; SEQ-ND-OK\n{endif}" },
});
CHECK(gcode.find("; SEQ-ND-OK") != std::string::npos);
}
}