test_print.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/libslic3r.h"
  3. #include "libslic3r/Print.hpp"
  4. #include "libslic3r/Layer.hpp"
  5. #include "test_data.hpp"
  6. using namespace Slic3r;
  7. using namespace Slic3r::Test;
  8. SCENARIO("PrintObject: Perimeter generation", "[PrintObject]") {
  9. GIVEN("20mm cube and default config") {
  10. WHEN("make_perimeters() is called") {
  11. Slic3r::Print print;
  12. Slic3r::Test::init_and_process_print({TestMesh::cube_20x20x20}, print, { { "fill_density", 0 } });
  13. const PrintObject &object = *print.objects().front();
  14. THEN("67 layers exist in the model") {
  15. REQUIRE(object.layers().size() == 66);
  16. }
  17. THEN("Every layer in region 0 has 1 island of perimeters") {
  18. for (const Layer *layer : object.layers())
  19. REQUIRE(layer->regions().front()->perimeters().size() == 1);
  20. }
  21. THEN("Every layer in region 0 has 3 paths in its perimeters list.") {
  22. for (const Layer *layer : object.layers())
  23. REQUIRE(layer->regions().front()->perimeters().items_count() == 3);
  24. }
  25. }
  26. }
  27. }
  28. SCENARIO("Print: Skirt generation", "[Print]") {
  29. GIVEN("20mm cube and default config") {
  30. WHEN("Skirts is set to 2 loops") {
  31. Slic3r::Print print;
  32. Slic3r::Test::init_and_process_print({TestMesh::cube_20x20x20}, print, {
  33. { "skirt_height", 1 },
  34. { "skirt_distance", 1 },
  35. { "skirts", 2 }
  36. });
  37. THEN("Skirt Extrusion collection has 2 loops in it") {
  38. REQUIRE(print.skirt().items_count() == 2);
  39. REQUIRE(print.skirt().flatten().entities.size() == 2);
  40. }
  41. }
  42. }
  43. }
  44. SCENARIO("Print: Changing number of solid surfaces does not cause all surfaces to become internal.", "[Print]") {
  45. GIVEN("sliced 20mm cube and config with top_solid_surfaces = 2 and bottom_solid_surfaces = 1") {
  46. Slic3r::DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  47. config.set_deserialize_strict({
  48. { "top_solid_layers", 2 },
  49. { "bottom_solid_layers", 1 },
  50. { "layer_height", 0.25 }, // get a known number of layers
  51. { "first_layer_height", 0.25 }
  52. });
  53. Slic3r::Print print;
  54. Slic3r::Model model;
  55. Slic3r::Test::init_print({TestMesh::cube_20x20x20}, print, model, config);
  56. // Precondition: Ensure that the model has 2 solid top layers (39, 38)
  57. // and one solid bottom layer (0).
  58. auto test_is_solid_infill = [&print](size_t obj_id, size_t layer_id) {
  59. const Layer &layer = *print.objects()[obj_id]->get_layer((int)layer_id);
  60. // iterate over all of the regions in the layer
  61. for (const LayerRegion *region : layer.regions()) {
  62. // for each region, iterate over the fill surfaces
  63. for (const Surface &surface : region->fill_surfaces())
  64. CHECK(surface.is_solid());
  65. }
  66. };
  67. print.process();
  68. test_is_solid_infill(0, 0); // should be solid
  69. test_is_solid_infill(0, 79); // should be solid
  70. test_is_solid_infill(0, 78); // should be solid
  71. WHEN("Model is re-sliced with top_solid_layers == 3") {
  72. config.set("top_solid_layers", 3);
  73. print.apply(model, config);
  74. print.process();
  75. THEN("Print object does not have 0 solid bottom layers.") {
  76. test_is_solid_infill(0, 0);
  77. }
  78. AND_THEN("Print object has 3 top solid layers") {
  79. test_is_solid_infill(0, 79);
  80. test_is_solid_infill(0, 78);
  81. test_is_solid_infill(0, 77);
  82. }
  83. }
  84. }
  85. }
  86. SCENARIO("Print: Brim generation", "[Print]") {
  87. GIVEN("20mm cube and default config, 1mm first layer width") {
  88. WHEN("Brim is set to 3mm") {
  89. Slic3r::Print print;
  90. Slic3r::Test::init_and_process_print({TestMesh::cube_20x20x20}, print, {
  91. { "first_layer_extrusion_width", 1 },
  92. { "brim_width", 3 }
  93. });
  94. THEN("Brim Extrusion collection has 3 loops in it") {
  95. REQUIRE(print.brim().items_count() == 3);
  96. }
  97. }
  98. WHEN("Brim is set to 6mm") {
  99. Slic3r::Print print;
  100. Slic3r::Test::init_and_process_print({TestMesh::cube_20x20x20}, print, {
  101. { "first_layer_extrusion_width", 1 },
  102. { "brim_width", 6 }
  103. });
  104. THEN("Brim Extrusion collection has 6 loops in it") {
  105. REQUIRE(print.brim().items_count() == 6);
  106. }
  107. }
  108. WHEN("Brim is set to 6mm, extrusion width 0.5mm") {
  109. Slic3r::Print print;
  110. Slic3r::Test::init_and_process_print({TestMesh::cube_20x20x20}, print, {
  111. { "first_layer_extrusion_width", 1 },
  112. { "brim_width", 6 },
  113. { "first_layer_extrusion_width", 0.5 }
  114. });
  115. print.process();
  116. THEN("Brim Extrusion collection has 12 loops in it") {
  117. REQUIRE(print.brim().items_count() == 14);
  118. }
  119. }
  120. }
  121. }
  122. SCENARIO("Ported from Perl", "[Print]") {
  123. GIVEN("20mm cube") {
  124. WHEN("Print center is set to 100x100 (test framework default)") {
  125. auto config = Slic3r::DynamicPrintConfig::full_print_config();
  126. std::string gcode = Slic3r::Test::slice({ TestMesh::cube_20x20x20 }, config);
  127. GCodeReader parser;
  128. Points extrusion_points;
  129. parser.parse_buffer(gcode, [&extrusion_points](Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line)
  130. {
  131. if (line.cmd_is("G1") && line.extruding(self) && line.dist_XY(self) > 0)
  132. extrusion_points.emplace_back(line.new_XY_scaled(self));
  133. });
  134. Vec2d center = unscaled<double>(BoundingBox(extrusion_points).center());
  135. THEN("print is centered around print_center") {
  136. REQUIRE(is_approx(center.x(), 100.));
  137. REQUIRE(is_approx(center.y(), 100.));
  138. }
  139. }
  140. }
  141. GIVEN("Model with multiple objects") {
  142. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  143. { "nozzle_diameter", { 0.4, 0.4, 0.4, 0.4 } }
  144. });
  145. Print print;
  146. Model model;
  147. Slic3r::Test::init_print({ TestMesh::cube_20x20x20 }, print, model, config);
  148. // User sets a per-region option, also testing a deep copy of Model.
  149. Model model2(model);
  150. model2.objects.front()->config.set_deserialize_strict("fill_density", "100%");
  151. WHEN("fill_density overridden") {
  152. print.apply(model2, config);
  153. THEN("region config inherits model object config") {
  154. REQUIRE(print.get_print_region(0).config().fill_density == 100);
  155. }
  156. }
  157. model2.objects.front()->config.erase("fill_density");
  158. WHEN("fill_density resetted") {
  159. print.apply(model2, config);
  160. THEN("region config is resetted") {
  161. REQUIRE(print.get_print_region(0).config().fill_density == 20);
  162. }
  163. }
  164. WHEN("extruder is assigned") {
  165. model2.objects.front()->config.set("extruder", 3);
  166. model2.objects.front()->config.set("perimeter_extruder", 2);
  167. print.apply(model2, config);
  168. THEN("extruder setting is correctly expanded") {
  169. REQUIRE(print.get_print_region(0).config().infill_extruder == 3);
  170. }
  171. THEN("extruder setting does not override explicitely specified extruders") {
  172. REQUIRE(print.get_print_region(0).config().perimeter_extruder == 2);
  173. }
  174. }
  175. }
  176. }