diff --git a/src/slic3r/GUI/AMSMaterialsSetting.cpp b/src/slic3r/GUI/AMSMaterialsSetting.cpp index 35db1a3955..2e436e8462 100644 --- a/src/slic3r/GUI/AMSMaterialsSetting.cpp +++ b/src/slic3r/GUI/AMSMaterialsSetting.cpp @@ -4,6 +4,7 @@ #include "GUI_App.hpp" #include "libslic3r/Preset.hpp" #include "I18N.hpp" +#include #include #include #include @@ -1075,20 +1076,7 @@ void AMSMaterialsSetting::Popup(wxString filament, wxString sn, wxString temp_mi // Sort the filaments { - std::unordered_map sorted_names = - { {"Bambu PLA Basic", 0}, - {"Bambu PLA Matte", 1}, - {"Bambu PETG HF", 2}, - {"Bambu ABS", 3}, - {"Bambu PLA Silk", 4}, - {"Bambu PLA-CF" , 5}, - {"Bambu PLA Galaxy", 6}, - {"Bambu PLA Metal", 7}, - {"Bambu PLA Marble", 8}, - {"Bambu PETG-CF", 9}, - {"Bambu PETG Translucent", 10}, - {"Bambu ABS-GF", 11} - }; + std::unordered_map selected_filament_ranks; // Helper lambda to find a filament Preset by name. We can call this multiple times to walk the inheritance chain and find the base filament. auto find_filament_by_name = [](const std::string& wanted, const PresetCollection& filaments) -> const Preset* { @@ -1100,12 +1088,12 @@ void AMSMaterialsSetting::Popup(wxString filament, wxString sn, wxString temp_mi return nullptr; }; - // For each active filament preset, find matching Preset in bundle->filaments and add the base filament alias to sorted_names in highest rank in extruder order + // For each active filament preset, find its base filament alias and promote it in extruder order. auto bundle = wxGetApp().preset_bundle; const auto& preset_names = bundle->filament_presets; for (size_t i = preset_names.size(); i-- > 0; ) { std::string wanted = preset_names[i]; - const int sort_rank = -((int)preset_names.size() - i); + const int sort_rank = -static_cast(preset_names.size() - i); const Preset* match = nullptr; @@ -1136,42 +1124,57 @@ void AMSMaterialsSetting::Popup(wxString filament, wxString sn, wxString temp_mi BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " Update filament rank to " + std::to_string(sort_rank) + " for preset Name: " << match->name << " - Alias: " << match->alias; - sorted_names.insert_or_assign(match->alias, sort_rank); + selected_filament_ranks.insert_or_assign(match->alias, sort_rank); } - static std::vector sorted_vendors { "Bambu Lab", "Generic" }; - static std::vector sorted_types { "PLA", "PETG", "ABS", "TPU" }; - auto _filament_sorter = [&query_filament_vendors, &query_filament_types, &sorted_names](const wxString& left, const wxString& right) -> bool + static const std::vector sorted_vendors { "Generic" }; + static const std::vector sorted_types { "PLA", "PETG", "ABS", "TPU" }; + auto priority_rank = [](const std::vector& priorities, const wxString& value) { + const auto iter = std::find_if(priorities.begin(), priorities.end(), [&value](const wxString& priority) { + return priority.CmpNoCase(value) == 0; + }); + return iter - priorities.begin(); + }; + auto _filament_sorter = [&query_filament_vendors, &query_filament_types, &selected_filament_ranks, &priority_rank](const wxString& left, const wxString& right) -> bool { - { // Compare name order - const auto& iter1 = sorted_names.find(left); - int name_order1 = (iter1 != sorted_names.end()) ? iter1->second : INT_MAX; + { // Compare selected filament order + const auto& iter1 = selected_filament_ranks.find(left); + int selected_order1 = (iter1 != selected_filament_ranks.end()) ? iter1->second : INT_MAX; - const auto& iter2 = sorted_names.find(right); - int name_order2 = (iter2 != sorted_names.end()) ? iter2->second : INT_MAX; - if (name_order1 != name_order2) + const auto& iter2 = selected_filament_ranks.find(right); + int selected_order2 = (iter2 != selected_filament_ranks.end()) ? iter2->second : INT_MAX; + if (selected_order1 != selected_order2) { - return name_order1 < name_order2; + return selected_order1 < selected_order2; } } { // Compare vendor - auto iter1 = std::find(sorted_vendors.begin(), sorted_vendors.end(), query_filament_vendors[left]); - auto iter2 = std::find(sorted_vendors.begin(), sorted_vendors.end(), query_filament_vendors[right]); - if (iter1 != iter2) - { - return iter1 < iter2; - }; + const wxString& vendor1 = query_filament_vendors.at(left); + const wxString& vendor2 = query_filament_vendors.at(right); + const auto rank1 = priority_rank(sorted_vendors, vendor1); + const auto rank2 = priority_rank(sorted_vendors, vendor2); + if (rank1 != rank2) + return rank1 < rank2; + + const int vendor_compare = vendor1.CmpNoCase(vendor2); + if (vendor_compare != 0) + return vendor_compare < 0; } { // Compare type - auto iter1 = std::find(sorted_types.begin(), sorted_types.end(), query_filament_types[left]); - auto iter2 = std::find(sorted_types.begin(), sorted_types.end(), query_filament_types[right]); - if (iter1 != iter2) - { - return iter1 < iter2; - } + const wxString& type1 = query_filament_types.at(left); + const wxString& type2 = query_filament_types.at(right); + const auto rank1 = priority_rank(sorted_types, type1); + const auto rank2 = priority_rank(sorted_types, type2); + if (rank1 != rank2) + return rank1 < rank2; + + const int type_compare = type1.CmpNoCase(type2); + if (type_compare != 0) + return type_compare < 0; } - return left < right; + const int name_compare = left.CmpNoCase(right); + return name_compare != 0 ? name_compare < 0 : left < right; }; std::sort(filament_items.begin(), filament_items.end(), _filament_sorter);