Files
OrcaSlicer/tests/slic3rutils/test_creality_cfs_match.cpp
T
HanifKoh c77020e654 Keep a Loaded Creality CFS Spool the Printer Cannot Name (#17034)
A K2 SE lists a loaded spool whose RFID tag the firmware does not know
with only its colour: vendor, type and name blank, state 2. The printer
shows the spool, but parse_cfs_response skipped every material with a
blank vendor and type regardless of state, so the sync dialog never
offered that slot. No firmware has been seen marking an empty slot that
way; empty slots carry state 0 and no colour.

Skip a non-zero state entry only when its colour is blank too. The slot
then shows in the sync dialog as an unread tray, like a Bambu tray with
an unknown spool: mapping it yields Generic PLA in the default colour.
The preset matcher returns no preset for an empty type instead of
scanning every preset for nothing.

The sync code for Moonraker printers assumes every tray either carries
a filament id or is a placeholder for an empty slot; an existing tray
with neither shifts the entries of the trays after it in an overwrite
sync. So a tray that resolved to no preset is published as a placeholder
as well: the overwrite sync keeps the project filament in that position,
as it does for an empty slot, and the tray stays visible in its colour.

Read the box and material fields through the null-safe JSON helpers the
K1 parser already uses, so a null or mistyped field reads as blank
instead of throwing out of the sync.

Tests: parse the reporter's boxsInfo payload and check that the unnamed
spool is listed, a truly blank entry is still skipped, a null field
reads as blank, and an empty type matches no preset; an overwrite sync
with such a placeholder between loaded slots keeps every slot's colour.
2026-11-01 03:14:28 +08:00

310 lines
15 KiB
C++

#include <algorithm>
#include <catch2/catch_test_macros.hpp>
#include <string>
#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");
}
TEST_CASE("A spool the printer cannot name matches no preset", "[CFS][Creality]")
{
CHECK(match(k2_stock_nozzle, "", "", "").empty());
}
// A K2 SE lists a loaded spool whose RFID tag the firmware does not know with only its colour
// (vendor, type and name blank, state 2) and shows it on the printer; the slicer must list it
// too. The empty slot 3 and the external spool holder (box type 1) are what gets skipped.
TEST_CASE("The K2 CFS box response keeps a loaded spool the printer cannot name", "[CFS][Creality]")
{
const std::string response = R"({"boxsInfo":{"name":"MF003","materialBoxs":[
{"id":0,"state":0,"type":1,"materials":[
{"id":0,"vendor":"","type":"","color":"","name":"","rfid":"0","state":1,"percent":100,"editStatus":2}]},
{"id":1,"state":1,"type":0,"temp":23,"humidity":56,"materials":[
{"id":0,"vendor":"Creality","type":"PLA","name":"Hyper PLA","rfid":"01001","color":"#0ffffff",
"percent":100,"state":2,"selected":0,"editStatus":0,"remaining_length":330000},
{"id":1,"vendor":"Creality","type":"PLA","name":"Hyper PLA","rfid":"01001","color":"#0000000",
"percent":58,"state":2,"selected":0,"editStatus":0,"remaining_length":191400},
{"id":2,"vendor":"","type":"","name":"","rfid":"504C4","color":"#7592d31",
"percent":96,"state":2,"selected":0,"editStatus":0,"remaining_length":2915520},
{"id":3,"vendor":"","type":"","name":"","rfid":"","color":"",
"percent":100,"state":0,"selected":0,"editStatus":0}]}]}})";
std::vector<CrealityPrintAgent::CFSSlot> slots;
int box_count = 0;
std::string error;
REQUIRE(CrealityPrintAgent::parse_cfs_response(response, slots, box_count, error));
CHECK(box_count == 1);
REQUIRE(slots.size() == 3);
CHECK(slots[0].slot_id == 0);
CHECK(slots[0].filament_type == "PLA");
CHECK(slots[0].brand_name == "Hyper PLA");
CHECK(slots[0].color_hex == "#ffffff");
CHECK(slots[1].slot_id == 1);
CHECK(slots[2].slot_id == 2);
CHECK(slots[2].box_id == 0);
CHECK(slots[2].vendor.empty());
CHECK(slots[2].filament_type.empty());
CHECK(slots[2].color_hex == "#592d31");
}
TEST_CASE("The K2 CFS box response skips a slot that reports neither material nor colour", "[CFS][Creality]")
{
const std::string response = R"({"boxsInfo":{"materialBoxs":[
{"id":1,"state":1,"type":0,"materials":[
{"id":0,"vendor":"","type":"","name":"","color":"","state":2},
{"id":1,"vendor":"Creality","type":"PETG","name":"","color":"#0ff1e1e","state":1}]}]}})";
std::vector<CrealityPrintAgent::CFSSlot> slots;
int box_count = 0;
std::string error;
REQUIRE(CrealityPrintAgent::parse_cfs_response(response, slots, box_count, error));
CHECK(box_count == 1);
REQUIRE(slots.size() == 1);
CHECK(slots[0].slot_id == 1);
CHECK(slots[0].filament_type == "PETG");
}
TEST_CASE("The K2 CFS box response reads a null or mistyped field as blank", "[CFS][Creality]")
{
const std::string response = R"({"boxsInfo":{"materialBoxs":[
{"id":1,"state":1,"type":0,"materials":[
{"id":0,"vendor":null,"type":"PLA","name":7,"color":"#0ffffff","state":1}]}]}})";
std::vector<CrealityPrintAgent::CFSSlot> slots;
int box_count = 0;
std::string error;
REQUIRE(CrealityPrintAgent::parse_cfs_response(response, slots, box_count, error));
REQUIRE(slots.size() == 1);
CHECK(slots[0].vendor.empty());
CHECK(slots[0].brand_name.empty());
CHECK(slots[0].filament_type == "PLA");
}