Files
OrcaSlicer/tests/libslic3r/test_calib.cpp
T
SoftFever 0616068e26 Run commands, tools and the plugin API through one operations layer
A Speed Dial command, the MCP tool that does the same thing and the plugin
API's preset calls now run one shared operation, so every route accepts and
refuses the same states, with one wording and a code for each refusal. An
import by a command or a tool is one undo step, and import_file adds geometry
only.

The cases where two routes behaved differently now behave the same: a project
is not saved under a .gcode.3mf name, an empty project exports as .3mf but not
as STL, arrange runs from any view and with a gizmo open, a mixed-colour
filament slot is refused as one, a hidden preset is shown before it is
selected, and a layer is refused while the Preview has none.
2026-11-12 22:06:10 +08:00

132 lines
4.2 KiB
C++

#include <catch2/catch_all.hpp>
#include <algorithm>
#include "libslic3r/CustomGCode.hpp"
#include <sstream>
#include <string>
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
#include "libslic3r/calib.hpp"
#include "libslic3r/Model.hpp"
#include "libslic3r/TriangleMesh.hpp"
#include "libslic3r/PrintConfig.hpp"
#include "libslic3r/Point.hpp"
using namespace Slic3r;
namespace {
// The width-resolution getters are protected; expose them so the resolution can be asserted directly.
struct PaPatternProbe : public CalibPressureAdvancePattern
{
using CalibPressureAdvancePattern::CalibPressureAdvancePattern;
using CalibPressureAdvancePattern::line_width;
using CalibPressureAdvancePattern::line_width_first_layer;
};
} // namespace
TEST_CASE("Zero calibration line width resolves to a positive default", "[Calib][Regression]")
{
DynamicPrintConfig config = DynamicPrintConfig::full_print_config();
config.set_deserialize_strict({
{"line_width", "0"},
{"initial_layer_line_width", "0"},
});
Model model;
model.add_object("cube", "", make_cube(20, 20, 20))->add_instance();
Calib_Params params;
params.mode = CalibMode::Calib_PA_Pattern;
PaPatternProbe pattern(params, config, /* is_bbl_machine */ true, *model.objects.front(), Vec3d(0, 0, 0));
REQUIRE(pattern.line_width() > 0.);
REQUIRE(pattern.line_width_first_layer() > 0.);
}
namespace {
struct EndState { double final_e; double max_e; };
EndState simulate_absolute_e(const std::string &gcode)
{
double final_e = 0.;
double max_e = 0.;
std::istringstream lines(gcode);
std::string line;
while (std::getline(lines, line)) {
std::istringstream words(line);
std::string op;
if (!(words >> op))
continue;
if (op != "G1" && op != "G0" && op != "G92")
continue;
std::string word;
while (words >> word) {
if (word.size() >= 2 && word[0] == 'E') {
final_e = std::stod(word.substr(1));
max_e = std::max(max_e, final_e);
break;
}
}
}
return {final_e, max_e};
}
} // namespace
TEST_CASE("PA pattern resets the extruder after the final layer in absolute E mode", "[Calib][Regression]")
{
DynamicPrintConfig config = DynamicPrintConfig::full_print_config();
config.set_deserialize_strict({
{"use_relative_e_distances", "0"},
{"line_width", "0.45"},
{"initial_layer_line_width", "0.45"},
});
Model model;
model.add_object("cube", "", make_cube(20, 20, 20))->add_instance();
Calib_Params params;
params.mode = CalibMode::Calib_PA_Pattern;
params.start = 0.;
params.end = 0.08;
params.step = 0.002;
CalibPressureAdvancePattern pattern(params, config, /* is_bbl_machine */ false, *model.objects.front(), Vec3d(0, 0, 0));
const CustomGCode::Info info = pattern.generate_custom_gcodes(config, /* is_bbl_machine */ false, *model.objects.front(),
Vec3d(0, 0, 0));
std::string gcode;
for (const CustomGCode::Item &item : info.gcodes)
gcode += item.extra;
const EndState state = simulate_absolute_e(gcode);
REQUIRE(state.max_e > 1.);
REQUIRE_THAT(state.final_e, Catch::Matchers::WithinAbs(0., 1e-9));
}
TEST_CASE("a temperature tower's temperatures are brought into 155-500 and rounded down to 5 degree blocks", "[Calib]")
{
using Catch::Matchers::WithinAbs;
CHECK_THAT(clamp_calib_temp(154.), WithinAbs(155., 1e-9));
CHECK_THAT(clamp_calib_temp(155.), WithinAbs(155., 1e-9));
CHECK_THAT(clamp_calib_temp(230.), WithinAbs(230., 1e-9));
CHECK_THAT(clamp_calib_temp(500.), WithinAbs(500., 1e-9));
CHECK_THAT(clamp_calib_temp(501.), WithinAbs(500., 1e-9));
CHECK_THAT(round_calib_temp(232.), WithinAbs(230., 1e-9));
CHECK_THAT(round_calib_temp(234.9), WithinAbs(230., 1e-9));
CHECK_THAT(round_calib_temp(235.), WithinAbs(235., 1e-9));
CHECK_THAT(round_calib_temp(clamp_calib_temp(999.)), WithinAbs(500., 1e-9));
CHECK_THAT(round_calib_temp(clamp_calib_temp(1.)), WithinAbs(155., 1e-9));
}