mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-09-27 19:01:02 +00:00
Fixed issues with remapping mixed filaments when importing published 3MF
This commit is contained in:
@@ -3598,6 +3598,15 @@ void FacetsAnnotation::shift_states_above(const ModelVolume &mv, EnforcerBlocker
|
||||
this->set(selector);
|
||||
}
|
||||
|
||||
void FacetsAnnotation::remap_states(const ModelVolume &mv, const EnforcerBlockerStateMap &state_map)
|
||||
{
|
||||
if (empty()) return;
|
||||
TriangleSelector selector(mv.mesh());
|
||||
selector.deserialize(m_data, false);
|
||||
selector.remap_triangle_state(state_map);
|
||||
this->set(selector);
|
||||
}
|
||||
|
||||
void FacetsAnnotation::set_enforcer_block_type_limit(const ModelVolume &mv,
|
||||
EnforcerBlockerType max_type,
|
||||
EnforcerBlockerType to_delete_filament,
|
||||
@@ -3862,6 +3871,43 @@ bool model_has_advanced_features(const Model &model)
|
||||
return false;
|
||||
}
|
||||
|
||||
void remap_model_filament_slots(Model &model, const std::map<int, int> &slot_relocations)
|
||||
{
|
||||
if (slot_relocations.empty())
|
||||
return;
|
||||
|
||||
// Paint states and the object/volume "extruder" configs store one-based slot numbers
|
||||
// (see Sidebar::on_action_add_filament's insertion remap for the same encoding).
|
||||
std::map<int, int> one_based_slots;
|
||||
for (const auto &[from, to] : slot_relocations)
|
||||
one_based_slots.emplace(from + 1, to + 1);
|
||||
|
||||
EnforcerBlockerStateMap paint_state_map;
|
||||
for (size_t state = 0; state < paint_state_map.size(); ++state)
|
||||
paint_state_map[state] = EnforcerBlockerType(state);
|
||||
for (const auto &[one_based_from, one_based_to] : one_based_slots) {
|
||||
assert(one_based_from >= 0 && size_t(one_based_from) < paint_state_map.size());
|
||||
assert(one_based_to > 0 && size_t(one_based_to) < paint_state_map.size());
|
||||
paint_state_map[size_t(one_based_from)] = EnforcerBlockerType(one_based_to);
|
||||
}
|
||||
|
||||
auto remap_extruder_config = [&one_based_slots](ModelConfig &config) -> bool {
|
||||
const auto it = config.has("extruder") ? one_based_slots.find(config.extruder()) : one_based_slots.end();
|
||||
if (it == one_based_slots.end())
|
||||
return false;
|
||||
config.set("extruder", it->second);
|
||||
return true;
|
||||
};
|
||||
|
||||
for (ModelObject *object : model.objects) {
|
||||
remap_extruder_config(object->config);
|
||||
for (ModelVolume *volume : object->volumes) {
|
||||
remap_extruder_config(volume->config);
|
||||
volume->mmu_segmentation_facets.remap_states(*volume, paint_state_map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
// Verify whether the IDs of Model / ModelObject / ModelVolume / ModelInstance / ModelMaterial are valid and unique.
|
||||
void check_model_ids_validity(const Model &model)
|
||||
|
||||
@@ -745,6 +745,10 @@ public:
|
||||
// Shift painted filament indices >= threshold by delta. Used when a physical filament is
|
||||
// inserted ahead of existing slots (mixed-color slots are kept at the end of the list).
|
||||
void shift_states_above(const ModelVolume &mv, EnforcerBlockerType threshold, int delta);
|
||||
// Relabel painted filament indices according to state_map (old state value -> new state
|
||||
// value; untouched states keep their identity). Used when published-3MF import relocates
|
||||
// mixed-filament definitions onto new slot numbers.
|
||||
void remap_states(const ModelVolume &mv, const EnforcerBlockerStateMap &state_map);
|
||||
indexed_triangle_set get_facets_strict(const ModelVolume& mv, EnforcerBlockerType type) const;
|
||||
bool has_facets(const ModelVolume& mv, EnforcerBlockerType type) const;
|
||||
bool empty() const { return m_data.triangles_to_split.empty(); }
|
||||
@@ -1790,6 +1794,13 @@ bool model_has_multi_part_objects(const Model &model);
|
||||
// If the model has advanced features, then it cannot be processed in simple mode.
|
||||
bool model_has_advanced_features(const Model &model);
|
||||
|
||||
// Remap the model's filament-slot references after a published-3MF import relocated
|
||||
// mixed-filament definitions onto new slot numbers: object/volume "extruder" configs and
|
||||
// multi-material color-painting states (paint state stores the one-based slot number).
|
||||
// slot_relocations maps the author's zero-based slot number to its final zero-based slot;
|
||||
// entries are applied simultaneously (no chained lookups), untouched slots keep everything.
|
||||
void remap_model_filament_slots(Model &model, const std::map<int, int> &slot_relocations);
|
||||
|
||||
#ifndef NDEBUG
|
||||
// Verify whether the IDs of Model / ModelObject / ModelVolume / ModelInstance / ModelMaterial are valid and unique.
|
||||
void check_model_ids_validity(const Model &model);
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "Utils.hpp"
|
||||
#include "LocalesUtils.hpp"
|
||||
#include "Model.hpp"
|
||||
#include "TriangleSelector.hpp"
|
||||
#include "libslic3r_version.h"
|
||||
|
||||
#include <algorithm>
|
||||
@@ -4871,6 +4872,49 @@ static std::map<std::string, std::map<std::string, std::string>> filament_preset
|
||||
{{"Bambu PETG HF @BBL H2D 0.6 nozzle", "Bambu PETG HF @BBL H2D 0.8 nozzle"},
|
||||
{"Bambu ASA @BBL H2D 0.6 nozzle", "Bambu ASA @BBL H2D 0.8 nozzle"}}}};
|
||||
|
||||
// Relocate the per-slot cells of one mixed-filament project vector inside the imported
|
||||
// config, applying every authored-slot -> destination move at once. Each move reads its
|
||||
// source cell from a frozen snapshot of the config's current cells, so relocations never
|
||||
// cross-contaminate when an earlier destination overlaps a later source (adjacent published
|
||||
// tail mixes relocate onto consecutive slots). Cells the snapshot lacks degrade to
|
||||
// empty/false defaults - the missing-data handling in the material pass reports them
|
||||
// downstream. Left-behind source cells stay as-is: nothing else consumes the imported config
|
||||
// at those indices in published mode.
|
||||
static void apply_mixed_config_relocations(DynamicPrintConfig& config,
|
||||
const std::string& key,
|
||||
const std::vector<std::pair<size_t, size_t>>& moves)
|
||||
{
|
||||
ConfigOption* opt = config.optptr(key);
|
||||
if (opt == nullptr || moves.empty())
|
||||
return;
|
||||
std::unique_ptr<ConfigOption> snapshot(opt->clone());
|
||||
switch (opt->type()) {
|
||||
case coBools: {
|
||||
auto* live = static_cast<ConfigOptionBools*>(opt);
|
||||
const auto* frozen = static_cast<const ConfigOptionBools*>(snapshot.get());
|
||||
for (const auto [from, to] : moves) {
|
||||
const unsigned char cell = from < frozen->values.size() ? frozen->values[from] : 0;
|
||||
if (live->values.size() <= to)
|
||||
live->values.resize(to + 1, 0);
|
||||
live->values[to] = cell;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case coStrings: {
|
||||
auto* live = static_cast<ConfigOptionStrings*>(opt);
|
||||
const auto* frozen = static_cast<const ConfigOptionStrings*>(snapshot.get());
|
||||
for (const auto [from, to] : moves) {
|
||||
const std::string cell = from < frozen->values.size() ? frozen->values[from] : std::string();
|
||||
if (live->values.size() <= to)
|
||||
live->values.resize(to + 1, std::string{});
|
||||
live->values[to] = cell;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
// convert the old filament preset to new one after split
|
||||
static void convert_filament_preset_name(std::string& machine_name, std::string& filament_name)
|
||||
{
|
||||
@@ -5379,12 +5423,87 @@ void PresetBundle::load_config_file_config(const std::string& name_or_path,
|
||||
if (entry.slot >= 0)
|
||||
grow_target = std::max(grow_target, size_t(entry.slot) + 1);
|
||||
}
|
||||
// Mixed-filament definitions live in project-level virtual slots, so applying one
|
||||
// positionally onto a receiver slot that holds a real, physical filament would
|
||||
// silently convert hardware-backed state into a virtual mix. Compute each mixed
|
||||
// entry's destination before anything consumes entry.slot (growth, seeding,
|
||||
// de-aliasing, the overlay below):
|
||||
// - a definition landing on a receiver slot that already carries a mixed
|
||||
// definition keeps its place (a like-for-like override of a virtual slot);
|
||||
// - everything else goes through one monotone append counter preserving author
|
||||
// order: dest = max(authored, next_free). With a receiver shorter than the
|
||||
// publish this keeps the authored positions intact; past them (or around a
|
||||
// collision with a real filament) the mixes pack onto consecutive fresh slots
|
||||
// AFTER every positional (real-filament) territory. The definition's cells are
|
||||
// shifted inside the file-side per-slot mixed arrays so they stay readable
|
||||
// from the new index. No existing slot changes meaning.
|
||||
// - destinations are also capped: appends past the extruder limit are dropped
|
||||
// and reported instead of being forced onto a physical filament.
|
||||
const std::set<std::string>& mixed_definitions = publish_mixed_keys();
|
||||
auto is_mixed_definition = [&mixed_definitions](const PublishedMaterialEntry& entry) {
|
||||
return std::any_of(entry.keys.begin(), entry.keys.end(), [&](const std::string& key) {
|
||||
return mixed_definitions.count(publish_base_key(key)) != 0;
|
||||
});
|
||||
};
|
||||
size_t next_free_slot = this->filament_presets.size();
|
||||
bool any_mixed_relocated = false;
|
||||
// All authored-slot -> destination moves decided by this pass, applied to the
|
||||
// incoming config in one batched snapshot step below (an earlier move's
|
||||
// destination can overlap a later move's source: adjacent tail mixes relocate
|
||||
// onto consecutive slots, so incremental in-place shifts would overwrite a
|
||||
// definition that has not been moved yet).
|
||||
std::vector<std::pair<size_t, size_t>> mixed_moves;
|
||||
for (auto entry_it = published_config->material_keys.begin(); entry_it != published_config->material_keys.end();) {
|
||||
PublishedMaterialEntry& entry = *entry_it;
|
||||
if (entry.slot < 0 || !is_mixed_definition(entry) ||
|
||||
// Like-for-like override of a virtual receiver slot (bounds-checked).
|
||||
this->is_mixed_filament(size_t(entry.slot))) {
|
||||
++entry_it;
|
||||
continue;
|
||||
}
|
||||
if (std::max(size_t(entry.slot), next_free_slot) >= size_t(EnforcerBlockerType::ExtruderMax)) {
|
||||
// No free virtual slot left: report instead of destroying a real filament.
|
||||
const std::string material_label = !entry.filament_id.empty() ? entry.filament_id :
|
||||
!entry.publish_type_value.empty() ? entry.publish_type_value :
|
||||
entry.filament_type;
|
||||
published_config->skipped_keys.emplace_back("material:" + material_label +
|
||||
" (mixed filament definition: filament slot limit reached)");
|
||||
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << ": published 3MF mixed filament from slot " << entry.slot
|
||||
<< " could not be placed: all " << next_free_slot << " slots exhausted";
|
||||
entry_it = published_config->material_keys.erase(entry_it);
|
||||
continue;
|
||||
}
|
||||
const int authored_slot = entry.slot;
|
||||
const int dest_slot = int(std::max(size_t(authored_slot), next_free_slot));
|
||||
next_free_slot = size_t(dest_slot) + 1;
|
||||
if (dest_slot == authored_slot)
|
||||
// Uncontended fresh tail slot: the definition is already readable there.
|
||||
++entry_it;
|
||||
else {
|
||||
entry.slot = dest_slot;
|
||||
any_mixed_relocated = true;
|
||||
mixed_moves.emplace_back(size_t(authored_slot), size_t(entry.slot));
|
||||
published_config->mixed_slot_relocations.emplace(authored_slot, entry.slot);
|
||||
published_config->material_replacements.emplace_back("slot " + std::to_string(authored_slot) + " -> slot " +
|
||||
std::to_string(entry.slot) +
|
||||
": mixed filament relocated (would have replaced a physical filament)");
|
||||
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": published 3MF relocated mixed filament slot " << authored_slot
|
||||
<< " -> " << entry.slot;
|
||||
++entry_it;
|
||||
}
|
||||
}
|
||||
if (!mixed_moves.empty())
|
||||
for (const std::string& mixed_key : mixed_definitions)
|
||||
apply_mixed_config_relocations(config, mixed_key, mixed_moves);
|
||||
if (has_published_entries) {
|
||||
// Defensive cap: growth never exceeds the file's own filament count. The
|
||||
// receiver's current slot count is a floor: neither the preset list nor the
|
||||
// project vectors are ever shrunk, even when the file carries fewer filaments
|
||||
// than the receiver has slots.
|
||||
const size_t target_slots = std::max(this->filament_presets.size(), std::min(grow_target, num_filaments));
|
||||
// than the receiver has slots. Relocated mixed entries legitimately land past
|
||||
// the file's own slot count (virtual slots consume no nozzle or tray), so
|
||||
// their destinations lift the ceiling explicitly.
|
||||
const size_t target_slots = std::max({this->filament_presets.size(), std::min(grow_target, num_filaments),
|
||||
any_mixed_relocated ? next_free_slot : size_t(0)});
|
||||
// Slots carrying published content, steering the initial preset selection of
|
||||
// newly grown slots.
|
||||
std::set<int> published_slots;
|
||||
@@ -5999,9 +6118,7 @@ void PresetBundle::load_config_file_config(const std::string& name_or_path,
|
||||
// A mixed-definition entry carries the mix's blended colour for the
|
||||
// swatch only: never write it into the slot's (possibly shared) preset
|
||||
// config, only into the project-level colour arrays.
|
||||
const bool is_mixed_entry = std::any_of(entry.keys.begin(), entry.keys.end(), [&](const std::string& key) {
|
||||
return publish_mixed_keys().count(publish_base_key(key)) != 0;
|
||||
});
|
||||
const bool is_mixed_entry = is_mixed_definition(entry);
|
||||
if (!is_mixed_entry && recv != nullptr) {
|
||||
// Create the key when the target preset lacks it: the colour is a
|
||||
// requirement, not an override.
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "enum_bitmask.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <shared_mutex>
|
||||
#include <unordered_map>
|
||||
@@ -186,6 +187,11 @@ struct PublishedConfig
|
||||
// Human-readable notices of the slot material replacements performed while loading a
|
||||
// published project, for the load notification.
|
||||
std::vector<std::string> material_replacements;
|
||||
// Mixed-filament entries that had to be moved off their authored slot on load (a real,
|
||||
// physical filament occupied it): maps the author's zero-based slot number to its final
|
||||
// zero-based slot. Consumers (e.g. model extruder/color-painting remapping) use this to
|
||||
// keep geometry references pointing at the relocated definitions.
|
||||
std::map<int, int> mixed_slot_relocations;
|
||||
};
|
||||
|
||||
// Bundle of Print + Filament + Printer presets.
|
||||
|
||||
@@ -9053,6 +9053,17 @@ std::vector<size_t> Plater::priv::load_files(const std::vector<fs::path>& input_
|
||||
|
||||
preset_bundle->load_config_model(filename.string(), std::move(config), file_version, &published_config);
|
||||
|
||||
// Mixed-filament definitions that collided with one of the
|
||||
// receiver's real slots were relocated during the preset load.
|
||||
// Re-point the freshly parsed model's extruder references and
|
||||
// color painting from the author's slot numbers to where each
|
||||
// definition landed, so volumes colored with a mix follow it.
|
||||
// Runs before the objects are handed over to the plater below.
|
||||
if (load_model && !published_config.mixed_slot_relocations.empty())
|
||||
Slic3r::remap_model_filament_slots(model, published_config.mixed_slot_relocations);
|
||||
|
||||
// BBS: notify the user about published settings that could not be applied.
|
||||
|
||||
// BBS: notify the user about published settings that could not be applied.
|
||||
if (!published_config.skipped_keys.empty()) {
|
||||
NotificationManager* notify_manager = q->get_notification_manager();
|
||||
|
||||
Reference in New Issue
Block a user