ENH: preset: add logic to check the modified gcodes when loading 3mf

when the 3mf contains modified gcodes or self defined presets
a popup window will be shown
JIRA: STUDIO-4628

Change-Id: I975758132ba9d200255e7bf7d3a606fd609da5c8
This commit is contained in:
lane.wei 2023-10-09 09:02:30 +08:00 committed by Lane.Wei
parent 32198fdafc
commit 6c2ad86415
5 changed files with 127 additions and 55 deletions

View file

@ -1748,28 +1748,33 @@ void PresetCollection::update_after_user_presets_loaded()
return;
}
//BBS: validate_printers
bool PresetCollection::validate_printers(const std::string &name, DynamicPrintConfig& config, std::string &inherit)
//BBS: validate_preset
bool PresetCollection::validate_preset(const std::string &preset_name, std::string &inherit_name)
{
std::string& original_name = config.opt_string("printer_settings_id", true);
std::deque<Preset>::iterator it = this->find_preset_internal(original_name);
bool found = it != m_presets.end() && it->name == original_name && (it->is_system || it->is_default);
std::deque<Preset>::iterator it = this->find_preset_internal(preset_name);
bool found = (it != m_presets.end()) && (it->name == preset_name) && (it->is_system || it->is_default);
if (!found) {
it = this->find_preset_renamed(original_name);
it = this->find_preset_renamed(preset_name);
found = it != m_presets.end() && (it->is_system || it->is_default);
}
if (!found) {
if (!inherit.empty()) {
it = this->find_preset_internal(inherit);
found = it != m_presets.end() && it->name == inherit && (it->is_system || it->is_default);
if (!inherit_name.empty()) {
it = this->find_preset_internal(inherit_name);
found = it != m_presets.end() && it->name == inherit_name && (it->is_system || it->is_default);
if (found)
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(": preset_name %1%, inherit_name %2%, found inherit in list")%preset_name %inherit_name;
else
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << boost::format(": preset_name %1%, inherit_name %2%, can not found preset and inherit in list")%preset_name %inherit_name;
}
else {
//inherit is null , should not happen , just consider it as valid
found = false;
BOOST_LOG_TRIVIAL(warning) << boost::format(": name %1%, printer_settings %2%, no inherit, set to not found")%name %original_name;
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << boost::format(": preset_name %1%, no inherit, set to not found")%preset_name;
}
}
BOOST_LOG_TRIVIAL(warning) << boost::format(": name %1%, printer_settings %2%, inherit %3%, found result %4%")%name %original_name % inherit % found;
else {
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(": preset_name %1%, found in list")%preset_name;
}
return found;
}

View file

@ -391,8 +391,8 @@ public:
typedef std::function<void(Preset* preset, std::string sync_info)> SyncFunc;
//BBS get m_presets begin
Iterator lbegin() { return m_presets.begin(); }
//BBS: validate_printers
bool validate_printers(const std::string &name, DynamicPrintConfig& config, std::string &inherit);
//BBS: validate_preset
bool validate_preset(const std::string &name, std::string &inherit);
Iterator begin() { return m_presets.begin() + m_num_default_presets; }
ConstIterator begin() const { return m_presets.cbegin() + m_num_default_presets; }

View file

@ -937,26 +937,79 @@ void PresetBundle::update_system_preset_setting_ids(std::map<std::string, std::m
}
//BBS: validate printers from previous project
bool PresetBundle::validate_printers(const std::string &name, DynamicPrintConfig& config)
static std::set<std::string> gcodes_key_set = {"filament_end_gcode", "filament_start_gcode", "change_filament_gcode", "layer_change_gcode", "machine_end_gcode", "machine_pause_gcode", "machine_start_gcode", "template_custom_gcode"};
int PresetBundle::validate_presets(const std::string &file_name, DynamicPrintConfig& config, std::set<std::string>& different_gcodes)
{
// BBS TODO:
#if 0
std::vector<std::string> inherits_values;
PrinterTechnology printer_technology = Preset::printer_technology(config);
size_t num_extruders = (printer_technology == ptFFF) ?
std::min(config.option<ConfigOptionFloats>("nozzle_diameter" )->values.size(),
config.option<ConfigOptionFloats>("filament_diameter")->values.size()) : 1;
inherits_values.resize(num_extruders + 2, std::string());
inherits_values = config.option<ConfigOptionStrings>("inherits_group", true)->values;
bool validated = false;
std::vector<std::string> inherits_values = config.option<ConfigOptionStrings>("inherits_group", true)->values;
std::vector<std::string> filament_preset_name = config.option<ConfigOptionStrings>("filament_settings_id", true)->values;
std::string printer_preset = config.option<ConfigOptionString>("printer_settings_id", true)->value;
bool has_different_settings_to_system = config.option("different_settings_to_system")?true:false;
std::vector<std::string> different_values;
int ret = VALIDATE_PRESETS_SUCCESS;
std::string inherits;
if (inherits_values.size() >= (num_extruders + 2))
inherits = inherits_values[num_extruders + 1];
if (has_different_settings_to_system)
different_values = config.option<ConfigOptionStrings>("different_settings_to_system", true)->values;
return this->printers.validate_printers(name, config, inherits);
#else
return true;
#endif
//PrinterTechnology printer_technology = Preset::printer_technology(config);
size_t filament_count = config.option<ConfigOptionFloats>("filament_diameter")->values.size();
inherits_values.resize(filament_count + 2, std::string());
different_values.resize(filament_count + 2, std::string());
filament_preset_name.resize(filament_count, std::string());
std::string printer_inherits = inherits_values[filament_count + 1];
validated = this->printers.validate_preset(printer_preset, printer_inherits);
if (!validated) {
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << boost::format(":file_name %1%, found the printer preset not inherit from system") % file_name;
different_gcodes.emplace(printer_preset);
ret = VALIDATE_PRESETS_PRINTER_NOT_FOUND;
}
for(unsigned int index = 0; index < filament_count; index ++)
{
std::string filament_preset = filament_preset_name[index];
std::string filament_inherits = inherits_values[index+1];
validated = this->filaments.validate_preset(filament_preset, filament_inherits);
if (!validated) {
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << boost::format(":file_name %1%, found the filament %2% preset not inherit from system") % file_name %(index+1);
different_gcodes.emplace(filament_preset);
ret = VALIDATE_PRESETS_FILAMENTS_NOT_FOUND;
}
}
//self defined presets, return directly
if (ret)
{
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << boost::format(":file_name %1%, found self defined presets, count %2%") %file_name %different_gcodes.size();
return ret;
}
for(unsigned int index = 1; index < filament_count+2; index ++)
{
std::string different_settingss = different_values[index];
std::vector<std::string> different_keys;
Slic3r::unescape_strings_cstyle(different_settingss, different_keys);
for (unsigned int j = 0; j < different_keys.size(); j++) {
if (gcodes_key_set.find(different_keys[j]) != gcodes_key_set.end()) {
different_gcodes.emplace(different_keys[j]);
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << boost::format(":preset index %1%, different key %2%") %index %different_keys[j];
}
}
}
if (!different_gcodes.empty())
{
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << boost::format(":file_name %1%, found different gcodes count %2%") %file_name %different_gcodes.size();
return VALIDATE_PRESETS_MODIFIED_GCODES;
}
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(":file_name %1%, validate presets success!") % file_name;
return VALIDATE_PRESETS_SUCCESS;
}
void PresetBundle::remove_users_preset(AppConfig &config, std::map<std::string, std::map<std::string, std::string>> *my_presets)

View file

@ -12,6 +12,12 @@
#define DEFAULT_USER_FOLDER_NAME "default"
#define BUNDLE_STRUCTURE_JSON_NAME "bundle_structure.json"
#define VALIDATE_PRESETS_SUCCESS 0
#define VALIDATE_PRESETS_PRINTER_NOT_FOUND 1
#define VALIDATE_PRESETS_FILAMENTS_NOT_FOUND 2
#define VALIDATE_PRESETS_MODIFIED_GCODES 3
namespace Slic3r {
// Bundle of Print + Filament + Printer presets.
@ -64,7 +70,7 @@ public:
void update_system_preset_setting_ids(std::map<std::string, std::map<std::string, std::string>>& system_presets);
//BBS: add API to get previous machine
bool validate_printers(const std::string &name, DynamicPrintConfig& config);
int validate_presets(const std::string &file_name, DynamicPrintConfig& config, std::set<std::string>& different_gcodes);
//BBS: add function to generate differed preset for save
//the pointer should be freed by the caller

View file

@ -3491,22 +3491,30 @@ std::vector<size_t> Plater::priv::load_files(const std::vector<fs::path>& input_
Preset::normalize(config);
PresetBundle *preset_bundle = wxGetApp().preset_bundle;
// BBS: first validate the printer
// TODO: remove it after released""
bool validated = preset_bundle->validate_printers(filename.string(), config);
if (!validated) {
load_config = false;
load_old_project = true;
// select view to 3D
q->select_view_3D("3D");
// select plate 0 as default
q->select_plate(0);
show_info(q, _L("The 3mf is not compatible, load geometry data only!"), _L("Incompatible 3mf"));
for (ModelObject *model_object : model.objects) {
model_object->config.reset();
// Is there any modifier or advanced config data?
for (ModelVolume *model_volume : model_object->volumes) model_volume->config.reset();
}
} else {
// validate the system profiles
std::set<std::string> modified_gcodes;
int validated = preset_bundle->validate_presets(filename.string(), config, modified_gcodes);
if (validated == VALIDATE_PRESETS_MODIFIED_GCODES) {
std::string warning_message = L("The 3mf has following modified G-codes in filament or printer presets:");
warning_message += "\n";
for (std::set<std::string>::iterator it=modified_gcodes.begin(); it!=modified_gcodes.end(); ++it)
warning_message += "-" + *it + "\n";
warning_message += "\n";
warning_message += L("Please confirm that these modified G-codes are safe to prevent any damage to the machine!");
show_info(q, warning_message, _L("Modified G-codes"));
}
else if ((validated == VALIDATE_PRESETS_PRINTER_NOT_FOUND) || (validated == VALIDATE_PRESETS_FILAMENTS_NOT_FOUND)) {
std::string warning_message = L("The 3mf has following customized filament or printer presets:");
warning_message += "\n";
for (std::set<std::string>::iterator it=modified_gcodes.begin(); it!=modified_gcodes.end(); ++it)
warning_message += "-" + *it + "\n";
warning_message += "\n";
warning_message += L("Please confirm that the G-codes within these presets are safe to prevent any damage to the machine!");
show_info(q, warning_message, _L("Customized Preset"));
}
//always load config
{
preset_bundle->load_config_model(filename.string(), std::move(config), file_version);
ConfigOption* bed_type_opt = preset_bundle->project_config.option("curr_bed_type");