combineinfill.t 5.3 KB

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