mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-10-18 05:00:55 +00:00
fix: print statistics placeholders fail in custom G-code (#16348)
The G-code editor lists the Print Statistics placeholders for every custom G-code field, but total_weight, used_filament, total_cost, print_time, normal_print_time, silent_print_time, total_wipe_tower_cost and total_wipe_tower_filament were only defined for filename_format. Using one in custom G-code, including the File header G-code, stopped slicing with "Not a variable name". They are known only once the whole G-code is generated, so they use the reserved-tag mechanism of print_time_sec and used_filament_length: the placeholder parser gets a tag such as @TOTAL_WEIGHT@ and GCodeProcessor::run_post_process() replaces it with the same value filename_format gets, with numbers written to two decimals. The per-tag replacement loops are folded into one helper. The running extruded_* totals are unchanged. Fixes #15320 Fixes #7870
This commit is contained in:
1 parent
6d40ec93f6
commit
c6804fbb68
4 files changed
+122
-71
No files matched your search
@@ -735,6 +735,65 @@ TEST_CASE("Custom G-code placeholders are substituted", "[Print]")
|
||||
CHECK(per_layer.find(";Layer:399 ") != std::string::npos);
|
||||
}
|
||||
|
||||
// The final print statistics are known only after the whole G-code is generated, yet every custom G-code,
|
||||
// the file header included, gets the same values as filename_format.
|
||||
TEST_CASE("Print statistics in custom G-code match the final print statistics", "[Print][Regression]")
|
||||
{
|
||||
const std::string gcode_key = GENERATE(as<std::string>{}, "file_start_gcode", "machine_start_gcode", "machine_end_gcode");
|
||||
// Bambu printers use another tag table and G-code layout.
|
||||
const bool is_bbl = GENERATE(false, true);
|
||||
CAPTURE(gcode_key, is_bbl);
|
||||
|
||||
const std::vector<std::string> number_keys = { "total_weight", "used_filament", "total_cost", "total_wipe_tower_cost",
|
||||
"total_wipe_tower_filament" };
|
||||
const std::vector<std::string> time_keys = { "print_time", "normal_print_time", "silent_print_time" };
|
||||
std::string templ;
|
||||
for (const std::vector<std::string> *keys : { &number_keys, &time_keys })
|
||||
for (const std::string &key : *keys)
|
||||
templ += "; stat " + key + "={" + key + "}\n";
|
||||
|
||||
DynamicPrintConfig config = DynamicPrintConfig::full_print_config();
|
||||
config.set_deserialize_strict({
|
||||
{ gcode_key, templ },
|
||||
{ "filament_density", "1.25" },
|
||||
{ "filament_cost", "20" },
|
||||
{ "time_cost", "2" },
|
||||
});
|
||||
Print print;
|
||||
Model model;
|
||||
init_print({ cube(20) }, print, model, config);
|
||||
print.is_BBL_printer() = is_bbl;
|
||||
|
||||
std::string gcode;
|
||||
REQUIRE_NOTHROW(gcode = Slic3r::Test::gcode(print));
|
||||
|
||||
// Match whole lines only: the config block repeats the template on a single line, and Bambu printers
|
||||
// write that block before the start G-code.
|
||||
const std::string lines = "\n" + gcode;
|
||||
const auto value_of = [&lines](const std::string &key) {
|
||||
const std::string prefix = "\n; stat " + key + "=";
|
||||
const size_t start = lines.find(prefix);
|
||||
REQUIRE(start != std::string::npos);
|
||||
const size_t begin = start + prefix.size();
|
||||
return lines.substr(begin, lines.find('\n', begin) - begin);
|
||||
};
|
||||
// The same statistics filename_format is expanded with.
|
||||
DynamicConfig stats = print.print_statistics().config();
|
||||
// A 20mm cube with filament_density 1.25 weighs something, unlike the running extruded_weight_total in the header.
|
||||
CHECK(stats.opt_float("total_weight") > 0.);
|
||||
for (const std::string &key : number_keys) {
|
||||
CAPTURE(key);
|
||||
const std::string value = value_of(key);
|
||||
REQUIRE(value.find('@') == std::string::npos);
|
||||
// Written with two decimals.
|
||||
CHECK_THAT(std::stod(value), Catch::Matchers::WithinAbs(stats.opt_float(key), 0.006));
|
||||
}
|
||||
for (const std::string &key : time_keys) {
|
||||
CAPTURE(key);
|
||||
CHECK(value_of(key) == stats.opt_string(key));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("export_gcode writes G-code without a result pointer", "[Print][export_gcode]")
|
||||
{
|
||||
Print print;
|
||||
|
||||
Reference in new issue
Block a user