From a02f6ed9be12486a67c9b58a7acf30d7652597b4 Mon Sep 17 00:00:00 2001 From: HanifKoh <76276251+HanifKoh@users.noreply.github.com> Date: Thu, 29 Oct 2026 12:08:50 +0800 Subject: [PATCH] Validate Only the Edited Variant's Adaptive PA Model and Accept Spaces in It (#16978) * Validate Only the Edited Variant's Adaptive PA Model and Accept Spaces in It Since pressure advance became a per-variant filament setting, the Filament tab validated every variant column of the adaptive pressure advance model whenever one was edited. The columns the user never touched still hold the default placeholder, which the validator rejects, so every edit of a multi-variant filament ended in a "Line 1: flow value must be greater than PA value" dialog about a column the user could not see. The check now covers the variant being edited only, as the nozzle temperature checks on the same tab do. The validator also rejected whitespace between the values, which the model parser accepts, so a model typed as "0.027, 8.1416, 2000.0" sliced correctly but warned on every slice. Whitespace is now ignored before the values are checked. Enabling adaptive pressure advance before entering a model warned that the model "may contain invalid values", which only meant that the placeholder is all zeros. That case now gets its own warning saying that no measurements have been entered. * Keep the Empty Adaptive PA Model a Single Filament Column in Its Test Deserializing an empty string into the model option leaves it with no entries, and get_at on an empty vector aborts on a libstdc++ built with assertions, as the Flatpak unit-test lanes are. Presets always hold one column per filament, so set the value directly. (cherry picked from commit 2cb2f0d2e710931b835d1b31282479686d961b47) --- src/libslic3r/GCode/AdaptivePAProcessor.cpp | 49 +++++++++++-- src/libslic3r/GCode/AdaptivePAProcessor.hpp | 10 ++- src/libslic3r/Print.cpp | 16 +++-- src/slic3r/GUI/ConfigManipulation.cpp | 14 ++-- src/slic3r/GUI/ConfigManipulation.hpp | 2 +- src/slic3r/GUI/Tab.cpp | 2 +- .../test_adaptive_pressure_advance.cpp | 71 +++++++++++++++++++ 7 files changed, 138 insertions(+), 26 deletions(-) diff --git a/src/libslic3r/GCode/AdaptivePAProcessor.cpp b/src/libslic3r/GCode/AdaptivePAProcessor.cpp index 06894c111a..73c15c4272 100644 --- a/src/libslic3r/GCode/AdaptivePAProcessor.cpp +++ b/src/libslic3r/GCode/AdaptivePAProcessor.cpp @@ -9,6 +9,7 @@ #include "AdaptivePAProcessor.hpp" #include #include +#include #include #include #include @@ -329,6 +330,25 @@ std::string AdaptivePAProcessor::process_layer(std::string &&gcode) { return output.str(); } +// The model line with the whitespace around each comma-separated value removed. +static std::string trim_adaptive_pa_model_fields(const std::string& line) +{ + std::string trimmed; + std::istringstream line_stream(line); + std::string field; + bool first = true; + while (std::getline(line_stream, field, ',')) { + const auto begin = field.find_first_not_of(" \t\r"); + const auto end = field.find_last_not_of(" \t\r"); + if (!first) + trimmed += ','; + if (begin != std::string::npos) + trimmed += field.substr(begin, end - begin + 1); + first = false; + } + return trimmed; +} + std::string AdaptivePAProcessor::validate_adaptive_pa_model(const std::string& model_str) { if (model_str.empty()) @@ -341,14 +361,11 @@ std::string AdaptivePAProcessor::validate_adaptive_pa_model(const std::string& m while (std::getline(model_stream, line)) { ++line_number; - // Trim whitespace - const auto first = line.find_first_not_of(" \t\r\n"); - if (first == std::string::npos) + // Whitespace around each value is ignored, as std::stod ignores it when the model is parsed + line = trim_adaptive_pa_model_fields(line); + if (line.empty()) continue; // Skip empty lines - - const auto last = line.find_last_not_of(" \t\r\n"); - line = line.substr(first, last - first + 1); - + // Only numbers, commas and dots are allowed (no letters or other characters) for (char c : line) { if (!std::isdigit(static_cast(c)) && c != ',' && c != '.') { @@ -404,4 +421,22 @@ std::string AdaptivePAProcessor::validate_adaptive_pa_model(const std::string& m return {}; // All validations passed } +bool AdaptivePAProcessor::adaptive_pa_model_has_measurements(const std::string& model_str) +{ + std::istringstream model_stream(model_str); + std::string line; + while (std::getline(model_stream, line)) { + std::istringstream line_stream(line); + std::string value; + while (std::getline(line_stream, value, ',')) { + // a field that is not a number is left to validate_adaptive_pa_model + char* end = nullptr; + const double number = std::strtod(value.c_str(), &end); + if (end != value.c_str() && number != 0.0) + return true; + } + } + return false; +} + } // namespace Slic3r diff --git a/src/libslic3r/GCode/AdaptivePAProcessor.hpp b/src/libslic3r/GCode/AdaptivePAProcessor.hpp index e3d9ea5bfb..80ae9b76d6 100644 --- a/src/libslic3r/GCode/AdaptivePAProcessor.hpp +++ b/src/libslic3r/GCode/AdaptivePAProcessor.hpp @@ -68,7 +68,15 @@ public: * @return Empty string if valid, or an error message describing the first issue found */ static std::string validate_adaptive_pa_model(const std::string& model_str); - + + /** + * @brief Tells whether a model string holds any measurement. + * + * The default model is a placeholder of zeros, so a filament whose model is blank or + * all zeros has no measurements to interpolate from. + */ + static bool adaptive_pa_model_has_measurements(const std::string& model_str); + private: GCode &m_gcodegen; ///< Reference to the GCode object. std::unordered_map> m_AdaptivePAInterpolators; ///< Map between Interpolator objects and filament config indices (null when adaptive PA is off) diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index 67bebe43f2..1eaceb20bf 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -2979,13 +2979,15 @@ StringObjectException Print::validate(std::vector *warnin m_config.enable_pressure_advance.get_at(column)) { const std::string pa_model = m_config.adaptive_pressure_advance_model.get_at(column); - if (!pa_model.empty()) { - std::string validation_error = AdaptivePAProcessor::validate_adaptive_pa_model(pa_model); - if (!validation_error.empty()) { - warn(L("The Adaptive Pressure Advance model for one or more extruders may contain invalid values."), - "adaptive_pressure_advance_model"); - break; - } + if (!AdaptivePAProcessor::adaptive_pa_model_has_measurements(pa_model)) { + warn(L("Adaptive pressure advance is enabled for one or more extruders, but no measurements have been entered in their Adaptive Pressure Advance model."), + "adaptive_pressure_advance_model"); + break; + } + if (!AdaptivePAProcessor::validate_adaptive_pa_model(pa_model).empty()) { + warn(L("The Adaptive Pressure Advance model for one or more extruders may contain invalid values."), + "adaptive_pressure_advance_model"); + break; } } } diff --git a/src/slic3r/GUI/ConfigManipulation.cpp b/src/slic3r/GUI/ConfigManipulation.cpp index 5f0998e7fd..f4a9abc2f1 100644 --- a/src/slic3r/GUI/ConfigManipulation.cpp +++ b/src/slic3r/GUI/ConfigManipulation.cpp @@ -174,22 +174,18 @@ void ConfigManipulation::check_nozzle_temperature_initial_layer_range(DynamicPri } } -void ConfigManipulation::check_adaptive_pressure_advance_model(DynamicPrintConfig* config) +void ConfigManipulation::check_adaptive_pressure_advance_model(DynamicPrintConfig* config, unsigned int variant_index) { if (is_msg_dlg_already_exist || !config->has("adaptive_pressure_advance_model")) return; const auto* model = config->option("adaptive_pressure_advance_model"); - if (model == nullptr) + if (model == nullptr || model->values.empty()) return; - // Each extruder variant holds its own model. - std::string error; - for (const std::string& variant_model : model->values) { - error = AdaptivePAProcessor::validate_adaptive_pa_model(variant_model); - if (!error.empty()) - break; - } + // Each extruder variant holds its own model; only the one being edited is checked, the + // others may still hold the default placeholder. + const std::string error = AdaptivePAProcessor::validate_adaptive_pa_model(model->get_at(variant_index)); if (!error.empty()) { wxString msg_text = _L("Adaptive Pressure Advance model validation failed:\n"); msg_text += from_u8(error); diff --git a/src/slic3r/GUI/ConfigManipulation.hpp b/src/slic3r/GUI/ConfigManipulation.hpp index c31f8ef443..4e381a634a 100644 --- a/src/slic3r/GUI/ConfigManipulation.hpp +++ b/src/slic3r/GUI/ConfigManipulation.hpp @@ -97,7 +97,7 @@ public: void check_nozzle_recommended_temperature_range(DynamicPrintConfig *config, unsigned int variant_index); void check_nozzle_temperature_range(DynamicPrintConfig* config, unsigned int variant_index); void check_nozzle_temperature_initial_layer_range(DynamicPrintConfig* config, unsigned int variant_index); - void check_adaptive_pressure_advance_model(DynamicPrintConfig* config); + void check_adaptive_pressure_advance_model(DynamicPrintConfig* config, unsigned int variant_index); void check_filament_max_volumetric_speed(DynamicPrintConfig *config); void check_filament_ironing_spacing(DynamicPrintConfig *config); void check_chamber_temperature(DynamicPrintConfig* config); diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index 9107e9bd95..80e047cad7 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -4830,7 +4830,7 @@ void TabFilament::build() update_dirty(); if (opt_key.substr(0, opt_key.find('#')) == "adaptive_pressure_advance_model") - m_config_manipulation.check_adaptive_pressure_advance_model(&filament_config); + m_config_manipulation.check_adaptive_pressure_advance_model(&filament_config, selected_variant_index()); on_value_change(opt_key, value); }; diff --git a/tests/fff_print/test_adaptive_pressure_advance.cpp b/tests/fff_print/test_adaptive_pressure_advance.cpp index 700e79de96..4383b8030c 100644 --- a/tests/fff_print/test_adaptive_pressure_advance.cpp +++ b/tests/fff_print/test_adaptive_pressure_advance.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,6 +7,11 @@ #include #include +#include "libslic3r/Config.hpp" +#include "libslic3r/GCode/AdaptivePAProcessor.hpp" +#include "libslic3r/Model.hpp" +#include "libslic3r/Print.hpp" +#include "libslic3r/PrintBase.hpp" #include "libslic3r/PrintConfig.hpp" #include "test_helpers.hpp" @@ -70,3 +76,68 @@ TEST_CASE("Adaptive pressure advance reads the speed of an extrusion that inheri CHECK_THAT(speed, Catch::Matchers::WithinAbs(150.0, 0.01)); } } + +TEST_CASE("The adaptive pressure advance model validator ignores whitespace around the values", "[AdaptivePressureAdvance]") +{ + CHECK(AdaptivePAProcessor::validate_adaptive_pa_model("0.027, 8.1416, 2000.0\n0.017,\t20.354, 10000.0").empty()); + CHECK(AdaptivePAProcessor::validate_adaptive_pa_model("0.027,8.1416,2000.0\n0.017,20.354,10000.0").empty()); + // the values themselves are still checked, and whitespace inside a number is not a number + CHECK_FALSE(AdaptivePAProcessor::validate_adaptive_pa_model("0.027, 0, 2000.0").empty()); + CHECK_FALSE(AdaptivePAProcessor::validate_adaptive_pa_model("0.0 27, 8.1416, 2000.0").empty()); + CHECK_FALSE(AdaptivePAProcessor::validate_adaptive_pa_model("0.027, 8.1416, x").empty()); +} + +TEST_CASE("The default adaptive pressure advance model holds no measurements", "[AdaptivePressureAdvance]") +{ + const std::string default_model = DynamicPrintConfig::full_print_config().opt_string("adaptive_pressure_advance_model", 0u); + CHECK_FALSE(AdaptivePAProcessor::adaptive_pa_model_has_measurements(default_model)); + CHECK_FALSE(AdaptivePAProcessor::adaptive_pa_model_has_measurements("")); + CHECK(AdaptivePAProcessor::adaptive_pa_model_has_measurements("0.04,3.96,3000")); +} + +// The warnings Print::validate raises about the adaptive pressure advance model. +static std::vector adaptive_pa_model_warnings(const std::string &model) +{ + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.set_deserialize_strict({ + { "enable_pressure_advance", "1" }, + { "adaptive_pressure_advance", "1" }, + // the default config fails validation on relative extrusion without a G92 E0 at each layer + { "use_relative_e_distances", "0" }, + }); + // set as a value so that an empty model stays one filament column, as a preset holds it + config.option("adaptive_pressure_advance_model")->values = { model }; + Print print; + Model model_instance; + init_print({ cube(20) }, print, model_instance, config); + std::vector warnings; + const StringObjectException error = print.validate(&warnings); + INFO(error.string); + REQUIRE(error.string.empty()); + std::vector texts; + for (const StringObjectException &warning : warnings) + if (warning.opt_key == "adaptive_pressure_advance_model") + texts.push_back(warning.string); + return texts; +} + +TEST_CASE("Slicing warns when adaptive pressure advance is enabled without a model", "[AdaptivePressureAdvance]") +{ + const std::string default_model = DynamicPrintConfig::full_print_config().opt_string("adaptive_pressure_advance_model", 0u); + const std::string model = GENERATE_COPY(default_model, std::string()); + const std::vector warnings = adaptive_pa_model_warnings(model); + REQUIRE(warnings.size() == 1); + CHECK(warnings.front().find("no measurements") != std::string::npos); +} + +TEST_CASE("Slicing accepts an adaptive pressure advance model typed with spaces after the commas", "[AdaptivePressureAdvance]") +{ + CHECK(adaptive_pa_model_warnings("0.027, 8.1416, 2000.0\n0.027, 20.354, 2000.0\n0.017, 8.1416, 10000.0\n0.017, 20.354, 10000.0").empty()); +} + +TEST_CASE("Slicing warns about an adaptive pressure advance model whose flow is not above its pressure advance", "[AdaptivePressureAdvance]") +{ + const std::vector warnings = adaptive_pa_model_warnings("0.5,0.2,2000\n0.4,0.1,10000"); + REQUIRE(warnings.size() == 1); + CHECK(warnings.front().find("invalid values") != std::string::npos); +}