SPE-2544: Refactoring of trimming repo prefix

This commit is contained in:
David Kocik 2024-11-14 17:27:33 +01:00 committed by Lukas Matena
parent dde66d5243
commit de32b37a84
7 changed files with 42 additions and 103 deletions

View File

@ -451,6 +451,23 @@ void Preset::set_visible_from_appconfig(const AppConfig &app_config)
}
}
std::string Preset::trim_vendor_repo_prefix(const std::string& id) const
{
return Preset::trim_vendor_repo_prefix(id, this->vendor);
}
std::string Preset::trim_vendor_repo_prefix(const std::string& id, const VendorProfile* vendor_profile) const
{
if (!vendor_profile) {
return id;
}
std::string res = id;
if (boost::algorithm::starts_with(res, vendor_profile->repo_prefix)) {
boost::algorithm::erase_head(res, vendor_profile->repo_prefix.size());
boost::algorithm::trim_left(res);
}
return res;
}
static std::vector<std::string> s_Preset_print_options {
"layer_height", "first_layer_height", "perimeters", "spiral_vase", "slice_closing_radius", "slicing_mode",
"top_solid_layers", "top_solid_min_thickness", "bottom_solid_layers", "bottom_solid_min_thickness",

View File

@ -224,6 +224,10 @@ public:
// Sort lexicographically by a preset name. The preset name shall be unique across a single PresetCollection.
bool operator<(const Preset &other) const { return this->name < other.name; }
// Returns id without trimmed prefix if present and vendor has any.
std::string trim_vendor_repo_prefix(const std::string& id) const;
std::string trim_vendor_repo_prefix(const std::string& id, const VendorProfile* vendor_profile) const;
static const std::vector<std::string>& print_options();
static const std::vector<std::string>& filament_options();
// Printer options contain the nozzle options.

View File

@ -3888,20 +3888,8 @@ const Preset* find_preset_by_nozzle_and_options(
for (const Preset &preset : collection) {
// trim repo prefix
std::string printer_model = preset.config.opt_string("printer_model");
std::string vendor_repo_prefix;
if (preset.vendor) {
vendor_repo_prefix = preset.vendor->repo_prefix;
} else if (std::string inherits = preset.inherits(); !inherits.empty()) {
const Preset *parent = wxGetApp().preset_bundle->printers.find_preset(inherits);
if (parent && parent->vendor) {
vendor_repo_prefix = parent->vendor->repo_prefix;
}
}
if (printer_model.find(vendor_repo_prefix) == 0) {
printer_model = printer_model.substr(vendor_repo_prefix.size()
);
boost::trim_left(printer_model);
}
const PresetWithVendorProfile& printer_with_vendor = collection.get_preset_with_vendor_profile(preset);
printer_model = preset.trim_vendor_repo_prefix(printer_model, printer_with_vendor.vendor);
if (!preset.is_system || printer_model != model_id)
continue;

View File

@ -732,13 +732,7 @@ void PhysicalPrinterDialog::update_host_type(bool printer_change)
if (Preset* preset = wxGetApp().preset_bundle->printers.find_preset(preset_name)) {
std::string model_id = preset->config.opt_string("printer_model");
if (preset->vendor) {
std::string model_id_no_pref = model_id;
std::string vendor_repo_prefix;
vendor_repo_prefix = preset->vendor->repo_prefix;
if (model_id_no_pref.find(vendor_repo_prefix) == 0) {
model_id_no_pref = model_id_no_pref.substr(vendor_repo_prefix.size());
boost::trim_left(model_id_no_pref);
}
std::string model_id_no_pref = preset->trim_vendor_repo_prefix(model_id);
if (preset->vendor->name.find("Prusa Research") != std::string::npos) {
const std::vector<VendorProfile::PrinterModel>& models = preset->vendor->models;
auto it = std::find_if(models.begin(), models.end(),
@ -763,15 +757,7 @@ void PhysicalPrinterDialog::update_host_type(bool printer_change)
break;
}
std::string model_id = preset->config.opt_string("printer_model");
// remove prefix from printer_model
if (preset->vendor) {
std::string vendor_repo_prefix;
vendor_repo_prefix = preset->vendor->repo_prefix;
if (model_id.find(vendor_repo_prefix) == 0) {
model_id = model_id.substr(vendor_repo_prefix.size());
boost::trim_left(model_id);
}
}
model_id = preset->trim_vendor_repo_prefix(model_id);
if (preset->vendor && preset->vendor->name.find("Prusa Research") == std::string::npos) {
connect.supported = false;
break;

View File

@ -2157,33 +2157,19 @@ unsigned int Plater::priv::update_background_process(bool force_validation, bool
if (full_config.has("binary_gcode")) // needed for SLA
full_config.set("binary_gcode", bool(full_config.opt_bool("binary_gcode") & wxGetApp().app_config->get_bool("use_binary_gcode_when_supported")));
// Set printer_model in full_config a model without repo prefix (including inherited profiles)
const Preset &selected_printer = wxGetApp().preset_bundle->printers.get_selected_preset();
std::string printer_model_serialized = full_config.option("printer_model")->serialize();
std::string printer_model = selected_printer.config.opt_string("printer_model");
const PresetWithVendorProfile& printer_with_vendor = wxGetApp().preset_bundle->printers.get_preset_with_vendor_profile(selected_printer);
printer_model = selected_printer.trim_vendor_repo_prefix(printer_model, printer_with_vendor.vendor);
full_config.set("printer_model", printer_model);
const VendorProfile* vendor = nullptr;
std::string vendor_repo_prefix;
if (selected_printer.vendor) {
vendor = selected_printer.vendor;
} else if (std::string inherits = selected_printer.inherits(); !inherits.empty()) {
const Preset *parent = wxGetApp().preset_bundle->printers.find_preset(inherits);
if (parent && parent->vendor) {
vendor_repo_prefix = parent->vendor->repo_prefix;
}
}
if (vendor) {
vendor_repo_prefix = vendor->repo_prefix;
// Passing extra info about preset vendor and version, so these can be inserted as metadata to GCode
full_config.set("profile_vendor", vendor->name, true);
full_config.set("profile_version", vendor->config_version.to_string(), true);
full_config.set("profile_vendor", selected_printer.vendor->name, true);
full_config.set("profile_version", selected_printer.vendor->config_version.to_string(), true);
}
if (printer_model_serialized.find(vendor_repo_prefix) == 0) {
printer_model_serialized = printer_model_serialized.substr(vendor_repo_prefix.size());
boost::trim_left(printer_model_serialized);
full_config.set("printer_model", printer_model_serialized);
}
// If the update_background_process() was not called by the timer, kill the timer,
// so the update_restart_background_process() will not be called again in vain.
background_process_timer.Stop();

View File

@ -979,19 +979,8 @@ static std::string get_connect_state_suffix_for_printer(const Preset& printer_pr
for (const auto& [printer_model_nozzle_pair, states] : printer_state_map) {
std::string printer_model = printer_preset.config.opt_string("printer_model");
std::string vendor_repo_prefix;
if (printer_preset.vendor) {
vendor_repo_prefix = printer_preset.vendor->repo_prefix;
} else if (std::string inherits = printer_preset.inherits(); !inherits.empty()) {
const Preset *parent = wxGetApp().preset_bundle->printers.find_preset(inherits);
if (parent && parent->vendor) {
vendor_repo_prefix = parent->vendor->repo_prefix;
}
}
if (printer_model.find(vendor_repo_prefix) == 0) {
printer_model = printer_model.substr(vendor_repo_prefix.size());
boost::trim_left(printer_model);
}
const PresetWithVendorProfile& printer_with_vendor = wxGetApp().preset_bundle->printers.get_preset_with_vendor_profile(printer_preset);
printer_model = printer_preset.trim_vendor_repo_prefix(printer_model, printer_with_vendor.vendor);
if (printer_preset.config.has("nozzle_diameter")) {
double nozzle_diameter = static_cast<const ConfigOptionFloats*>(printer_preset.config.option("nozzle_diameter"))->values[0];
@ -1043,19 +1032,8 @@ static bool fill_data_to_connect_info_line( const Preset& printer_preset,
for (const auto& [printer_model_nozzle_pair, states] : printer_state_map) {
// get printer_model without repo prefix
std::string printer_model = printer_preset.config.opt_string("printer_model");
std::string vendor_repo_prefix;
if (printer_preset.vendor) {
vendor_repo_prefix = printer_preset.vendor->repo_prefix;
} else if (std::string inherits = printer_preset.inherits(); !inherits.empty()) {
const Preset *parent = wxGetApp().preset_bundle->printers.find_preset(inherits);
if (parent && parent->vendor) {
vendor_repo_prefix = parent->vendor->repo_prefix;
}
}
if (printer_model.find(vendor_repo_prefix) == 0) {
printer_model = printer_model.substr(vendor_repo_prefix.size());
boost::trim_left(printer_model);
}
const PresetWithVendorProfile& printer_with_vendor = wxGetApp().preset_bundle->printers.get_preset_with_vendor_profile(printer_preset);
printer_model = printer_preset.trim_vendor_repo_prefix(printer_model, printer_with_vendor.vendor);
if (printer_preset.config.has("nozzle_diameter")) {
double nozzle_diameter = static_cast<const ConfigOptionFloats*>(printer_preset.config.option("nozzle_diameter"))->values[0];

View File

@ -577,19 +577,8 @@ void PrinterPickWebViewDialog::request_compatible_printers_FFF() {
filament_abrasive_serialized += "]";
std::string printer_model_serialized = full_config.option("printer_model")->serialize();
std::string vendor_repo_prefix;
if (selected_printer.vendor) {
vendor_repo_prefix = selected_printer.vendor->repo_prefix;
} else if (std::string inherits = selected_printer.inherits(); !inherits.empty()) {
const Preset *parent = wxGetApp().preset_bundle->printers.find_preset(inherits);
if (parent && parent->vendor) {
vendor_repo_prefix = parent->vendor->repo_prefix;
}
}
if (printer_model_serialized.find(vendor_repo_prefix) == 0) {
printer_model_serialized = printer_model_serialized.substr(vendor_repo_prefix.size());
boost::trim_left(printer_model_serialized);
}
const PresetWithVendorProfile& printer_with_vendor = wxGetApp().preset_bundle->printers.get_preset_with_vendor_profile(selected_printer);
printer_model_serialized = selected_printer.trim_vendor_repo_prefix(printer_model_serialized, printer_with_vendor.vendor);
const std::string uuid = wxGetApp().plater()->get_user_account()->get_current_printer_uuid_from_connect(printer_model_serialized);
const std::string filename = wxGetApp().plater()->get_upload_filename();
@ -615,18 +604,9 @@ void PrinterPickWebViewDialog::request_compatible_printers_SLA()
std::string printer_model_serialized = selected_printer.config.option("printer_model")->serialize();
std::string vendor_repo_prefix;
if (selected_printer.vendor) {
vendor_repo_prefix = selected_printer.vendor->repo_prefix;
} else if (std::string inherits = selected_printer.inherits(); !inherits.empty()) {
const Preset *parent = wxGetApp().preset_bundle->printers.find_preset(inherits);
if (parent && parent->vendor) {
vendor_repo_prefix = parent->vendor->repo_prefix;
}
}
if (printer_model_serialized.find(vendor_repo_prefix) == 0) {
printer_model_serialized = printer_model_serialized.substr(vendor_repo_prefix.size());
boost::trim_left(printer_model_serialized);
}
const PresetWithVendorProfile& printer_with_vendor = wxGetApp().preset_bundle->printers.get_preset_with_vendor_profile(selected_printer);
printer_model_serialized = selected_printer.trim_vendor_repo_prefix(printer_model_serialized, printer_with_vendor.vendor);
const Preset& selected_material = wxGetApp().preset_bundle->sla_materials.get_selected_preset();
const std::string material_type_serialized = selected_material.config.option("material_type")->serialize();
const std::string uuid = wxGetApp().plater()->get_user_account()->get_current_printer_uuid_from_connect(printer_model_serialized);