test_layers.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Ported from t/layers.t
  3. */
  4. #include <catch2/catch.hpp>
  5. #include "test_data.hpp"
  6. using namespace Slic3r;
  7. using namespace Slic3r::Test;
  8. void check_layers(const DynamicPrintConfig& config) {
  9. GCodeReader parser;
  10. std::string gcode = Slic3r::Test::slice({TestMesh::cube_20x20x20}, config);
  11. std::vector<double> z;
  12. std::vector<double> increments;
  13. parser.parse_buffer(gcode, [&] (Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line) {
  14. if (line.has_z()) {
  15. z.emplace_back(line.z());
  16. increments.emplace_back(line.dist_Z(self));
  17. }
  18. });
  19. const double first_layer_height = config.opt_float("first_layer_height");
  20. const double z_offset = config.opt_float("z_offset");
  21. const double layer_height = config.opt_float("layer_height");
  22. INFO("Correct first layer height.");
  23. CHECK(z.at(0) == Approx(first_layer_height + z_offset));
  24. INFO("Correct second layer height")
  25. CHECK(z.at(1) == Approx(first_layer_height + layer_height + z_offset));
  26. INFO("Correct layer height")
  27. for (const double increment : tcb::span{increments}.subspan(1)) {
  28. CHECK(increment == Approx(layer_height));
  29. }
  30. }
  31. TEST_CASE("Layer heights are correct", "[Layers]") {
  32. DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  33. config.set_deserialize_strict({
  34. { "start_gcode", "" },
  35. { "layer_height", 0.3 },
  36. { "first_layer_height", 0.2 },
  37. { "retract_length", "0" }
  38. });
  39. SECTION("Absolute first layer height") {
  40. check_layers(config);
  41. }
  42. SECTION("Relative layer height") {
  43. const double layer_height = config.opt_float("layer_height");
  44. config.set_deserialize_strict({
  45. { "first_layer_height", 0.6 * layer_height },
  46. });
  47. check_layers(config);
  48. }
  49. SECTION("Positive z offset") {
  50. config.set_deserialize_strict({
  51. { "z_offset", 0.9 },
  52. });
  53. check_layers(config);
  54. }
  55. SECTION("Negative z offset") {
  56. config.set_deserialize_strict({
  57. { "z_offset", -0.8 },
  58. });
  59. check_layers(config);
  60. }
  61. }
  62. TEST_CASE("GCode has reasonable height", "[Layers]") {
  63. DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  64. config.set_deserialize_strict({
  65. { "fill_density", 0 },
  66. { "gcode_binary", 0 },
  67. });
  68. Print print;
  69. Model model;
  70. TriangleMesh test_mesh{mesh(TestMesh::cube_20x20x20)};
  71. test_mesh.scale(2);
  72. Test::init_print({test_mesh}, print, model, config);
  73. const std::string gcode{Test::gcode(print)};
  74. std::vector<double> z;
  75. GCodeReader parser;
  76. parser.parse_buffer(gcode, [&] (Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line) {
  77. if (line.dist_Z(self) != Approx(0)) {
  78. z.emplace_back(line.z());
  79. }
  80. });
  81. REQUIRE(!z.empty());
  82. INFO("Last Z is: " + std::to_string(z.back()));
  83. CHECK((z.back() > 20*1.8 && z.back() < 20*2.2));
  84. }