Skip to content

Using GCodeReader in Tests

Joseph Lenox edited this page Sep 14, 2018 · 2 revisions

Overview

GCodeReader is a powerful software tool to process GCode snippets to verify certain behavior of the system.

It is chiefly useful when examining the final gcode export is the best way of verifying that Slic3r is doing what the test expects.

In general, however, it's usually better/easier to directly query the stages right before gcode construction for many features.

Usage

The basic usage is to write a method that is expected to execute once for every line in the input gcode. State is maintained through passing external variables.

(this method is passed as a lambda method here to support inlining the processing method directly in the test).

Example

From src/test/libslic3r/test_printgcode.cpp

THEN("Z height resets on object change") {
    double final_z {0.0};
    bool reset {false};
    auto reader {GCodeReader()};
    reader.apply_config(print->config);
    reader.parse(exported, [&final_z, &reset] (GCodeReader& self, const GCodeReader::GCodeLine& line) 
    {
        if (final_z > 0 && std::abs(self.Z - 0.3) < 0.01 ) { // saw higher Z before this, now it's lower
            reset = true;
        } else {
            final_z = std::max(final_z, static_cast<double>(self.Z)); // record the highest Z point we reach
        }
    });
    REQUIRE(reset == true);
}

Reference: http://manual.slic3r.org/libslic3r-doc/class_slic3r_1_1_g_code_reader.html