combineinfill.t 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. # first infill layer is never combined, so we don't consider it
  38. $layers_with_infill--;
  39. $layers_with_perimeters--;
  40. # we expect that infill is generated for half the number of combined layers
  41. # plus for each single layer that was not combined (remainder)
  42. is $layers_with_infill,
  43. int($layers_with_perimeters/$config->infill_every_layers) + ($layers_with_perimeters % $config->infill_every_layers),
  44. 'infill is only present in correct number of layers';
  45. };
  46. my $config = Slic3r::Config->new_from_defaults;
  47. $config->set('layer_height', 0.2);
  48. $config->set('first_layer_height', 0.2);
  49. $config->set('nozzle_diameter', [0.5]);
  50. $config->set('infill_every_layers', 2);
  51. $config->set('perimeter_extruder', 1);
  52. $config->set('infill_extruder', 2);
  53. $config->set('support_material_extruder', 3);
  54. $config->set('support_material_interface_extruder', 3);
  55. $config->set('top_solid_layers', 0);
  56. $config->set('bottom_solid_layers', 0);
  57. $test->($config);
  58. $config->set('skirts', 0); # prevent usage of perimeter_extruder in raft layers
  59. $config->set('raft_layers', 5);
  60. $test->($config);
  61. }
  62. {
  63. my $config = Slic3r::Config->new_from_defaults;
  64. $config->set('layer_height', 0.2);
  65. $config->set('first_layer_height', 0.2);
  66. $config->set('nozzle_diameter', [0.5]);
  67. $config->set('infill_every_layers', 2);
  68. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  69. $print->process;
  70. ok defined(first { @{$_->get_region(0)->fill_surfaces->filter_by_type(S_TYPE_INTERNAL + S_TYPE_VOID)} > 0 }
  71. @{$print->print->get_object(0)->layers}),
  72. 'infill combination produces internal void surfaces';
  73. # we disable combination after infill has been generated
  74. $config->set('infill_every_layers', 1);
  75. $print->apply_config($config);
  76. $print->process;
  77. ok !(defined first { @{$_->get_region(0)->fill_surfaces} == 0 }
  78. @{$print->print->get_object(0)->layers}),
  79. 'infill combination is idempotent';
  80. }
  81. # the following needs to be adapted to the new API
  82. if (0) {
  83. my $config = Slic3r::Config->new_from_defaults;
  84. $config->set('skirts', 0);
  85. $config->set('solid_layers', 0);
  86. $config->set('bottom_solid_layers', 0);
  87. $config->set('top_solid_layers', 0);
  88. $config->set('infill_every_layers', 6);
  89. $config->set('layer_height', 0.06);
  90. $config->set('perimeters', 1);
  91. my $test = sub {
  92. my ($shift) = @_;
  93. my $self = Slic3r::Test::init_print('20mm_cube', config => $config);
  94. $shift /= &Slic3r::SCALING_FACTOR;
  95. my $scale = 4; # make room for fat infill lines with low layer height
  96. # Put a slope on the box's sides by shifting x and y coords by $tilt * (z / boxheight).
  97. # The test here is to put such a slight slope on the walls that it should
  98. # not trigger any extra fill on fill layers that should be empty when
  99. # combine infill is enabled.
  100. $_->[0] += $shift * ($_->[2] / (20 / &Slic3r::SCALING_FACTOR)) for @{$self->objects->[0]->meshes->[0]->vertices};
  101. $_->[1] += $shift * ($_->[2] / (20 / &Slic3r::SCALING_FACTOR)) for @{$self->objects->[0]->meshes->[0]->vertices};
  102. $_ = [$_->[0]*$scale, $_->[1]*$scale, $_->[2]] for @{$self->objects->[0]->meshes->[0]->vertices};
  103. # copy of Print::export_gcode() up to the point
  104. # after fill surfaces are combined
  105. $_->slice for @{$self->objects};
  106. $_->make_perimeters for @{$self->objects};
  107. $_->detect_surfaces_type for @{$self->objects};
  108. $_->prepare_fill_surfaces for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
  109. $_->process_external_surfaces for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
  110. $_->discover_horizontal_shells for @{$self->objects};
  111. $_->combine_infill for @{$self->objects};
  112. # Only layers with id % 6 == 0 should have fill.
  113. my $spurious_infill = 0;
  114. foreach my $layer (map @{$_->layers}, @{$self->objects}) {
  115. ++$spurious_infill if ($layer->id % 6 && grep @{$_->fill_surfaces} > 0, @{$layer->regions});
  116. }
  117. $spurious_infill -= scalar(@{$self->objects->[0]->layers} - 1) % 6;
  118. 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)"
  119. unless $spurious_infill == 0;
  120. 1;
  121. };
  122. # Test with mm skew offsets for the top of the 20mm-high box
  123. for my $shift (0, 0.0001, 1) {
  124. ok $test->($shift), "no spurious fill surfaces with box walls " . sprintf("%.4f",Slic3r::Geometry::rad2deg(atan2($shift, 20))) . " degrees off of vertical";
  125. }
  126. }
  127. __END__