From ab2404393790fb8f6c6e13e0bb455b2d1cda4b38 Mon Sep 17 00:00:00 2001 From: yw4z Date: Mon, 26 Oct 2026 21:25:16 +0300 Subject: [PATCH] Make plate renames undoable and refresh G-code that uses {plate_name} (#16900) Renaming a plate now creates an undo snapshot, so a rename is its own undo step and marks the project as modified. All four rename paths use the new `Plater::rename_plate()`. When a G-code template uses `{plate_name}`, renaming a sliced plate invalidates only the G-code export step, so the next slice only re-exports the G-code. `filename_format` is excluded because the file name is built from the current plate name at export time. The "Rename Plate" undo snapshot is from #16141. Co-authored-by: cook0001 <79376116+cook0001@users.noreply.github.com> --- src/slic3r/GUI/GUI_ObjectList.cpp | 5 +---- src/slic3r/GUI/NativeCommands.cpp | 2 +- src/slic3r/GUI/PartPlate.cpp | 33 +++++++++++++++++++++++++---- src/slic3r/GUI/PartPlate.hpp | 3 +++ src/slic3r/GUI/Plater.cpp | 26 +++++++++++++++++++++-- src/slic3r/GUI/Plater.hpp | 2 ++ tests/fff_print/test_print.cpp | 35 +++++++++++++++++++++++++++++++ 7 files changed, 95 insertions(+), 11 deletions(-) diff --git a/src/slic3r/GUI/GUI_ObjectList.cpp b/src/slic3r/GUI/GUI_ObjectList.cpp index 2a9076dcd1..4bd988e3fb 100644 --- a/src/slic3r/GUI/GUI_ObjectList.cpp +++ b/src/slic3r/GUI/GUI_ObjectList.cpp @@ -1202,10 +1202,7 @@ void ObjectList::update_name_in_model(const wxDataViewItem& item) const int plate_idx = -1; const ItemType type0 = m_objects_model->GetItemType(item, plate_idx); if (plate_idx >= 0) { - auto plate = wxGetApp().plater()->get_partplate_list().get_plate(plate_idx); - if (plate->get_plate_name() != name) { - plate->set_plate_name(name); - } + wxGetApp().plater()->rename_plate(plate_idx, name); m_objects_model->SetCurSelectedPlateFullName(plate_idx, name); } return; diff --git a/src/slic3r/GUI/NativeCommands.cpp b/src/slic3r/GUI/NativeCommands.cpp index 6f3cea8096..d396bda3e3 100644 --- a/src/slic3r/GUI/NativeCommands.cpp +++ b/src/slic3r/GUI/NativeCommands.cpp @@ -475,7 +475,7 @@ std::vector build_command_catalog() PlateNameEditDialog dlg((wxWindow*) wxGetApp().mainframe, wxID_ANY, _L("Edit Plate Name")); dlg.set_plate_name(from_u8(curr->get_plate_name())); if (dlg.ShowModal() == wxID_YES) - curr->set_plate_name(dlg.get_plate_name().ToUTF8().data()); + plater->rename_plate(plater->get_partplate_list().get_curr_plate_index(), dlg.get_plate_name().ToUTF8().data()); return AppActionRunResult{AppActionRunResult::Level::Success}; }); add_with_icon("plate_toggle_lock", L("Toggle Plate Lock"), L("Plate"), "lock_normal", [](const std::string&) { diff --git a/src/slic3r/GUI/PartPlate.cpp b/src/slic3r/GUI/PartPlate.cpp index f0d4f3f6c6..8cf57fd431 100644 --- a/src/slic3r/GUI/PartPlate.cpp +++ b/src/slic3r/GUI/PartPlate.cpp @@ -4335,6 +4335,32 @@ void PartPlate::invalidate_plate_name_texture() } } +// True if a G-code template uses {plate_name}. filename_format is skipped: the file name is +// built from the current plate name at export time, so it never needs a new export. +static bool uses_plate_name(const DynamicPrintConfig& config) +{ + for (const std::string& key : config.keys()) { + if (key == "filename_format") + continue; + const ConfigOption* opt = config.option(key); + if ((opt->type() == coString || opt->type() == coStrings) && opt->serialize().find("plate_name") != std::string::npos) + return true; + } + return false; +} + +void PartPlate::sync_plate_name_to_print() +{ + if (m_print == nullptr || m_print->get_plate_name() == m_name) + return; + // The G-code may hold the old name. Invalidate first so a running export stops. + if (!(m_plater && m_plater->is_loading_project()) && uses_plate_name(m_print->full_print_config())) { + m_print->set_gcode_file_invalidated(); + update_slice_result_valid_state(false); + } + m_print->set_plate_name(m_name); +} + void PartPlate::set_plate_name(const std::string& name) { // compare if name equal to m_name, case sensitive @@ -4343,8 +4369,7 @@ void PartPlate::set_plate_name(const std::string& name) const std::string previous_name = m_name; m_name = name; - if (m_print != nullptr) - m_print->set_plate_name(name); + sync_plate_name_to_print(); invalidate_plate_name_texture(); @@ -4385,8 +4410,8 @@ void PartPlate::set_print(PrintBase* print, GCodeResult* result, int index) m_print->set_plate_origin(m_origin); invalidate_exclusion_volume_preview(); - // Keep a newly attached Print synchronized with the plate metadata used for output filenames. - m_print->set_plate_name(m_name); + // Undo/redo reattaches Prints that may still carry another name. + sync_plate_name_to_print(); return; } diff --git a/src/slic3r/GUI/PartPlate.hpp b/src/slic3r/GUI/PartPlate.hpp index d90e6f2767..bfaf9c0318 100644 --- a/src/slic3r/GUI/PartPlate.hpp +++ b/src/slic3r/GUI/PartPlate.hpp @@ -218,6 +218,9 @@ private: wxCoord m_name_texture_width; wxCoord m_name_texture_height; + // Copies m_name to the Print; invalidates the G-code export if a G-code template uses {plate_name}. + void sync_plate_name_to_print(); + void init(); bool valid_instance(int obj_id, int instance_id) const; void generate_print_polygon(ExPolygon &print_polygon); diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 61d3c3a82e..e8df816036 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -23201,7 +23201,7 @@ void Plater::open_platesettings_dialog(wxCommandEvent& evt) { dlg.set_plate_name(from_u8(curr_plate->get_plate_name())); dlg.ShowModal(); - curr_plate->set_plate_name(dlg.get_plate_name().ToUTF8().data()); + rename_plate(p->partplate_list.get_curr_plate_index(), dlg.get_plate_name().ToUTF8().data()); } void Plater::open_filament_map_setting_dialog(wxCommandEvent &evt) @@ -23459,7 +23459,7 @@ int Plater::select_plate_by_hover_id(int hover_id, bool right_click, bool isModi int result=dlg.ShowModal(); if (result == wxID_YES) { wxString dlg_plate_name = dlg.get_plate_name(); - curr_plate->set_plate_name(dlg_plate_name.ToUTF8().data()); + rename_plate(p->partplate_list.get_curr_plate_index(), dlg_plate_name.ToUTF8().data()); } } else { BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << "can not select plate %1%" << plate_index; @@ -23905,6 +23905,28 @@ bool Plater::can_delete_all() const { return p->can_delete_all(); } bool Plater::can_add_model() const { return !is_background_process_slicing(); } bool Plater::can_add_plate() const { return !is_background_process_slicing() && p->can_add_plate(); } bool Plater::can_delete_plate() const { return p->can_delete_plate(); } + +void Plater::rename_plate(int plate_index, const std::string& name) +{ + PartPlate* plate = p->partplate_list.get_plate(plate_index); + if (plate == nullptr || plate->get_plate_name() == name) + return; + + take_snapshot(_u8L("Rename Plate")); + const bool was_valid = plate->is_slice_result_valid(); + plate->set_plate_name(name); + + // The rename invalidated a G-code export that uses {plate_name}: refresh the current plate. + if (was_valid && !plate->is_slice_result_valid() && plate_index == p->partplate_list.get_curr_plate_index()) { + p->main_frame->update_slice_print_status(MainFrame::eEventPlateUpdate, true); + // Start one re-export: auto-reslice would cancel a background run started here. + if (p->background_processing_enabled()) + p->restart_background_process(priv::UPDATE_BACKGROUND_PROCESS_FORCE_RESTART); + else + p->schedule_auto_reslice_if_needed(); + } +} + bool Plater::can_increase_instances() const { return p->can_increase_instances(); } bool Plater::can_decrease_instances() const { return p->can_decrease_instances(); } bool Plater::can_set_instance_to_object() const { return p->can_set_instance_to_object(); } diff --git a/src/slic3r/GUI/Plater.hpp b/src/slic3r/GUI/Plater.hpp index e4764487e4..9f96f45c59 100644 --- a/src/slic3r/GUI/Plater.hpp +++ b/src/slic3r/GUI/Plater.hpp @@ -791,6 +791,8 @@ public: bool can_add_model() const; bool can_add_plate() const; bool can_delete_plate() const; + // Undoable rename; use for user edits instead of PartPlate::set_plate_name(). + void rename_plate(int plate_index, const std::string& name); bool can_increase_instances() const; bool can_decrease_instances() const; bool can_set_instance_to_object() const; diff --git a/tests/fff_print/test_print.cpp b/tests/fff_print/test_print.cpp index 0759f4cd96..de0cf4b864 100644 --- a/tests/fff_print/test_print.cpp +++ b/tests/fff_print/test_print.cpp @@ -57,6 +57,7 @@ #include #include #include +#include #include #include #include @@ -863,6 +864,40 @@ TEST_CASE("Custom G-code placeholders are substituted", "[Print]") CHECK(per_layer.find(";Layer:200 (0.1 mm) max 20\n") != std::string::npos); } +// A finished export is reused, so a renamed plate needs the export step invalidated. +TEST_CASE("Plate name reaches the G-code once G-code export is invalidated", "[Print]") +{ + Print print; + Model model; + init_print({ cube(20) }, print, model, { { "machine_start_gcode", "; Plate {plate_name}" } }); + print.set_status_silent(); + + ScopedTemporaryFile output(".gcode"); + const auto export_and_read = [&print, &output]() { + print.process(); + print.export_gcode(output.string(), nullptr, nullptr); + std::ifstream in(output.string(), std::ios::binary); + return std::string((std::istreambuf_iterator(in)), std::istreambuf_iterator()); + }; + + print.set_plate_name("First"); + CHECK(export_and_read().find("; Plate First") != std::string::npos); + REQUIRE(print.finished()); + + // Renaming alone keeps the old export. + print.set_plate_name("Second"); + CHECK(print.finished()); + CHECK(export_and_read().find("; Plate First") != std::string::npos); + + // Invalidating the export step re-emits the name without re-slicing. + print.set_gcode_file_invalidated(); + CHECK_FALSE(print.finished()); + CHECK(print.objects().front()->is_step_done(posSlice)); + const std::string regenerated = export_and_read(); + CHECK(regenerated.find("; Plate Second") != std::string::npos); + CHECK(regenerated.find("; Plate First") == std::string::npos); +} + // The layer change G-code runs before its layer prints, so the layer mass it gets is the previous layer's. TEST_CASE("Layer change G-code gets the previous layer's mass under both placeholder names", "[Print]") {