test_support_material.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/GCodeReader.hpp"
  3. #include "libslic3r/Layer.hpp"
  4. #include "test_data.hpp" // get access to init_print, etc
  5. using namespace Slic3r::Test;
  6. using namespace Slic3r;
  7. TEST_CASE("SupportMaterial: Three raft layers created", "[SupportMaterial]")
  8. {
  9. Slic3r::Print print;
  10. Slic3r::Test::init_and_process_print({ TestMesh::cube_20x20x20 }, print, {
  11. { "support_material", 1 },
  12. { "raft_layers", 3 }
  13. });
  14. REQUIRE(print.objects().front()->support_layers().size() == 3);
  15. }
  16. SCENARIO("SupportMaterial: support_layers_z and contact_distance", "[SupportMaterial]")
  17. {
  18. // Box h = 20mm, hole bottom at 5mm, hole height 10mm (top edge at 15mm).
  19. TriangleMesh mesh = Slic3r::Test::mesh(Slic3r::Test::TestMesh::cube_with_hole);
  20. mesh.rotate_x(float(M_PI / 2));
  21. // mesh.write_binary("d:\\temp\\cube_with_hole.stl");
  22. auto check = [](Slic3r::Print &print, bool &first_support_layer_height_ok, bool &layer_height_minimum_ok, bool &layer_height_maximum_ok, bool &top_spacing_ok)
  23. {
  24. SpanOfConstPtrs<SupportLayer> support_layers = print.objects().front()->support_layers();
  25. first_support_layer_height_ok = support_layers.front()->print_z == print.config().first_layer_height.value;
  26. layer_height_minimum_ok = true;
  27. layer_height_maximum_ok = true;
  28. double min_layer_height = print.config().min_layer_height.values.front();
  29. double max_layer_height = print.config().nozzle_diameter.values.front();
  30. if (print.config().max_layer_height.values.front() > EPSILON)
  31. max_layer_height = std::min(max_layer_height, print.config().max_layer_height.values.front());
  32. for (size_t i = 1; i < support_layers.size(); ++ i) {
  33. if (support_layers[i]->print_z - support_layers[i - 1]->print_z < min_layer_height - EPSILON)
  34. layer_height_minimum_ok = false;
  35. if (support_layers[i]->print_z - support_layers[i - 1]->print_z > max_layer_height + EPSILON)
  36. layer_height_maximum_ok = false;
  37. }
  38. #if 0
  39. double expected_top_spacing = print.default_object_config().layer_height + print.config().nozzle_diameter.get_at(0);
  40. bool wrong_top_spacing = 0;
  41. std::vector<coordf_t> top_z { 1.1 };
  42. for (coordf_t top_z_el : top_z) {
  43. // find layer index of this top surface.
  44. size_t layer_id = -1;
  45. for (size_t i = 0; i < support_z.size(); ++ i) {
  46. if (abs(support_z[i] - top_z_el) < EPSILON) {
  47. layer_id = i;
  48. i = static_cast<int>(support_z.size());
  49. }
  50. }
  51. // check that first support layer above this top surface (or the next one) is spaced with nozzle diameter
  52. if (abs(support_z[layer_id + 1] - support_z[layer_id] - expected_top_spacing) > EPSILON &&
  53. abs(support_z[layer_id + 2] - support_z[layer_id] - expected_top_spacing) > EPSILON) {
  54. wrong_top_spacing = 1;
  55. }
  56. }
  57. d = ! wrong_top_spacing;
  58. #else
  59. top_spacing_ok = true;
  60. #endif
  61. };
  62. GIVEN("A print object having one modelObject") {
  63. WHEN("First layer height = 0.4") {
  64. Slic3r::Print print;
  65. Slic3r::Test::init_and_process_print({ mesh }, print, {
  66. { "support_material", 1 },
  67. { "layer_height", 0.2 },
  68. { "first_layer_height", 0.4 },
  69. { "dont_support_bridges", false },
  70. });
  71. bool a, b, c, d;
  72. check(print, a, b, c, d);
  73. THEN("First layer height is honored") { REQUIRE(a == true); }
  74. THEN("No null or negative support layers") { REQUIRE(b == true); }
  75. THEN("No layers thicker than nozzle diameter") { REQUIRE(c == true); }
  76. // THEN("Layers above top surfaces are spaced correctly") { REQUIRE(d == true); }
  77. }
  78. WHEN("Layer height = 0.2 and, first layer height = 0.3") {
  79. Slic3r::Print print;
  80. Slic3r::Test::init_and_process_print({ mesh }, print, {
  81. { "support_material", 1 },
  82. { "layer_height", 0.2 },
  83. { "first_layer_height", 0.3 },
  84. { "dont_support_bridges", false },
  85. });
  86. bool a, b, c, d;
  87. check(print, a, b, c, d);
  88. THEN("First layer height is honored") { REQUIRE(a == true); }
  89. THEN("No null or negative support layers") { REQUIRE(b == true); }
  90. THEN("No layers thicker than nozzle diameter") { REQUIRE(c == true); }
  91. // THEN("Layers above top surfaces are spaced correctly") { REQUIRE(d == true); }
  92. }
  93. WHEN("Layer height = nozzle_diameter[0]") {
  94. Slic3r::Print print;
  95. Slic3r::Test::init_and_process_print({ mesh }, print, {
  96. { "support_material", 1 },
  97. { "layer_height", 0.2 },
  98. { "first_layer_height", 0.3 },
  99. { "dont_support_bridges", false },
  100. });
  101. bool a, b, c, d;
  102. check(print, a, b, c, d);
  103. THEN("First layer height is honored") { REQUIRE(a == true); }
  104. THEN("No null or negative support layers") { REQUIRE(b == true); }
  105. THEN("No layers thicker than nozzle diameter") { REQUIRE(c == true); }
  106. // THEN("Layers above top surfaces are spaced correctly") { REQUIRE(d == true); }
  107. }
  108. }
  109. }
  110. #if 0
  111. // Test 8.
  112. TEST_CASE("SupportMaterial: forced support is generated", "[SupportMaterial]")
  113. {
  114. // Create a mesh & modelObject.
  115. TriangleMesh mesh = TriangleMesh::make_cube(20, 20, 20);
  116. Model model = Model();
  117. ModelObject *object = model.add_object();
  118. object->add_volume(mesh);
  119. model.add_default_instances();
  120. model.align_instances_to_origin();
  121. Print print = Print();
  122. std::vector<coordf_t> contact_z = {1.9};
  123. std::vector<coordf_t> top_z = {1.1};
  124. print.default_object_config.support_material_enforce_layers = 100;
  125. print.default_object_config.support_material = 0;
  126. print.default_object_config.layer_height = 0.2;
  127. print.default_object_config.set_deserialize("first_layer_height", "0.3");
  128. print.add_model_object(model.objects[0]);
  129. print.objects.front()->_slice();
  130. SupportMaterial *support = print.objects.front()->_support_material();
  131. auto support_z = support->support_layers_z(contact_z, top_z, print.default_object_config.layer_height);
  132. bool check = true;
  133. for (size_t i = 1; i < support_z.size(); i++) {
  134. if (support_z[i] - support_z[i - 1] <= 0)
  135. check = false;
  136. }
  137. REQUIRE(check == true);
  138. }
  139. // TODO
  140. bool test_6_checks(Print& print)
  141. {
  142. bool has_bridge_speed = true;
  143. // Pre-Processing.
  144. PrintObject* print_object = print.objects.front();
  145. print_object->infill();
  146. SupportMaterial* support_material = print.objects.front()->_support_material();
  147. support_material->generate(print_object);
  148. // TODO but not needed in test 6 (make brims and make skirts).
  149. // Exporting gcode.
  150. // TODO validation found in Simple.pm
  151. return has_bridge_speed;
  152. }
  153. // Test 6.
  154. SCENARIO("SupportMaterial: Checking bridge speed", "[SupportMaterial]")
  155. {
  156. GIVEN("Print object") {
  157. // Create a mesh & modelObject.
  158. TriangleMesh mesh = TriangleMesh::make_cube(20, 20, 20);
  159. Model model = Model();
  160. ModelObject *object = model.add_object();
  161. object->add_volume(mesh);
  162. model.add_default_instances();
  163. model.align_instances_to_origin();
  164. Print print = Print();
  165. print.config.brim_width = 0;
  166. print.config.skirts = 0;
  167. print.config.skirts = 0;
  168. print.default_object_config.support_material = 1;
  169. print.default_region_config.top_solid_layers = 0; // so that we don't have the internal bridge over infill.
  170. print.default_region_config.bridge_speed = 99;
  171. print.config.cooling = 0;
  172. print.config.set_deserialize("first_layer_speed", "100%");
  173. WHEN("support_material_contact_distance = 0.2") {
  174. print.default_object_config.support_material_contact_distance = 0.2;
  175. print.add_model_object(model.objects[0]);
  176. bool check = test_6_checks(print);
  177. REQUIRE(check == true); // bridge speed is used.
  178. }
  179. WHEN("support_material_contact_distance = 0") {
  180. print.default_object_config.support_material_contact_distance = 0;
  181. print.add_model_object(model.objects[0]);
  182. bool check = test_6_checks(print);
  183. REQUIRE(check == true); // bridge speed is not used.
  184. }
  185. WHEN("support_material_contact_distance = 0.2 & raft_layers = 5") {
  186. print.default_object_config.support_material_contact_distance = 0.2;
  187. print.default_object_config.raft_layers = 5;
  188. print.add_model_object(model.objects[0]);
  189. bool check = test_6_checks(print);
  190. REQUIRE(check == true); // bridge speed is used.
  191. }
  192. WHEN("support_material_contact_distance = 0 & raft_layers = 5") {
  193. print.default_object_config.support_material_contact_distance = 0;
  194. print.default_object_config.raft_layers = 5;
  195. print.add_model_object(model.objects[0]);
  196. bool check = test_6_checks(print);
  197. REQUIRE(check == true); // bridge speed is not used.
  198. }
  199. }
  200. }
  201. #endif
  202. /*
  203. Old Perl tests, which were disabled by Vojtech at the time of first Support Generator refactoring.
  204. #if 0
  205. {
  206. my $config = Slic3r::Config::new_from_defaults;
  207. $config->set('support_material', 1);
  208. my @contact_z = my @top_z = ();
  209. my $test = sub {
  210. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  211. my $object_config = $print->print->objects->[0]->config;
  212. my $flow = Slic3r::Flow->new_from_width(
  213. width => $object_config->support_material_extrusion_width || $object_config->extrusion_width,
  214. role => FLOW_ROLE_SUPPORT_MATERIAL,
  215. nozzle_diameter => $print->config->nozzle_diameter->[$object_config->support_material_extruder-1] // $print->config->nozzle_diameter->[0],
  216. layer_height => $object_config->layer_height,
  217. );
  218. my $support = Slic3r::Print::SupportMaterial->new(
  219. object_config => $print->print->objects->[0]->config,
  220. print_config => $print->print->config,
  221. flow => $flow,
  222. interface_flow => $flow,
  223. first_layer_flow => $flow,
  224. );
  225. my $support_z = $support->support_layers_z($print->print->objects->[0], \@contact_z, \@top_z, $config->layer_height);
  226. my $expected_top_spacing = $support->contact_distance($config->layer_height, $config->nozzle_diameter->[0]);
  227. is $support_z->[0], $config->first_layer_height,
  228. 'first layer height is honored';
  229. is scalar(grep { $support_z->[$_]-$support_z->[$_-1] <= 0 } 1..$#$support_z), 0,
  230. 'no null or negative support layers';
  231. is scalar(grep { $support_z->[$_]-$support_z->[$_-1] > $config->nozzle_diameter->[0] + epsilon } 1..$#$support_z), 0,
  232. 'no layers thicker than nozzle diameter';
  233. my $wrong_top_spacing = 0;
  234. foreach my $top_z (@top_z) {
  235. # find layer index of this top surface
  236. my $layer_id = first { abs($support_z->[$_] - $top_z) < epsilon } 0..$#$support_z;
  237. # check that first support layer above this top surface (or the next one) is spaced with nozzle diameter
  238. $wrong_top_spacing = 1
  239. if ($support_z->[$layer_id+1] - $support_z->[$layer_id]) != $expected_top_spacing
  240. && ($support_z->[$layer_id+2] - $support_z->[$layer_id]) != $expected_top_spacing;
  241. }
  242. ok !$wrong_top_spacing, 'layers above top surfaces are spaced correctly';
  243. };
  244. $config->set('layer_height', 0.2);
  245. $config->set('first_layer_height', 0.3);
  246. @contact_z = (1.9);
  247. @top_z = (1.1);
  248. $test->();
  249. $config->set('first_layer_height', 0.4);
  250. $test->();
  251. $config->set('layer_height', $config->nozzle_diameter->[0]);
  252. $test->();
  253. }
  254. {
  255. my $config = Slic3r::Config::new_from_defaults;
  256. $config->set('raft_layers', 3);
  257. $config->set('brim_width', 0);
  258. $config->set('skirts', 0);
  259. $config->set('support_material_extruder', 2);
  260. $config->set('support_material_interface_extruder', 2);
  261. $config->set('layer_height', 0.4);
  262. $config->set('first_layer_height', 0.4);
  263. my $print = Slic3r::Test::init_print('overhang', config => $config);
  264. ok my $gcode = Slic3r::Test::gcode($print), 'no conflict between raft/support and brim';
  265. my $tool = 0;
  266. Slic3r::GCode::Reader->new->parse($gcode, sub {
  267. my ($self, $cmd, $args, $info) = @_;
  268. if ($cmd =~ /^T(\d+)/) {
  269. $tool = $1;
  270. } elsif ($info->{extruding}) {
  271. if ($self->Z <= ($config->raft_layers * $config->layer_height)) {
  272. fail 'not extruding raft with support material extruder'
  273. if $tool != ($config->support_material_extruder-1);
  274. } else {
  275. fail 'support material exceeds raft layers'
  276. if $tool == $config->support_material_extruder-1;
  277. # TODO: we should test that full support is generated when we use raft too
  278. }
  279. }
  280. });
  281. }
  282. {
  283. my $config = Slic3r::Config::new_from_defaults;
  284. $config->set('skirts', 0);
  285. $config->set('raft_layers', 3);
  286. $config->set('support_material_pattern', 'honeycomb');
  287. $config->set('support_material_extrusion_width', 0.6);
  288. $config->set('first_layer_extrusion_width', '100%');
  289. $config->set('bridge_speed', 99);
  290. $config->set('cooling', [ 0 ]); # prevent speed alteration
  291. $config->set('first_layer_speed', '100%'); # prevent speed alteration
  292. $config->set('start_gcode', ''); # prevent any unexpected Z move
  293. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  294. my $layer_id = -1; # so that first Z move sets this to 0
  295. my @raft = my @first_object_layer = ();
  296. my %first_object_layer_speeds = (); # F => 1
  297. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  298. my ($self, $cmd, $args, $info) = @_;
  299. if ($info->{extruding} && $info->{dist_XY} > 0) {
  300. if ($layer_id <= $config->raft_layers) {
  301. # this is a raft layer or the first object layer
  302. my $line = Slic3r::Line->new_scale([ $self->X, $self->Y ], [ $info->{new_X}, $info->{new_Y} ]);
  303. my @path = @{$line->grow(scale($config->support_material_extrusion_width/2))};
  304. if ($layer_id < $config->raft_layers) {
  305. # this is a raft layer
  306. push @raft, @path;
  307. } else {
  308. push @first_object_layer, @path;
  309. $first_object_layer_speeds{ $args->{F} // $self->F } = 1;
  310. }
  311. }
  312. } elsif ($cmd eq 'G1' && $info->{dist_Z} > 0) {
  313. $layer_id++;
  314. }
  315. });
  316. ok !@{diff(\@first_object_layer, \@raft)},
  317. 'first object layer is completely supported by raft';
  318. is scalar(keys %first_object_layer_speeds), 1,
  319. 'only one speed used in first object layer';
  320. ok +(keys %first_object_layer_speeds)[0] == $config->bridge_speed*60,
  321. 'bridge speed used in first object layer';
  322. }
  323. {
  324. my $config = Slic3r::Config::new_from_defaults;
  325. $config->set('skirts', 0);
  326. $config->set('layer_height', 0.35);
  327. $config->set('first_layer_height', 0.3);
  328. $config->set('nozzle_diameter', [0.5]);
  329. $config->set('support_material_extruder', 2);
  330. $config->set('support_material_interface_extruder', 2);
  331. my $test = sub {
  332. my ($raft_layers) = @_;
  333. $config->set('raft_layers', $raft_layers);
  334. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  335. my %raft_z = (); # z => 1
  336. my $tool = undef;
  337. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  338. my ($self, $cmd, $args, $info) = @_;
  339. if ($cmd =~ /^T(\d+)/) {
  340. $tool = $1;
  341. } elsif ($info->{extruding} && $info->{dist_XY} > 0) {
  342. if ($tool == $config->support_material_extruder-1) {
  343. $raft_z{$self->Z} = 1;
  344. }
  345. }
  346. });
  347. is scalar(keys %raft_z), $config->raft_layers, 'correct number of raft layers is generated';
  348. };
  349. $test->(2);
  350. $test->(70);
  351. $config->set('layer_height', 0.4);
  352. $config->set('first_layer_height', 0.35);
  353. $test->(3);
  354. $test->(70);
  355. }
  356. {
  357. my $config = Slic3r::Config::new_from_defaults;
  358. $config->set('brim_width', 0);
  359. $config->set('skirts', 0);
  360. $config->set('support_material', 1);
  361. $config->set('top_solid_layers', 0); # so that we don't have the internal bridge over infill
  362. $config->set('bridge_speed', 99);
  363. $config->set('cooling', [ 0 ]);
  364. $config->set('first_layer_speed', '100%');
  365. my $test = sub {
  366. my $print = Slic3r::Test::init_print('overhang', config => $config);
  367. my $has_bridge_speed = 0;
  368. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  369. my ($self, $cmd, $args, $info) = @_;
  370. if ($info->{extruding}) {
  371. if (($args->{F} // $self->F) == $config->bridge_speed*60) {
  372. $has_bridge_speed = 1;
  373. }
  374. }
  375. });
  376. return $has_bridge_speed;
  377. };
  378. $config->set('support_material_contact_distance', 0.2);
  379. ok $test->(), 'bridge speed is used when support_material_contact_distance > 0';
  380. $config->set('support_material_contact_distance', 0);
  381. ok !$test->(), 'bridge speed is not used when support_material_contact_distance == 0';
  382. $config->set('raft_layers', 5);
  383. $config->set('support_material_contact_distance', 0.2);
  384. ok $test->(), 'bridge speed is used when raft_layers > 0 and support_material_contact_distance > 0';
  385. $config->set('support_material_contact_distance', 0);
  386. ok !$test->(), 'bridge speed is not used when raft_layers > 0 and support_material_contact_distance == 0';
  387. }
  388. {
  389. my $config = Slic3r::Config::new_from_defaults;
  390. $config->set('skirts', 0);
  391. $config->set('start_gcode', '');
  392. $config->set('raft_layers', 8);
  393. $config->set('nozzle_diameter', [0.4, 1]);
  394. $config->set('layer_height', 0.1);
  395. $config->set('first_layer_height', 0.8);
  396. $config->set('support_material_extruder', 2);
  397. $config->set('support_material_interface_extruder', 2);
  398. $config->set('support_material_contact_distance', 0);
  399. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  400. ok my $gcode = Slic3r::Test::gcode($print), 'first_layer_height is validated with support material extruder nozzle diameter when using raft layers';
  401. my $tool = undef;
  402. my @z = (0);
  403. my %layer_heights_by_tool = (); # tool => [ lh, lh... ]
  404. Slic3r::GCode::Reader->new->parse($gcode, sub {
  405. my ($self, $cmd, $args, $info) = @_;
  406. if ($cmd =~ /^T(\d+)/) {
  407. $tool = $1;
  408. } elsif ($cmd eq 'G1' && exists $args->{Z} && $args->{Z} != $self->Z) {
  409. push @z, $args->{Z};
  410. } elsif ($info->{extruding} && $info->{dist_XY} > 0) {
  411. $layer_heights_by_tool{$tool} ||= [];
  412. push @{ $layer_heights_by_tool{$tool} }, $z[-1] - $z[-2];
  413. }
  414. });
  415. ok !defined(first { $_ > $config->nozzle_diameter->[0] + epsilon }
  416. @{ $layer_heights_by_tool{$config->perimeter_extruder-1} }),
  417. 'no object layer is thicker than nozzle diameter';
  418. ok !defined(first { abs($_ - $config->layer_height) < epsilon }
  419. @{ $layer_heights_by_tool{$config->support_material_extruder-1} }),
  420. 'no support material layer is as thin as object layers';
  421. }
  422. */