diff --git a/src/libslic3r/IMEXZones.cpp b/src/libslic3r/IMEXZones.cpp index f5164f0500..3629ee065b 100644 --- a/src/libslic3r/IMEXZones.cpp +++ b/src/libslic3r/IMEXZones.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -376,4 +377,38 @@ ImexZoneLayout compute_imex_zone_layout(const DynamicPrintConfig& printer_cfg, return out; } +bool update_imex_layout_key(std::optional& key, const DynamicPrintConfig* printer_cfg, const std::string& mode) +{ + static const std::string no_mode; + static const std::vector no_strings; + const auto* is_imex = printer_cfg != nullptr ? printer_cfg->option("is_imex") : nullptr; + + ImexLayoutKey::Scalars s; + const ConfigOptionStrings* names = nullptr; + const ConfigOptionStrings* tools = nullptr; + if (is_imex != nullptr && is_imex->value) { + // The same accessors compute_imex_zone_layout() uses, so the key holds the values it lays + // out from. + s.imex = true; + s.gantry_count = imex_cfg_int(*printer_cfg, "imex_gantry_count"); + s.tools_per_gantry = imex_cfg_int(*printer_cfg, "imex_tools_per_gantry"); + s.tool_layout = int(imex_cfg_enum(*printer_cfg, "imex_tool_layout")); + s.clearance_x = imex_cfg_float(*printer_cfg, "imex_nozzle_clearance_x"); + s.clearance_y = imex_cfg_float(*printer_cfg, "imex_nozzle_clearance_y"); + s.carriage_margin = imex_cfg_float(*printer_cfg, "imex_carriage_margin"); + s.firmware_managed = imex_cfg_bool(*printer_cfg, "imex_firmware_managed_zones"); + names = printer_cfg->option("imex_mode_names"); + tools = printer_cfg->option("imex_mode_active_tools"); + } + const std::string& resolved_mode = s.imex ? mode : no_mode; + const std::vector& mode_names = names != nullptr ? names->values : no_strings; + const std::vector& mode_tools = tools != nullptr ? tools->values : no_strings; + + if (key.has_value() && key->s == s && key->mode == resolved_mode && key->mode_names == mode_names && + key->mode_active_tools == mode_tools) + return false; + key = ImexLayoutKey{ s, resolved_mode, mode_names, mode_tools }; + return true; +} + } // namespace Slic3r diff --git a/src/libslic3r/IMEXZones.hpp b/src/libslic3r/IMEXZones.hpp index 45bc959101..5896264f67 100644 --- a/src/libslic3r/IMEXZones.hpp +++ b/src/libslic3r/IMEXZones.hpp @@ -1,8 +1,10 @@ #pragma once +#include #include #include #include +#include #include #include "libslic3r/BoundingBox.hpp" @@ -91,4 +93,46 @@ ImexZoneLayout compute_imex_zone_layout(const DynamicPrintConfig& printer_cfg, const std::string& process_mode, const BoundingBoxf& bed_extents); +// Every config input to a plate's IMEX zones and ghosts: what compute_imex_zone_layout() reads from +// the printer preset, plus the plate's resolved mode and imex_firmware_managed_zones, which decides +// whether the plate draws ghosts. The bed is left out because a plate rebuilds its zones in +// set_shape(). An option that moves a zone or a ghost must be added here, or they go stale silently. +struct ImexLayoutKey +{ + // Every other field stays at its default while imex is false. + struct Scalars + { + bool imex = false; + int gantry_count = 0; + int tools_per_gantry = 0; + int tool_layout = -1; + double clearance_x = 0.0; + double clearance_y = 0.0; + double carriage_margin = 0.0; + bool firmware_managed = false; + + bool operator==(const Scalars& rhs) const + { + // A clearance field accepts "nan", so NaN equals itself here, or the key would change + // every frame. + auto same = [](double a, double b) { return a == b || (std::isnan(a) && std::isnan(b)); }; + return std::tie(imex, gantry_count, tools_per_gantry, tool_layout, firmware_managed) == + std::tie(rhs.imex, rhs.gantry_count, rhs.tools_per_gantry, rhs.tool_layout, rhs.firmware_managed) && + same(clearance_x, rhs.clearance_x) && same(clearance_y, rhs.clearance_y) && + same(carriage_margin, rhs.carriage_margin); + } + }; + Scalars s; + // The resolved mode and the printer's whole mode table. A name doesn't identify a roster: two + // presets can give one name different rosters, and editing a roster keeps its name. + std::string mode; + std::vector mode_names; + std::vector mode_active_tools; +}; + +// Brings `key` up to date with `printer_cfg` and the plate's resolved `mode`, and returns +// whether it changed. A null `printer_cfg` reads as IMEX off. Plates and the G-code preview call +// this every frame, so it compares against the live config and copies only when something changed. +bool update_imex_layout_key(std::optional& key, const DynamicPrintConfig* printer_cfg, const std::string& mode); + } // namespace Slic3r diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index 85ebd4d109..6f6c4c8f5c 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -2245,34 +2245,17 @@ void GCodeViewer::render_markers(int canvas_width, int canvas_height) // Cache guard. The marker plan below is resolved from preset/plate state only, // so it is rebuilt when that state moves and reused otherwise — the sequential // slider is sticky, and re-resolving the zone layout on every frame of the rest - // of the session showed up as a measurable per-frame cost. The comparison is - // deliberately made against the live config rather than against a freshly built - // key, so an unchanged frame allocates nothing at all. - static const std::vector s_no_strings; - auto* names_opt = printer_cfg.opt("imex_mode_names"); - auto* tools_opt = printer_cfg.opt("imex_mode_active_tools"); - const std::vector& mode_names = names_opt ? names_opt->values : s_no_strings; - const std::vector& active_tools = tools_opt ? tools_opt->values : s_no_strings; + // of the session showed up as a measurable per-frame cost. + ImexMarkerKey::Plate plate; + plate.plate_index = plate_list.get_curr_plate_index(); + plate.bed_min_x = bed_extents.min.x(); + plate.bed_min_y = bed_extents.min.y(); + plate.bed_max_x = bed_extents.max.x(); + plate.bed_max_y = bed_extents.max.y(); - ImexMarkerKey::Scalars key; - key.plate_index = plate_list.get_curr_plate_index(); - // Same accessor the plan is built from: a key holding a different fallback than - // the value actually laid out would compare equal across a real change. - key.gantry_count = imex_cfg_int(printer_cfg, "imex_gantry_count"); - key.tools_per_gantry = imex_cfg_int(printer_cfg, "imex_tools_per_gantry"); - key.tool_layout = (int)imex_cfg_enum(printer_cfg, "imex_tool_layout"); - key.clearance_x = imex_cfg_float(printer_cfg, "imex_nozzle_clearance_x"); - key.clearance_y = imex_cfg_float(printer_cfg, "imex_nozzle_clearance_y"); - key.firmware_managed = imex_cfg_bool(printer_cfg, "imex_firmware_managed_zones"); - key.bed_min_x = bed_extents.min.x(); - key.bed_min_y = bed_extents.min.y(); - key.bed_max_x = bed_extents.max.x(); - key.bed_max_y = bed_extents.max.y(); - - if (!(m_imex_marker_key.s == key) || m_imex_marker_key.mode != mode || - m_imex_marker_key.mode_names != mode_names || - m_imex_marker_key.mode_active_tools != active_tools) { - m_imex_marker_key = ImexMarkerKey{ key, mode, mode_names, active_tools }; + const bool layout_changed = update_imex_layout_key(m_imex_marker_key.layout, &printer_cfg, mode); + if (layout_changed || !(m_imex_marker_key.plate == plate)) { + m_imex_marker_key.plate = plate; m_imex_marker_plan = resolve_imex_marker_plan(printer_cfg, mode, bed_extents); // The carriage roster — and with it the marker count — can change with any // of those inputs, so drop the markers and let the lazy init below rebuild diff --git a/src/slic3r/GUI/GCodeViewer.hpp b/src/slic3r/GUI/GCodeViewer.hpp index ff24afa96b..714a359ab8 100644 --- a/src/slic3r/GUI/GCodeViewer.hpp +++ b/src/slic3r/GUI/GCodeViewer.hpp @@ -19,8 +19,10 @@ #include "libslic3r/BoundingBox.hpp" #include #include "libslic3r/Config.hpp" +#include "libslic3r/IMEXZones.hpp" #include "libvgcode/include/GCodeInputData.hpp" #include +#include #include "libvgcode/include/PathVertex.hpp" #include @@ -271,50 +273,23 @@ private: // Invalidation key for the plan above: every input the plan is derived from, and // nothing that changes between frames. A stale plan would put the preview markers // somewhere the plate's own zones and ghosts do not agree with, which is exactly the - // drift the shared layout call exists to prevent -- so this deliberately mirrors - // PartPlate::build_imex_cache_key(). The two keys are not field-for-field identical, and - // the differences are deliberate rather than incidental: - // - bed extents and plate_index are here and not there. Zone centres scale with the - // extents, and one preview serves every plate, so the preview must key what the plate - // gets for free -- it re-bakes on set_shape() and is keyed by being that plate. - // - mode_names / mode_active_tools hold the printer's WHOLE mode table; the plate resolves - // one active mode and keys that roster plus its primary head. Same information reached - // two ways, so a change to the active mode moves both keys. - // - imex_carriage_margin is there and not here: it only sizes the plate's advisory bands, - // which the preview never draws. - // imex_tool_layout is in both, which the plate's key gained for the reason this one has it: - // the T0-corner flip moves every zone rectangle while nothing else keyed changes. + // drift the shared layout call exists to prevent -- so the layout half is the plate's own + // key. The preview adds the plate index and bed extents, which a plate gets for free: zone + // centers scale with the bed, and one preview serves every plate, while each plate rebuilds + // its zones in set_shape(). The layout key also holds imex_carriage_margin, which only sizes + // the plate's advisory bands, so a margin change re-resolves the plan needlessly. struct ImexMarkerKey { - // Scalar half. Built fresh on the stack each frame -- it allocates nothing -- and - // compared as a tuple. - struct Scalars + std::optional layout; + struct Plate { - int plate_index = -1; - int gantry_count = 0; - int tools_per_gantry = 0; - int tool_layout = -1; - double clearance_x = 0.0; - double clearance_y = 0.0; - bool firmware_managed = false; + int plate_index = -1; double bed_min_x = 0.0, bed_min_y = 0.0, bed_max_x = 0.0, bed_max_y = 0.0; - auto tied() const { - return std::tie(plate_index, gantry_count, tools_per_gantry, tool_layout, - clearance_x, clearance_y, firmware_managed, - bed_min_x, bed_min_y, bed_max_x, bed_max_y); - } - bool operator==(const Scalars& rhs) const { return tied() == rhs.tied(); } + auto tied() const { return std::tie(plate_index, bed_min_x, bed_min_y, bed_max_x, bed_max_y); } + bool operator==(const Plate& rhs) const { return tied() == rhs.tied(); } }; - Scalars s; - // Resolved active mode (the plate's mode beats the process preset), plus the printer - // preset's whole mode table. The name alone is not identity: two presets can carry - // the same mode name over different tool rosters, and editing a roster in place - // moves neither the name nor any scalar above. Compared by value, never copied - // unless something actually changed. - std::string mode; - std::vector mode_names; - std::vector mode_active_tools; + Plate plate; }; ImexMarkerKey m_imex_marker_key; ImexMarkerPlan m_imex_marker_plan; diff --git a/src/slic3r/GUI/PartPlate.cpp b/src/slic3r/GUI/PartPlate.cpp index bf2692db92..8f487b3080 100644 --- a/src/slic3r/GUI/PartPlate.cpp +++ b/src/slic3r/GUI/PartPlate.cpp @@ -472,7 +472,7 @@ std::string PartPlate::get_effective_imex_mode() const std::string mode = get_imex_mode(); if (mode != kImexPrimaryMode) return mode; - // wxGetApp() is not available headless -- see build_imex_cache_key, which guards on the + // wxGetApp() is not available headless -- see refresh_imex_layout_key, which guards on the // same thing. Nothing calls this from the CLI today; this keeps that from being fatal. if (!m_plater) return mode; @@ -492,16 +492,14 @@ void PartPlate::set_imex_mode(const std::string& mode) m_config.set_key_value("imex_parallel_mode", new ConfigOptionString(mode)); } update_slice_result_valid_state(false); - m_imex_zones_mode_cache = "\x01"; // force zone rebuild - m_imex_ghost_cache_key = "\x01"; // force ghost rebuild + m_imex_layout_key.reset(); // force zone and ghost rebuild } void PartPlate::reset_imex_mode() { m_config.erase("imex_parallel_mode"); update_slice_result_valid_state(false); - m_imex_zones_mode_cache = "\x01"; - m_imex_ghost_cache_key = "\x01"; + m_imex_layout_key.reset(); } std::map PartPlate::get_imex_head_filament_map() const @@ -678,7 +676,6 @@ void PartPlate::calc_imex_zones() m_imex_margin_overlay.clear(); m_imex_primary_zone_box = std::nullopt; m_imex_head_zone_centers.clear(); - m_imex_primary_head = -1; if (!wxGetApp().preset_bundle) return; @@ -695,7 +692,6 @@ void PartPlate::calc_imex_zones() const ImexZoneLayout layout = compute_imex_zone_layout(printer_cfg, get_imex_mode(), process_mode, get_extents(m_shape)); - m_imex_primary_head = layout.primary_head; m_imex_head_zone_centers = layout.head_zone_centers; m_imex_primary_zone_box = layout.primary_zone_box; m_imex_secondary_zone_boxes = layout.secondary_zone_boxes; @@ -737,62 +733,32 @@ void PartPlate::calc_imex_zones() push_zone_fill(m_imex_margin_overlay, b.min.x(), b.max.x(), b.min.y(), b.max.y()); } -// Build a cache key from the current IDEX/IQEX config options, or "" if IDEX/IQEX is off. -// Reads per-plate mode from m_config first, falling back to the process preset. -// -// Inputs that contribute to the key (any change must invalidate the IMEX zone cache): -// - active_mode (per-plate or process-preset fallback) -// - imex_tools_per_gantry, imex_gantry_count (grid shape) -// - imex_tool_layout (which corner tool 0 occupies) -// - imex_nozzle_clearance_x, imex_nozzle_clearance_y (zone widths / collision strips) -// - imex_carriage_margin (zone shrink) -// - the active mode's tool roster and its Primary head (same name, different roster) -// - imex_firmware_managed_zones (suppresses ghost generation) -// -// IMPORTANT: if you add a printer config option that affects zone geometry, ghost transforms, -// or collision strips, it MUST be incorporated here — otherwise ghost meshes and zone overlays -// will go stale silently after a config change. Float values are scaled by 10 before integer -// cast so 0.1 mm steps invalidate the cache; if you add a float option needing finer precision, -// adjust the scale. -std::string PartPlate::build_imex_cache_key() const +void PartPlate::refresh_imex_layout_key() { // CLI / headless slice: no GUI_App is initialized, so `wxGetApp()` returns // memory that segfaults on member access. `m_plater` is set only by the GUI - // construction path; treat its absence as "no IMEX state to compute" and - // return an empty key. Same guard pattern used by calc_imex_ghosts. - if (!m_plater || !wxGetApp().preset_bundle) - return ""; - const DynamicPrintConfig& printer_cfg = wxGetApp().preset_bundle->printers.get_edited_preset().config; - auto* is_imex_opt = printer_cfg.option("is_imex"); - if (!is_imex_opt || !is_imex_opt->value) - return ""; - std::string active_mode = get_effective_imex_mode(); - // Mode NAME alone is not printer identity: two presets can define the same mode - // name with different tool rosters/primary, and imex_firmware_managed_zones - // suppresses ghost generation entirely. Both shape the baked zone/ghost set, so - // key them directly (previously caught only incidentally, via the ghost key's - // since-removed pem entry). - std::string active_tools; - int primary_phys = -1; - resolve_active_mode_tools(active_tools, primary_phys); // empty on failure — keyed as such - // imex_tool_layout decides which physical corner tool 0 occupies (flip_x / flip_y inside - // compute_imex_zone_layout), so every zone rectangle, collision strip and ghost offset moves - // when it changes while every other keyed field stays put. Without it the key is identical - // across a layout change and the baked zones go stale; that this currently appears to work is - // incidental -- some other path happens to rebuild -- not something to rely on. - // Geometry values go through imex_cfg_* so the key is built from the same numbers - // compute_imex_zone_layout() lays out with; a literal here could key on a value the layout - // never used. - return active_mode - + "|" + std::to_string(imex_cfg_int(printer_cfg, "imex_tools_per_gantry")) - + "x" + std::to_string(imex_cfg_int(printer_cfg, "imex_gantry_count")) - + "|ly" + std::to_string((int)imex_cfg_enum(printer_cfg, "imex_tool_layout")) - + "|cw" + std::to_string((int)(imex_cfg_float(printer_cfg, "imex_nozzle_clearance_x") * 10)) - + "|ch" + std::to_string((int)(imex_cfg_float(printer_cfg, "imex_nozzle_clearance_y") * 10)) - + "|mg" + std::to_string((int)(imex_cfg_float(printer_cfg, "imex_carriage_margin") * 10)) - + "|t" + active_tools - + "|p" + std::to_string(primary_phys) - + "|fw" + (imex_cfg_bool(printer_cfg, "imex_firmware_managed_zones") ? "1" : "0"); + // construction path; treat its absence as "IMEX off". Same guard pattern used + // by calc_imex_ghosts. + const DynamicPrintConfig* printer_cfg = nullptr; + if (m_plater && wxGetApp().preset_bundle) { + const DynamicPrintConfig& cfg = wxGetApp().preset_bundle->printers.get_edited_preset().config; + const auto* is_imex = cfg.option("is_imex"); + if (is_imex != nullptr && is_imex->value) + printer_cfg = &cfg; + } + // With IMEX off the key ignores the mode, so don't resolve it. + if (!update_imex_layout_key(m_imex_layout_key, printer_cfg, printer_cfg != nullptr ? get_effective_imex_mode() : std::string())) + return; + + ++m_imex_layout_key_version; + m_imex_zones_stale = true; + m_imex_active_mode = {}; + if (m_imex_layout_key->s.imex && m_imex_layout_key->mode != kImexPrimaryMode) { + // A mode missing from the table, or past the end of a short imex_mode_active_tools, gets + // empty tools, which declare no Primary, so the mode stays unresolved. + m_imex_active_mode.tools = find_imex_mode(*printer_cfg, m_imex_layout_key->mode).active_tools; + m_imex_active_mode.primary = imex_primary_tool_for_mode(m_imex_active_mode.tools); + } } // Reposition the IMEX mode icon without requiring a full set_shape() rebuild. @@ -824,66 +790,36 @@ void PartPlate::refresh_imex_icon() } // Ensure zone geometry (secondary boxes, collision strips, visual meshes) is up to date. -// Called before any collision check AND before rendering so both paths share the same data. void PartPlate::ensure_imex_zones() { // CLI / headless slice: no GUI_App, no plater, no IMEX state to track. Bail out // before touching `wxGetApp()` (which segfaults without a GUI_App) — placement // checks reach this from the headless 3MF-load path through `check_outside`. if (!m_plater) return; - std::string cache_key = build_imex_cache_key(); - if (cache_key != m_imex_zones_mode_cache) { - m_imex_zones_mode_cache = cache_key; - calc_imex_zones(); - } + refresh_imex_layout_key(); + rebuild_stale_imex_zones(); } -std::string PartPlate::build_imex_ghost_cache_key() const +void PartPlate::rebuild_stale_imex_zones() { - // Ghost shape depends on: mode topology (same inputs as zone key) + set of - // objects on plate + each primary instance's transform. Filament routing and - // colors are deliberately NOT keyed: ghost RGB is restamped live every frame - // by update_imex_ghost_colors, so color-only changes never need a mesh rebuild. - std::string k = build_imex_cache_key(); - if (k.empty()) return ""; // ghost-off when zones-off - - k += "|obj"; - for (const auto& oi : obj_to_instance_set) - k += std::to_string(oi.first) + "." + std::to_string(oi.second) + ","; - return k; + if (!m_plater || !m_imex_zones_stale) return; + m_imex_zones_stale = false; + calc_imex_zones(); } void PartPlate::ensure_imex_ghosts() { - std::string key = build_imex_ghost_cache_key(); - if (key != m_imex_ghost_cache_key) { - m_imex_ghost_cache_key = key; - calc_imex_ghosts(); - } -} - -bool PartPlate::resolve_active_mode_tools(std::string& out_tools_str, int& out_primary_phys) const -{ - if (!wxGetApp().preset_bundle) return false; - const DynamicPrintConfig& printer_cfg = wxGetApp().preset_bundle->printers.get_edited_preset().config; - auto* is_imex_opt = printer_cfg.option("is_imex"); - if (!is_imex_opt || !is_imex_opt->value) return false; - - const std::string active_mode = get_effective_imex_mode(); - if (active_mode == kImexPrimaryMode) return false; - - const ImexMode mode = find_imex_mode(printer_cfg, active_mode); - if (!mode.found()) return false; - - // Also the ragged case: a tools array too short to reach this row hands back an empty - // string, which has no declared primary, which is the same "no zones here" answer the - // explicit bounds check used to give. - const int primary_phys = imex_primary_tool_for_mode(mode.active_tools); - if (primary_phys < 0) return false; - - out_tools_str = mode.active_tools; - out_primary_phys = primary_phys; - return true; + // Ghost meshes depend on: mode topology (the layout key) + set of objects on + // plate. Instance transforms are applied live by update_imex_ghost_transforms. + // Filament routing and colors are deliberately NOT keyed: ghost RGB is restamped + // live every frame by update_imex_ghost_colors, so color-only changes never need + // a mesh rebuild. With IMEX off there are no ghosts, whatever the plate holds. + if (m_imex_ghost_layout_version == m_imex_layout_key_version && + (!m_imex_layout_key || !m_imex_layout_key->s.imex || m_imex_ghost_objects == obj_to_instance_set)) + return; + m_imex_ghost_layout_version = m_imex_layout_key_version; + m_imex_ghost_objects = obj_to_instance_set; + calc_imex_ghosts(); } // Ghost opacity, stamped together with RGB by update_imex_ghost_colors every @@ -896,9 +832,10 @@ void PartPlate::calc_imex_ghosts() if (!m_plater || !m_model) return; if (obj_to_instance_set.empty()) return; - std::string active_tools_str; - int primary_phys = -1; - if (!resolve_active_mode_tools(active_tools_str, primary_phys)) return; + const ImexActiveMode& active = m_imex_active_mode; + if (!active.resolved()) return; + const std::string& active_tools_str = active.tools; + const int primary_phys = active.primary; // IMEX firmware-managed zones: the slicer emits a centered single-half slice and the // firmware fans copies/mirrors out from there — placement is not slicer-authoritative. @@ -911,9 +848,8 @@ void PartPlate::calc_imex_ghosts() return; // Zone centers are the basis for ghost placement; make sure they exist before - // we read them. render_imex_zones already ensures this, but ghost rebuild can - // also be driven from mode/preset invalidation paths that don't touch zones. - ensure_imex_zones(); + // we read them. + rebuild_stale_imex_zones(); const auto heads = parse_imex_active_tools(active_tools_str); @@ -1051,13 +987,13 @@ void PartPlate::update_imex_ghost_transforms( { if (m_imex_ghost_volumes.empty() || !m_plater || !m_model) return; - std::string active_tools_str; - int primary_phys = -1; - if (!resolve_active_mode_tools(active_tools_str, primary_phys)) return; - // Reuse zone-center-derived offsets (same source calc_imex_ghosts uses), so // update and rebuild paths always agree on where each ghost belongs. ensure_imex_zones(); + const ImexActiveMode& active = m_imex_active_mode; + if (!active.resolved()) return; + const std::string& active_tools_str = active.tools; + const int primary_phys = active.primary; auto center_for = [&](int phys) -> Vec2d { auto it = m_imex_head_zone_centers.find(phys); return (it == m_imex_head_zone_centers.end()) ? Vec2d::Zero() : it->second; @@ -1294,11 +1230,11 @@ bool PartPlate::has_imex_multimaterial_conflict() const // which the raw key's single default entry does not describe. const ConfigOptionInts pem = effective_physical_extruder_map(*pb); - // Resolve the active mode's tools string from the printer config. Print::validate() + // The active mode's roster, as refresh_imex_layout_key() resolved it. Print::validate() // resolves the same name the same way and keeps going on an empty result -- the mixed // filament rule below does not depend on the roster -- so this must too, or the badge // goes quiet on exactly the plates the slicer refuses. - const std::string active_tools_str = find_imex_mode(printer_cfg, mode).active_tools; + const std::string& active_tools_str = m_imex_active_mode.tools; // Convert PartPlate's 1-based extruder list to the 0-based form the helper expects. // @@ -1330,7 +1266,7 @@ void PartPlate::render_imex_zones(bool force_default_color) if (force_default_color) return; - ensure_imex_zones(); + rebuild_stale_imex_zones(); ensure_imex_ghosts(); // Read visualization theme from printer config. @@ -2673,11 +2609,8 @@ void PartPlate::render_icons(bool bottom, bool only_name, int hover_id) auto* is_imex_opt = pb ? pb->printers.get_edited_preset().config.option("is_imex") : nullptr; if (is_imex_opt && is_imex_opt->value) { // The icon shows the mode the plate slices as, the one its ghosts follow. - std::string active_tools; - int primary_phys = -1; - const size_t kind = size_t(resolve_active_mode_tools(active_tools, primary_phys) ? - imex_mode_kind(active_tools) : - ImexModeKind::Primary); + const ImexActiveMode& active = m_imex_active_mode; + const size_t kind = size_t(active.resolved() ? imex_mode_kind(active.tools) : ImexModeKind::Primary); // Both the mode name and the conflict warning describe the same hovered icon, // and set_hover_tooltip records one string per frame, so they are composed here. std::string hover_tip; @@ -5222,7 +5155,7 @@ bool PartPlate::set_shape(const Pointfs& shape, const Pointfs& exclude_areas, co const BoundingBox& pp_bbox = poly.contour.bounding_box(); calc_gridlines(poly, pp_bbox); - m_imex_zones_mode_cache = "\x01"; // force rebuild on next render + m_imex_zones_stale = true; // force rebuild on next render calc_imex_zones(); calc_vertex_for_icons(0, m_del_icon); @@ -5340,6 +5273,8 @@ bool PartPlate::intersects(const BoundingBoxf3& bb) const void PartPlate::render(const Transform3d& view_matrix, const Transform3d& projection_matrix, bool bottom, bool only_body, bool force_background_color, HeightLimitMode mode, int hover_id, bool render_cali, bool show_grid, bool hide_chrome) { + refresh_imex_layout_key(); + glsafe(::glEnable(GL_DEPTH_TEST)); GLShaderProgram *shader = wxGetApp().get_shader("flat"); diff --git a/src/slic3r/GUI/PartPlate.hpp b/src/slic3r/GUI/PartPlate.hpp index bfaf9c0318..7ae0ce2d3a 100644 --- a/src/slic3r/GUI/PartPlate.hpp +++ b/src/slic3r/GUI/PartPlate.hpp @@ -32,6 +32,7 @@ #include "libslic3r/ObjectID.hpp" #include "libslic3r/GCode/GCodeProcessor.hpp" #include "libslic3r/Format/bbs_3mf.hpp" +#include "libslic3r/IMEXZones.hpp" #include "libslic3r/Slicing.hpp" #include "libslic3r/GCode/WipeTowerEstimate.hpp" #include "libslic3r/Arrange.hpp" @@ -181,10 +182,23 @@ private: std::vector m_imex_margin_overlay; // amber advisory bands just inside collision strips std::optional m_imex_primary_zone_box; // primary zone extents in mm; empty when IDEX/IQEX is off/primary-only std::map m_imex_head_zone_centers; // physical-head → zone-center (mm); populated by calc_imex_zones - int m_imex_primary_head{ -1 }; // physical-head index of the primary tool in the active mode; -1 when no mode - std::string m_imex_zones_mode_cache{ "\x01" }; // cached mode+topology key; sentinel = unbuilt + // What the zones, the ghosts and m_imex_active_mode were last built from; nullopt rebuilds all three. + std::optional m_imex_layout_key; + unsigned m_imex_layout_key_version{ 0 }; // bumped whenever m_imex_layout_key changes + bool m_imex_zones_stale{ true }; // set when the key changes, cleared by the zone rebuild + // Resolved only when m_imex_layout_key changes, because find_imex_mode() copies the whole + // mode, G-code included. + struct ImexActiveMode + { + std::string tools; // the mode's imex_mode_active_tools entry; empty if there's none + int primary = -1; // physical head of the mode's Primary tool + bool resolved() const { return primary >= 0; } + }; + ImexActiveMode m_imex_active_mode; std::vector> m_imex_ghost_volumes; - std::string m_imex_ghost_cache_key{ "\x01" }; // sentinel = unbuilt + // The layout key version and the objects the ghosts were built from; nullopt = unbuilt. + std::optional m_imex_ghost_layout_version; + std::set> m_imex_ghost_objects; GLModel m_height_limit_common; GLModel m_height_limit_bottom; GLModel m_height_limit_top; @@ -233,16 +247,14 @@ private: void calc_gridlines(const ExPolygon& poly, const BoundingBox& pp_bbox); void calc_height_limit(); void calc_imex_zones(); - void ensure_imex_zones(); - std::string build_imex_cache_key() const; + void ensure_imex_zones(); // checks the layout key, then rebuild_stale_imex_zones() + void rebuild_stale_imex_zones(); // rebuilds the zones if they're stale; doesn't refresh the key + // Brings m_imex_layout_key up to date; when it changes, re-resolves m_imex_active_mode and + // marks the zones stale. Reads per-plate mode first, falling back to the process preset. + // render() calls it once a frame, and ensure_imex_zones() for callers outside render(). + void refresh_imex_layout_key(); void calc_imex_ghosts(); // full rebuild void ensure_imex_ghosts(); // cached rebuild on key change - std::string build_imex_ghost_cache_key() const; - // Resolves the currently-active IMEX mode's tools string + primary physical head. - // Plate override wins; else falls back to process preset `imex_parallel_mode`. - // Returns false (and leaves outputs untouched) if IMEX is off, mode is empty/"primary", - // mode config is missing, the mode isn't listed, or no primary is found. - bool resolve_active_mode_tools(std::string& out_tools_str, int& out_primary_phys) const; void render_imex_zones(bool force_default_color); void refresh_imex_icon(); float icon_scale_factor() const; @@ -273,6 +285,9 @@ private: void render_icon_texture(GLModel &buffer, GLTexture &texture); void set_hover_tooltip(const std::string& tooltip); void render_icons(bool bottom, bool only_name = false, int hover_id = -1); + // Returns true when IMEX parallel mode is active (non-primary) AND the plate + // has objects assigned to more than one filament — a combination that warrants caution. + bool has_imex_multimaterial_conflict() const; void render_only_numbers(bool bottom); void render_plate_name_texture(); void invalidate_plate_name_texture(); @@ -419,7 +434,7 @@ public: void update_imex_ghost_colors(); // Invalidate the ghost cache so the next render_imex_zones() call rebuilds fully. - void invalidate_imex_ghosts() { m_imex_ghost_cache_key = "\x01"; } + void invalidate_imex_ghosts() { m_imex_ghost_layout_version.reset(); } std::vector get_plate_wrapping_detection_area() const; @@ -635,9 +650,6 @@ public: // Footprint of the prime tower as the scene draws it, in scaled plate-list coords. // Empty when the plate has no tower. Polygon imex_wipe_tower_hull() const; - // Returns true when IMEX parallel mode is active (non-primary) AND the plate - // has objects assigned to more than one filament — a combination that warrants caution. - bool has_imex_multimaterial_conflict() const; // Returns the primary-zone bounding box in mm when an IDEX/IQEX parallel mode is active. // Empty (nullopt) when IDEX/IQEX is off or the mode is "primary" (full-bed). std::optional imex_primary_zone() { ensure_imex_zones(); return m_imex_primary_zone_box; } diff --git a/tests/libslic3r/test_imex_zones.cpp b/tests/libslic3r/test_imex_zones.cpp index 67123854e8..a2f2bac22f 100644 --- a/tests/libslic3r/test_imex_zones.cpp +++ b/tests/libslic3r/test_imex_zones.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include #include #include @@ -577,3 +579,85 @@ TEST_CASE("an aggregated gantry raises its collision strip from the zone it pain } } } + +TEST_CASE("a plate's IMEX layout key changes with each input of its zones and ghosts", "[IMEXZones]") +{ + PrinterCfg p; + p.active_tools = "0:P,1:M"; + DynamicPrintConfig cfg = make_cfg(p); + cfg.set_key_value("imex_firmware_managed_zones", new ConfigOptionBool(false)); + std::string mode = kMode; + const DynamicPrintConfig* cfg_ptr = &cfg; + + std::optional key; + REQUIRE(update_imex_layout_key(key, cfg_ptr, mode)); + REQUIRE_FALSE(update_imex_layout_key(key, cfg_ptr, mode)); + + SECTION("imex_gantry_count") { cfg.set_key_value("imex_gantry_count", new ConfigOptionInt(2)); } + SECTION("imex_tools_per_gantry") { cfg.set_key_value("imex_tools_per_gantry", new ConfigOptionInt(3)); } + SECTION("imex_tool_layout") + { + cfg.set_deserialize_strict("imex_tool_layout", ConfigOptionEnum(ImexToolLayout::RearRight).serialize()); + } + // Clearances and the margin are compared exactly, so even 0.05 mm counts. + SECTION("imex_nozzle_clearance_x") { cfg.set_key_value("imex_nozzle_clearance_x", new ConfigOptionFloat(0.05)); } + SECTION("imex_nozzle_clearance_y") { cfg.set_key_value("imex_nozzle_clearance_y", new ConfigOptionFloat(0.05)); } + SECTION("imex_carriage_margin") { cfg.set_key_value("imex_carriage_margin", new ConfigOptionFloat(0.05)); } + SECTION("imex_firmware_managed_zones") { cfg.set_key_value("imex_firmware_managed_zones", new ConfigOptionBool(true)); } + SECTION("the plate's mode") { mode = kImexPrimaryMode; } + SECTION("the printer config going away") { cfg_ptr = nullptr; } + SECTION("the mode's roster, under the same name") { cfg.option("imex_mode_active_tools")->values[1] = "0:P,1:C"; } + // The plate's mode no longer names a row, so it has no zones. + SECTION("the mode's name in the table") { cfg.option("imex_mode_names")->values[1] = "renamed"; } + SECTION("another mode in the table") + { + cfg.option("imex_mode_names")->values.emplace_back("other"); + cfg.option("imex_mode_active_tools")->values.emplace_back("0:P,1:C"); + } + + CHECK(update_imex_layout_key(key, cfg_ptr, mode)); + CHECK_FALSE(update_imex_layout_key(key, cfg_ptr, mode)); +} + +TEST_CASE("a plate's IMEX layout key ignores a mode's G-code and settles on a NaN clearance", "[IMEXZones]") +{ + PrinterCfg p; + p.active_tools = "0:P,1:M"; + DynamicPrintConfig cfg = make_cfg(p); + cfg.set_key_value("imex_mode_gcodes", new ConfigOptionStrings(std::vector{ std::string(), "M400" })); + + std::optional key; + REQUIRE(update_imex_layout_key(key, &cfg, kMode)); + + SECTION("a mode's G-code, which moves no zone") + { + cfg.option("imex_mode_gcodes")->values[1] = "G28"; + CHECK_FALSE(update_imex_layout_key(key, &cfg, kMode)); + } + // A clearance field accepts "nan". Setting it changes the key once, then the key holds still. + SECTION("a NaN clearance") + { + cfg.set_key_value("imex_nozzle_clearance_x", new ConfigOptionFloat(std::nan(""))); + CHECK(update_imex_layout_key(key, &cfg, kMode)); + CHECK_FALSE(update_imex_layout_key(key, &cfg, kMode)); + } +} + +TEST_CASE("a plate's IMEX layout key ignores the IMEX inputs while IMEX is off", "[IMEXZones]") +{ + PrinterCfg p; + p.is_imex = false; + p.active_tools = "0:P,1:M"; + DynamicPrintConfig cfg = make_cfg(p); + + std::optional key; + REQUIRE(update_imex_layout_key(key, &cfg, kMode)); + cfg.set_key_value("imex_nozzle_clearance_x", new ConfigOptionFloat(5.0)); + cfg.option("imex_mode_active_tools")->values[1] = "0:P,1:C"; + CHECK_FALSE(update_imex_layout_key(key, &cfg, "other")); + // No printer config at all reads the same as IMEX off. + CHECK_FALSE(update_imex_layout_key(key, nullptr, kMode)); + + cfg.set_key_value("is_imex", new ConfigOptionBool(true)); + CHECK(update_imex_layout_key(key, &cfg, kMode)); +}