ENH: [STUDIO-4047] Export presets dialog

Jira: 4047

Change-Id: I91c5b684784c6c3e31fa0ac53698976732f843cf
This commit is contained in:
maosheng.wei 2023-09-12 19:46:55 +08:00 committed by Lane.Wei
parent 4ebbb7db42
commit 998f53a585
11 changed files with 852 additions and 341 deletions

View File

@ -525,7 +525,7 @@ void Preset::save(DynamicPrintConfig* parent_config)
temp_config.save_to_json(this->file, this->name, from_str, this->version.to_string(), this->custom_defined);
} else if (!filament_id.empty() && inherits().empty()) {
DynamicPrintConfig temp_config = config;
temp_config.set_key_value("filament_id", new ConfigOptionString(filament_id));
temp_config.set_key_value(BBL_JSON_KEY_FILAMENT_ID, new ConfigOptionString(filament_id));
temp_config.save_to_json(this->file, this->name, from_str, this->version.to_string(), this->custom_defined);
} else {
this->config.save_to_json(this->file, this->name, from_str, this->version.to_string(), this->custom_defined);

View File

@ -57,7 +57,6 @@
#define BBL_JSON_KEY_DEFAULT_MATERIALS "default_materials"
#define BBL_JSON_KEY_MODEL_ID "model_id"
namespace Slic3r {
class AppConfig;

View File

@ -21,6 +21,7 @@
#include <boost/property_tree/ptree.hpp>
#include <boost/locale.hpp>
#include <boost/log/trivial.hpp>
#include <miniz/miniz.h>
// Store the print/filament/printer presets into a "presets" subdirectory of the Slic3rPE config dir.
@ -646,6 +647,90 @@ PresetsConfigSubstitutions PresetBundle::import_presets(std::vector<std::string>
std::vector<std::string> result;
for (auto &file : files) {
if (Slic3r::is_json_file(file)) {
import_json_presets(substitutions, file, override_confirm, rule, overwrite, result);
}
// Determine if it is a preset bundle
if (boost::iends_with(file, ".bbscfg") || boost::iends_with(file, ".bbsflmt") || boost::iends_with(file, ".zip")) {
boost::system::error_code ec;
// create user folder
fs::path user_folder(data_dir() + "/" + PRESET_USER_DIR);
if (!fs::exists(user_folder)) fs::create_directory(user_folder, ec);
if (ec) BOOST_LOG_TRIVIAL(error) << "create directory failed: " << ec.message();
// create default folder
fs::path default_folder(user_folder / DEFAULT_USER_FOLDER_NAME);
if (!fs::exists(default_folder)) fs::create_directory(default_folder, ec);
if (ec) BOOST_LOG_TRIVIAL(error) << "create directory failed: " << ec.message();
//create temp folder
//std::string user_default_temp_dir = data_dir() + "/" + PRESET_USER_DIR + "/" + DEFAULT_USER_FOLDER_NAME + "/" + "temp";
fs::path temp_folder(default_folder / "temp");
std::string user_default_temp_dir = temp_folder.make_preferred().string();
if (fs::exists(temp_folder)) fs::remove_all(temp_folder);
fs::create_directory(temp_folder, ec);
if (ec) BOOST_LOG_TRIVIAL(error) << "create directory failed: " << ec.message();
file = boost::filesystem::path(file).make_preferred().string();
mz_zip_archive zip_archive;
mz_zip_zero_struct(&zip_archive);
mz_bool status;
/*if (!open_zip_reader(&zip_archive, file)) {
BOOST_LOG_TRIVIAL(info) << "Failed to initialize reader ZIP archive";
return substitutions;
} else {
BOOST_LOG_TRIVIAL(info) << "Success to initialize reader ZIP archive";
}*/
FILE *zipFile = boost::nowide::fopen(file.c_str(), "rb");
status = mz_zip_reader_init_cfile(&zip_archive, zipFile, 0, MZ_ZIP_FLAG_CASE_SENSITIVE | MZ_ZIP_FLAG_IGNORE_PATH);
if (MZ_FALSE == status) {
BOOST_LOG_TRIVIAL(info) << "Failed to initialize reader ZIP archive";
return substitutions;
} else {
BOOST_LOG_TRIVIAL(info) << "Success to initialize reader ZIP archive";
}
// Extract Files
int num_files = mz_zip_reader_get_num_files(&zip_archive);
for (int i = 0; i < num_files; i++) {
mz_zip_archive_file_stat file_stat;
status = mz_zip_reader_file_stat(&zip_archive, i, &file_stat);
if (status) {
std::string file_name = file_stat.m_filename;
BOOST_LOG_TRIVIAL(info) << "Form zip file: " << file << ". Read file name: " << file_stat.m_filename;
size_t index = file_name.find_last_of('/');
if (std::string::npos != index) {
file_name = file_name.substr(index + 1);
}
if (BUNDLE_STRUCTURE_JSON_NAME == file_name) continue;
// create target file path
std::string target_file_path = boost::filesystem::path(temp_folder / file_name).make_preferred().string();
status = mz_zip_reader_extract_to_file(&zip_archive, i, target_file_path.c_str(), MZ_ZIP_FLAG_CASE_SENSITIVE);
// target file is opened
if (MZ_FALSE == status) {
BOOST_LOG_TRIVIAL(info) << "Failed to open target file: " << target_file_path;
} else {
import_json_presets(substitutions, target_file_path, override_confirm, rule, overwrite, result);
BOOST_LOG_TRIVIAL(info) << "Successed to open target file: " << target_file_path;
}
}
}
fclose(zipFile);
if (fs::exists(temp_folder)) fs::remove_all(temp_folder, ec);
if (ec) BOOST_LOG_TRIVIAL(error) << "create directory failed: " << ec.message();
}
}
files = result;
return substitutions;
}
bool PresetBundle::import_json_presets(PresetsConfigSubstitutions & substitutions,
std::string & file,
std::function<int(std::string const &)> override_confirm,
ForwardCompatibilitySubstitutionRule rule,
int & overwrite,
std::vector<std::string> & result)
{
try {
DynamicPrintConfig config;
// BBS: change to json format
@ -656,11 +741,11 @@ PresetsConfigSubstitutions PresetBundle::import_presets(std::vector<std::string>
std::string name = key_values[BBL_JSON_KEY_NAME];
std::string version_str = key_values[BBL_JSON_KEY_VERSION];
boost::optional<Semver> version = Semver::parse(version_str);
if (!version) continue;
if (!version) return false;
Semver app_version = *(Semver::parse(SLIC3R_VERSION));
if (version->maj() != app_version.maj()) {
BOOST_LOG_TRIVIAL(warning) << "Preset incompatibla, not loading: " << name;
continue;
return false;
}
PresetCollection *collection = nullptr;
@ -672,19 +757,19 @@ PresetsConfigSubstitutions PresetBundle::import_presets(std::vector<std::string>
collection = &filaments;
if (collection == nullptr) {
BOOST_LOG_TRIVIAL(warning) << "Preset type is unknown, not loading: " << name;
continue;
return false;
}
if (overwrite == 0) overwrite = 1;
if (auto p = collection->find_preset(name, false)) {
if (p->is_default || p->is_system) {
BOOST_LOG_TRIVIAL(warning) << "Preset already present and is system preset, not loading: " << name;
continue;
return false;
}
overwrite = override_confirm(name);
}
if (overwrite == 0 || overwrite == 2) {
BOOST_LOG_TRIVIAL(warning) << "Preset already present, not loading: " << name;
continue;
return false;
}
DynamicPrintConfig new_config;
@ -704,7 +789,7 @@ PresetsConfigSubstitutions PresetBundle::import_presets(std::vector<std::string>
if (inherits_config2 && !inherits_config2->value.empty()) {
// we should skip this preset here
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << boost::format(", can not find inherit preset for user preset %1%, just skip") % name;
continue;
return false;
}
// Find a default preset for the config. The PrintPresetCollection provides different default preset based on the "printer_technology" field.
const Preset &default_preset = collection->default_preset_for(config);
@ -713,11 +798,12 @@ PresetsConfigSubstitutions PresetBundle::import_presets(std::vector<std::string>
new_config.apply(std::move(config));
Preset &preset = collection->load_preset(collection->path_from_name(name, inherit_preset == nullptr), name, std::move(new_config), false);
if (key_values.find(BBL_JSON_KEY_FILAMENT_ID) != key_values.end())
preset.filament_id = key_values[BBL_JSON_KEY_FILAMENT_ID];
preset.is_external = true;
preset.version = *version;
inherit_preset = collection->find_preset(inherits_value, false, true); // pointer maybe wrong after insert, redo find
if (inherit_preset)
preset.base_id = inherit_preset->setting_id;
if (inherit_preset) preset.base_id = inherit_preset->setting_id;
Preset::normalize(preset.config);
// Report configuration fields, which are misplaced into a wrong group.
const Preset &default_preset = collection->default_preset_for(new_config);
@ -735,10 +821,7 @@ PresetsConfigSubstitutions PresetBundle::import_presets(std::vector<std::string>
} catch (const std::runtime_error &err) {
BOOST_LOG_TRIVIAL(error) << boost::format("Failed importing config file: %1%. Reason: %2%") % file % err.what();
}
}
}
files = result;
return substitutions;
return true;
}
//BBS save user preset to user_id preset folder

View File

@ -10,6 +10,7 @@
#include <boost/filesystem/path.hpp>
#define DEFAULT_USER_FOLDER_NAME "default"
#define BUNDLE_STRUCTURE_JSON_NAME "bundle_structure.json"
namespace Slic3r {
@ -50,6 +51,12 @@ public:
PresetsConfigSubstitutions load_user_presets(std::string user, ForwardCompatibilitySubstitutionRule rule);
PresetsConfigSubstitutions load_user_presets(AppConfig &config, std::map<std::string, std::map<std::string, std::string>>& my_presets, ForwardCompatibilitySubstitutionRule rule);
PresetsConfigSubstitutions import_presets(std::vector<std::string> &files, std::function<int(std::string const &)> override_confirm, ForwardCompatibilitySubstitutionRule rule);
bool import_json_presets(PresetsConfigSubstitutions & substitutions,
std::string & file,
std::function<int(std::string const &)> override_confirm,
ForwardCompatibilitySubstitutionRule rule,
int & overwrite,
std::vector<std::string> & result);
void save_user_presets(AppConfig& config, std::vector<std::string>& need_to_delete_list);
void remove_users_preset(AppConfig &config, std::map<std::string, std::map<std::string, std::string>> * my_presets = nullptr);
void update_user_presets_directory(const std::string preset_folder);

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,7 @@
#include "Widgets/RadioBox.hpp"
#include "Widgets/CheckBox.hpp"
#include "Widgets/ComboBox.hpp"
#include "miniz.h"
namespace Slic3r {
namespace GUI {
@ -100,8 +101,6 @@ protected:
wxBoxSizer *create_hot_bed_svg_item(wxWindow *parent);
wxBoxSizer *create_max_print_height_item(wxWindow *parent);
wxBoxSizer *create_page1_btns_item(wxWindow *parent);
void load_texture();
void load_model_stl();
//Improt Presets Page2
void create_printer_page2(wxWindow *parent);
wxBoxSizer *create_printer_preset_item(wxWindow *parent);
@ -123,6 +122,8 @@ protected:
bool save_printable_area_config(Preset *preset);
bool check_printable_area();
bool validate_input_valid();
void load_texture();
void load_model_stl();
wxArrayString printer_preset_sort_with_nozzle_diameter(const VendorProfile &vendor_profile, float nozzle_diameter);
wxBoxSizer *create_radio_item(wxString title, wxWindow *parent, wxString tooltip, std::vector<std::pair<RadioBox *, wxString>> &radiobox_list);
@ -201,16 +202,11 @@ public:
~ExportConfigsDialog();//to do: delete preset
protected:
void on_dpi_changed(const wxRect &suggested_rect) override;
wxBoxSizer *create_txport_config_item(wxWindow* parent);
wxBoxSizer *create_button_item(wxWindow *parent);
wxBoxSizer *create_select_printer(wxWindow *parent);
wxBoxSizer *create_radio_item(wxString title, wxWindow *parent, wxString tooltip, std::vector<std::pair<RadioBox *, wxString>> &radiobox_list);
struct ExportType
{
wxString preset_bundle;
wxString filament_bundle;
wxString printer_preset;
wxString filament_preset;
wxString process_preset;
@ -219,30 +215,48 @@ protected:
enum ExportCase {
INITIALIZE_FAIL = 0,
ADD_FILE_FAIL,
ADD_BUNDLE_STRUCTURE_FAIL,
FINALIZE_FAIL,
OPEN_ZIP_WRITTEN_FILE,
EXPORT_CANCEL,
EXPORT_SUCCESS,
CASE_COUNT,
};
private:
void data_init();
void select_curr_radiobox(std::vector<std::pair<RadioBox *, wxString>> &radiobox_list, int btn_idx);
void on_dpi_changed(const wxRect &suggested_rect) override;
void show_export_result(const ExportCase &export_case);
std::string initial_file_path(const wxString &path, const std::string &sub_file_path);
std::string initial_file_name(const wxString &path, const std::string file_name);
wxBoxSizer *create_txport_config_item(wxWindow *parent);
wxBoxSizer *create_button_item(wxWindow *parent);
wxBoxSizer *create_select_printer(wxWindow *parent);
wxBoxSizer *create_radio_item(wxString title, wxWindow *parent, wxString tooltip, std::vector<std::pair<RadioBox *, wxString>> &radiobox_list);
int initial_zip_archive(mz_zip_archive &zip_archive, const std::string &file_path);
ExportCase save_zip_archive_to_file(mz_zip_archive &zip_archive);
ExportCase save_presets_to_zip(const std::string &export_file, const std::vector<std::pair<std::string, std::string>> &config_paths);
ExportCase archive_preset_bundle_to_file(const wxString &path);
ExportCase archive_filament_bundle_to_file(const wxString &path);
ExportCase archive_printer_preset_to_file(const wxString &path);
ExportCase archive_filament_preset_to_file(const wxString &path);
ExportCase archive_process_preset_to_file(const wxString &path);
private:
std::vector<std::pair<RadioBox *, wxString>> m_export_type_btns;
std::vector<std::pair<CheckBox *, Preset *>> m_preset;
std::unordered_map<std::string, Preset *> m_printer_presets;
std::unordered_map<std::string, std::vector<Preset *>> m_filament_presets;
std::unordered_map<std::string, std::vector<Preset *>> m_process_presets;
std::vector<std::pair<CheckBox *, Preset *>> m_preset;//for printer preset bundle,printer preset, process preset export
std::vector<std::pair<CheckBox *, std::string>> m_printer_name;//for filament and peocess preset export, collaborate with m_filament_name_to_presets
std::unordered_map<std::string, Preset *> m_printer_presets;//first: printer name, second: printer presets have same printer name
std::unordered_map<std::string, std::vector<Preset *>> m_filament_presets;//first: printer name, second: filament presets have same printer name
std::unordered_map<std::string, std::vector<Preset *>> m_process_presets;//first: printer name, second: filament presets have same printer name
std::unordered_map<std::string, std::vector<std::pair<std::string, Preset *>>> m_filament_name_to_presets;//first: filament name, second presets have same filament name and printer name in vector
ExportType m_exprot_type;
wxGridSizer * m_preset_sizer = nullptr;
wxWindow * m_presets_window = nullptr;
Button * m_button_ok = nullptr;
Button * m_button_cancel = nullptr;
wxStaticText * m_serial_text = nullptr;
};
}

View File

@ -3173,7 +3173,7 @@ void MainFrame::load_config_file()
// return;
wxFileDialog dlg(this, _L("Select profile to load:"),
!m_last_config.IsEmpty() ? get_dir_name(m_last_config) : wxGetApp().app_config->get_last_dir(),
"config.json", "Config files (*.json)|*.json", wxFD_OPEN | wxFD_MULTIPLE | wxFD_FILE_MUST_EXIST);
"config.json", "Config files (*.json;*.zip;*.bbscfg;*.bbsflmt)|*.json;*.zip;*.bbscfg;*.bbsflmt", wxFD_OPEN | wxFD_MULTIPLE | wxFD_FILE_MUST_EXIST);
wxArrayString files;
if (dlg.ShowModal() != wxID_OK)
return;

View File

@ -565,50 +565,10 @@ Sidebar::Sidebar(Plater *parent)
wxGetApp().run_wizard(ConfigWizard::RR_USER, ConfigWizard::SP_PRINTERS);
});
StateColor create_printer_bg_col(std::pair<wxColour, int>(wxColour(219, 253, 231), StateColor::Pressed), std::pair<wxColour, int>(wxColour(238, 238, 238), StateColor::Hovered),
std::pair<wxColour, int>(wxColour(238, 238, 238), StateColor::Normal));
StateColor create_printer_fg_col(std::pair<wxColour, int>(wxColour(107, 107, 106), StateColor::Pressed),
std::pair<wxColour, int>(wxColour(107, 107, 106), StateColor::Hovered),
std::pair<wxColour, int>(wxColour(107, 107, 106), StateColor::Normal));
StateColor create_printer_bd_col(std::pair<wxColour, int>(wxColour(0, 174, 66), StateColor::Pressed), std::pair<wxColour, int>(wxColour(0, 174, 66), StateColor::Hovered),
std::pair<wxColour, int>(wxColour(172, 172, 172), StateColor::Normal));
auto create_printer_preset_btn = new Button(p->m_panel_printer_title, _L("Create Printer"));
create_printer_preset_btn->SetFont(Label::Body_10);
create_printer_preset_btn->SetPaddingSize(wxSize(FromDIP(8), FromDIP(3)));
create_printer_preset_btn->SetCornerRadius(FromDIP(8));
create_printer_preset_btn->SetBackgroundColor(create_printer_bg_col);
create_printer_preset_btn->SetBorderColor(create_printer_bd_col);
create_printer_preset_btn->SetTextColor(create_printer_fg_col);
create_printer_preset_btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent &e) {
//CreateFilamentPresetDialog dlg(p->m_panel_printer_title);
CreatePrinterPresetDialog dlg(p->m_panel_printer_title);
int res = dlg.ShowModal();
if (wxID_OK == res) {
wxGetApp().mainframe->update_side_preset_ui();
update_all_preset_comboboxes();
Tab *printer_tab = wxGetApp().get_tab(Preset::TYPE_PRINTER);
if (printer_tab) {
printer_tab->load_current_preset();
printer_tab->update();
}
CreatePresetSuccessfulDialog success_dlg(p->m_panel_filament_title, SuccessType::PRINTER);
int res = success_dlg.ShowModal();
if (res == wxID_OK) {
p->editing_filament = -1;
if (p->combo_printer->switch_to_tab())
p->editing_filament = 0;
}
}
});
wxBoxSizer* h_sizer_title = new wxBoxSizer(wxHORIZONTAL);
h_sizer_title->Add(p->m_printer_icon, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, em);
h_sizer_title->Add(p->m_text_printer_settings, 0, wxALIGN_CENTER);
h_sizer_title->AddStretchSpacer();
h_sizer_title->Add(create_printer_preset_btn, 0, wxRIGHT | wxALIGN_CENTER, FromDIP(10));
h_sizer_title->Add(p->m_printer_setting, 0, wxALIGN_CENTER);
h_sizer_title->Add(15 * em / 10, 0, 0, 0, 0);
h_sizer_title->SetMinSize(-1, 3 * em);
@ -1045,7 +1005,29 @@ Sidebar::Sidebar(Plater *parent)
Sidebar::~Sidebar() {}
void Sidebar::init_filament_combo(PlaterPresetComboBox **combo, const int filament_idx) {
void Sidebar::create_printer_preset()
{
CreatePrinterPresetDialog dlg(p->m_panel_printer_title);
int res = dlg.ShowModal();
if (wxID_OK == res) {
wxGetApp().mainframe->update_side_preset_ui();
update_all_preset_comboboxes();
Tab *printer_tab = wxGetApp().get_tab(Preset::TYPE_PRINTER);
if (printer_tab) {
printer_tab->load_current_preset();
printer_tab->update();
}
CreatePresetSuccessfulDialog success_dlg(p->m_panel_filament_title, SuccessType::PRINTER);
int res = success_dlg.ShowModal();
if (res == wxID_OK) {
p->editing_filament = -1;
if (p->combo_printer->switch_to_tab()) p->editing_filament = 0;
}
}
}
void Sidebar::init_filament_combo(PlaterPresetComboBox **combo, const int filament_idx)
{
*combo = new PlaterPresetComboBox(p->m_panel_filament_content, Slic3r::Preset::TYPE_FILAMENT);
(*combo)->set_filament_idx(filament_idx);
@ -5867,6 +5849,12 @@ void Plater::priv::on_select_preset(wxCommandEvent &evt)
// So, use GetSelection() from event parameter
int selection = evt.GetSelection();
auto marker = reinterpret_cast<size_t>(combo->GetClientData(selection));
if (PresetComboBox::LabelItemType::LABEL_ITEM_WIZARD_ADD_PRINTERS == marker) {
sidebar->create_printer_preset();
return;
}
auto idx = combo->get_filament_idx();
// BBS:Save the plate parameters before switching

View File

@ -111,6 +111,7 @@ public:
Sidebar &operator=(const Sidebar &) = delete;
~Sidebar();
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 update_all_preset_comboboxes();

View File

@ -749,6 +749,10 @@ void PlaterPresetComboBox::OnSelect(wxCommandEvent &evt)
auto marker = reinterpret_cast<Marker>(this->GetClientData(selected_item));
if (marker >= LABEL_ITEM_MARKER && marker < LABEL_ITEM_MAX) {
this->SetSelection(m_last_selected);
if (LABEL_ITEM_WIZARD_ADD_PRINTERS == marker) {
evt.Skip();
return;
}
evt.StopPropagation();
if (marker == LABEL_ITEM_MARKER)
return;
@ -1082,8 +1086,10 @@ void PlaterPresetComboBox::update()
set_label_marker(Append(separator(L("Add/Remove filaments")), *bmp), LABEL_ITEM_WIZARD_FILAMENTS);
else if (m_type == Preset::TYPE_SLA_MATERIAL)
set_label_marker(Append(separator(L("Add/Remove materials")), *bmp), LABEL_ITEM_WIZARD_MATERIALS);
else
set_label_marker(Append(separator(L("Add/Remove printers")), *bmp), LABEL_ITEM_WIZARD_PRINTERS);
else {
set_label_marker(Append(separator(L("Select/Remove printers(system presets)")), *bmp), LABEL_ITEM_WIZARD_PRINTERS);
set_label_marker(Append(separator(L("Create printer")), *bmp), LABEL_ITEM_WIZARD_ADD_PRINTERS);
}
}
update_selection();

View File

@ -45,6 +45,7 @@ public:
LABEL_ITEM_WIZARD_PRINTERS,
LABEL_ITEM_WIZARD_FILAMENTS,
LABEL_ITEM_WIZARD_MATERIALS,
LABEL_ITEM_WIZARD_ADD_PRINTERS,
LABEL_ITEM_MAX,
};