diff --git a/src/slic3r/GUI/AMSMaterialsSetting.cpp b/src/slic3r/GUI/AMSMaterialsSetting.cpp index 74165c20fe..35db1a3955 100644 --- a/src/slic3r/GUI/AMSMaterialsSetting.cpp +++ b/src/slic3r/GUI/AMSMaterialsSetting.cpp @@ -1075,7 +1075,7 @@ void AMSMaterialsSetting::Popup(wxString filament, wxString sn, wxString temp_mi // Sort the filaments { - static std::unordered_map sorted_names + std::unordered_map sorted_names = { {"Bambu PLA Basic", 0}, {"Bambu PLA Matte", 1}, {"Bambu PETG HF", 2}, @@ -1090,9 +1090,58 @@ void AMSMaterialsSetting::Popup(wxString filament, wxString sn, wxString temp_mi {"Bambu ABS-GF", 11} }; + // 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* { + for (auto it = filaments.begin(); it != filaments.end(); ++it) { + if (it->name == wanted) { + return &(*it); + } + } + 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 + 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 Preset* match = nullptr; + + do { + auto find_result = find_filament_by_name(wanted, bundle->filaments); + if (!find_result) { + BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << " No available filament name matches " << wanted; + break; + } + + match = find_result; + + BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " Found available filament matching current preset name " << wanted + << " - Name: " << match->name << " - Alias: " << match->alias + << " - Inherits: " << match->inherits(); + + if (match->inherits().length() == 0) { + BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << " No more inherits so we reached the base filament"; + break; + } + + wanted = match->inherits(); + } while (1); // Or loop while (match->alias.length() == 0) because existence of alias and inherits on a Preset seem to be exclusive + + if (!match) { + continue; + } + + 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); + } + 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](const wxString& left, const wxString& right) -> bool + auto _filament_sorter = [&query_filament_vendors, &query_filament_types, &sorted_names](const wxString& left, const wxString& right) -> bool { { // Compare name order const auto& iter1 = sorted_names.find(left);