diff --git a/src/libslic3r/GCodeWriter.cpp b/src/libslic3r/GCodeWriter.cpp index 51fca58f6..364ba12ae 100644 --- a/src/libslic3r/GCodeWriter.cpp +++ b/src/libslic3r/GCodeWriter.cpp @@ -269,7 +269,7 @@ std::string GCodeWriter::set_speed(double F, const std::string &comment, const s assert(F > 0.); assert(F < 100000.); std::ostringstream gcode; - gcode << "G1 F" << F; + gcode << "G1 F" << XYZF_NUM(F); COMMENT(comment); gcode << cooling_marker; gcode << "\n"; diff --git a/tests/data/fff_print_tests/test_gcodewriter/config_lift_unlift.ini b/tests/data/fff_print_tests/test_gcodewriter/config_lift_unlift.ini new file mode 100644 index 000000000..9d44cd43e --- /dev/null +++ b/tests/data/fff_print_tests/test_gcodewriter/config_lift_unlift.ini @@ -0,0 +1,30 @@ +before_layer_gcode = +between_objects_gcode = +end_filament_gcode = "; Filament-specific end gcode \n;END gcode for filament\n" +end_gcode = M104 S0 ; turn off temperature\nG28 X0 ; home X axis\nM84 ; disable motors\n +extrusion_axis = E +extrusion_multiplier = 1 +filament_cost = 0 +filament_density = 0 +filament_diameter = 3 +filament_max_volumetric_speed = 0 +gcode_comments = 0 +gcode_flavor = reprap +layer_gcode = +max_print_speed = 80 +max_volumetric_speed = 0 +retract_length = 2 +retract_length_toolchange = 10 +retract_lift = 1.5 +retract_lift_above = 0 +retract_lift_below = 0 +retract_restart_extra = 0 +retract_restart_extra_toolchange = 0 +retract_speed = 40 +start_filament_gcode = "; Filament gcode\n" +start_gcode = G28 ; home all axes\nG1 Z5 F5000 ; lift nozzle\n +toolchange_gcode = +travel_speed = 130 +use_firmware_retraction = 0 +use_relative_e_distances = 0 +use_volumetric_e = 0 diff --git a/tests/fff_print/CMakeLists.txt b/tests/fff_print/CMakeLists.txt index 3c47d637e..1ba953b8b 100644 --- a/tests/fff_print/CMakeLists.txt +++ b/tests/fff_print/CMakeLists.txt @@ -4,6 +4,7 @@ add_executable(${_TEST_NAME}_tests test_data.cpp test_data.hpp test_flow.cpp + test_gcodewriter.cpp test_skirt_brim.cpp test_trianglemesh.cpp ) diff --git a/tests/fff_print/test_data.cpp b/tests/fff_print/test_data.cpp index a07360af7..7144ce5de 100644 --- a/tests/fff_print/test_data.cpp +++ b/tests/fff_print/test_data.cpp @@ -11,7 +11,6 @@ #include #include -using namespace std::string_literals; using namespace std; namespace Slic3r { namespace Test { diff --git a/tests/fff_print/test_gcodewriter.cpp b/tests/fff_print/test_gcodewriter.cpp new file mode 100644 index 000000000..4f1c550eb --- /dev/null +++ b/tests/fff_print/test_gcodewriter.cpp @@ -0,0 +1,124 @@ +#include + +#include + +#include "libslic3r/GCodeWriter.hpp" + +using namespace Slic3r; + +SCENARIO("lift() and unlift() behavior with large values of Z", "[!shouldfail]") { + GIVEN("A config from a file and a single extruder.") { + GCodeWriter writer; + GCodeConfig &config = writer.config; + config.load(std::string(TEST_DATA_DIR) + "/fff_print_tests/test_gcodewriter/config_lift_unlift.ini"); + + std::vector extruder_ids {0}; + writer.set_extruders(extruder_ids); + writer.set_extruder(0); + + WHEN("Z is set to 9007199254740992") { + double trouble_Z = 9007199254740992; + writer.travel_to_z(trouble_Z); + AND_WHEN("GcodeWriter::Lift() is called") { + REQUIRE(writer.lift().size() > 0); + AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") { + REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0); + AND_WHEN("GCodeWriter::Unlift() is called") { + REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens. + THEN("GCodeWriter::Lift() emits gcode.") { + REQUIRE(writer.lift().size() > 0); + } + } + } + } + } + } +} + +SCENARIO("lift() is not ignored after unlift() at normal values of Z") { + GIVEN("A config from a file and a single extruder.") { + GCodeWriter writer; + GCodeConfig &config = writer.config; + config.load(std::string(TEST_DATA_DIR) + "/fff_print_tests/test_gcodewriter/config_lift_unlift.ini"); + + std::vector extruder_ids {0}; + writer.set_extruders(extruder_ids); + writer.set_extruder(0); + + WHEN("Z is set to 203") { + double trouble_Z = 203; + writer.travel_to_z(trouble_Z); + AND_WHEN("GcodeWriter::Lift() is called") { + REQUIRE(writer.lift().size() > 0); + AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") { + REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0); + AND_WHEN("GCodeWriter::Unlift() is called") { + REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens. + THEN("GCodeWriter::Lift() emits gcode.") { + REQUIRE(writer.lift().size() > 0); + } + } + } + } + } + WHEN("Z is set to 500003") { + double trouble_Z = 500003; + writer.travel_to_z(trouble_Z); + AND_WHEN("GcodeWriter::Lift() is called") { + REQUIRE(writer.lift().size() > 0); + AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") { + REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0); + AND_WHEN("GCodeWriter::Unlift() is called") { + REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens. + THEN("GCodeWriter::Lift() emits gcode.") { + REQUIRE(writer.lift().size() > 0); + } + } + } + } + } + WHEN("Z is set to 10.3") { + double trouble_Z = 10.3; + writer.travel_to_z(trouble_Z); + AND_WHEN("GcodeWriter::Lift() is called") { + REQUIRE(writer.lift().size() > 0); + AND_WHEN("Z is moved post-lift to the same delta as the config Z lift") { + REQUIRE(writer.travel_to_z(trouble_Z + config.retract_lift.values[0]).size() == 0); + AND_WHEN("GCodeWriter::Unlift() is called") { + REQUIRE(writer.unlift().size() == 0); // we're the same height so no additional move happens. + THEN("GCodeWriter::Lift() emits gcode.") { + REQUIRE(writer.lift().size() > 0); + } + } + } + } + } + } +} + +SCENARIO("set_speed emits values with fixed-point output.") { + + GIVEN("GCodeWriter instance") { + GCodeWriter writer; + WHEN("set_speed is called to set speed to 99999.123") { + THEN("Output string is G1 F99999.123") { + REQUIRE_THAT(writer.set_speed(99999.123), Catch::Equals("G1 F99999.123\n")); + } + } + WHEN("set_speed is called to set speed to 1") { + THEN("Output string is G1 F1.000") { + REQUIRE_THAT(writer.set_speed(1.0), Catch::Equals("G1 F1.000\n")); + } + } + WHEN("set_speed is called to set speed to 203.200022") { + THEN("Output string is G1 F203.200") { + REQUIRE_THAT(writer.set_speed(203.200022), Catch::Equals("G1 F203.200\n")); + } + } + WHEN("set_speed is called to set speed to 203.200522") { + THEN("Output string is G1 F203.201") { + REQUIRE_THAT(writer.set_speed(203.200522), Catch::Equals("G1 F203.201\n")); + } + } + } +}