test_custom_gcode.cpp 13 KB

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