test+i18n: multi-nozzle filament-group goldens and ported strings

Filament-group golden harness (config_a subset) and .3mf multi-nozzle round-trip tests, plus i18n msgids for the ported H2C/A2L strings.
This commit is contained in:
SoftFever
2026-07-09 00:06:27 +08:00
parent 28b7127150
commit 9810397546
53 changed files with 5347 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ add_executable(${_TEST_NAME}_tests
test_clipper_offset.cpp
test_clipper_utils.cpp
test_config.cpp
test_toolordering_nozzle_group.cpp
test_preset_bundle_loading.cpp
test_preset_setting_id.cpp
test_elephant_foot_compensation.cpp

View File

@@ -1,7 +1,13 @@
#include "libslic3r/Model.hpp"
#include "libslic3r/Format/3mf.hpp"
#include "libslic3r/Format/bbs_3mf.hpp"
#include "libslic3r/Format/STL.hpp"
#include "libslic3r/PrintConfig.hpp"
#include "libslic3r/Semver.hpp"
#include "libslic3r/Preset.hpp"
#include "libslic3r/MultiNozzleUtils.hpp"
#include "libslic3r/ProjectTask.hpp"
#include <boost/filesystem/operations.hpp>
@@ -133,6 +139,294 @@ SCENARIO("Export+Import geometry to/from 3mf file cycle", "[3mf]") {
}
}
// .3mf multi-nozzle round-trip.
// Locks the load/save handling for the H2C multi-nozzle plate metadata:
// * filament_volume_maps -> plate config "filament_volume_map" (with the >1 -> 0 clamp)
// * nozzle_volume_type -> PlateData::nozzle_volume_types (previously write-only)
// and pins the deliberately-lossy keys (enable_filament_dynamic_map) so a future change has to
// consciously unpin them. Uses a store_bbs_3mf -> load_bbs_3mf cycle (no external fixture needed).
SCENARIO("H2C multi-nozzle .3mf round-trip", "[3mf][MultiNozzle]") {
GIVEN("a plate carrying multi-nozzle filament assignment metadata") {
Model model;
std::string src_file = std::string(TEST_DATA_DIR) + "/test_3mf/Prusa.stl";
REQUIRE(load_stl(src_file.c_str(), &model));
model.add_default_instances();
// store_bbs_3mf stages Metadata/project_settings.config through the model's backup path;
// point it at a writable temp dir (the default lives under a read-only root in CI).
std::string backup_dir =
(boost::filesystem::temp_directory_path() / boost::filesystem::unique_path("orca_mn_%%%%%%%%")).string();
boost::filesystem::create_directories(backup_dir);
model.set_backup_path(backup_dir);
// Global (printer) config: give nozzle_volume_type a non-default value so the slice_info
// read-back is a meaningful assertion (High Flow == 1).
DynamicPrintConfig config = DynamicPrintConfig::full_print_config();
config.set_key_value("nozzle_volume_type",
new ConfigOptionEnumsGeneric({ (int) NozzleVolumeType::nvtHighFlow }));
PlateData* plate = new PlateData();
plate->plate_index = 0;
plate->is_sliced_valid = true; // gate for the slice_info.config writer (nozzle_volume_type)
plate->filament_maps = { 1, 2, 1 }; // slice_info uses this; keep it == model_settings' value
plate->config.set_key_value("filament_map_mode", new ConfigOptionEnum<FilamentMapMode>(fmmManual));
plate->config.set_key_value("filament_map", new ConfigOptionInts({ 1, 2, 1 }));
// Deliberately include out-of-range volume-type ids (2 == Hybrid, 3 == TPU High Flow):
// the loader must clamp them back to Standard (0).
plate->config.set_key_value("filament_volume_map", new ConfigOptionInts({ 0, 2, 1, 3 }));
// Known-lossy: a true value must NOT survive the round-trip (slice_info hardcodes false,
// model_settings never writes it).
plate->config.set_key_value("enable_filament_dynamic_map", new ConfigOptionBool(true));
WHEN("stored to and reloaded from a .3mf") {
std::string test_file = std::string(TEST_DATA_DIR) + "/test_3mf/mn_roundtrip.3mf";
StoreParams store_params;
store_params.path = test_file.c_str();
store_params.model = &model;
store_params.config = &config;
store_params.plate_data_list.push_back(plate);
store_params.strategy = SaveStrategy::Zip64 | SaveStrategy::Silence;
REQUIRE(store_bbs_3mf(store_params));
Model dst_model;
DynamicPrintConfig dst_config;
ConfigSubstitutionContext ctxt{ ForwardCompatibilitySubstitutionRule::Enable };
PlateDataPtrs dst_plates;
std::vector<Preset*> project_presets;
bool is_bbl_3mf = false, is_orca_3mf = false;
Semver file_version;
// LoadConfig is required for slice_info.config (nozzle_volume_type) to be parsed —
// matches how the app loads projects.
bool loaded = load_bbs_3mf(test_file.c_str(), &dst_config, &ctxt, &dst_model, &dst_plates,
&project_presets, &is_bbl_3mf, &is_orca_3mf, &file_version, nullptr,
LoadStrategy::LoadModel | LoadStrategy::LoadConfig);
boost::filesystem::remove(test_file);
THEN("every multi-nozzle key round-trips as expected") {
REQUIRE(loaded);
REQUIRE(dst_plates.size() >= 1);
PlateData* rt = dst_plates.front();
// filament_map (model_settings + slice_info; already round-tripped)
auto* fmap = rt->config.option<ConfigOptionInts>("filament_map");
REQUIRE(fmap != nullptr);
REQUIRE(fmap->values == std::vector<int>({ 1, 2, 1 }));
// filament_volume_map (model_settings) with the >1 -> 0 clamp
auto* fvmap = rt->config.option<ConfigOptionInts>("filament_volume_map");
REQUIRE(fvmap != nullptr);
REQUIRE(fvmap->values == std::vector<int>({ 0, 0, 1, 0 }));
// nozzle_volume_type read-back into PlateData::nozzle_volume_types
REQUIRE(rt->nozzle_volume_types == "1");
// enable_filament_dynamic_map pinned lossy: model_settings never serializes it and
// slice_info hardcodes false, so the `true` we set is dropped. Pinned here
// (absent or false, never true) so a future change that persists it must update this.
auto* dyn = rt->config.option<ConfigOptionBool>("enable_filament_dynamic_map");
const bool persisted_true = (dyn != nullptr && dyn->value);
REQUIRE_FALSE(persisted_true);
}
release_PlateData_list(dst_plates);
}
delete plate; // store_bbs_3mf does not take ownership of the source plate
boost::filesystem::remove_all(backup_dir);
}
}
// A legacy / foreign project (no multi-nozzle metadata) must load crash-safe through the BBS
// importer and must not fabricate a filament_volume_map.
SCENARIO("Legacy project loads crash-safe via load_bbs_3mf", "[3mf][MultiNozzle]") {
GIVEN("a project without any multi-nozzle metadata") {
std::string path = std::string(TEST_DATA_DIR) + "/test_3mf/Geräte/Büchse.3mf";
Model model;
DynamicPrintConfig config;
ConfigSubstitutionContext ctxt{ ForwardCompatibilitySubstitutionRule::Enable };
PlateDataPtrs plates;
std::vector<Preset*> project_presets;
bool is_bbl_3mf = false, is_orca_3mf = false;
Semver file_version;
WHEN("loaded through the BBS importer") {
bool loaded = false;
REQUIRE_NOTHROW(loaded = load_bbs_3mf(path.c_str(), &config, &ctxt, &model, &plates,
&project_presets, &is_bbl_3mf, &is_orca_3mf,
&file_version, nullptr,
LoadStrategy::LoadModel | LoadStrategy::LoadConfig));
THEN("it does not crash and invents no per-filament volume map") {
for (PlateData* p : plates) {
REQUIRE(p->config.option<ConfigOptionInts>("filament_volume_map") == nullptr);
}
}
release_PlateData_list(plates);
}
}
}
// Device-side nozzle-grouping serialization surface.
// Direct unit coverage for the pure serialize/deserialize + StaticNozzleGroupResult helpers that the
// gcode.3mf writer/reader lean on.
SCENARIO("MultiNozzle serialization helpers", "[3mf][MultiNozzle]") {
using namespace Slic3r::MultiNozzleUtils;
GIVEN("NozzleInfo / NozzleGroupInfo") {
NozzleInfo n0; n0.group_id = 0; n0.extruder_id = 0; n0.diameter = "0.4"; n0.volume_type = nvtStandard;
NozzleInfo n1; n1.group_id = 1; n1.extruder_id = 1; n1.diameter = "0.4"; n1.volume_type = nvtHighFlow;
THEN("NozzleInfo::serialize matches the <nozzle> tag attributes (extruder_id 1-based)") {
REQUIRE(n0.serialize() == "id=\"0\" extruder_id=\"1\" nozzle_diameter=\"0.4\" volume_type=\"Standard\"");
REQUIRE(n1.serialize() == "id=\"1\" extruder_id=\"2\" nozzle_diameter=\"0.4\" volume_type=\"High Flow\"");
}
THEN("NozzleGroupInfo serialize/deserialize round-trips and rejects malformed input") {
NozzleGroupInfo g("0.4", nvtHighFlow, 1, 3);
REQUIRE(g.serialize() == "1-0.4-High Flow-3");
auto rt = NozzleGroupInfo::deserialize(g.serialize());
REQUIRE(rt.has_value());
REQUIRE(*rt == g);
REQUIRE_FALSE(NozzleGroupInfo::deserialize("1-0.4-Standard").has_value()); // too few tokens
REQUIRE_FALSE(NozzleGroupInfo::deserialize("x-0.4-Standard-3").has_value()); // non-numeric extruder
}
}
GIVEN("a StaticNozzleGroupResult built from filament + nozzle infos") {
std::vector<NozzleInfo> nozzles;
{ NozzleInfo n; n.group_id = 0; n.extruder_id = 0; n.diameter = "0.4"; n.volume_type = nvtStandard; nozzles.push_back(n); }
{ NozzleInfo n; n.group_id = 1; n.extruder_id = 1; n.diameter = "0.4"; n.volume_type = nvtHighFlow; nozzles.push_back(n); }
std::vector<FilamentInfo> filaments(3);
filaments[0].id = 0; filaments[0].group_id = { 0 };
filaments[1].id = 1; filaments[1].group_id = { 1 };
filaments[2].id = 2; filaments[2].group_id = { 0, 1 };
auto result = StaticNozzleGroupResult::create(filaments, nozzles, { 0, 1, 2 }, { 0, 1, 0 }, false);
REQUIRE(result.has_value());
THEN("filament->nozzle queries resolve to the stored mapping") {
REQUIRE(result->get_extruder_count() == 2);
REQUIRE(result->get_used_extruders() == std::vector<int>({ 0, 1 }));
REQUIRE(result->get_used_filaments() == std::vector<unsigned int>({ 0, 1, 2 }));
REQUIRE(result->get_nozzles_for_filament(0).size() == 1);
REQUIRE(result->get_nozzles_for_filament(2).size() == 2);
// first-use resolves through the (filament,nozzle) change sequences.
auto first = result->get_first_nozzle_for_filament(1);
REQUIRE(first.has_value());
REQUIRE(first->group_id == 1);
}
THEN("empty inputs yield nullopt") {
REQUIRE_FALSE(StaticNozzleGroupResult::create({}, nozzles, {}, {}, false).has_value());
REQUIRE_FALSE(StaticNozzleGroupResult::create(filaments, {}, {}, {}, false).has_value());
}
}
GIVEN("load_nozzle_infos_with_compatibility fallbacks") {
std::vector<NozzleInfo> new_format;
{ NozzleInfo n; n.group_id = 1; n.extruder_id = 1; n.diameter = "0.4"; n.volume_type = nvtHighFlow; new_format.push_back(n); }
{ NozzleInfo n; n.group_id = 0; n.extruder_id = 0; n.diameter = "0.4"; n.volume_type = nvtStandard; new_format.push_back(n); }
THEN("new-format <nozzle> tags are returned sorted by logical id") {
auto out = load_nozzle_infos_with_compatibility(new_format, {}, {}, {}, {});
REQUIRE(out.size() == 2);
REQUIRE(out[0].group_id == 0);
REQUIRE(out[1].group_id == 1);
}
THEN("oldest single-nozzle 3mf (no tags, no filament group_id) rebuilds from diameters/volume types") {
std::vector<NozzleVolumeType> vt = { nvtStandard, nvtHighFlow };
std::vector<double> dia = { 0.4, 0.4 };
auto out = load_nozzle_infos_with_compatibility({}, {}, {}, vt, dia);
REQUIRE(out.size() == 2);
REQUIRE(out[0].extruder_id == 0);
REQUIRE(out[0].volume_type == nvtStandard);
REQUIRE(out[1].volume_type == nvtHighFlow);
}
}
}
// The layer-aware grouping result must survive the gcode.3mf write/read as
// <nozzle> tags and the enable_filament_dynamic_map flag. Proves the parse_filament_info stamping,
// the NOZZLE_TAG writer, the _handle_config_nozzle reader, and the nozzles_info plate copy.
SCENARIO("Nozzle-group metadata .3mf round-trip", "[3mf][MultiNozzle]") {
GIVEN("a plate carrying a two-nozzle LayeredNozzleGroupResult") {
Model model;
std::string src_file = std::string(TEST_DATA_DIR) + "/test_3mf/Prusa.stl";
REQUIRE(load_stl(src_file.c_str(), &model));
model.add_default_instances();
std::string backup_dir =
(boost::filesystem::temp_directory_path() / boost::filesystem::unique_path("orca_ng_%%%%%%%%")).string();
boost::filesystem::create_directories(backup_dir);
model.set_backup_path(backup_dir);
DynamicPrintConfig config = DynamicPrintConfig::full_print_config();
std::vector<MultiNozzleUtils::NozzleInfo> nozzles;
{ MultiNozzleUtils::NozzleInfo n; n.group_id = 0; n.extruder_id = 0; n.diameter = "0.4"; n.volume_type = NozzleVolumeType::nvtStandard; nozzles.push_back(n); }
{ MultiNozzleUtils::NozzleInfo n; n.group_id = 1; n.extruder_id = 1; n.diameter = "0.4"; n.volume_type = NozzleVolumeType::nvtHighFlow; nozzles.push_back(n); }
auto group = MultiNozzleUtils::LayeredNozzleGroupResult::create(
std::vector<int>{ 0, 1, 0 }, nozzles, std::vector<unsigned int>{ 0, 1, 2 });
REQUIRE(group.has_value());
PlateData* plate = new PlateData();
plate->plate_index = 0;
plate->is_sliced_valid = true;
plate->filament_maps = { 1, 2, 1 };
plate->nozzle_group_result = group;
plate->config.set_key_value("filament_map_mode", new ConfigOptionEnum<FilamentMapMode>(fmmManual));
plate->config.set_key_value("filament_map", new ConfigOptionInts({ 1, 2, 1 }));
WHEN("stored to and reloaded from a .3mf") {
std::string test_file = std::string(TEST_DATA_DIR) + "/test_3mf/ng_roundtrip.3mf";
StoreParams store_params;
store_params.path = test_file.c_str();
store_params.model = &model;
store_params.config = &config;
store_params.plate_data_list.push_back(plate);
store_params.strategy = SaveStrategy::Zip64 | SaveStrategy::Silence;
REQUIRE(store_bbs_3mf(store_params));
Model dst_model;
DynamicPrintConfig dst_config;
ConfigSubstitutionContext ctxt{ ForwardCompatibilitySubstitutionRule::Enable };
PlateDataPtrs dst_plates;
std::vector<Preset*> project_presets;
bool is_bbl_3mf = false, is_orca_3mf = false;
Semver file_version;
bool loaded = load_bbs_3mf(test_file.c_str(), &dst_config, &ctxt, &dst_model, &dst_plates,
&project_presets, &is_bbl_3mf, &is_orca_3mf, &file_version, nullptr,
LoadStrategy::LoadModel | LoadStrategy::LoadConfig);
boost::filesystem::remove(test_file);
THEN("the <nozzle> tags round-trip into the loaded plate's nozzles_info") {
REQUIRE(loaded);
REQUIRE(dst_plates.size() >= 1);
PlateData* rt = dst_plates.front();
REQUIRE(rt->nozzles_info.size() == 2);
// reader stores extruder_id 0-based (tag is 1-based), diameter/volume_type preserved.
std::sort(rt->nozzles_info.begin(), rt->nozzles_info.end());
REQUIRE(rt->nozzles_info[0].group_id == 0);
REQUIRE(rt->nozzles_info[0].extruder_id == 0);
REQUIRE(rt->nozzles_info[0].diameter == "0.4");
REQUIRE(rt->nozzles_info[0].volume_type == NozzleVolumeType::nvtStandard);
REQUIRE(rt->nozzles_info[1].group_id == 1);
REQUIRE(rt->nozzles_info[1].extruder_id == 1);
REQUIRE(rt->nozzles_info[1].volume_type == NozzleVolumeType::nvtHighFlow);
// A static (non-selector) result must persist enable_filament_dynamic_map = false.
auto* dyn = rt->config.option<ConfigOptionBool>("enable_filament_dynamic_map");
const bool persisted_true = (dyn != nullptr && dyn->value);
REQUIRE_FALSE(persisted_true);
}
release_PlateData_list(dst_plates);
}
delete plate;
boost::filesystem::remove_all(backup_dir);
}
}
SCENARIO("2D convex hull of sinking object", "[3mf][.]") {
GIVEN("model") {
// load a model

View File

@@ -401,3 +401,38 @@ SCENARIO("update_diff_values_to_child_config tolerates legacy machine-limit vect
// }
// }
// }
TEST_CASE("H2C/A2L-era multi-nozzle and pre-heat config keys exist", "[config]") {
// Foundation keys backing H2C 6-nozzle cluster grouping, the pre-heat/pre-cool time
// model, and wipe-tower nozzle-change handling. Defaults must keep existing
// single-nozzle printers behaving identically.
Slic3r::DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
// Printer / per-extruder options
REQUIRE(config.option<ConfigOptionIntsNullable>("extruder_max_nozzle_count") != nullptr);
REQUIRE(config.option<ConfigOptionIntsNullable>("extruder_max_nozzle_count")->values == std::vector<int>{1});
REQUIRE(config.option<ConfigOptionBool>("enable_pre_heating") != nullptr);
REQUIRE(config.option<ConfigOptionBool>("enable_pre_heating")->value == false);
REQUIRE(config.option<ConfigOptionFloatsNullable>("hotend_cooling_rate") != nullptr);
REQUIRE(config.option<ConfigOptionFloatsNullable>("hotend_heating_rate") != nullptr);
REQUIRE(config.option<ConfigOptionFloat>("machine_hotend_change_time") != nullptr);
REQUIRE(config.option<ConfigOptionFloat>("machine_prepare_compensation_time") != nullptr);
// Filament pre-cooling / ramming / nozzle-change (nc) options
REQUIRE(config.option<ConfigOptionIntsNullable>("filament_pre_cooling_temperature") != nullptr);
REQUIRE(config.option<ConfigOptionIntsNullable>("filament_pre_cooling_temperature_nc") != nullptr);
REQUIRE(config.option<ConfigOptionFloatsNullable>("filament_preheat_temperature_delta") != nullptr);
REQUIRE(config.option<ConfigOptionFloatsNullable>("filament_retract_length_nc") != nullptr);
REQUIRE(config.option<ConfigOptionFloats>("filament_change_length_nc") != nullptr);
REQUIRE(config.option<ConfigOptionFloats>("filament_prime_volume_nc") != nullptr);
REQUIRE(config.option<ConfigOptionFloatsNullable>("filament_ramming_travel_time") != nullptr);
REQUIRE(config.option<ConfigOptionFloatsNullable>("filament_ramming_travel_time_nc") != nullptr);
REQUIRE(config.option<ConfigOptionFloatsNullable>("filament_ramming_volumetric_speed") != nullptr);
REQUIRE(config.option<ConfigOptionFloatsNullable>("filament_ramming_volumetric_speed_nc") != nullptr);
// Spot-check defaults that must not alter existing behavior.
REQUIRE(config.option<ConfigOptionFloatsNullable>("filament_retract_length_nc")->values == std::vector<double>{10.});
REQUIRE(config.option<ConfigOptionFloats>("filament_prime_volume_nc")->values == std::vector<double>{60.});
REQUIRE(config.option<ConfigOptionIntsNullable>("filament_pre_cooling_temperature_nc")->values == std::vector<int>{0});
REQUIRE(config.option<ConfigOptionFloatsNullable>("filament_ramming_volumetric_speed")->values == std::vector<double>{-1});
}

View File

@@ -0,0 +1,350 @@
#include <catch2/catch_all.hpp>
#include "libslic3r/MultiNozzleUtils.hpp"
#include "libslic3r/PrintConfig.hpp"
#include "libslic3r/GCode/ToolOrdering.hpp"
#include <algorithm>
#include <map>
#include <set>
#include <vector>
// H2C/A2L multi-nozzle filament grouping core.
//
// These tests pin the behaviour of the grouping result type
// (Slic3r::MultiNozzleUtils::LayeredNozzleGroupResult) that GCode consumes via
// group_result->get_nozzle_id(filament, layer) and
// group_result->get_first_nozzle_for_filament(filament)->group_id.
//
// The central requirement is ZERO behaviour change for existing (single-nozzle)
// printers: with extruder_max_nozzle_count == 1 per extruder the result collapses
// to the classic filament->extruder grouping (nozzle id == extruder id).
using namespace Slic3r;
using namespace Slic3r::MultiNozzleUtils;
namespace {
// Build a trivial "one logical nozzle per extruder" list, the single-nozzle case
// that every current printer profile produces.
std::vector<NozzleInfo> single_nozzle_per_extruder(int extruder_count)
{
std::vector<NozzleInfo> nozzle_list;
for (int e = 0; e < extruder_count; ++e) {
NozzleInfo n;
n.diameter = "0.4";
n.volume_type = nvtStandard;
n.extruder_id = e;
n.group_id = e; // one nozzle per extruder => nozzle id == extruder id
nozzle_list.push_back(n);
}
return nozzle_list;
}
} // namespace
TEST_CASE("Multi-nozzle gate predicate mirrors BambuStudio", "[ToolOrdering][H2C]")
{
// The multi-nozzle gate: std::any_of(extruder_max_nozzle_count > 1).
DynamicPrintConfig config = DynamicPrintConfig::full_print_config();
auto *opt = config.option<ConfigOptionIntsNullable>("extruder_max_nozzle_count");
REQUIRE(opt != nullptr); // extruder_max_nozzle_count must be a real config option
// extruder_nozzle_stats must be a real config option so printer profiles and
// 3mf projects round-trip the per-extruder nozzle inventory (GUI producers wire it later).
REQUIRE(config.option<ConfigOptionStrings>("extruder_nozzle_stats") != nullptr);
auto has_multiple_nozzle = [](const std::vector<int> &values) {
return std::any_of(values.begin(), values.end(), [](int v) { return v > 1; });
};
// Default for every existing printer: 1 nozzle per extruder => gate is closed.
REQUIRE_FALSE(has_multiple_nozzle(opt->values));
// Synthetic H2C-like machine: extruder 1 is a 6-nozzle cluster => gate opens.
REQUIRE(has_multiple_nozzle(std::vector<int>{1, 6}));
}
TEST_CASE("Single-nozzle grouping: every filament maps to its extruder nozzle", "[ToolOrdering][H2C]")
{
SECTION("single extruder => all filaments map to nozzle 0")
{
auto nozzle_list = single_nozzle_per_extruder(1);
// 3 filaments, all assigned to the single extruder 0.
std::vector<int> filament_nozzle_map = {0, 0, 0};
std::vector<unsigned int> used_filaments = {0, 1, 2};
auto group_opt = LayeredNozzleGroupResult::create(filament_nozzle_map, nozzle_list, used_filaments);
REQUIRE(group_opt.has_value());
auto &group = *group_opt;
for (int f = 0; f < 3; ++f) {
REQUIRE(group.get_nozzle_id(f) == 0);
REQUIRE(group.get_extruder_id(f) == 0);
auto first = group.get_first_nozzle_for_filament(f);
REQUIRE(first.has_value());
REQUIRE(first->group_id == 0);
}
REQUIRE_FALSE(group.is_support_dynamic_nozzle_map());
}
SECTION("dual extruder => nozzle id equals the classic extruder grouping")
{
auto nozzle_list = single_nozzle_per_extruder(2);
// filament -> extruder map (the map Orca's reorder already computes).
std::vector<int> filament_map = {0, 1, 0, 1};
std::vector<unsigned int> used_filaments = {0, 1, 2, 3};
auto group_opt = LayeredNozzleGroupResult::create(filament_map, nozzle_list, used_filaments);
REQUIRE(group_opt.has_value());
auto &group = *group_opt;
REQUIRE(group.get_nozzle_id(0) == 0);
REQUIRE(group.get_nozzle_id(1) == 1);
REQUIRE(group.get_nozzle_id(2) == 0);
REQUIRE(group.get_nozzle_id(3) == 1);
// With one nozzle per extruder, nozzle id and extruder id agree.
for (int f = 0; f < 4; ++f)
REQUIRE(group.get_nozzle_id(f) == group.get_extruder_id(f));
}
}
TEST_CASE("H2C multi-nozzle: filaments get distinct nozzles on the 6-nozzle extruder", "[ToolOrdering][H2C]")
{
// Synthetic H2C-like config: 2 extruders, extruder_max_nozzle_count = {1, 6},
// 4 filaments all assigned to extruder 1 (0-based). Each filament requests a
// distinct logical nozzle cluster (as the grouping algorithm would emit), so the
// create() overload must resolve them to 4 distinct physical nozzles.
std::vector<unsigned int> used_filaments = {0, 1, 2, 3};
std::vector<int> filament_map = {1, 1, 1, 1}; // extruder 1
std::vector<int> filament_volume_map = {0, 0, 0, 0}; // nvtStandard
std::vector<int> filament_nozzle_map = {0, 1, 2, 3}; // distinct clusters
std::vector<std::map<NozzleVolumeType, int>> nozzle_count(2);
nozzle_count[0] = {}; // extruder 0: 1-nozzle (unused here)
nozzle_count[1] = {{nvtStandard, 6}}; // extruder 1: 6-nozzle cluster
auto group_opt = LayeredNozzleGroupResult::create(
used_filaments, filament_map, filament_volume_map, filament_nozzle_map, nozzle_count, 0.4f);
REQUIRE(group_opt.has_value());
auto &group = *group_opt;
// All four filaments live on extruder 1, on four distinct physical nozzles.
std::set<int> distinct_nozzles;
for (int f = 0; f < 4; ++f) {
REQUIRE(group.get_extruder_id(f) == 1);
int nid = group.get_nozzle_id(f);
REQUIRE(nid >= 0);
distinct_nozzles.insert(nid);
}
REQUIRE(distinct_nozzles.size() == 4);
// get_nozzle_id must be stable across layers (no per-layer / selector map here).
for (int f = 0; f < 4; ++f) {
int base = group.get_nozzle_id(f, -1);
REQUIRE(group.get_nozzle_id(f, 0) == base);
REQUIRE(group.get_nozzle_id(f, 5) == base);
}
// first-nozzle lookup agrees with the per-layer lookup for a static map.
for (int f = 0; f < 4; ++f) {
auto first = group.get_first_nozzle_for_filament(f);
REQUIRE(first.has_value());
REQUIRE(first->extruder_id == 1);
REQUIRE(first->group_id == group.get_nozzle_id(f));
}
}
TEST_CASE("H2C dynamic selector: per-layer nozzle ids reach the g-code surface", "[ToolOrdering][H2C][Dynamic]")
{
// The per-layer regroup engine
// (plan_filament_mapping_and_order_by_combo_ranges -> 4-arg LayeredNozzleGroupResult::create)
// produces a *selector* result whose filament->nozzle map varies across layers. This is exactly
// what GCode reads for H2C dynamic mode: hotend_id_for_gcode_placeholder /
// nozzle_id_for_gcode_placeholder call group->is_support_dynamic_nozzle_map() and, when true,
// group->get_nozzle_id(filament, layer) / get_first_nozzle_for_filament(filament). Here we build
// the selector result directly (the engine's output shape) and assert those accessors return
// per-layer values -- the surface that "goes live" only in dynamic mode. The static path (every
// other test above) keeps is_support_dynamic_nozzle_map() == false and a stable nozzle id, so its
// g-code is unchanged.
// H2C-like fleet: extruder 0 = 1 nozzle (group 0), extruder 1 = a 3-nozzle rack (groups 1..3).
std::vector<NozzleInfo> nozzle_list;
for (int g = 0; g < 4; ++g) {
NozzleInfo n;
n.diameter = "0.4";
n.volume_type = nvtStandard;
n.extruder_id = (g == 0) ? 0 : 1;
n.group_id = g;
nozzle_list.push_back(n);
}
// Three filaments; filament 2 is reassigned from physical nozzle 2 (layers 0-1) to nozzle 3
// (layers 2-3) by the per-layer selector -- the case that sets support_dynamic_nozzle_map.
std::vector<std::vector<int>> layer_filament_nozzle_maps = {
{0, 1, 2}, // layer 0
{0, 1, 2}, // layer 1
{0, 1, 3}, // layer 2: filament 2 moved to nozzle 3
{0, 1, 3}, // layer 3
};
std::vector<std::vector<unsigned int>> layer_filament_sequences = {
{0, 1, 2}, {0, 1, 2}, {0, 1, 2}, {0, 1, 2},
};
std::vector<unsigned int> used_filaments = {0, 1, 2};
auto group_opt = LayeredNozzleGroupResult::create(layer_filament_nozzle_maps, nozzle_list, used_filaments, layer_filament_sequences);
REQUIRE(group_opt.has_value());
auto &group = *group_opt;
// The selector is active: a filament maps to more than one physical nozzle across layers.
REQUIRE(group.is_support_dynamic_nozzle_map());
// Per-layer hotend/nozzle ids -- the values the dynamic g-code placeholders emit.
REQUIRE(group.get_nozzle_id(2, 0) == 2);
REQUIRE(group.get_nozzle_id(2, 1) == 2);
REQUIRE(group.get_nozzle_id(2, 2) == 3); // reassigned on layer 2
REQUIRE(group.get_nozzle_id(2, 3) == 3);
REQUIRE(group.get_extruder_id(2, 0) == 1);
REQUIRE(group.get_extruder_id(2, 2) == 1);
// Unmoved filaments keep a stable id across layers.
REQUIRE(group.get_nozzle_id(0, 0) == 0);
REQUIRE(group.get_nozzle_id(0, 3) == 0);
REQUIRE(group.get_nozzle_id(1, 0) == 1);
REQUIRE(group.get_nozzle_id(1, 3) == 1);
// first-nozzle lookup (used by the *_first_* placeholders / start g-code) is the first layer's id.
auto first2 = group.get_first_nozzle_for_filament(2);
REQUIRE(first2.has_value());
REQUIRE(first2->group_id == 2);
// every physical nozzle a filament visits is reported (3mf metadata / nozzle_diameters_by_nozzle_id).
std::set<int> fil2_nozzles;
for (const auto &n : group.get_nozzles_for_filament(2))
fil2_nozzles.insert(n.group_id);
REQUIRE(fil2_nozzles == std::set<int>({2, 3}));
}
TEST_CASE("Multi-nozzle reorder tolerates a filament with no nozzle (RL-48)", "[ToolOrdering][H2C][Dynamic]")
{
// The per-layer engine can hand reorder_filaments_for_multi_nozzle_extruder a group result that
// resolves no nozzle for a layer's filament (a degenerate/malformed input where a layer references
// a filament index outside the grouping map). Unguarded, that dereferences std::max_element() on an
// empty extruder set (SIGSEGV). The guard must instead emit each layer's filaments in order and
// return, so a bad input degrades gracefully rather than crashing.
auto nozzle_list = single_nozzle_per_extruder(2);
std::vector<int> filament_nozzle_map = {0}; // map only covers filament 0
auto group_opt = LayeredNozzleGroupResult::create(filament_nozzle_map, nozzle_list, std::vector<unsigned int>{0});
REQUIRE(group_opt.has_value());
std::vector<unsigned int> filament_lists = {3}; // filament 3 resolves to no nozzle
std::vector<std::vector<unsigned int>> layer_filaments = {{3}, {3}};
std::vector<std::vector<std::vector<float>>> flush_matrix(2, {{0.f}}); // unused on the guard path
std::vector<std::vector<unsigned int>> sequences;
REQUIRE_NOTHROW(reorder_filaments_for_multi_nozzle_extruder(filament_lists, *group_opt, layer_filaments, flush_matrix, nullptr, &sequences));
// Each layer still gets a valid sequence (its own filaments) — no reorder, no crash.
REQUIRE(sequences.size() == layer_filaments.size());
REQUIRE(sequences[0] == std::vector<unsigned int>{3});
REQUIRE(sequences[1] == std::vector<unsigned int>{3});
}
// The round-robin build_multi_nozzle_group_result adapter was superseded by the
// nozzle-centric FilamentGroup engine (get_recommended_filament_maps now decides nozzle co-location
// by flush cost, not round-robin). The two former pipeline tests are dropped:
// * H2C multi-nozzle physical-nozzle resolution (6-arg create) is covered above by the
// "H2C multi-nozzle: filaments get distinct nozzles" case;
// * the single-nozzle "nozzle id == extruder id" degradation is covered above by the
// "Single-nozzle grouping" case (build_default_nozzle_list + 3-arg create is the exact path the
// gate-closed branch and by-object fallback use);
// * end-to-end H2C/H2D grouping co-location is now pinned by the filament_group golden suite
// (tests/filament_group, config_b/config_c).
TEST_CASE("extruder_nozzle_stats round-trips through save/parse", "[ToolOrdering][H2C]")
{
// The per-extruder nozzle inventory must survive save_extruder_nozzle_stats_to_string ->
// get_extruder_nozzle_stats unchanged, so printer presets and 3mf projects persist it.
std::vector<std::map<NozzleVolumeType, int>> stats = {
{{nvtStandard, 1}}, // extruder 0: single standard nozzle
{{nvtStandard, 5}, {nvtHighFlow, 1}}, // extruder 1: 6-nozzle mixed cluster
};
REQUIRE(get_extruder_nozzle_stats(save_extruder_nozzle_stats_to_string(stats)) == stats);
}
// The filament-change-time model (MultiNozzleUtils::simulate_filament_change_time) is self-contained
// analytic code with no slicing-pipeline caller yet; these fixtures pin its numeric output so future
// changes and its first consumer (the filament_group golden harness) build on a locked model. Expected
// values are hand-traced through the AMS -> selector -> extruder transport model.
TEST_CASE("Filament-change-time model matches the BBS analytic simulation", "[MultiNozzle][H2C][ChangeTime]")
{
using Catch::Matchers::WithinAbs;
// Load/unload constants mirror the golden config_c change_time_params
// (selector 1/1, standard 3/2): a selector move costs 1, a full AMS load 3 / unload 2.
FilamentChangeTimeParams params;
params.selector_load_time = 1.0f;
params.selector_unload_time = 1.0f;
params.standard_load_time = 3.0f;
params.standard_unload_time = 2.0f;
// One extruder carrying one physical nozzle (nozzle id == extruder id == 0).
std::vector<NozzleInfo> nozzle_list(1);
nozzle_list[0].diameter = "0.4";
nozzle_list[0].volume_type = nvtStandard;
nozzle_list[0].extruder_id = 0;
nozzle_list[0].group_id = 0;
// Two filaments in distinct AMS groups, printed in the order A, B, A on nozzle 0.
std::vector<int> logical_filaments = {0, 1};
std::vector<int> group_of_filament = {0, 1};
std::vector<int> filament_change_seq = {0, 1, 0};
std::vector<int> nozzle_change_seq = {0, 0, 0};
SECTION("no AMS pre-load: each change is a full AMS<->extruder transport")
{
auto r = simulate_filament_change_time(
logical_filaments, nozzle_list, filament_change_seq, nozzle_change_seq,
group_of_filament, params, /*ams_preload_enabled=*/{}, /*calc_sliced_time=*/true);
// load0(3) + [unload0(2)+load1(3)] + [unload1(2)+load0(3)] = 13
REQUIRE_THAT(r.actual_time, WithinAbs(13.0, 1e-6));
// Single nozzle, no selector overlap => slicer estimate equals the actual time.
REQUIRE_THAT(r.sliced_time, WithinAbs(13.0, 1e-6));
}
SECTION("AMS pre-load overlaps transport, shrinking the actual time")
{
std::vector<bool> preload = {true, true};
auto r = simulate_filament_change_time(
logical_filaments, nozzle_list, filament_change_seq, nozzle_change_seq,
group_of_filament, params, preload, /*calc_sliced_time=*/false);
// Pre-loading the next filament into the selector runs in parallel with the current
// extruder move, so the selector<->extruder legs dominate: 3 + (1+1) + (1+1) = 7.
REQUIRE_THAT(r.actual_time, WithinAbs(7.0, 1e-6));
}
SECTION("degenerate inputs return zero")
{
auto r = simulate_filament_change_time({}, nozzle_list, filament_change_seq,
nozzle_change_seq, {}, params);
REQUIRE_THAT(r.actual_time, WithinAbs(0.0, 1e-6));
REQUIRE_THAT(r.sliced_time, WithinAbs(0.0, 1e-6));
}
}
TEST_CASE("NozzleStatusRecorder tracks nozzle/extruder occupancy", "[MultiNozzle][H2C][ChangeTime]")
{
NozzleStatusRecorder rec;
REQUIRE(rec.is_nozzle_empty(0));
REQUIRE(rec.get_filament_in_nozzle(0) == -1);
REQUIRE(rec.get_nozzle_in_extruder(0) == -1);
rec.set_nozzle_status(2, 5, 1); // nozzle 2 holds filament 5, mounted on extruder 1
REQUIRE_FALSE(rec.is_nozzle_empty(2));
REQUIRE(rec.get_filament_in_nozzle(2) == 5);
REQUIRE(rec.get_nozzle_in_extruder(1) == 2);
rec.clear_nozzle_status(2);
REQUIRE(rec.is_nozzle_empty(2));
REQUIRE(rec.get_filament_in_nozzle(2) == -1);
// Clearing a nozzle leaves the extruder->nozzle association intact.
REQUIRE(rec.get_nozzle_in_extruder(1) == 2);
}