test_custom_gcode.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include <catch2/catch.hpp>
  2. #include <exception>
  3. #include <numeric>
  4. #include <sstream>
  5. #include <boost/regex.hpp>
  6. #include "libslic3r/Config.hpp"
  7. #include "libslic3r/Print.hpp"
  8. #include "libslic3r/PrintConfig.hpp"
  9. #include "libslic3r/libslic3r.h"
  10. #include "test_data.hpp"
  11. using namespace Slic3r;
  12. SCENARIO("Output file format", "[CustomGCode]")
  13. {
  14. WHEN("output_file_format set") {
  15. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  16. { "travel_speed", "130"},
  17. { "layer_height", "0.4"},
  18. { "output_filename_format", "ts_[travel_speed]_lh_[layer_height].gcode" },
  19. { "start_gcode", "TRAVEL:[travel_speed] HEIGHT:[layer_height]\n" }
  20. });
  21. Print print;
  22. Model model;
  23. Test::init_print({ Test::TestMesh::cube_2x20x10 }, print, model, config);
  24. std::string output_file = print.output_filepath({}, {});
  25. THEN("print config options are replaced in output filename") {
  26. REQUIRE(output_file == "ts_130_lh_0.4.gcode");
  27. }
  28. }
  29. }
  30. SCENARIO("Custom G-code", "[CustomGCode]")
  31. {
  32. WHEN("start_gcode and layer_gcode set") {
  33. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  34. { "start_gcode", "_MY_CUSTOM_START_GCODE_" }, // to avoid dealing with the nozzle lift in start G-code
  35. { "layer_gcode", "_MY_CUSTOM_LAYER_GCODE_" }
  36. });
  37. GCodeReader parser;
  38. bool last_move_was_z_change = false;
  39. int num_layer_changes_not_applied = 0;
  40. parser.parse_buffer(Slic3r::Test::slice({ Test::TestMesh::cube_2x20x10 }, config),
  41. [&last_move_was_z_change, &num_layer_changes_not_applied](Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line)
  42. {
  43. if (last_move_was_z_change != line.cmd_is("_MY_CUSTOM_LAYER_GCODE_"))
  44. ++ num_layer_changes_not_applied;
  45. last_move_was_z_change = line.dist_Z(self) > 0;
  46. });
  47. THEN("custom layer G-code is applied after Z move and before other moves") {
  48. REQUIRE(num_layer_changes_not_applied == 0);
  49. }
  50. };
  51. auto config = Slic3r::DynamicPrintConfig::new_with({
  52. { "nozzle_diameter", { 0.6,0.6,0.6,0.6 } },
  53. { "extruder", 2 },
  54. { "first_layer_temperature", { 200, 205 } }
  55. });
  56. config.normalize_fdm();
  57. WHEN("Printing with single but non-zero extruder") {
  58. std::string gcode = Slic3r::Test::slice({ Slic3r::Test::TestMesh::cube_20x20x20 }, config);
  59. THEN("temperature set correctly for non-zero yet single extruder") {
  60. REQUIRE(Slic3r::Test::contains(gcode, "\nM104 S205 T1 ;"));
  61. }
  62. THEN("unused extruder correctly ignored") {
  63. REQUIRE(! Slic3r::Test::contains_regex(gcode, "M104 S\\d+ T0"));
  64. }
  65. }
  66. WHEN("Printing with two extruders") {
  67. config.opt_int("infill_extruder") = 1;
  68. std::string gcode = Slic3r::Test::slice({ Slic3r::Test::TestMesh::cube_20x20x20 }, config);
  69. THEN("temperature set correctly for first extruder") {
  70. REQUIRE(Slic3r::Test::contains(gcode, "\nM104 S200 T0 ;"));
  71. };
  72. THEN("temperature set correctly for second extruder") {
  73. REQUIRE(Slic3r::Test::contains(gcode, "\nM104 S205 T1 ;"));
  74. };
  75. }
  76. auto test = [](DynamicPrintConfig &config) {
  77. // we use the [infill_extruder] placeholder to make sure this test doesn't
  78. // catch a false positive caused by the unparsed start G-code option itself
  79. // being embedded in the G-code
  80. config.opt_int("infill_extruder") = 1;
  81. std::string gcode = Slic3r::Test::slice({ Slic3r::Test::TestMesh::cube_20x20x20 }, config);
  82. THEN("temperature placeholder for first extruder correctly populated") {
  83. REQUIRE(Slic3r::Test::contains(gcode, "temp0:200"));
  84. }
  85. THEN("temperature placeholder for second extruder correctly populated") {
  86. REQUIRE(Slic3r::Test::contains(gcode, "temp1:205"));
  87. }
  88. THEN("temperature placeholder for unused extruder populated with first value") {
  89. REQUIRE(Slic3r::Test::contains(gcode, "temp2:200"));
  90. }
  91. };
  92. WHEN("legacy syntax") {
  93. config.set_deserialize_strict("start_gcode",
  94. ";__temp0:[first_layer_temperature_0]__\n"
  95. ";__temp1:[first_layer_temperature_1]__\n"
  96. ";__temp2:[first_layer_temperature_2]__\n");
  97. test(config);
  98. }
  99. WHEN("new syntax") {
  100. config.set_deserialize_strict("start_gcode",
  101. ";__temp0:{first_layer_temperature[0]}__\n"
  102. ";__temp1:{first_layer_temperature[1]}__\n"
  103. ";__temp2:{first_layer_temperature[2]}__\n");
  104. test(config);
  105. }
  106. WHEN("Vojtech's syntax") {
  107. config.set_deserialize_strict({
  108. { "infill_extruder", 1 },
  109. { "start_gcode",
  110. ";substitution:{if infill_extruder==1}extruder1"
  111. "{elsif infill_extruder==2}extruder2"
  112. "{else}extruder3{endif}"
  113. }
  114. });
  115. std::string gcode = Slic3r::Test::slice({ Slic3r::Test::TestMesh::cube_20x20x20 }, config);
  116. THEN("if / else / endif - first block returned") {
  117. REQUIRE(Test::contains(gcode, "\n;substitution:extruder1\n"));
  118. }
  119. }
  120. GIVEN("Layer change G-codes")
  121. {
  122. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  123. { "before_layer_gcode", ";BEFORE [layer_num]" },
  124. { "layer_gcode", ";CHANGE [layer_num]" },
  125. { "support_material", 1 },
  126. { "layer_height", 0.2 }
  127. });
  128. WHEN("before and after layer change G-codes set") {
  129. std::string gcode = Slic3r::Test::slice({ Slic3r::Test::TestMesh::overhang }, config);
  130. GCodeReader parser;
  131. std::vector<int> before;
  132. std::vector<int> change;
  133. parser.parse_buffer(gcode, [&before, &change](Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line){
  134. int d;
  135. if (sscanf(line.raw().c_str(), ";BEFORE %d", &d) == 1)
  136. before.emplace_back(d);
  137. else if (sscanf(line.raw().c_str(), ";CHANGE %d", &d) == 1) {
  138. change.emplace_back(d);
  139. if (d != before.back())
  140. throw std::runtime_error("inconsistent layer_num before and after layer change");
  141. }
  142. });
  143. THEN("layer_num is consistent before and after layer changes") {
  144. REQUIRE(before == change);
  145. }
  146. THEN("layer_num grows continously") {
  147. // i.e. no duplicates or regressions
  148. bool successive = true;
  149. for (size_t i = 1; i < change.size(); ++ i)
  150. if (change[i - 1] + 1 != change[i])
  151. successive = false;
  152. REQUIRE(successive);
  153. }
  154. }
  155. }
  156. GIVEN("if / elsif / elsif / elsif / else / endif")
  157. {
  158. auto config = Slic3r::DynamicPrintConfig::new_with({
  159. { "nozzle_diameter", { 0.6,0.6,0.6,0.6,0.6 } },
  160. { "start_gcode",
  161. ";substitution:{if infill_extruder==1}if block"
  162. "{elsif infill_extruder==2}elsif block 1"
  163. "{elsif infill_extruder==3}elsif block 2"
  164. "{elsif infill_extruder==4}elsif block 3"
  165. "{else}endif block{endif}"
  166. ":end"
  167. }
  168. });
  169. std::string returned[] = { "" /* indexed by one based extruder ID */, "if block", "elsif block 1", "elsif block 2", "elsif block 3", "endif block" };
  170. auto test = [&config, &returned](int i) {
  171. config.set_deserialize_strict({ { "infill_extruder", i } });
  172. std::string gcode = Slic3r::Test::slice({ Slic3r::Test::TestMesh::cube_20x20x20 }, config);
  173. int found_error = 0;
  174. for (int j = 1; j <= 5; ++ j)
  175. if (i != j && Slic3r::Test::contains(gcode, std::string("substitution:") + returned[j] + ":end"))
  176. // failure
  177. ++ found_error;
  178. THEN(std::string("if / else / endif returned ") + returned[i]) {
  179. REQUIRE(Slic3r::Test::contains(gcode, std::string("substitution:") + returned[i] + ":end"));
  180. }
  181. THEN(std::string("if / else / endif - only ") + returned[i] + "returned") {
  182. REQUIRE(found_error == 0);
  183. }
  184. };
  185. WHEN("infill_extruder == 1") { test(1); }
  186. WHEN("infill_extruder == 2") { test(2); }
  187. WHEN("infill_extruder == 3") { test(3); }
  188. WHEN("infill_extruder == 4") { test(4); }
  189. WHEN("infill_extruder == 5") { test(5); }
  190. }
  191. GIVEN("nested if / if / else / endif") {
  192. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  193. { "nozzle_diameter", { 0.6,0.6,0.6,0.6,0.6 } },
  194. { "start_gcode",
  195. ";substitution:{if infill_extruder==1}{if perimeter_extruder==1}block11{else}block12{endif}"
  196. "{elsif infill_extruder==2}{if perimeter_extruder==1}block21{else}block22{endif}"
  197. "{else}{if perimeter_extruder==1}block31{else}block32{endif}{endif}:end"
  198. }
  199. });
  200. auto test = [&config](int i) {
  201. config.opt_int("infill_extruder") = i;
  202. int failed = 0;
  203. for (int j = 1; j <= 2; ++ j) {
  204. config.opt_int("perimeter_extruder") = j;
  205. std::string gcode = Slic3r::Test::slice({ Slic3r::Test::TestMesh::cube_20x20x20 }, config);
  206. if (! Slic3r::Test::contains(gcode, std::string("substitution:block") + std::to_string(i) + std::to_string(j) + ":end"))
  207. ++ failed;
  208. }
  209. THEN(std::string("two level if / else / endif - block for infill_extruder ") + std::to_string(i) + "succeeded") {
  210. REQUIRE(failed == 0);
  211. }
  212. };
  213. WHEN("infill_extruder == 1") { test(1); }
  214. WHEN("infill_extruder == 2") { test(2); }
  215. WHEN("infill_extruder == 3") { test(3); }
  216. }
  217. GIVEN("printer type in notes") {
  218. auto config = Slic3r::DynamicPrintConfig::new_with({
  219. { "start_gcode",
  220. ";substitution:{if notes==\"MK2\"}MK2{elsif notes==\"MK3\"}MK3{else}MK1{endif}:end"
  221. }
  222. });
  223. auto test = [&config](const std::string &printer_name) {
  224. config.set_deserialize_strict("notes", printer_name);
  225. std::string gcode = Slic3r::Test::slice({ Slic3r::Test::TestMesh::cube_20x20x20 }, config);
  226. THEN(std::string("printer name ") + printer_name + " matched") {
  227. REQUIRE(Slic3r::Test::contains(gcode, std::string("substitution:") + printer_name + ":end"));
  228. }
  229. };
  230. WHEN("printer MK2") { test("MK2"); }
  231. WHEN("printer MK3") { test("MK3"); }
  232. WHEN("printer MK1") { test("MK1"); }
  233. }
  234. GIVEN("sequential print with between_objects_gcode") {
  235. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  236. { "complete_objects", 1 },
  237. { "between_objects_gcode", "_MY_CUSTOM_GCODE_" }
  238. });
  239. std::string gcode = Slic3r::Test::slice(
  240. // 3x 20mm box
  241. { Slic3r::Test::TestMesh::cube_20x20x20, Slic3r::Test::TestMesh::cube_20x20x20, Slic3r::Test::TestMesh::cube_20x20x20 },
  242. config);
  243. THEN("between_objects_gcode is applied correctly") {
  244. const boost::regex expression("^_MY_CUSTOM_GCODE_");
  245. const std::ptrdiff_t match_count =
  246. std::distance(boost::sregex_iterator(gcode.begin(), gcode.end(), expression), boost::sregex_iterator());
  247. REQUIRE(match_count == 2);
  248. }
  249. }
  250. GIVEN("before_layer_gcode increments global variable") {
  251. auto config = Slic3r::DynamicPrintConfig::new_with({
  252. { "start_gcode", "{global counter=0}" },
  253. { "before_layer_gcode", ";Counter{counter=counter+1;counter}\n" }
  254. });
  255. std::string gcode = Slic3r::Test::slice({ Slic3r::Test::TestMesh::cube_20x20x20 }, config);
  256. THEN("The counter is emitted multiple times before layer change.") {
  257. REQUIRE(Slic3r::Test::contains(gcode, ";Counter1\n"));
  258. REQUIRE(Slic3r::Test::contains(gcode, ";Counter2\n"));
  259. REQUIRE(Slic3r::Test::contains(gcode, ";Counter3\n"));
  260. }
  261. }
  262. }