From c0efd44e56dab8460dee36677c1f55bcdbeb7ecb Mon Sep 17 00:00:00 2001 From: Lam Wei Lun Date: Thu, 29 Oct 2026 10:55:35 +0800 Subject: [PATCH] Fix Publish 3MF export and import of filament variants (#16984) * Fix Publish 3MF export and import of filament variants Variant-bearing filament keys (filament_options_with_variant) hold one value per (slot, variant) in the uncompacted config, but the published path addressed them by slot: export kept the wrong column once an earlier filament had more than one variant, import wrote every material key to receiver variant 0, and Full Publish copied a single variant. Add a column model in PublishSettings: file columns from filament_self_index, exact variant matching, and the column a slot actually prints with (filament_map / filament_volume_map / nozzle types, hybrid mapped through the slot's own volume type). Export rewrites partial material entries to base#col, masks those keys by column instead of slot, and drops a key whose column is foreign, out of range, or whose vector does not match the file layout. Import maps base#col back to the receiver preset's variant with the same name and skips (reports) a miss instead of writing variant 0; plain keys from older publishes keep the positional import. Full Publish copies every variant of the author's slot and sets the copy's filament_extruder_variant. No new file keys: variant-bearing material keys gain a #col suffix. No UI change in this commit. * Fix missing includes * Fix unit tests * Fix unit tests --- src/libslic3r/PresetBundle.cpp | 122 +++++- src/libslic3r/PublishSettings.cpp | 343 +++++++++++++-- src/libslic3r/PublishSettings.hpp | 45 +- src/slic3r/GUI/Plater.cpp | 23 +- tests/libslic3r/CMakeLists.txt | 1 + tests/libslic3r/test_publish_settings.cpp | 500 ++++++++++++++++++++++ 6 files changed, 988 insertions(+), 46 deletions(-) create mode 100644 tests/libslic3r/test_publish_settings.cpp diff --git a/src/libslic3r/PresetBundle.cpp b/src/libslic3r/PresetBundle.cpp index 0f25f5d15d..77763059ba 100644 --- a/src/libslic3r/PresetBundle.cpp +++ b/src/libslic3r/PresetBundle.cpp @@ -7079,13 +7079,24 @@ void PresetBundle::load_config_file_config(const std::string &name_or_path, bool auto apply_slot_keys = [&](DynamicPrintConfig& preset_config, const std::vector& slot_keys, int author_slot, const std::string& material_label) { + // A variant-bearing key (filament_options_with_variant) addresses a file column + // ("key#col"): the column names its owner slot (filament_self_index) and its + // variant (filament_extruder_variant), and lands on the receiver variant with + // that name. Every other filament key is a per-slot value, applied positionally + // as before. A plain variant-bearing key from an older publish keeps the + // positional meaning. + // filament_self_index was moved out of `config` above; the local copy holds it. + const auto *variant_list = config.option("filament_extruder_variant"); for (const std::string& key : slot_keys) { const std::string base_key = publish_base_key(key); if (structural_keys.count(base_key) != 0) continue; + // Layout keys describe the file's variant columns; they are never a value + // to overlay (and overwriting one would corrupt every later lookup). + if (base_key == "filament_extruder_variant" || base_key == "filament_self_index") + continue; const ConfigOption* src_opt = config.option(base_key); - if (src_opt == nullptr || !src_opt->is_vector() || author_slot < 0 || - author_slot >= static_cast(static_cast(src_opt)->size())) { + if (src_opt == nullptr || !src_opt->is_vector()) { skipped_keys.emplace_back("material:" + material_label + " (" + key + ")"); continue; } @@ -7095,9 +7106,40 @@ void PresetBundle::load_config_file_config(const std::string &name_or_path, bool skipped_keys.emplace_back("material:" + material_label + " (" + key + ")"); continue; } - // Per-slot scalar copy: the receiver preset holds one value per key - // (vector of size 1), the file holds the per-slot vector. - static_cast(dst_opt)->set_at(src_opt, 0, author_slot); + const auto *src_vec = dynamic_cast(src_opt); + auto *dst_vec = dynamic_cast(dst_opt); + const int column = publish_key_index(key); + if (filament_options_with_variant.count(base_key) == 0 || column < 0) { + // Per-slot scalar copy: the receiver preset holds one value per key + // (vector of size 1), the file holds the per-slot vector. + if (author_slot < 0 || size_t(author_slot) >= src_vec->size()) { + skipped_keys.emplace_back("material:" + material_label + " (" + key + ")"); + continue; + } + dst_vec->set_at(src_opt, 0, author_slot); + material_applied = true; + continue; + } + // The file column must belong to this entry's author slot. + if (size_t(column) >= filament_self_indice.size() || variant_list == nullptr || + size_t(column) >= variant_list->size() || filament_self_indice[size_t(column)] - 1 != author_slot) { + skipped_keys.emplace_back("material:" + material_label + " (" + key + ", file column " + + std::to_string(column) + " does not belong to this slot)"); + continue; + } + if (size_t(column) >= src_vec->size()) { + skipped_keys.emplace_back("material:" + material_label + " (" + key + ", file column " + + std::to_string(column) + " is out of range)"); + continue; + } + const std::string &variant = variant_list->get_at(column); + const std::optional local = publish_match_variant(preset_config, std::nullopt, variant); + if (!local.has_value()) { + skipped_keys.emplace_back("material:" + material_label + " (" + key + ", variant \"" + variant + + "\" not in receiver preset)"); + continue; + } + dst_vec->set_at(src_opt, *local, column); material_applied = true; } }; @@ -7273,19 +7315,79 @@ void PresetBundle::load_config_file_config(const std::string &name_or_path, bool // Baseline: clone the receiver slot's stored preset config (schema- // complete across versions), then overlay the published full_keys. DynamicPrintConfig new_cfg = recv != nullptr ? recv->config : this->filaments.default_preset().config; + // A variant-bearing key stores one value per (slot, variant) in the + // file, so the copy takes every variant of the author's slot and the + // author's variant list for it. filament_self_index was moved out of + // `config` above; the local copy holds it. + const std::vector full_columns = entry.slot >= 0 ? + publish_filament_columns(filament_self_indice, size_t(entry.slot)) : + std::vector(); + bool copied_variant_key = false; for (const std::string& key : entry.full_keys) { const std::string base_key = publish_base_key(key); if (structural_keys.count(base_key) != 0) continue; - const ConfigOption* src_opt = config.option(base_key); - if (src_opt == nullptr || !src_opt->is_vector() || entry.slot < 0 || - entry.slot >= static_cast(static_cast(src_opt)->size())) + // Layout keys describe the file's variant columns; the block below + // writes the copy's variant list from the file. + if (base_key == "filament_extruder_variant" || base_key == "filament_self_index") continue; + const ConfigOption* src_opt = config.option(base_key); + if (src_opt == nullptr || !src_opt->is_vector()) { + // The file carries no value for this key: nothing to copy, not a + // value the receiver lost. + continue; + } ConfigOption* dst_opt = new_cfg.option(base_key); if (dst_opt == nullptr || !dst_opt->is_vector() || - static_cast(dst_opt)->empty() || dst_opt->type() != src_opt->type()) + static_cast(dst_opt)->empty() || dst_opt->type() != src_opt->type()) { + skipped_keys.emplace_back("material:" + material_label + " (" + base_key + ")"); continue; - static_cast(dst_opt)->set_at(src_opt, 0, entry.slot); + } + const auto *src_vec = dynamic_cast(src_opt); + auto *dst_vec = dynamic_cast(dst_opt); + if (filament_options_with_variant.count(base_key) != 0) { + // A file that carries the key short of the layout (a preset + // holding one default value, or a vector older than variant + // columns) publishes nothing for the slot's column: leave the + // copy's value alone instead of reporting the key as skipped. + if (full_columns.empty() || + std::any_of(full_columns.begin(), full_columns.end(), + [src_vec](size_t column) { return column >= src_vec->size(); })) { + BOOST_LOG_TRIVIAL(info) << "publish: not copying \"" << base_key << "\" for slot " + << entry.slot << " (the file has no value for its variant column)"; + continue; + } + dst_vec->resize(full_columns.size()); + for (size_t i = 0; i < full_columns.size(); ++i) + dst_vec->set_at(src_opt, i, full_columns[i]); + copied_variant_key = true; + } else { + if (entry.slot < 0 || size_t(entry.slot) >= src_vec->size()) { + // Nothing in the file for this slot; the copy keeps its own + // value. Only data that exists but cannot be applied is + // reported as skipped. + BOOST_LOG_TRIVIAL(info) << "publish: not copying \"" << base_key << "\" for slot " + << entry.slot << " (the file has no value for it)"; + continue; + } + dst_vec->set_at(src_opt, 0, entry.slot); + } + } + // The copy's variant list must describe the vectors written above, and + // only when at least one variant-bearing key was copied: rewriting it + // alone would make the copy claim variants its vectors do not hold. + if (copied_variant_key && !full_columns.empty()) { + const auto *src_variants = config.option("filament_extruder_variant"); + if (auto *dst_variants = new_cfg.option("filament_extruder_variant"); + src_variants != nullptr && dst_variants != nullptr) { + std::vector variants; + variants.reserve(full_columns.size()); + for (const size_t column : full_columns) + if (column < src_variants->size()) + variants.push_back(src_variants->get_at(column)); + if (variants.size() == full_columns.size()) + dst_variants->values = std::move(variants); + } } // The published colour is authoritative even for Full (the payload's // filament_colour plus the explicit publish_color field). diff --git a/src/libslic3r/PublishSettings.cpp b/src/libslic3r/PublishSettings.cpp index 7e6f3f142e..97a8097ec0 100644 --- a/src/libslic3r/PublishSettings.cpp +++ b/src/libslic3r/PublishSettings.cpp @@ -5,14 +5,20 @@ #include "Preset.hpp" #include "PrintConfig.hpp" #include "MaterialType.hpp" +#include "I18N.hpp" +#include #include #include +#include #include #include +#include #include #include +#include +#include #include namespace Slic3r { @@ -23,18 +29,16 @@ std::string publish_base_key(const std::string &key) return pos == std::string::npos ? key : key.substr(0, pos); } -// Parse the trailing "#N" variant index ("retraction_length#2" -> 2). Returns -1 when the key -// carries no '#' separator or its suffix is malformed; mirrors the importer's strict parse -// (PresetBundle.cpp) so the export side rejects the same variants the receiver would skip. -static int publish_variant_index(const std::string &key, const std::string &base_key) +// Parse the trailing "#N" index. Mirrors the importer's strict parse so the export side rejects +// the same variants a receiver would skip. +int publish_key_index(const std::string &key) { - if (key.size() <= base_key.size() || key.compare(0, base_key.size(), base_key) != 0 || key[base_key.size()] != '#') - return -1; - const std::string suffix = key.substr(base_key.size() + 1); - if (suffix.empty()) + const size_t pos = key.find('#'); + if (pos == std::string::npos || pos + 1 >= key.size()) return -1; int idx = 0; - for (const char c : suffix) { + for (size_t i = pos + 1; i < key.size(); ++i) { + const char c = key[i]; if (c < '0' || c > '9') return -1; idx = idx * 10 + (c - '0'); @@ -44,6 +48,179 @@ static int publish_variant_index(const std::string &key, const std::string &base return idx; } +static const ConfigOptionStrings *filament_variant_list(const DynamicPrintConfig &config) +{ + return config.option("filament_extruder_variant"); +} + +std::vector publish_filament_columns(const std::vector &self_index, size_t slot) +{ + std::vector columns; + for (size_t column = 0; column < self_index.size(); ++column) + if (self_index[column] == int(slot) + 1) + columns.push_back(column); + return columns; +} + +std::vector publish_filament_columns(const DynamicPrintConfig &uncompacted, size_t slot) +{ + const ConfigOptionInts *self_index = uncompacted.option("filament_self_index"); + if (self_index != nullptr) + return publish_filament_columns(self_index->values, slot); + // Hand-built config without the layout: one variant per slot, as the legacy loader assumes. + std::vector columns; + const ConfigOptionStrings *variants = filament_variant_list(uncompacted); + if (variants != nullptr && slot < variants->size()) + columns.push_back(slot); + return columns; +} + +std::optional publish_filament_local_to_file(const DynamicPrintConfig &uncompacted, size_t slot, size_t local) +{ + const std::vector columns = publish_filament_columns(uncompacted, slot); + if (local >= columns.size()) + return std::nullopt; + return columns[local]; +} + +std::optional publish_match_variant(const DynamicPrintConfig &config, std::optional slot, + std::string_view variant) +{ + const ConfigOptionStrings *variants = filament_variant_list(config); + if (variants == nullptr) + return std::nullopt; + if (slot.has_value()) { + for (const size_t column : publish_filament_columns(config, *slot)) + if (column < variants->size() && std::string_view(variants->get_at(column)) == variant) + return column; + return std::nullopt; + } + for (size_t local = 0; local < variants->size(); ++local) + if (std::string_view(variants->get_at(local)) == variant) + return local; + return std::nullopt; +} + +PublishResolvedColumn publish_resolved_column(const DynamicPrintConfig &uncompacted, size_t slot) +{ + PublishResolvedColumn out; + const std::vector columns = publish_filament_columns(uncompacted, slot); + if (columns.empty()) + return out; + + const ConfigOptionStrings *variants = filament_variant_list(uncompacted); + out.file_column = columns.front(); + if (variants != nullptr && columns.front() < variants->size()) + out.used_variant = variants->get_at(columns.front()); + + const ConfigOptionInts *filament_map = uncompacted.option("filament_map"); + const ConfigOptionInts *filament_volumes = uncompacted.option("filament_volume_map"); + const ConfigOptionEnumsGeneric *extruder_types = uncompacted.option("extruder_type"); + const ConfigOptionEnumsGeneric *nozzle_volumes = uncompacted.option("nozzle_volume_type"); + if (extruder_types == nullptr || nozzle_volumes == nullptr) + return out; + + // full_fff_config pads a short (or missing) map with 1 / Standard, so a one-element map on a + // multi-slot project still resolves every slot; mirror that instead of bailing out. + const int extruder = (filament_map != nullptr && !filament_map->empty() && slot < filament_map->size()) ? + filament_map->get_at(slot) : + 1; + if (extruder < 1 || size_t(extruder) > extruder_types->size() || size_t(extruder) > nozzle_volumes->size()) + return out; + + NozzleVolumeType filament_nozzle = nvtStandard; + // full_fff_config uses the volume map only when it is sized to the filament count; a partially + // sized map is stale state and means "Standard" for every slot. + const auto *colours = uncompacted.option("filament_colour"); + const auto *diameters = uncompacted.option("filament_diameter"); + const size_t filament_count = colours != nullptr ? colours->size() : (diameters != nullptr ? diameters->size() : 0); + if (filament_volumes != nullptr && filament_count > 0 && filament_volumes->size() == filament_count && + slot < filament_count) + filament_nozzle = NozzleVolumeType(filament_volumes->get_at(slot)); + // nvtHybrid is not a preset variant: get_index_for_extruder matches it as Standard. + if (filament_nozzle == nvtHybrid) + filament_nozzle = nvtStandard; + + // A hybrid extruder has no preset column of its own: the slot's own volume type selects it, + // exactly as full_fff_config resolves it. + const NozzleVolumeType printer_nozzle = NozzleVolumeType(nozzle_volumes->get_at(extruder - 1)); + const NozzleVolumeType nozzle = printer_nozzle == nvtHybrid ? filament_nozzle : printer_nozzle; + out.required_variant = get_extruder_variant_string(ExtruderType(extruder_types->get_at(extruder - 1)), nozzle); + if (out.required_variant.empty()) + return out; + + if (const std::optional exact = publish_match_variant(uncompacted, slot, out.required_variant); exact.has_value()) { + out.file_column = *exact; + out.exact = true; + out.used_variant = out.required_variant; + } + return out; +} + +void publish_normalize_material_entries(std::vector &entries, + const DynamicPrintConfig &uncompacted, + std::vector *notices) +{ + const std::set &mixed_keys = publish_mixed_keys(); + for (PublishedMaterialEntry &entry : entries) { + if (entry.slot < 0 || entry.full) + continue; + // A mixed definition is a project-level per-slot array: no variant-bearing keys. + bool is_mixed = false; + for (const std::string &key : entry.keys) + if (mixed_keys.count(publish_base_key(key)) != 0) { + is_mixed = true; + break; + } + if (is_mixed) + continue; + + const PublishResolvedColumn resolved = publish_resolved_column(uncompacted, size_t(entry.slot)); + const std::vector columns = publish_filament_columns(uncompacted, size_t(entry.slot)); + // No layout for this slot (legacy identity config): leave the keys untouched and let the + // filter's slot-indexed fallback mask them. + if (columns.empty()) + continue; + if (!resolved.exact && notices != nullptr) { + // One notice per partial slot that has variant-bearing keys to write. + const bool has_variant_key = std::any_of(entry.keys.begin(), entry.keys.end(), [](const std::string &key) { + return filament_options_with_variant.count(publish_base_key(key)) != 0; + }); + if (has_variant_key && resolved.file_column.has_value()) + notices->emplace_back((boost::format(L("Slot %1%: the printer needs the \"%2%\" variant, which the filament does not " + "define. Publishing the values printed with \"%3%\".")) % + (entry.slot + 1) % resolved.required_variant % resolved.used_variant) + .str()); + } + + auto in_slot = [&columns](int column) { + return column >= 0 && std::find(columns.begin(), columns.end(), size_t(column)) != columns.end(); + }; + std::vector keys; + keys.reserve(entry.keys.size()); + std::set seen; + for (const std::string &key : entry.keys) { + const std::string base = publish_base_key(key); + std::string normalized = key; + if (filament_options_with_variant.count(base) != 0) { + if (const int column = publish_key_index(key); column >= 0) { + // A caller-supplied column must address this slot; never ship another slot's value. + if (!in_slot(column)) { + BOOST_LOG_TRIVIAL(warning) << "publish: dropping \"" << key << "\" from slot " << (entry.slot + 1) + << " (the column belongs to another slot or is out of range)"; + continue; + } + } else { + normalized = base + "#" + std::to_string(*resolved.file_column); + } + } + if (seen.insert(normalized).second) + keys.push_back(std::move(normalized)); + } + entry.keys = std::move(keys); + } +} + std::string normalize_filament_type(const std::string& type) { if (type.empty()) @@ -202,10 +379,16 @@ DynamicPrintConfig filter_published_config( // Never masked (whole-vector serialization): identity, plate geometry, process keys and // printer keys without a "#N" variant. std::set mask_exempt_keys; - // Material entries: base key -> author slots whose values must survive; other slots are - // masked to their defaults so a publish (partial or full) does not leak unrelated slot - // data. + // Material entries: base key -> author slots/columns whose values must survive; other + // elements are masked to their defaults so a publish (partial or full) does not leak + // unrelated slot data. std::map> slot_mask_map; + // Base keys whose mask comes from a material entry: an out-of-range index drops the key. + // Printer and process keys keep their existing behaviour (whole vector or variant mask). + std::set material_masked_keys; + // Material keys masked by file column (not by slot): the flattened vector must be as long as + // the file's variant layout before any column can be trusted. + std::set column_masked_keys; // 1. Mandatory material identity & slot count keys for 3MF validation/normalization // (filament_ids: exported for validation, denylisted on apply - see publish_structural_keys). @@ -244,7 +427,7 @@ DynamicPrintConfig filter_published_config( continue; base_keys_to_include.insert(base_key); if (printer_keys.count(base_key) != 0 || print_options_with_variant.count(base_key) != 0) { - const int variant_idx = publish_variant_index(key, base_key); + const int variant_idx = publish_key_index(key); if (variant_idx >= 0) slot_mask_map[base_key].insert(variant_idx); else @@ -254,26 +437,95 @@ DynamicPrintConfig filter_published_config( } } - // 4. Material-specific published keys. Both partial (entry.keys) and full-publish - // (entry.full_keys) entries mask to the author's slot on export (see the copy loop below); - // slot-less entries (hand-crafted files) stay unmasked (whole vector). + // 4. Material-specific published keys. A variant-bearing key (filament_options_with_variant) + // addresses a file column ("key#col", written by publish_normalize_material_entries); every + // other filament vector key is addressed by slot. Full-publish entries mask to every column of + // the author's slot. Slot-less entries (hand-crafted files) stay unmasked (whole vector). + // + // A config whose filament_self_index is still the unexpanded identity layout (one variant per + // slot, e.g. a hand-built config or an old fixture) has no columns to resolve; there the slot + // index is the safe mask, as it was before variant columns existed. A real column layout with + // no column for the slot cannot be masked safely, so the key is dropped. + const auto *self_index = full_config.option("filament_self_index"); + const bool identity_layout = self_index == nullptr || self_index->empty() || + std::all_of(self_index->values.begin(), self_index->values.end(), + [index = 0](int id) mutable { return id == ++index; }); + auto add_material_key = [&](const PublishedMaterialEntry &entry, const std::string &key) { + const std::string base_key = publish_base_key(key); + if (base_key.empty()) + return; + if (entry.slot < 0) { + base_keys_to_include.insert(base_key); // hand-crafted, slot-less: whole vector + return; + } + if (filament_options_with_variant.count(base_key) == 0) { + base_keys_to_include.insert(base_key); + slot_mask_map[base_key].insert(entry.slot); + material_masked_keys.insert(base_key); + return; + } + + const std::vector columns = publish_filament_columns(full_config, size_t(entry.slot)); + const int supplied = publish_key_index(key); + if (supplied >= 0) { + // A caller-supplied column must address this slot; never ship another slot's value. + const bool valid = columns.empty() ? supplied == entry.slot : + std::find(columns.begin(), columns.end(), size_t(supplied)) != columns.end(); + if (!valid) { + BOOST_LOG_TRIVIAL(warning) << "publish: dropping material key \"" << key << "\" of slot " << (entry.slot + 1) + << " (the column belongs to another slot or is out of range)"; + return; + } + } + if (columns.empty() && !identity_layout) { + BOOST_LOG_TRIVIAL(warning) << "publish: dropping material key \"" << key << "\" of slot " << (entry.slot + 1) + << " (the file layout has no variant column for this slot)"; + return; + } + base_keys_to_include.insert(base_key); + + if (entry.full) { + if (columns.empty()) { + // Identity layout: one column per slot, so the slot index is the column. + slot_mask_map[base_key].insert(entry.slot); + material_masked_keys.insert(base_key); + } else { + for (const size_t column : columns) + slot_mask_map[base_key].insert(int(column)); + material_masked_keys.insert(base_key); + column_masked_keys.insert(base_key); + } + return; + } + if (supplied >= 0) { + if (columns.empty()) { + slot_mask_map[base_key].insert(entry.slot); + material_masked_keys.insert(base_key); + } else { + slot_mask_map[base_key].insert(supplied); + material_masked_keys.insert(base_key); + column_masked_keys.insert(base_key); + } + return; + } + // Plain variant-bearing key (a selection from before variant-aware publishing): keep the + // column the slot prints with, so the published value is the one the author printed. + const PublishResolvedColumn resolved = publish_resolved_column(full_config, size_t(entry.slot)); + if (resolved.file_column.has_value() && !columns.empty()) { + slot_mask_map[base_key].insert(int(*resolved.file_column)); + material_masked_keys.insert(base_key); + column_masked_keys.insert(base_key); + } else { + // Identity layout: keep the old slot-indexed masking. + slot_mask_map[base_key].insert(entry.slot); + material_masked_keys.insert(base_key); + } + }; for (const PublishedMaterialEntry &entry : material_keys) { - for (const std::string &key : entry.keys) { - const std::string base_key = publish_base_key(key); - if (base_key.empty()) - continue; - base_keys_to_include.insert(base_key); - if (entry.slot >= 0) - slot_mask_map[base_key].insert(entry.slot); - } - for (const std::string &key : entry.full_keys) { - const std::string base_key = publish_base_key(key); - if (base_key.empty()) - continue; - base_keys_to_include.insert(base_key); - if (entry.slot >= 0) - slot_mask_map[base_key].insert(entry.slot); - } + for (const std::string &key : entry.keys) + add_material_key(entry, key); + for (const std::string &key : entry.full_keys) + add_material_key(entry, key); } // Masking restores every non-published slot of a vector option with the option default, so @@ -307,6 +559,33 @@ DynamicPrintConfig filter_published_config( << "\" from the published payload (no usable option default)"; continue; } + if (needs_masking && column_masked_keys.count(key) != 0) { + // The flattened vector must be as long as the file's variant layout. A preset whose + // variant vector is shorter shifts every later column, so a column that looks in range + // can hold another slot's value: drop the key instead. + const auto *self_index = full_config.option("filament_self_index"); + const auto *variants = full_config.option("filament_extruder_variant"); + const size_t layout_sz = self_index != nullptr ? self_index->size() : + (variants != nullptr ? variants->size() : size_t(0)); + const auto *vec = dynamic_cast(opt); + if (layout_sz > 0 && vec->size() != layout_sz) { + BOOST_LOG_TRIVIAL(warning) << "publish: dropping key \"" << key + << "\" from the published payload (its variant vector does not match the file layout)"; + continue; + } + } + if (needs_masking && material_masked_keys.count(key) != 0) { + // A selected column/slot the author's vector does not have carries no value: drop the + // key rather than shipping the option default as if it were the author's setting. + const auto *vec = dynamic_cast(opt); + const bool in_range = std::all_of(mask_it->second.begin(), mask_it->second.end(), + [vec](int index) { return index >= 0 && size_t(index) < vec->size(); }); + if (!in_range) { + BOOST_LOG_TRIVIAL(warning) << "publish: dropping key \"" << key + << "\" from the published payload (selected variant column is out of range)"; + continue; + } + } ConfigOption *cloned = opt->clone(); if (needs_masking) mask_slots(*cloned, print_config_def.get(key), mask_it->second); diff --git a/src/libslic3r/PublishSettings.hpp b/src/libslic3r/PublishSettings.hpp index b8e9d1d45b..1f483aa966 100644 --- a/src/libslic3r/PublishSettings.hpp +++ b/src/libslic3r/PublishSettings.hpp @@ -1,15 +1,22 @@ #pragma once #include +#include #include #include +#include #include namespace Slic3r { class PresetBundle; +class DynamicPrintConfig; // Strip a trailing "#N" variant suffix ("retraction_length#2" -> "retraction_length"). std::string publish_base_key(const std::string &key); +// Parse a trailing "#N" index ("retraction_length#2" -> 2). Returns -1 when the key carries no +// '#' separator or its suffix is malformed. +int publish_key_index(const std::string &key); + // Structural keys that are never applied onto the receiver's presets when loading a published // 3MF (single source of truth for the denylist); applying them would rewrite the user's preset // inheritance/structure. filament_ids is still exported via the identity list (3MF validation @@ -90,7 +97,43 @@ struct PublishedExtruderLayout // "PLA High Speed" -> "PLA" (strip a space modifier); dash types like "PA-CF" are kept intact. std::string normalize_filament_type(const std::string& type); -class DynamicPrintConfig; +// File columns of one filament slot in the uncompacted config, from filament_self_index. A +// variant-bearing key (filament_options_with_variant) stores one element per (slot, variant), and +// those elements are concatenated across slots in preset order, so the element index is a file +// column rather than a slot. A config without filament_self_index falls back to one column at +// `slot`. The overload taking the index vector serves callers whose config no longer carries it. +std::vector publish_filament_columns(const std::vector &self_index, size_t slot); +std::vector publish_filament_columns(const DynamicPrintConfig &uncompacted, size_t slot); + +// The file column of a local variant index (a position in the slot's own variant list). +std::optional publish_filament_local_to_file(const DynamicPrintConfig &uncompacted, size_t slot, size_t local); + +// Exact variant-string match. With `slot`, `config` is the uncompacted config, the search is +// limited to that slot's columns, and the result is a file column. Without `slot`, `config` is one +// preset and the result is a local index. No fallback to another variant. +std::optional publish_match_variant(const DynamicPrintConfig &config, std::optional slot, + std::string_view variant); + +// The column the slot prints with. `uncompacted` is a full config (full_config(false)): it carries +// filament_map, filament_volume_map, nozzle_volume_type, extruder_type, filament_self_index and the +// concatenated filament variants. exact is false when the printer's variant is not defined by the +// preset; file_column is then the slot's first column, which is the value the slicer prints. +struct PublishResolvedColumn +{ + std::optional file_column; + bool exact{false}; + std::string required_variant; // the variant the printer needs + std::string used_variant; // the variant file_column belongs to +}; +PublishResolvedColumn publish_resolved_column(const DynamicPrintConfig &uncompacted, size_t slot); + +// Rewrite variant-bearing material keys to "base#file_column" for the column the slot prints with, +// so a receiver can address the variant. Full entries keep their base keys (they carry every +// variant). Adds one notice per slot whose printer variant the preset does not define. +void publish_normalize_material_entries(std::vector &entries, + const DynamicPrintConfig &uncompacted, + std::vector *notices = nullptr); + // Clear the compatibility lists/conditions on a filament config so it is universally // compatible once detached (empty lists + empty conditions = compatible with everything). void make_publish_universal(DynamicPrintConfig &config); diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 8c0474b4ba..79de5bb138 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -19794,8 +19794,26 @@ int Plater::export_published_3mf(const std::vector& published_keys, nlohmann::json j = nlohmann::json::array(); for (const std::string& key : published_keys) j.push_back(key); + // A variant-bearing material key names a file column ("key#col"). Normalize the dialog's + // selection to the column each slot prints with before the metadata and the payload are built, + // so the receiver can put the value back on the same variant. + std::vector material_entries = material_keys; + std::vector publish_notices; + DynamicPrintConfig full_cfg; + try { + full_cfg = wxGetApp().preset_bundle->full_config_secure(); + Slic3r::publish_normalize_material_entries(material_entries, full_cfg, &publish_notices); + } catch (...) { + MessageDialog(this, _L("Failed to export the published 3MF file.\nPlease check whether the folder exists online or if other programs " + "have the file open."), + _L("Publish"), wxOK | wxICON_WARNING) + .ShowModal(); + return wxID_CANCEL; + } + for (const std::string& notice : publish_notices) + BOOST_LOG_TRIVIAL(warning) << "publish: " << notice; nlohmann::json jm = nlohmann::json::array(); - for (const Slic3r::PublishedMaterialEntry& e : material_keys) + for (const Slic3r::PublishedMaterialEntry& e : material_entries) jm.push_back({{"material", {{"filament_type", e.filament_type}, {"filament_vendor", e.filament_vendor}, @@ -19880,8 +19898,7 @@ int Plater::export_published_3mf(const std::vector& published_keys, // filtered values are serialized into the published_config metadata payload instead, so // OrcaSlicer versions without the publish feature fall back to importing the geometry only // (keeping the receiver's presets) while new versions rebuild the config from the payload. - DynamicPrintConfig full_cfg = wxGetApp().preset_bundle->full_config_secure(); - DynamicPrintConfig filtered_cfg = filter_published_config(full_cfg, published_keys, material_keys); + DynamicPrintConfig filtered_cfg = filter_published_config(full_cfg, published_keys, material_entries); std::string payload; for (const std::string& key : filtered_cfg.keys()) { // A value containing a newline would break the INI written below (read_ini throws), diff --git a/tests/libslic3r/CMakeLists.txt b/tests/libslic3r/CMakeLists.txt index f090ee850d..df7324ca1d 100644 --- a/tests/libslic3r/CMakeLists.txt +++ b/tests/libslic3r/CMakeLists.txt @@ -36,6 +36,7 @@ add_executable(${_TEST_NAME}_tests test_preset_bundle_loading.cpp test_preset_setting_id.cpp test_preset_diff.cpp + test_publish_settings.cpp test_vendor_cache.cpp test_preset_options.cpp test_edge_grid.cpp diff --git a/tests/libslic3r/test_publish_settings.cpp b/tests/libslic3r/test_publish_settings.cpp new file mode 100644 index 0000000000..2bdb1289c7 --- /dev/null +++ b/tests/libslic3r/test_publish_settings.cpp @@ -0,0 +1,500 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "catch2/matchers/catch_matchers.hpp" +#include "libslic3r/Config.hpp" +#include "libslic3r/Preset.hpp" +#include "libslic3r/PresetBundle.hpp" +#include "libslic3r/PrintConfig.hpp" +#include "libslic3r/PublishSettings.hpp" + +using namespace Slic3r; + +namespace { + +void check_double_vector(const std::vector &actual, std::initializer_list expected) +{ + REQUIRE(actual.size() == expected.size()); + size_t index = 0; + for (double value : expected) + REQUIRE_THAT(actual[index++], Catch::Matchers::WithinAbs(value, 1e-6)); +} + +Preset &add_inmemory_preset(PresetCollection &coll, const std::string &name) +{ + DynamicPrintConfig config(coll.default_preset().config); + config.option(BBL_JSON_KEY_INHERITS, true)->value = std::string(); + return coll.load_preset(std::string(), name, config, /*select=*/false); +} + +// One slot with two variants (Standard, High Flow) and one slot with one variant, as +// full_config(false) lays them out: variant-bearing keys carry one element per (slot, variant) +// concatenated in slot order, every other filament key carries one element per slot. +DynamicPrintConfig make_variant_config() +{ + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.option("filament_diameter", true)->values = { 1.75, 1.75 }; + config.option("filament_colour", true)->values = { "#FF0000", "#00FF00" }; + config.option("filament_self_index", true)->values = { 1, 1, 2 }; + config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard", + "Direct Drive High Flow", + "Direct Drive Standard" }; + config.option("filament_retraction_length", true)->values = { 0.9, 1.0, 1.2 }; + config.option("filament_cost", true)->values = { 20.0, 30.0 }; + config.option("filament_map", true)->values = { 1, 1 }; + config.option("filament_volume_map", true)->values = { int(nvtStandard), int(nvtStandard) }; + config.option("nozzle_volume_type", true)->values = { int(nvtStandard) }; + config.option("extruder_type", true)->values = { int(etDirectDrive) }; + return config; +} + +} // namespace + +TEST_CASE("Slot columns follow filament_self_index for uneven variant counts", "[PublishSettings]") +{ + DynamicPrintConfig config; + config.set_key_value("filament_self_index", new ConfigOptionInts({ 1, 1, 2, 3, 3, 3 })); + config.set_key_value("filament_extruder_variant", + new ConfigOptionStrings({ "A", "B", "C", "D", "E", "F" })); + + CHECK(publish_filament_columns(config, 0) == std::vector{ 0, 1 }); + CHECK(publish_filament_columns(config, 1) == std::vector{ 2 }); + CHECK(publish_filament_columns(config, 2) == std::vector{ 3, 4, 5 }); + CHECK(publish_filament_columns(config, 3).empty()); + + for (size_t slot = 0; slot < 3; ++slot) { + const std::vector columns = publish_filament_columns(config, slot); + for (size_t local = 0; local < columns.size(); ++local) + CHECK(publish_filament_local_to_file(config, slot, local) == std::optional(columns[local])); + } + CHECK_FALSE(publish_filament_local_to_file(config, 1, 1).has_value()); +} + +TEST_CASE("Variant matching is exact and never falls back to another variant", "[PublishSettings]") +{ + DynamicPrintConfig config; + config.set_key_value("filament_self_index", new ConfigOptionInts({ 1, 1 })); + config.set_key_value("filament_extruder_variant", + new ConfigOptionStrings({ "Direct Drive Standard", "Direct Drive High Flow" })); + + CHECK(publish_match_variant(config, std::nullopt, "Direct Drive High Flow") == std::optional(1)); + CHECK(publish_match_variant(config, size_t(0), "Direct Drive High Flow") == std::optional(1)); + CHECK(publish_match_variant(config, std::nullopt, "Direct Drive Extra High Flow") == std::nullopt); + CHECK(publish_match_variant(config, size_t(0), "Bowden Standard") == std::nullopt); +} + +TEST_CASE("Publish key parsing rejects a missing or malformed suffix", "[PublishSettings]") +{ + CHECK(publish_key_index("retraction_length#2") == 2); + CHECK(publish_key_index("retraction_length#0") == 0); + CHECK(publish_key_index("retraction_length") == -1); + CHECK(publish_key_index("retraction_length#") == -1); + CHECK(publish_key_index("retraction_length#abc") == -1); + CHECK(publish_key_index("retraction_length#2x") == -1); + CHECK(publish_base_key("retraction_length#2") == "retraction_length"); +} + +TEST_CASE("Resolved column reports a missing variant as inexact and returns the first element", "[PublishSettings]") +{ + DynamicPrintConfig config = make_variant_config(); + + SECTION("the printer's variant is defined by the preset") { + const PublishResolvedColumn resolved = publish_resolved_column(config, 0); + CHECK(resolved.exact); + CHECK(resolved.file_column == std::optional(0)); + CHECK(resolved.required_variant == "Direct Drive Standard"); + CHECK(resolved.used_variant == "Direct Drive Standard"); + } + + SECTION("the printer needs a variant the preset does not define") { + config.option("nozzle_volume_type", true)->values = { int(nvtHighFlow) }; + // Slot 0 defines Standard and High Flow, so it resolves exactly. + CHECK(publish_resolved_column(config, 0).exact); + CHECK(publish_resolved_column(config, 0).file_column == std::optional(1)); + + // Slot 1 only defines Standard. + const PublishResolvedColumn resolved = publish_resolved_column(config, 1); + CHECK_FALSE(resolved.exact); + CHECK(resolved.file_column == std::optional(2)); + CHECK(resolved.required_variant == "Direct Drive High Flow"); + CHECK(resolved.used_variant == "Direct Drive Standard"); + } + + SECTION("a hybrid extruder resolves through the slot's own volume type") { + config.option("nozzle_volume_type", true)->values = { int(nvtHybrid) }; + config.option("filament_volume_map", true)->values = { int(nvtStandard), int(nvtStandard) }; + const PublishResolvedColumn resolved = publish_resolved_column(config, 0); + CHECK(resolved.exact); + CHECK(resolved.file_column == std::optional(0)); + + config.option("filament_volume_map", true)->values = { int(nvtHighFlow), int(nvtStandard) }; + const PublishResolvedColumn high_flow = publish_resolved_column(config, 0); + CHECK(high_flow.exact); + CHECK(high_flow.file_column == std::optional(1)); + } +} + +TEST_CASE("A variant-bearing material key keeps only that slot's own column", "[PublishSettings]") +{ + DynamicPrintConfig config = make_variant_config(); + + std::vector entries(1); + entries[0].slot = 1; // the one-variant slot after the two-variant slot + entries[0].keys = { "filament_retraction_length", "filament_cost" }; + + std::vector notices; + publish_normalize_material_entries(entries, config, ¬ices); + CHECK(notices.empty()); + REQUIRE(entries[0].keys.size() == 2); + // The variant-bearing key now names its file column; the per-slot key stays plain. + CHECK(entries[0].keys[0] == "filament_retraction_length#2"); + CHECK(entries[0].keys[1] == "filament_cost"); + + const DynamicPrintConfig filtered = filter_published_config(config, {}, entries); + + // The slot's own column is kept; the other slots' columns are masked to the option default. + const auto *retraction = filtered.opt("filament_retraction_length"); + REQUIRE(retraction != nullptr); + REQUIRE(retraction->size() == 3); + REQUIRE_THAT(retraction->get_at(2), Catch::Matchers::WithinAbs(1.2, 1e-6)); + const auto *retraction_default = + dynamic_cast(print_config_def.get("filament_retraction_length")->default_value.get()); + REQUIRE(retraction_default != nullptr); + REQUIRE_THAT(retraction->get_at(0), Catch::Matchers::WithinAbs(retraction_default->get_at(0), 1e-6)); + REQUIRE_THAT(retraction->get_at(1), Catch::Matchers::WithinAbs(retraction_default->get_at(0), 1e-6)); + + // The per-slot key keeps the slot's value and masks the other slot. + const auto *cost = filtered.opt("filament_cost"); + REQUIRE(cost != nullptr); + REQUIRE(cost->size() == 2); + REQUIRE_THAT(cost->get_at(1), Catch::Matchers::WithinAbs(30.0, 1e-6)); + const auto *cost_default = dynamic_cast(print_config_def.get("filament_cost")->default_value.get()); + REQUIRE(cost_default != nullptr); + REQUIRE_THAT(cost->get_at(0), Catch::Matchers::WithinAbs(cost_default->get_at(0), 1e-6)); +} + +TEST_CASE("A variant-bearing material key whose column is out of range is dropped", "[PublishSettings]") +{ + DynamicPrintConfig config = make_variant_config(); + + std::vector entries(1); + entries[0].slot = 0; + entries[0].keys = { "filament_retraction_length#4" }; + + const DynamicPrintConfig filtered = filter_published_config(config, {}, entries); + CHECK(filtered.option("filament_retraction_length") == nullptr); +} + +TEST_CASE("A supplied material column belonging to another slot is dropped on export", "[PublishSettings]") +{ + DynamicPrintConfig config = make_variant_config(); + + std::vector entries(1); + entries[0].slot = 0; + entries[0].keys = { "filament_retraction_length#2" }; // column 2 belongs to slot 1 + + const DynamicPrintConfig filtered = filter_published_config(config, {}, entries); + CHECK(filtered.option("filament_retraction_length") == nullptr); +} + +TEST_CASE("A variant vector shorter than the file layout is dropped instead of shifting columns", "[PublishSettings]") +{ + DynamicPrintConfig config = make_variant_config(); + // Slot 0 has two columns, slot 1 one, but the flattened vector carries only two values: every + // column past index 1 belongs to another slot's value, so no column can be trusted. + config.option("filament_retraction_length", true)->values = { 0.9, 1.0 }; + + std::vector entries(1); + entries[0].slot = 0; + entries[0].keys = { "filament_retraction_length#1" }; + + const DynamicPrintConfig filtered = filter_published_config(config, {}, entries); + CHECK(filtered.option("filament_retraction_length") == nullptr); +} + +TEST_CASE("A variant vector longer than the file layout is dropped instead of shifting columns", "[PublishSettings]") +{ + DynamicPrintConfig config = make_variant_config(); + // One value too many: every column after the extra value belongs to another slot. + config.option("filament_retraction_length", true)->values = { 0.9, 1.0, 1.2, 1.3 }; + + std::vector entries(1); + entries[0].slot = 0; + entries[0].keys = { "filament_retraction_length#0" }; + + const DynamicPrintConfig filtered = filter_published_config(config, {}, entries); + CHECK(filtered.option("filament_retraction_length") == nullptr); +} + +TEST_CASE("A slot with no exact variant match publishes the value it prints with", "[PublishSettings]") +{ + DynamicPrintConfig config = make_variant_config(); + config.option("filament_extruder_variant", true)->values = { "Bowden Standard", "Bowden Standard", + "Bowden Standard" }; + config.option("nozzle_volume_type", true)->values = { int(nvtStandard) }; + + std::vector entries(1); + entries[0].slot = 0; + entries[0].keys = { "filament_retraction_length" }; + + std::vector notices; + publish_normalize_material_entries(entries, config, ¬ices); + REQUIRE(entries[0].keys.size() == 1); + // The variant the printer needs is missing: publish the first column, and say so. + CHECK(entries[0].keys[0] == "filament_retraction_length#0"); + REQUIRE(notices.size() == 1); + CHECK(notices[0].find("Direct Drive Standard") != std::string::npos); + CHECK(notices[0].find("Bowden Standard") != std::string::npos); + + const DynamicPrintConfig filtered = filter_published_config(config, {}, entries); + const auto *retraction = filtered.opt("filament_retraction_length"); + REQUIRE(retraction != nullptr); + REQUIRE_THAT(retraction->get_at(0), Catch::Matchers::WithinAbs(0.9, 1e-6)); +} + +TEST_CASE("Published material keys name their file column and import onto that variant", "[PublishSettings]") +{ + // The author's file: one slot with two variants. Column 1 is the High Flow column. + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.option("filament_diameter", true)->values = { 1.75 }; + config.option("filament_colour", true)->values = { "#FF0000" }; + config.option("filament_self_index", true)->values = { 1, 1 }; + config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard", + "Direct Drive High Flow" }; + config.option("filament_type", true)->values = { "PLA" }; + config.option("filament_retraction_length", true)->values = { 0.9, 1.2 }; + Preset::normalize(config); + + PresetBundle bundle; + Preset &pla = add_inmemory_preset(bundle.filaments, "My PLA"); + pla.config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard", + "Direct Drive High Flow" }; + pla.config.option("filament_retraction_length", true)->values = { 0.5, 0.6 }; + bundle.filament_presets = { "My PLA" }; + + PublishedMaterialEntry entry; + entry.slot = 0; + entry.keys = { "filament_retraction_length#1" }; + + PublishedConfig pub; + pub.published = true; + pub.material_keys = { entry }; + bundle.load_config_model("test.3mf", std::move(config), Semver(), &pub); + + // Variant 1 (High Flow) takes the published value; variant 0 is untouched. + check_double_vector(bundle.filaments.find_preset("My PLA")->config.opt("filament_retraction_length")->values, + { 0.5, 1.2 }); + CHECK(pub.skipped_keys.empty()); +} + +TEST_CASE("A published variant the receiver lacks is reported and not written", "[PublishSettings]") +{ + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.option("filament_diameter", true)->values = { 1.75 }; + config.option("filament_colour", true)->values = { "#FF0000" }; + config.option("filament_self_index", true)->values = { 1, 1 }; + config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard", + "Direct Drive High Flow" }; + config.option("filament_retraction_length", true)->values = { 0.9, 1.2 }; + Preset::normalize(config); + + PresetBundle bundle; + Preset &pla = add_inmemory_preset(bundle.filaments, "My PLA"); + // The receiver defines only the Standard variant. + pla.config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard" }; + pla.config.option("filament_retraction_length", true)->values = { 0.5 }; + bundle.filament_presets = { "My PLA" }; + + PublishedMaterialEntry entry; + entry.slot = 0; + entry.keys = { "filament_retraction_length#1" }; + + PublishedConfig pub; + pub.published = true; + pub.material_keys = { entry }; + bundle.load_config_model("test.3mf", std::move(config), Semver(), &pub); + + check_double_vector(bundle.filaments.find_preset("My PLA")->config.opt("filament_retraction_length")->values, + { 0.5 }); + REQUIRE(pub.skipped_keys.size() == 1); + CHECK(pub.skipped_keys.front().find("Direct Drive High Flow") != std::string::npos); +} + +TEST_CASE("A published column belonging to another slot is skipped and reported", "[PublishSettings]") +{ + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.option("filament_diameter", true)->values = { 1.75, 1.75 }; + config.option("filament_colour", true)->values = { "#FF0000", "#00FF00" }; + config.option("filament_self_index", true)->values = { 1, 2 }; + config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard", + "Direct Drive Standard" }; + config.option("filament_retraction_length", true)->values = { 0.9, 1.2 }; + Preset::normalize(config); + + PresetBundle bundle; + Preset &pla = add_inmemory_preset(bundle.filaments, "My PLA"); + pla.config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard" }; + pla.config.option("filament_retraction_length", true)->values = { 0.5 }; + bundle.filament_presets = { "My PLA", "My PLA" }; + + PublishedMaterialEntry entry; + entry.slot = 0; // names slot 0, but column 1 belongs to slot 1 + entry.keys = { "filament_retraction_length#1" }; + + PublishedConfig pub; + pub.published = true; + pub.material_keys = { entry }; + bundle.load_config_model("test.3mf", std::move(config), Semver(), &pub); + + check_double_vector(bundle.filaments.find_preset("My PLA")->config.opt("filament_retraction_length")->values, + { 0.5 }); + REQUIRE(pub.skipped_keys.size() == 1); + CHECK(pub.skipped_keys.front().find("does not belong") != std::string::npos); +} + +TEST_CASE("A published file without file columns imports positionally", "[PublishSettings]") +{ + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.option("filament_diameter", true)->values = { 1.75, 1.75 }; + config.option("filament_colour", true)->values = { "#FF0000", "#00FF00" }; + config.option("filament_self_index", true)->values = { 1, 2 }; + config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard", + "Direct Drive Standard" }; + config.option("filament_retraction_length", true)->values = { 0.9, 1.2 }; + Preset::normalize(config); + + PresetBundle bundle; + Preset &pla = add_inmemory_preset(bundle.filaments, "My PLA"); + pla.config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard" }; + pla.config.option("filament_retraction_length", true)->values = { 0.5 }; + bundle.filament_presets = { "My PLA", "My PLA" }; + + PublishedMaterialEntry entry; + entry.slot = 1; + entry.keys = { "filament_retraction_length" }; + + PublishedConfig pub; + pub.published = true; + pub.material_keys = { entry }; + bundle.load_config_model("test.3mf", std::move(config), Semver(), &pub); + + check_double_vector(bundle.filaments.find_preset("My PLA")->config.opt("filament_retraction_length")->values, + { 1.2 }); + CHECK(pub.skipped_keys.empty()); +} + +TEST_CASE("Full Publish copies every variant of the author's slot", "[PublishSettings]") +{ + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.option("filament_diameter", true)->values = { 1.75 }; + config.option("filament_colour", true)->values = { "#FF0000" }; + config.option("filament_self_index", true)->values = { 1, 1 }; + config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard", + "Direct Drive High Flow" }; + config.option("filament_type", true)->values = { "PLA" }; + config.option("filament_retraction_length", true)->values = { 0.9, 1.2 }; + Preset::normalize(config); + + PresetBundle bundle; + Preset &pla = add_inmemory_preset(bundle.filaments, "My PLA"); + pla.config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard" }; + pla.config.option("filament_retraction_length", true)->values = { 0.5 }; + bundle.filament_presets = { "My PLA" }; + + PublishedMaterialEntry entry; + entry.slot = 0; + entry.full = true; + entry.full_keys = { "filament_retraction_length" }; + entry.preset_name = "Author PLA"; + + PublishedConfig pub; + pub.published = true; + pub.material_keys = { entry }; + bundle.load_config_model("test.3mf", std::move(config), Semver(), &pub); + + // The slot is re-pointed to a standalone copy carrying the author's two variants. + REQUIRE(bundle.filament_presets.size() == 1); + const Preset *copy = bundle.filaments.find_preset(bundle.filament_presets.front(), false, true); + REQUIRE(copy != nullptr); + CHECK(copy->config.opt("filament_extruder_variant")->values == + std::vector{ "Direct Drive Standard", "Direct Drive High Flow" }); + check_double_vector(copy->config.opt("filament_retraction_length")->values, { 0.9, 1.2 }); +} + +TEST_CASE("A per-slot filament key after a multi-variant slot keeps its own slot's value", "[PublishSettings]") +{ + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.option("filament_diameter", true)->values = { 1.75, 1.75 }; + config.option("filament_colour", true)->values = { "#FF0000", "#00FF00" }; + config.option("filament_self_index", true)->values = { 1, 1, 2 }; + config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard", + "Direct Drive High Flow", + "Direct Drive Standard" }; + config.option("filament_cost", true)->values = { 20.0, 30.0 }; + Preset::normalize(config); + + PresetBundle bundle; + Preset &pla = add_inmemory_preset(bundle.filaments, "My PLA"); + pla.config.option("filament_cost", true)->values = { 1.0 }; + bundle.filament_presets = { "My PLA", "My PLA" }; + + PublishedMaterialEntry entry; + entry.slot = 1; + entry.keys = { "filament_cost" }; + + PublishedConfig pub; + pub.published = true; + pub.material_keys = { entry }; + bundle.load_config_model("test.3mf", std::move(config), Semver(), &pub); + + check_double_vector(bundle.filaments.find_preset("My PLA")->config.opt("filament_cost")->values, { 30.0 }); + CHECK(pub.skipped_keys.empty()); +} + +TEST_CASE("Full Publish leaves the receiver's value when the file carries a variant key short of the layout", "[PublishSettings]") +{ + // A file that never laid a variant-bearing key out per variant carries one value for the whole + // slot list, so it publishes nothing for a slot past the first column: the copy keeps the + // receiver's value and the key is not reported as skipped. + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.option("filament_diameter", true)->values = { 1.75, 1.75 }; + config.option("filament_colour", true)->values = { "#FF0000", "#00FF00" }; + config.option("filament_self_index", true)->values = { 1, 2 }; + config.option("filament_extruder_variant", true)->values = { "Direct Drive Standard", + "Direct Drive Standard" }; + // The nullable filament overrides are added to the config def at run time, so they are not part + // of the static full_print_config() key set: create the key before reading it back. + config.option("filament_retraction_length", true)->values = { 0.9 }; + Preset::normalize(config); + REQUIRE(config.opt("filament_retraction_length")->size() == 1); + + PresetBundle bundle; + Preset &pla = add_inmemory_preset(bundle.filaments, "My PLA"); + pla.config.option("filament_retraction_length", true)->values = { 0.5 }; + bundle.filament_presets = { "My PLA", "My PLA" }; + + PublishedMaterialEntry entry; + entry.slot = 1; // column 1, past the file's single value + entry.full = true; + entry.full_keys = { "filament_retraction_length" }; + entry.preset_name = "Author PLA"; + + PublishedConfig pub; + pub.published = true; + pub.material_keys = { entry }; + bundle.load_config_model("test.3mf", std::move(config), Semver(), &pub); + + CHECK(pub.skipped_keys.empty()); + const Preset *copy = bundle.filaments.find_preset(bundle.filament_presets[1], false, true); + REQUIRE(copy != nullptr); + check_double_vector(copy->config.opt("filament_retraction_length")->values, { 0.5 }); +}