combineinfill.t 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. BEGIN {
  5. use FindBin;
  6. use lib "$FindBin::Bin/../lib";
  7. use local::lib "$FindBin::Bin/../local-lib";
  8. }
  9. use List::Util qw(first);
  10. use Slic3r;
  11. use Slic3r::Surface ':types';
  12. use Slic3r::Test;
  13. plan tests => 8;
  14. {
  15. my $test = sub {
  16. my ($config) = @_;
  17. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  18. ok my $gcode = Slic3r::Test::gcode($print), "infill_every_layers does not crash";
  19. my $tool = undef;
  20. my %layers = (); # layer_z => 1
  21. my %layer_infill = (); # layer_z => has_infill
  22. Slic3r::GCode::Reader->new->parse($gcode, sub {
  23. my ($self, $cmd, $args, $info) = @_;
  24. if ($cmd =~ /^T(\d+)/) {
  25. $tool = $1;
  26. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0 && $tool != $config->support_material_extruder-1) {
  27. $layer_infill{$self->Z} //= 0;
  28. if ($tool == $config->infill_extruder-1) {
  29. $layer_infill{$self->Z} = 1;
  30. }
  31. }
  32. $layers{$args->{Z}} = 1 if $cmd eq 'G1' && $info->{dist_Z} > 0;
  33. });
  34. my $layers_with_perimeters = scalar(keys %layer_infill);
  35. my $layers_with_infill = grep $_ > 0, values %layer_infill;
  36. is scalar(keys %layers), $layers_with_perimeters+$config->raft_layers, 'expected number of layers';
  37. if ($config->raft_layers == 0) {
  38. # first infill layer printed directly on print bed is not combined, so we don't consider it.
  39. $layers_with_infill--;
  40. $layers_with_perimeters--;
  41. }
  42. # we expect that infill is generated for half the number of combined layers
  43. # plus for each single layer that was not combined (remainder)
  44. is $layers_with_infill,
  45. int($layers_with_perimeters/$config->infill_every_layers) + ($layers_with_perimeters % $config->infill_every_layers),
  46. 'infill is only present in correct number of layers';
  47. };
  48. my $config = Slic3r::Config::new_from_defaults;
  49. $config->set('layer_height', 0.2);
  50. $config->set('first_layer_height', 0.2);
  51. $config->set('nozzle_diameter', [0.5,0.5,0.5,0.5]);
  52. $config->set('infill_every_layers', 2);
  53. $config->set('perimeter_extruder', 1);
  54. $config->set('infill_extruder', 2);
  55. $config->set('wipe_into_infill', 0);
  56. $config->set('support_material_extruder', 3);
  57. $config->set('support_material_interface_extruder', 3);
  58. $config->set('top_solid_layers', 0);
  59. $config->set('bottom_solid_layers', 0);
  60. $test->($config);
  61. $config->set('skirts', 0); # prevent usage of perimeter_extruder in raft layers
  62. $config->set('raft_layers', 5);
  63. $test->($config);
  64. }
  65. {
  66. my $config = Slic3r::Config::new_from_defaults;
  67. $config->set('layer_height', 0.2);
  68. $config->set('first_layer_height', 0.2);
  69. $config->set('nozzle_diameter', [0.5]);
  70. $config->set('infill_every_layers', 2);
  71. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  72. $print->process;
  73. ok defined(first { @{$_->get_region(0)->fill_surfaces->filter_by_type(S_TYPE_INTERNALVOID)} > 0 }
  74. @{$print->print->get_object(0)->layers}),
  75. 'infill combination produces internal void surfaces';
  76. # we disable combination after infill has been generated
  77. $config->set('infill_every_layers', 1);
  78. $print->apply($print->print->model->clone, $config);
  79. $print->process;
  80. ok !(defined first { @{$_->get_region(0)->fill_surfaces} == 0 }
  81. @{$print->print->get_object(0)->layers}),
  82. 'infill combination is idempotent';
  83. }
  84. # the following needs to be adapted to the new API
  85. if (0) {
  86. my $config = Slic3r::Config::new_from_defaults;
  87. $config->set('skirts', 0);
  88. $config->set('solid_layers', 0);
  89. $config->set('bottom_solid_layers', 0);
  90. $config->set('top_solid_layers', 0);
  91. $config->set('infill_every_layers', 6);
  92. $config->set('layer_height', 0.06);
  93. $config->set('perimeters', 1);
  94. my $test = sub {
  95. my ($shift) = @_;
  96. my $self = Slic3r::Test::init_print('20mm_cube', config => $config);
  97. $shift /= &Slic3r::SCALING_FACTOR;
  98. my $scale = 4; # make room for fat infill lines with low layer height
  99. # Put a slope on the box's sides by shifting x and y coords by $tilt * (z / boxheight).
  100. # The test here is to put such a slight slope on the walls that it should
  101. # not trigger any extra fill on fill layers that should be empty when
  102. # combine infill is enabled.
  103. $_->[0] += $shift * ($_->[2] / (20 / &Slic3r::SCALING_FACTOR)) for @{$self->objects->[0]->meshes->[0]->vertices};
  104. $_->[1] += $shift * ($_->[2] / (20 / &Slic3r::SCALING_FACTOR)) for @{$self->objects->[0]->meshes->[0]->vertices};
  105. $_ = [$_->[0]*$scale, $_->[1]*$scale, $_->[2]] for @{$self->objects->[0]->meshes->[0]->vertices};
  106. # copy of Print::export_gcode() up to the point
  107. # after fill surfaces are combined
  108. $_->slice for @{$self->objects};
  109. $_->make_perimeters for @{$self->objects};
  110. $_->detect_surfaces_type for @{$self->objects};
  111. $_->prepare_fill_surfaces for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
  112. $_->process_external_surfaces for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
  113. $_->discover_horizontal_shells for @{$self->objects};
  114. $_->combine_infill for @{$self->objects};
  115. # Only layers with id % 6 == 0 should have fill.
  116. my $spurious_infill = 0;
  117. foreach my $layer (map @{$_->layers}, @{$self->objects}) {
  118. ++$spurious_infill if ($layer->id % 6 && grep @{$_->fill_surfaces} > 0, @{$layer->regions});
  119. }
  120. $spurious_infill -= scalar(@{$self->objects->[0]->layers} - 1) % 6;
  121. fail "spurious fill surfaces found on layers that should have none (walls " . sprintf("%.4f", Slic3r::Geometry::rad2deg(atan2($shift, 20/&Slic3r::SCALING_FACTOR))) . " degrees off vertical)"
  122. unless $spurious_infill == 0;
  123. 1;
  124. };
  125. # Test with mm skew offsets for the top of the 20mm-high box
  126. for my $shift (0, 0.0001, 1) {
  127. ok $test->($shift), "no spurious fill surfaces with box walls " . sprintf("%.4f",Slic3r::Geometry::rad2deg(atan2($shift, 20))) . " degrees off of vertical";
  128. }
  129. }
  130. __END__