test_layers.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. });
  38. SECTION("Absolute first layer height") {
  39. check_layers(config);
  40. }
  41. SECTION("Relative layer height") {
  42. const double layer_height = config.opt_float("layer_height");
  43. config.set_deserialize_strict({
  44. { "first_layer_height", 0.6 * layer_height },
  45. });
  46. check_layers(config);
  47. }
  48. SECTION("Positive z offset") {
  49. config.set_deserialize_strict({
  50. { "z_offset", 0.9 },
  51. });
  52. check_layers(config);
  53. }
  54. SECTION("Negative z offset") {
  55. config.set_deserialize_strict({
  56. { "z_offset", -0.8 },
  57. });
  58. check_layers(config);
  59. }
  60. }
  61. TEST_CASE("GCode has reasonable height", "[Layers]") {
  62. DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  63. config.set_deserialize_strict({
  64. { "fill_density", 0 },
  65. { "gcode_binary", 0 },
  66. });
  67. Print print;
  68. Model model;
  69. TriangleMesh test_mesh{mesh(TestMesh::cube_20x20x20)};
  70. test_mesh.scale(2);
  71. Test::init_print({test_mesh}, print, model, config);
  72. const std::string gcode{Test::gcode(print)};
  73. std::vector<double> z;
  74. GCodeReader parser;
  75. parser.parse_buffer(gcode, [&] (Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line) {
  76. if (line.dist_Z(self) != Approx(0)) {
  77. z.emplace_back(line.z());
  78. }
  79. });
  80. REQUIRE(!z.empty());
  81. INFO("Last Z is: " + std::to_string(z.back()));
  82. CHECK((z.back() > 20*1.8 && z.back() < 20*2.2));
  83. }