Files
OrcaSlicer/tests/slic3rutils/test_creality_cfs_match.cpp
T
Rui Valim db53e2ca57 Fix Creality Hi CFS filament sync over Moonraker (#15857)
The Creality Hi runs Moonraker but reports its CFS in the `box` printer object, which the Moonraker agent didn't read, so filament sync found no slots. The agent now queries `box` when it finds no lane data or Happy Hare, and the parser lives in `Utils/CrealityCfs.cpp`. Creality material IDs map to filament types, with the K2 family's `same_material` list as a fallback for branded spools.

`update_filament_multi_color()` no longer stops at the first slot without a multi-colour list. It uses the slot's own colour instead, so the list stays aligned with the filament list and selecting a later filament no longer writes past its end.

The CrealityPrintAgent fallback no longer syncs a preset of a different material when no base preset of the reported type exists.

The K2 Plus `same_material` fallback is from mattjanus.

Co-authored-by: mattjanus <18539984+mattjanus@users.noreply.github.com>
(cherry picked from commit d67e0d8dc4)
2026-11-01 09:53:53 +08:00

238 lines
11 KiB
C++

#include <algorithm>
#include <catch2/catch_test_macros.hpp>
#include <vector>
#include "libslic3r/Preset.hpp"
#include "libslic3r/PresetBundle.hpp"
#include "libslic3r/PrintConfig.hpp"
#include "slic3r/Utils/CrealityCfs.hpp"
#include "slic3r/Utils/CrealityPrintAgent.hpp"
using namespace Slic3r;
namespace {
// A standalone filament collection, built the same way PresetBundle builds its own, so the
// CFS matcher can be exercised without loading the shipped profiles or touching a printer.
struct FilamentTestCollection : public PresetCollection
{
FilamentTestCollection()
: PresetCollection(Preset::TYPE_FILAMENT, Preset::filament_options(),
static_cast<const PrintRegionConfig &>(FullPrintConfig::defaults()))
{}
};
struct FilamentSpec
{
const char *name;
const char *filament_id;
const char *filament_type;
bool is_library = false; // belongs to the Orca Filament Library, not the printer vendor
bool is_system = true;
};
// Load the specs into the collection, then stamp the fields the matcher reads. Done in a second
// pass because load_preset() keeps m_presets sorted, so a reference taken during the first pass
// can be left dangling by a later load.
void populate(PresetCollection &filaments, const std::vector<FilamentSpec> &specs,
const VendorProfile &vendor, const VendorProfile &library)
{
for (const FilamentSpec &spec : specs) {
DynamicPrintConfig config(filaments.default_preset().config);
config.option<ConfigOptionStrings>("filament_type", true)->values = {spec.filament_type};
filaments.load_preset(std::string(), spec.name, config, /*select=*/false);
}
for (auto it = filaments.begin(); it != filaments.end(); ++it) {
const auto spec = std::find_if(specs.begin(), specs.end(),
[&](const FilamentSpec &s) { return it->name == s.name; });
if (spec == specs.end())
continue;
it->filament_id = spec->filament_id;
it->vendor = spec->is_library ? &library : &vendor;
it->is_system = spec->is_system;
it->is_default = false;
it->is_visible = true;
it->is_compatible = true;
}
}
// What a stock K2 with a 0.4 nozzle sees after the filament_id rework: the vendor's plain generics
// carry no "Creality" in their name (they are "Generic <material> @<scope>"), while the subtype
// variants that do keep a vendor scope are separate products with their own filament_ids.
const std::vector<FilamentSpec> k2_stock_nozzle{
{"AliZ PLA @System", "ALIZ-PLA", "PLA", /*is_library=*/true},
{"Generic PETG @K2-all", "PETG-GENERIC", "PETG"},
{"Generic PLA @K2-all", "PLA-GENERIC", "PLA"},
{"Generic PLA High Speed @Creality K2-all","PLA-HS", "PLA"},
{"Generic PLA Matte @Creality K2-all", "PLA-MATTE", "PLA"},
{"Generic PLA Silk @Creality K2-all", "PLA-SILK", "PLA"},
{"Hyper PLA @K2-all", "PLA-HYPER", "PLA"},
};
std::string match(const std::vector<FilamentSpec> &specs, const std::string &spool_vendor,
const std::string &spool_name, const std::string &base_type)
{
FilamentTestCollection filaments;
VendorProfile vendor("Creality");
VendorProfile library(PresetBundle::ORCA_FILAMENT_LIBRARY);
vendor.name = "Creality";
library.name = PresetBundle::ORCA_FILAMENT_LIBRARY;
populate(filaments, specs, vendor, library);
return CrealityPrintAgent::match_filament_preset(filaments, spool_vendor, spool_name, base_type);
}
} // namespace
TEST_CASE("The Creality Hi Moonraker box response exposes loaded CFS slots", "[CFS][Creality]")
{
const std::string response = R"({"result":{"status":{"box":{
"T1":{"state":"connect","material_type":["","000003","-1", "000001"],
"color_value":["","0FF1E1E","-1","000FF00"]},
"T2":{"state":"None","material_type":["000002","-1","-1","-1"],
"color_value":["0FFFFFF","-1","-1","-1"]}
}}}})";
std::vector<CrealityCfsSlot> slots;
REQUIRE(parse_creality_cfs_response(response, slots));
REQUIRE(slots.size() == 2);
CHECK(slots[0].slot_index == 1);
CHECK(slots[0].material_type == "PETG");
CHECK(slots[0].color == "FF1E1E");
CHECK(slots[1].slot_index == 3);
CHECK(slots[1].material_type == "PLA");
CHECK(slots[1].color == "00FF00");
}
TEST_CASE("The Creality Hi CFS parser rejects malformed and absent box responses", "[CFS][Creality]")
{
std::vector<CrealityCfsSlot> slots;
CHECK_FALSE(parse_creality_cfs_response("{", slots));
CHECK_FALSE(parse_creality_cfs_response(R"({"result":{"status":{}}})", slots));
CHECK(slots.empty());
}
TEST_CASE("The Creality Hi CFS parser skips unknown material IDs", "[CFS][Creality]")
{
const std::string response = R"({"result":{"status":{"box":{"T1":{"state":"connect",
"material_type":["999999","000003",null,"-1"],"color_value":["0FFFFFF","0FF1E1E","-1","-1"]}}}}})";
std::vector<CrealityCfsSlot> slots;
REQUIRE(parse_creality_cfs_response(response, slots));
REQUIRE(slots.size() == 1);
CHECK(slots[0].slot_index == 1);
CHECK(slots[0].material_type == "PETG");
}
// K2-family firmware reports branded/RFID spools with ids outside the generic 0000xx range: a
// Creality CR-Silk PLA (RFID 05001) reports "005001". The printer resolves the name itself in
// box.same_material, so a slot is only dropped when the printer cannot name it either.
TEST_CASE("The Creality K2 Plus CFS parser resolves branded IDs via same_material", "[CFS][Creality]")
{
const std::string response = R"({"result":{"status":{"box":{
"same_material":[["005001","0ffffff",["T1A"],"PLA"]],
"T1":{"state":"connect","material_type":["005001","-1","-1","-1"],
"color_value":["0ffffff","-1","-1","-1"]},
"T2":{"state":"None","material_type":["-1","-1","-1","-1"],
"color_value":["-1","-1","-1","-1"]}
}}}})";
std::vector<CrealityCfsSlot> slots;
REQUIRE(parse_creality_cfs_response(response, slots));
REQUIRE(slots.size() == 1);
CHECK(slots[0].slot_index == 0);
CHECK(slots[0].material_type == "PLA");
CHECK(slots[0].color == "ffffff");
}
TEST_CASE("The Creality CFS parser skips IDs that same_material cannot name", "[CFS][Creality]")
{
const std::string response = R"({"result":{"status":{"box":{
"same_material":[["0e1003","0fff014",["T1A"],""]],
"T1":{"state":"connect","material_type":["0e1003","000003","-1","-1"],
"color_value":["0fff014","0FF1E1E","-1","-1"]}
}}}})";
std::vector<CrealityCfsSlot> slots;
REQUIRE(parse_creality_cfs_response(response, slots));
REQUIRE(slots.size() == 1);
CHECK(slots[0].slot_index == 1);
CHECK(slots[0].material_type == "PETG");
}
TEST_CASE("The Creality CFS parser tolerates a malformed same_material list", "[CFS][Creality]")
{
const std::string response = R"({"result":{"status":{"box":{
"same_material":[["005001"],"nonsense",[1,2,3,4]],
"T1":{"state":"connect","material_type":["000001","-1","-1","-1"],
"color_value":["0FFFFFF","-1","-1","-1"]}
}}}})";
std::vector<CrealityCfsSlot> slots;
REQUIRE(parse_creality_cfs_response(response, slots));
REQUIRE(slots.size() == 1);
CHECK(slots[0].material_type == "PLA");
}
// Orca: a CFS spool that names no recognised product must map to the vendor's plain generic. The
// subtype variants ("High Speed", "Matte", "Silk") score just as well on vendor alone, and since
// each is its own product with its own filament_id, letting one of them win sends the printer the
// id of a filament the user does not have loaded.
TEST_CASE("An unbranded CFS spool maps to the vendor's plain generic, not a subtype", "[CFS][Creality]")
{
CHECK(match(k2_stock_nozzle, "Creality", "", "PLA") == "PLA-GENERIC");
}
// Orca: the vendor bonus reads the preset's owning VendorProfile. "Generic PETG @K2-all" does not
// repeat "Creality" anywhere in its name, so a name based vendor test scored nothing for it and the
// spool fell through to the collection wide first-of-type - an unrelated third party PETG.
TEST_CASE("A CFS spool matches its vendor's presets even when the name omits the vendor", "[CFS][Creality]")
{
CHECK(match(k2_stock_nozzle, "Creality", "", "PETG") == "PETG-GENERIC");
}
TEST_CASE("A branded CFS spool still beats the generic", "[CFS][Creality]")
{
CHECK(match(k2_stock_nozzle, "Creality", "Hyper PLA", "PLA") == "PLA-HYPER");
}
// Orca: the specificity penalty must not stop a spool that genuinely asks for a subtype from
// getting it - only unclaimed qualifiers are penalised.
TEST_CASE("A CFS spool that names a subtype gets that subtype", "[CFS][Creality]")
{
CHECK(match(k2_stock_nozzle, "Creality", "Generic PLA Silk", "PLA") == "PLA-SILK");
CHECK(match(k2_stock_nozzle, "Creality", "Generic PLA Matte", "PLA") == "PLA-MATTE");
}
// Orca: a third party spool matches no preset by brand or vendor, so it falls back to the first
// visible preset of the same type rather than returning nothing. Which one that is depends on the
// collection's ordering, so only the type is pinned here - what matters is that a PLA spool never
// comes back empty and never comes back as another material.
TEST_CASE("A CFS spool from an unknown vendor falls back to a preset of the same type", "[CFS][Creality]")
{
const std::string matched = match(k2_stock_nozzle, "SomeOtherBrand", "", "PLA");
REQUIRE_FALSE(matched.empty());
const auto spec = std::find_if(k2_stock_nozzle.begin(), k2_stock_nozzle.end(),
[&](const FilamentSpec &s) { return matched == s.filament_id; });
REQUIRE(spec != k2_stock_nozzle.end());
CHECK(std::string(spec->filament_type) == "PLA");
}
TEST_CASE("A CFS fallback never selects another material when its type is unavailable", "[CFS][Creality]")
{
const std::vector<FilamentSpec> no_system_abs{
{"Generic TPU @System", "TPU-GENERIC", "TPU", /*is_library=*/true},
{"My ABS", "ABS-USER", "ABS", /*is_library=*/false, /*is_system=*/false},
};
CHECK(match(no_system_abs, "Unknown", "", "ABS").empty());
}
// Orca: Creality's bundle also ships third party filaments ("eSUN PLA+ @K2 Plus-all"). Those carry
// Creality's VendorProfile but name their real brand, so the vendor bonus has to accept a name
// match as well as a profile match - otherwise an eSUN spool stops matching its own preset.
TEST_CASE("A third party spool matches its preset inside the printer vendor's bundle", "[CFS][Creality]")
{
const std::vector<FilamentSpec> with_third_party{
{"Generic PLA @K2-all", "PLA-GENERIC", "PLA"},
{"eSUN PLA+ @K2 Plus-all", "ESUN-PLA", "PLA"}, // shipped by Creality, branded eSUN
};
CHECK(match(with_third_party, "eSUN", "", "PLA") == "ESUN-PLA");
// ... and a Creality spool still prefers Creality's own generic over the eSUN preset, which
// carries Creality's profile too but adds a brand word the spool never claimed.
CHECK(match(with_third_party, "Creality", "", "PLA") == "PLA-GENERIC");
}