Skip to content

Commit 4a73af4

Browse files
committed
Restrict speed setting to fixed-point output with 3 decimal places. Includes test.
Fixes #4769
1 parent 05a2f90 commit 4a73af4

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/test/libslic3r/test_gcodewriter.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,30 @@ SCENARIO("lift() is not ignored after unlift() at normal values of Z") {
9898
}
9999
}
100100
}
101+
102+
SCENARIO("set_speed emits values with fixed-point output.") {
103+
104+
GIVEN("GCodeWriter instance") {
105+
GCodeWriter writer;
106+
WHEN("set_speed is called to set speed to 1.09321e+06") {
107+
THEN("Output string is G1 F1093210.000") {
108+
REQUIRE_THAT(writer.set_speed(1.09321e+06), Catch::Equals("G1 F1093210.000\n"));
109+
}
110+
}
111+
WHEN("set_speed is called to set speed to 1") {
112+
THEN("Output string is G1 F1.000") {
113+
REQUIRE_THAT(writer.set_speed(1.0), Catch::Equals("G1 F1.000\n"));
114+
}
115+
}
116+
WHEN("set_speed is called to set speed to 203.200022") {
117+
THEN("Output string is G1 F203.200") {
118+
REQUIRE_THAT(writer.set_speed(203.200022), Catch::Equals("G1 F203.200\n"));
119+
}
120+
}
121+
WHEN("set_speed is called to set speed to 203.200522") {
122+
THEN("Output string is G1 F203.200") {
123+
REQUIRE_THAT(writer.set_speed(203.200522), Catch::Equals("G1 F203.201\n"));
124+
}
125+
}
126+
}
127+
}

xs/src/libslic3r/GCodeWriter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ GCodeWriter::set_speed(double F, const std::string &comment,
309309
const std::string &cooling_marker) const
310310
{
311311
std::ostringstream gcode;
312-
gcode << "G1 F" << F;
312+
gcode.precision(3);
313+
gcode << "G1 F" << std::fixed << F;
313314
COMMENT(comment);
314315
gcode << cooling_marker;
315316
gcode << "\n";

0 commit comments

Comments
 (0)