From 4b8f9a4cc60021fe5e57907bb9846e92bc5ffe10 Mon Sep 17 00:00:00 2001 From: "zhou.xu" Date: Sat, 9 Dec 2023 16:26:24 +0800 Subject: [PATCH] NEW:add "bed type to follow machine preset" function Jira: STUDIO-5535 Change-Id: I2ef99a0a783a074d9379685adde11fddfd6f8cec --- src/libslic3r/AppConfig.cpp | 3 +- src/libslic3r/Preset.hpp | 2 + src/libslic3r/PresetBundle.cpp | 3 + src/slic3r/GUI/GLCanvas3D.cpp | 4 + src/slic3r/GUI/GUI_App.hpp | 2 +- src/slic3r/GUI/Plater.cpp | 124 ++++++++++++++++++++++++---- src/slic3r/GUI/Plater.hpp | 15 +++- src/slic3r/GUI/Preferences.cpp | 5 +- src/slic3r/GUI/Tab.cpp | 14 +++- src/slic3r/GUI/Widgets/ComboBox.hpp | 2 +- 10 files changed, 150 insertions(+), 24 deletions(-) diff --git a/src/libslic3r/AppConfig.cpp b/src/libslic3r/AppConfig.cpp index 3daed893c..a45d979af 100644 --- a/src/libslic3r/AppConfig.cpp +++ b/src/libslic3r/AppConfig.cpp @@ -172,7 +172,8 @@ void AppConfig::set_defaults() if (get("zoom_to_mouse").empty()) set_bool("zoom_to_mouse", false); - + if (get("user_bed_type").empty()) + set_bool("user_bed_type", true); //#ifdef SUPPORT_SHOW_HINTS if (get("show_hints").empty()) set_bool("show_hints", true); diff --git a/src/libslic3r/Preset.hpp b/src/libslic3r/Preset.hpp index 65534f0fa..61e05f635 100644 --- a/src/libslic3r/Preset.hpp +++ b/src/libslic3r/Preset.hpp @@ -57,6 +57,7 @@ #define BBL_JSON_KEY_FAMILY "family" #define BBL_JSON_KEY_BED_MODEL "bed_model" #define BBL_JSON_KEY_BED_TEXTURE "bed_texture" +#define BBL_JSON_KEY_DEFAULT_BED_TYPE "default_bed_type" #define BBL_JSON_KEY_HOTEND_MODEL "hotend_model" #define BBL_JSON_KEY_DEFAULT_MATERIALS "default_materials" #define BBL_JSON_KEY_MODEL_ID "model_id" @@ -112,6 +113,7 @@ public: // Vendor & Printer Model specific print bed model & texture. std::string bed_model; std::string bed_texture; + std::string default_bed_type; std::string hotend_model; PrinterVariant* variant(const std::string &name) { diff --git a/src/libslic3r/PresetBundle.cpp b/src/libslic3r/PresetBundle.cpp index 3b630ee4d..0e763c5c5 100644 --- a/src/libslic3r/PresetBundle.cpp +++ b/src/libslic3r/PresetBundle.cpp @@ -3170,6 +3170,9 @@ std::pair PresetBundle::load_vendor_configs_ else if (boost::iequals(it.key(), BBL_JSON_KEY_BED_MODEL)) { //get bed model model.bed_model = it.value(); + } else if (boost::iequals(it.key(), BBL_JSON_KEY_DEFAULT_BED_TYPE)) { + // get bed type + model.default_bed_type = it.value(); } else if (boost::iequals(it.key(), BBL_JSON_KEY_BED_TEXTURE)) { //get bed texture diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 25c9f3ce1..39e7eef81 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -2900,6 +2900,10 @@ void GLCanvas3D::on_idle(wxIdleEvent& evt) m_dirty |= m_assemble_view_toolbar.update_items_state(); // BBS //m_dirty |= wxGetApp().plater()->get_view_toolbar().update_items_state(); + m_dirty |= wxGetApp().plater()->sidebar().get_update_3d_state(); + if (wxGetApp().plater()->sidebar().get_update_3d_state()) { + wxGetApp().plater()->sidebar().cancel_update_3d_state(); + } m_dirty |= wxGetApp().plater()->get_collapse_toolbar().update_items_state(); _update_imgui_select_plate_toolbar(); bool mouse3d_controller_applied = wxGetApp().plater()->get_mouse3d_controller().apply(wxGetApp().plater()->get_camera()); diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp index 3eca6bc86..d3ce76fe9 100644 --- a/src/slic3r/GUI/GUI_App.hpp +++ b/src/slic3r/GUI/GUI_App.hpp @@ -314,7 +314,7 @@ public: //explicit GUI_App(EAppMode mode = EAppMode::Editor); ~GUI_App() override; - + bool get_app_conf_exists() { return m_app_conf_exists; } void show_message_box(std::string msg) { wxMessageBox(msg); } EAppMode get_app_mode() const { return m_app_mode; } Slic3r::DeviceManager* getDeviceManager() { return m_device_manager; } diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index b31620417..8ab219d26 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -573,6 +573,7 @@ Sidebar::Sidebar(Plater *parent) // p->editing_filament = -1; // wxGetApp().params_dialog()->Popup(); // wxGetApp().get_tab(Preset::TYPE_FILAMENT)->restore_last_select_item(); + m_soft_first_start = false; wxGetApp().run_wizard(ConfigWizard::RR_USER, ConfigWizard::SP_PRINTERS); }); @@ -618,6 +619,7 @@ Sidebar::Sidebar(Plater *parent) edit_btn->SetToolTip(_L("Click to edit preset")); edit_btn->Bind(wxEVT_BUTTON, [this, combo_printer](wxCommandEvent) { + m_soft_first_start = false; p->editing_filament = -1; if (combo_printer->switch_to_tab()) p->editing_filament = 0; @@ -1097,13 +1099,29 @@ void Sidebar::remove_unused_filament_combos(const size_t current_extruder_count) } } +void Sidebar::set_bed_by_curr_bed_type(AppConfig *config) { + if (config && !config->get("curr_bed_type").empty()) { + int bed_type_idx = 0; + std::string str_bed_type = config->get("curr_bed_type"); + int bed_type_value = (int) btPC; + try { + bed_type_value = atoi(str_bed_type.c_str()); + } catch (...) { + } + bed_type_idx = bed_type_value - 1; + m_bed_type_list->SelectAndNotify(bed_type_idx); + } else { + BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << ":error:curr_bed_type is empty"; + } +} + void Sidebar::update_all_preset_comboboxes() { PresetBundle &preset_bundle = *wxGetApp().preset_bundle; const auto print_tech = preset_bundle.printers.get_edited_preset().printer_technology(); bool is_bbl_preset = preset_bundle.printers.get_edited_preset().is_bbl_vendor_preset(&preset_bundle); - + auto cur_preset_name = preset_bundle.printers.get_edited_preset().name; auto p_mainframe = wxGetApp().mainframe; p_mainframe->show_device(is_bbl_preset); @@ -1115,18 +1133,38 @@ void Sidebar::update_all_preset_comboboxes() //update print button default value for bbl or third-party printer p_mainframe->set_print_button_to_default(MainFrame::PrintSelectType::ePrintPlate); AppConfig* config = wxGetApp().app_config; - if (config && !config->get("curr_bed_type").empty()) { - int bed_type_idx = 0; - std::string str_bed_type = config->get("curr_bed_type"); - int bed_type_value = (int)btPC; - try { - bed_type_value = atoi(str_bed_type.c_str()); - } catch(...) {} - bed_type_idx = bed_type_value - 1; - m_bed_type_list->SelectAndNotify(bed_type_idx); + if (config) { + m_update_3d_state = true; + if (m_soft_first_start && !wxGetApp().get_app_conf_exists()) { + use_default_bed_type(); + } else { + auto user_bed_type_flag = config->get("user_bed_type") == "true"; + if (!user_bed_type_flag) { //bed_type not follow machine + set_bed_by_curr_bed_type(config); + } else {//bed_type follow machine + if (m_soft_first_start || m_is_gcode_file) { + if (m_is_gcode_file) { + m_is_gcode_file = false; + } + // should include soft close then open ,.gcode.3mf case + set_bed_by_curr_bed_type(config); + } + else if (user_bed_type_flag) { + if (config->has_section("user_bed_type_list")) { + auto user_bed_type_list = config->get_section("user_bed_type_list"); + if (user_bed_type_list.size() > 0 && user_bed_type_list[cur_preset_name].size() > 0) { + set_bed_type(user_bed_type_list[cur_preset_name]); + } else { + use_default_bed_type(); + } + } else { + use_default_bed_type(); + } + } + } + } } else { - BedType bed_type = preset_bundle.printers.get_edited_preset().get_default_bed_type(&preset_bundle); - m_bed_type_list->SelectAndNotify((int)bed_type - 1); + BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << ":error:AppConfig is nullptr"; } m_bed_type_list->Enable(); } else { @@ -1290,6 +1328,50 @@ void Sidebar::update_presets_from_to(Slic3r::Preset::Type preset_type, std::stri BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << boost::format(": exit!"); } +bool Sidebar::set_bed_type(const std::string &bed_type_name) +{ + auto bed_type_keys = print_config_def.get("curr_bed_type")->enum_values; + for (size_t i = 0; i < bed_type_keys.size(); i++) { + if (bed_type_name == bed_type_keys[i]) { + m_bed_type_list->SelectAndNotify(i); + return true; + } + } + return false; +} + +void Sidebar::save_bed_type_to_config(const std::string &bed_type_name) +{ + PresetBundle &preset_bundle = *wxGetApp().preset_bundle; + auto cur_preset_name = preset_bundle.printers.get_edited_preset().name; + if (cur_preset_name.size() > 0) { + if (!wxGetApp().app_config->has_section("user_bed_type_list")) { + std::map data; + data[cur_preset_name] = bed_type_name; + wxGetApp().app_config->set_section("user_bed_type_list", data); + } else { + auto data = wxGetApp().app_config->get_section("user_bed_type_list"); + auto data_modify = const_cast *>(&data); + (*data_modify)[cur_preset_name] = bed_type_name; + wxGetApp().app_config->set_section("user_bed_type_list", *data_modify); + } + } +} + +bool Sidebar::use_default_bed_type(bool is_bbl_preset) +{ + auto bundle = wxGetApp().preset_bundle; + const Preset *curr = &bundle->printers.get_selected_preset(); + const VendorProfile::PrinterModel *pm = PresetUtils::system_printer_model(*curr); + if (is_bbl_preset && pm && pm->default_bed_type.size() > 0) { + return set_bed_type(pm->default_bed_type); + } + int selection = m_bed_type_list->GetSelection(); + std::string bed_type_name = print_config_def.get("curr_bed_type")->enum_values[selection]; + save_bed_type_to_config(bed_type_name); + return false; +} + void Sidebar::change_top_border_for_mode_sizer(bool increase_border) { // BBS @@ -3599,7 +3681,10 @@ std::vector Plater::priv::load_files(const std::vector& input_ if (app_config) app_config->set("curr_bed_type", std::to_string(int(bed_type))); } - q->on_bed_type_change(bed_type); + q->on_bed_type_change(bed_type, (boost::algorithm::ends_with(filename.string(), ".gcode.3mf") || + boost::algorithm::ends_with(filename.string(), ".gcode")) ? + true : + false); } // BBS: moved this logic to presetcollection @@ -5874,6 +5959,10 @@ void Plater::priv::set_current_panel(wxPanel* panel, bool no_slice) // BBS void Plater::priv::on_combobox_select(wxCommandEvent &evt) { + ComboBox *combo = static_cast(evt.GetEventObject()); + if (combo && combo->is_drop_down()) { + sidebar->update_soft_first_start_state(); + } PlaterPresetComboBox* preset_combo_box = dynamic_cast(evt.GetEventObject()); if (preset_combo_box) { this->on_select_preset(evt); @@ -5892,7 +5981,10 @@ void Plater::priv::on_select_bed_type(wxCommandEvent &evt) PresetBundle& preset_bundle = *wxGetApp().preset_bundle; DynamicPrintConfig& proj_config = wxGetApp().preset_bundle->project_config; const t_config_enum_values* keys_map = print_config_def.get("curr_bed_type")->enum_keys_map; - + auto user_bed_type_flag = wxGetApp().app_config->get("user_bed_type") == "true"; + if (combo && combo->is_drop_down() && user_bed_type_flag) { // save user behavior + sidebar->save_bed_type_to_config(bed_type_name); + } if (keys_map) { BedType new_bed_type = btCount; for (auto item : *keys_map) { @@ -11616,8 +11708,8 @@ void Plater::on_filaments_change(size_t num_filaments) } } -void Plater::on_bed_type_change(BedType bed_type) -{ +void Plater::on_bed_type_change(BedType bed_type, bool is_gcode_file) { + sidebar().set_is_gcode_file(is_gcode_file); sidebar().on_bed_type_change(bed_type); } diff --git a/src/slic3r/GUI/Plater.hpp b/src/slic3r/GUI/Plater.hpp index be344bc2a..c48450d80 100644 --- a/src/slic3r/GUI/Plater.hpp +++ b/src/slic3r/GUI/Plater.hpp @@ -115,12 +115,15 @@ public: void create_printer_preset(); void init_filament_combo(PlaterPresetComboBox **combo, const int filament_idx); void remove_unused_filament_combos(const size_t current_extruder_count); + void set_bed_by_curr_bed_type(AppConfig *config); void update_all_preset_comboboxes(); //void update_partplate(PartPlateList& list); void update_presets(Slic3r::Preset::Type preset_type); //BBS void update_presets_from_to(Slic3r::Preset::Type preset_type, std::string from, std::string to); - + bool set_bed_type(const std::string& bed_type_name); + void save_bed_type_to_config(const std::string &bed_type_name); + bool use_default_bed_type(bool is_bbl_preset = true); void change_top_border_for_mode_sizer(bool increase_border); void msw_rescale(); void sys_color_changed(); @@ -172,7 +175,10 @@ public: std::vector& combos_filament(); Search::OptionsSearcher& get_searcher(); std::string& get_search_line(); - + void set_is_gcode_file(bool flag) { m_is_gcode_file = flag; } + void update_soft_first_start_state() { m_soft_first_start = false; } + void cancel_update_3d_state() { m_update_3d_state = false; } + bool get_update_3d_state() { return m_update_3d_state; } private: struct priv; std::unique_ptr p; @@ -181,6 +187,9 @@ private: ComboBox* m_bed_type_list = nullptr; ScalableButton* connection_btn = nullptr; ScalableButton* ams_btn = nullptr; + bool m_soft_first_start {true }; + bool m_is_gcode_file{ false }; + bool m_update_3d_state{false}; }; class Plater: public wxPanel @@ -406,7 +415,7 @@ public: void on_filaments_change(size_t extruders_count); // BBS - void on_bed_type_change(BedType bed_type); + void on_bed_type_change(BedType bed_type,bool is_gcode_file = false); bool update_filament_colors_in_full_config(); void config_change_notification(const DynamicPrintConfig &config, const std::string& key); void on_config_change(const DynamicPrintConfig &config); diff --git a/src/slic3r/GUI/Preferences.cpp b/src/slic3r/GUI/Preferences.cpp index fc709a67d..320fd63c4 100644 --- a/src/slic3r/GUI/Preferences.cpp +++ b/src/slic3r/GUI/Preferences.cpp @@ -995,7 +995,9 @@ wxWindow* PreferencesDialog::create_general_page() auto item_currency = create_item_combobox(_L("Units"), page, _L("Units"), "use_inches", Units); auto item_mouse_zoom_settings = create_item_checkbox(_L("Zoom to mouse position"), page, _L("Zoom in towards the mouse pointer's position in the 3D view, rather than the 2D window center."), 50, "zoom_to_mouse"); - + auto item_bed_type_follow_preset = create_item_checkbox(_L("Auto Bed Type"), page, + _L("Studio will remember build plate selected last time for certain printer model."), 50, + "user_bed_type"); //auto item_hints = create_item_checkbox(_L("Show \"Tip of the day\" notification after start"), page, _L("If enabled, useful hints are displayed at startup."), 50, "show_hints"); auto item_calc_mode = create_item_checkbox(_L("Flushing volumes: Auto-calculate everytime the color changed."), page, _L("If enabled, auto-calculate everytime the color changed."), 50, "auto_calculate"); auto title_presets = create_item_title(_L("Presets"), page, _L("Presets")); @@ -1054,6 +1056,7 @@ wxWindow* PreferencesDialog::create_general_page() sizer_page->Add(item_region, 0, wxTOP, FromDIP(3)); sizer_page->Add(item_currency, 0, wxTOP, FromDIP(3)); sizer_page->Add(item_mouse_zoom_settings, 0, wxTOP, FromDIP(3)); + sizer_page->Add(item_bed_type_follow_preset, 0, wxTOP, FromDIP(3)); //sizer_page->Add(item_hints, 0, wxTOP, FromDIP(3)); sizer_page->Add(item_calc_mode, 0, wxTOP, FromDIP(3)); sizer_page->Add(title_presets, 0, wxTOP | wxEXPAND, FromDIP(20)); diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index 52e03482c..f2d376389 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -5135,7 +5135,19 @@ void Tab::delete_preset() //wxID_YES != wxMessageDialog(parent(), msg, title, wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION).ShowModal()) wxID_YES == MessageDialog(parent(), msg, title, wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION).ShowModal())) return; - + auto delete_cur_bed_type_to_config = [this]() { + PresetBundle &preset_bundle = *wxGetApp().preset_bundle; + auto cur_preset_name = preset_bundle.printers.get_edited_preset().name; + if (cur_preset_name.size() > 0 && wxGetApp().app_config->has_section("user_bed_type_list")) { + auto data = wxGetApp().app_config->get_section("user_bed_type_list"); + auto data_modify = const_cast *>(&data); + if ((*data_modify).find(cur_preset_name) != data_modify->end()) { + data_modify->erase(cur_preset_name); + wxGetApp().app_config->set_section("user_bed_type_list", *data_modify); + } + } + }; + delete_cur_bed_type_to_config(); // if we just delete preset from the physical printer if (m_presets_choice->is_selected_physical_printer()) { PhysicalPrinter& printer = physical_printers.get_selected_printer(); diff --git a/src/slic3r/GUI/Widgets/ComboBox.hpp b/src/slic3r/GUI/Widgets/ComboBox.hpp index e79ca3ee5..82b719d6f 100644 --- a/src/slic3r/GUI/Widgets/ComboBox.hpp +++ b/src/slic3r/GUI/Widgets/ComboBox.hpp @@ -61,7 +61,7 @@ public: wxBitmap GetItemBitmap(unsigned int n); void SetItemBitmap(unsigned int n, wxBitmap const &bitmap); - + bool is_drop_down(){return drop_down;} protected: virtual int DoInsertItems(const wxArrayStringsAdapter &items, unsigned int pos,