test_skirt_brim.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/GCodeReader.hpp"
  3. #include "libslic3r/Config.hpp"
  4. #include "libslic3r/Geometry.hpp"
  5. #include <boost/algorithm/string.hpp>
  6. #include "test_data.hpp" // get access to init_print, etc
  7. using namespace Slic3r::Test;
  8. using namespace Slic3r;
  9. /// Helper method to find the tool used for the brim (always the first extrusion)
  10. static int get_brim_tool(const std::string &gcode)
  11. {
  12. int brim_tool = -1;
  13. int tool = -1;
  14. GCodeReader parser;
  15. parser.parse_buffer(gcode, [&tool, &brim_tool] (Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line)
  16. {
  17. // if the command is a T command, set the the current tool
  18. if (boost::starts_with(line.cmd(), "T")) {
  19. tool = atoi(line.cmd().data() + 1);
  20. } else if (line.cmd() == "G1" && line.extruding(self) && line.dist_XY(self) > 0 && brim_tool < 0) {
  21. brim_tool = tool;
  22. }
  23. });
  24. return brim_tool;
  25. }
  26. TEST_CASE("Skirt height is honored", "[Skirt]") {
  27. DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  28. config.set_deserialize_strict({
  29. { "skirts", 1 },
  30. { "skirt_height", 5 },
  31. { "perimeters", 0 },
  32. { "support_material_speed", 99 },
  33. // avoid altering speeds unexpectedly
  34. { "cooling", false },
  35. { "first_layer_speed", "100%" }
  36. });
  37. std::string gcode;
  38. SECTION("printing a single object") {
  39. gcode = Slic3r::Test::slice({TestMesh::cube_20x20x20}, config);
  40. }
  41. SECTION("printing multiple objects") {
  42. gcode = Slic3r::Test::slice({TestMesh::cube_20x20x20, TestMesh::cube_20x20x20}, config);
  43. }
  44. std::map<double, bool> layers_with_skirt;
  45. double support_speed = config.opt<Slic3r::ConfigOptionFloat>("support_material_speed")->value * MM_PER_MIN;
  46. GCodeReader parser;
  47. parser.parse_buffer(gcode, [&layers_with_skirt, &support_speed] (Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line) {
  48. if (line.extruding(self) && self.f() == Approx(support_speed)) {
  49. layers_with_skirt[self.z()] = 1;
  50. }
  51. });
  52. REQUIRE(layers_with_skirt.size() == (size_t)config.opt_int("skirt_height"));
  53. }
  54. SCENARIO("Original Slic3r Skirt/Brim tests", "[SkirtBrim]") {
  55. GIVEN("A default configuration") {
  56. DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  57. config.set_num_extruders(4);
  58. config.set_deserialize_strict({
  59. { "support_material_speed", 99 },
  60. { "first_layer_height", 0.3 },
  61. { "gcode_comments", true },
  62. // avoid altering speeds unexpectedly
  63. { "cooling", false },
  64. { "first_layer_speed", "100%" },
  65. // remove noise from top/solid layers
  66. { "top_solid_layers", 0 },
  67. { "bottom_solid_layers", 1 },
  68. { "start_gcode", "T[initial_tool]\n" }
  69. });
  70. WHEN("Brim width is set to 5") {
  71. config.set_deserialize_strict({
  72. { "perimeters", 0 },
  73. { "skirts", 0 },
  74. { "brim_width", 5 }
  75. });
  76. THEN("Brim is generated") {
  77. std::string gcode = Slic3r::Test::slice({TestMesh::cube_20x20x20}, config);
  78. bool brim_generated = false;
  79. double support_speed = config.opt<Slic3r::ConfigOptionFloat>("support_material_speed")->value * MM_PER_MIN;
  80. Slic3r::GCodeReader parser;
  81. parser.parse_buffer(gcode, [&brim_generated, support_speed] (Slic3r::GCodeReader& self, const Slic3r::GCodeReader::GCodeLine& line) {
  82. if (self.z() == Approx(0.3) || line.new_Z(self) == Approx(0.3)) {
  83. if (line.extruding(self) && self.f() == Approx(support_speed)) {
  84. brim_generated = true;
  85. }
  86. }
  87. });
  88. REQUIRE(brim_generated);
  89. }
  90. }
  91. WHEN("Skirt area is smaller than the brim") {
  92. config.set_deserialize_strict({
  93. { "skirts", 1 },
  94. { "brim_width", 10}
  95. });
  96. THEN("Gcode generates") {
  97. REQUIRE(! Slic3r::Test::slice({TestMesh::cube_20x20x20}, config).empty());
  98. }
  99. }
  100. WHEN("Skirt height is 0 and skirts > 0") {
  101. config.set_deserialize_strict({
  102. { "skirts", 2 },
  103. { "skirt_height", 0 }
  104. });
  105. THEN("Gcode generates") {
  106. REQUIRE(! Slic3r::Test::slice({TestMesh::cube_20x20x20}, config).empty());
  107. }
  108. }
  109. #if 0
  110. // This is a real error! One shall print the brim with the external perimeter extruder!
  111. WHEN("Perimeter extruder = 2 and support extruders = 3") {
  112. THEN("Brim is printed with the extruder used for the perimeters of first object") {
  113. config.set_deserialize_strict({
  114. { "skirts", 0 },
  115. { "brim_width", 5 },
  116. { "perimeter_extruder", 2 },
  117. { "support_material_extruder", 3 },
  118. { "infill_extruder", 4 }
  119. });
  120. std::string gcode = Slic3r::Test::slice({TestMesh::cube_20x20x20}, config);
  121. int tool = get_brim_tool(gcode);
  122. REQUIRE(tool == config.opt_int("perimeter_extruder") - 1);
  123. }
  124. }
  125. WHEN("Perimeter extruder = 2, support extruders = 3, raft is enabled") {
  126. THEN("brim is printed with same extruder as skirt") {
  127. config.set_deserialize_strict({
  128. { "skirts", 0 },
  129. { "brim_width", 5 },
  130. { "perimeter_extruder", 2 },
  131. { "support_material_extruder", 3 },
  132. { "infill_extruder", 4 },
  133. { "raft_layers", 1 }
  134. });
  135. std::string gcode = Slic3r::Test::slice({TestMesh::cube_20x20x20}, config);
  136. int tool = get_brim_tool(gcode);
  137. REQUIRE(tool == config.opt_int("support_material_extruder") - 1);
  138. }
  139. }
  140. #endif
  141. WHEN("brim width to 1 with layer_width of 0.5") {
  142. config.set_deserialize_strict({
  143. { "skirts", 0 },
  144. { "first_layer_extrusion_width", 0.5 },
  145. { "brim_width", 1 }
  146. });
  147. THEN("2 brim lines") {
  148. Slic3r::Print print;
  149. Slic3r::Test::init_and_process_print({TestMesh::cube_20x20x20}, print, config);
  150. REQUIRE(print.brim().entities.size() == 2);
  151. }
  152. }
  153. #if 0
  154. WHEN("brim ears on a square") {
  155. config.set_deserialize_strict({
  156. { "skirts", 0 },
  157. { "first_layer_extrusion_width", 0.5 },
  158. { "brim_width", 1 },
  159. { "brim_ears", 1 },
  160. { "brim_ears_max_angle", 91 }
  161. });
  162. Slic3r::Print print;
  163. Slic3r::Test::init_and_process_print({TestMesh::cube_20x20x20}, print, config);
  164. THEN("Four brim ears") {
  165. REQUIRE(print.brim().entities.size() == 4);
  166. }
  167. }
  168. WHEN("brim ears on a square but with a too small max angle") {
  169. config.set_deserialize_strict({
  170. { "skirts", 0 },
  171. { "first_layer_extrusion_width", 0.5 },
  172. { "brim_width", 1 },
  173. { "brim_ears", 1 },
  174. { "brim_ears_max_angle", 89 }
  175. });
  176. THEN("no brim") {
  177. Slic3r::Print print;
  178. Slic3r::Test::init_and_process_print({ TestMesh::cube_20x20x20 }, print, config);
  179. REQUIRE(print.brim().entities.size() == 0);
  180. }
  181. }
  182. #endif
  183. WHEN("Object is plated with overhang support and a brim") {
  184. config.set_deserialize_strict({
  185. { "layer_height", 0.4 },
  186. { "first_layer_height", 0.4 },
  187. { "skirts", 1 },
  188. { "skirt_distance", 0 },
  189. { "support_material_speed", 99 },
  190. { "perimeter_extruder", 1 },
  191. { "support_material_extruder", 2 },
  192. { "infill_extruder", 3 }, // ensure that a tool command gets emitted.
  193. { "cooling", false }, // to prevent speeds to be altered
  194. { "first_layer_speed", "100%" }, // to prevent speeds to be altered
  195. { "start_gcode", "T[initial_tool]\n" }
  196. });
  197. THEN("overhang generates?") {
  198. //FIXME does it make sense?
  199. REQUIRE(! Slic3r::Test::slice({TestMesh::overhang}, config).empty());
  200. }
  201. // config.set("support_material", true); // to prevent speeds to be altered
  202. #if 0
  203. // This test is not finished.
  204. THEN("skirt length is large enough to contain object with support") {
  205. CHECK(config.opt_bool("support_material")); // test is not valid if support material is off
  206. std::string gcode = Slic3r::Test::slice({TestMesh::cube_20x20x20}, config);
  207. double support_speed = config.opt<ConfigOptionFloat>("support_material_speed")->value * MM_PER_MIN;
  208. double skirt_length = 0.0;
  209. Points extrusion_points;
  210. int tool = -1;
  211. GCodeReader parser;
  212. parser.parse_buffer(gcode, [config, &extrusion_points, &tool, &skirt_length, support_speed] (Slic3r::GCodeReader& self, const Slic3r::GCodeReader::GCodeLine& line) {
  213. // std::cerr << line.cmd() << "\n";
  214. if (boost::starts_with(line.cmd(), "T")) {
  215. tool = atoi(line.cmd().data() + 1);
  216. } else if (self.z() == Approx(config.opt<ConfigOptionFloat>("first_layer_height")->value)) {
  217. // on first layer
  218. if (line.extruding(self) && line.dist_XY(self) > 0) {
  219. float speed = ( self.f() > 0 ? self.f() : line.new_F(self));
  220. // std::cerr << "Tool " << tool << "\n";
  221. if (speed == Approx(support_speed) && tool == config.opt_int("perimeter_extruder") - 1) {
  222. // Skirt uses first material extruder, support material speed.
  223. skirt_length += line.dist_XY(self);
  224. } else
  225. extrusion_points.push_back(Slic3r::Point::new_scale(line.new_X(self), line.new_Y(self)));
  226. }
  227. }
  228. if (self.z() == Approx(0.3) || line.new_Z(self) == Approx(0.3)) {
  229. if (line.extruding(self) && self.f() == Approx(support_speed)) {
  230. }
  231. }
  232. });
  233. Slic3r::Polygon convex_hull = Slic3r::Geometry::convex_hull(extrusion_points);
  234. double hull_perimeter = unscale<double>(convex_hull.split_at_first_point().length());
  235. REQUIRE(skirt_length > hull_perimeter);
  236. }
  237. #endif
  238. }
  239. WHEN("Large minimum skirt length is used.") {
  240. config.set("min_skirt_length", 20);
  241. THEN("Gcode generation doesn't crash") {
  242. REQUIRE(! Slic3r::Test::slice({TestMesh::cube_20x20x20}, config).empty());
  243. }
  244. }
  245. }
  246. }