Layer.pm 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package Slic3r::Layer;
  2. use Moo;
  3. use List::Util qw(first);
  4. use Slic3r::Geometry qw(scale);
  5. use Slic3r::Geometry::Clipper qw(union_ex);
  6. has 'id' => (is => 'rw', required => 1, trigger => 1); # sequential number of layer, 0-based
  7. has 'object' => (is => 'ro', weak_ref => 1, required => 1);
  8. has 'regions' => (is => 'ro', default => sub { [] });
  9. has 'slicing_errors' => (is => 'rw');
  10. has 'slice_z' => (is => 'ro', required => 1); # Z used for slicing in scaled coordinates
  11. has 'print_z' => (is => 'ro', required => 1); # Z used for printing in scaled coordinates
  12. has 'height' => (is => 'ro', required => 1); # layer height in unscaled coordinates
  13. # collection of expolygons generated by slicing the original geometry;
  14. # also known as 'islands' (all regions are merged here)
  15. has 'slices' => (is => 'rw');
  16. # ordered collection of extrusion paths to fill surfaces for support material
  17. has 'support_islands' => (is => 'rw');
  18. has 'support_fills' => (is => 'rw');
  19. has 'support_contact_fills' => (is => 'rw');
  20. sub _trigger_id {
  21. my $self = shift;
  22. $_->_trigger_layer for @{$self->regions || []};
  23. }
  24. # layer height of contact paths in unscaled coordinates
  25. sub support_material_contact_height {
  26. my $self = shift;
  27. return $self->height if $self->id == 0;
  28. # TODO: check what upper region applies instead of considering the first one
  29. my $upper_layer = $self->object->layers->[ $self->id + 1 ] // $self;
  30. my $h = ($self->height + $upper_layer->height) - $upper_layer->regions->[0]->extruders->{infill}->bridge_flow->width;
  31. # If layer height is less than half the bridge width then we'll get a negative height for contact area.
  32. # The optimal solution would be to skip some layers during support material generation, but for now
  33. # we'll apply a (dirty) workaround that should still work.
  34. if ($h <= 0) {
  35. $h = $self->height;
  36. }
  37. return $h;
  38. }
  39. # Z used for printing support material contact in scaled coordinates
  40. sub support_material_contact_z {
  41. my $self = shift;
  42. return $self->print_z - ($self->height - $self->support_material_contact_height) / &Slic3r::SCALING_FACTOR;
  43. }
  44. sub region {
  45. my $self = shift;
  46. my ($region_id) = @_;
  47. for (my $i = @{$self->regions}; $i <= $region_id; $i++) {
  48. $self->regions->[$i] //= Slic3r::Layer::Region->new(
  49. layer => $self,
  50. region => $self->object->print->regions->[$i],
  51. );
  52. }
  53. return $self->regions->[$region_id];
  54. }
  55. # merge all regions' slices to get islands
  56. sub make_slices {
  57. my $self = shift;
  58. # optimization for single-region layers
  59. my @regions_with_slices = grep { @{$_->slices} } @{$self->regions};
  60. if (@regions_with_slices == 1) {
  61. $self->slices([ map $_->expolygon, @{$regions_with_slices[0]->slices} ]);
  62. return;
  63. }
  64. $self->slices(union_ex([ map $_->p, map @{$_->slices}, @{$self->regions} ]));
  65. }
  66. sub make_perimeters {
  67. my $self = shift;
  68. Slic3r::debugf "Making perimeters for layer %d\n", $self->id;
  69. $_->make_perimeters for @{$self->regions};
  70. }
  71. sub support_islands_enclose_line {
  72. my $self = shift;
  73. my ($line) = @_;
  74. return 0 if !$self->support_islands; # why can we arrive here if there are no support islands?
  75. return (first { $_->encloses_line($line) } @{$self->support_islands}) ? 1 : 0;
  76. }
  77. 1;