1
package Slic3r::Print::Object;
4
use List::Util qw(min max sum first);
5
use Slic3r::Flow ':roles';
6
use Slic3r::Geometry qw(X Y Z PI scale unscale deg2rad rad2deg scaled_epsilon chained_path);
7
use Slic3r::Geometry::Clipper qw(diff diff_ex intersection intersection_ex union union_ex
8
offset offset_ex offset2 offset2_ex CLIPPER_OFFSET_SCALE JT_MITER);
9
use Slic3r::Print::State ':steps';
10
use Slic3r::Surface ':types';
12
has 'print' => (is => 'ro', weak_ref => 1, required => 1);
13
has 'model_object' => (is => 'ro', required => 1); # caller is responsible for holding the Model object
14
has 'region_volumes' => (is => 'rw', default => sub { [] }); # by region_id
15
has 'copies' => (is => 'ro'); # Slic3r::Point objects in scaled G-code coordinates
16
has 'config' => (is => 'ro', default => sub { Slic3r::Config::PrintObject->new });
17
has 'layer_height_ranges' => (is => 'rw', default => sub { [] }); # [ z_min, z_max, layer_height ]
19
has 'size' => (is => 'rw'); # XYZ in scaled coordinates
20
has '_copies_shift' => (is => 'rw'); # scaled coordinates to add to copies (to compensate for the alignment operated when creating the object but still preserving a coherent API for external callers)
21
has '_shifted_copies' => (is => 'rw'); # Slic3r::Point objects in scaled G-code coordinates in our coordinates
22
has 'layers' => (is => 'rw', default => sub { [] });
23
has 'support_layers' => (is => 'rw', default => sub { [] });
24
has 'fill_maker' => (is => 'lazy');
25
has '_state' => (is => 'ro', default => sub { Slic3r::Print::State->new });
30
# Compute the translation to be applied to our meshes so that we work with smaller coordinates
32
my $bb = $self->model_object->bounding_box;
34
# Translate meshes so that our toolpath generation algorithms work with smaller
35
# XY coordinates; this translation is an optimization and not strictly required.
36
# A cloned mesh will be aligned to 0 before slicing in _slice_region() since we
37
#Ā don't assume it's already aligned and we don't alter the original position in model.
38
# We store the XY translation so that we can place copies correctly in the output G-code
39
# (copies are expressed in G-code coordinates and this translation is not publicly exposed).
40
$self->_copies_shift(Slic3r::Point->new_scale($bb->x_min, $bb->y_min));
41
$self->_trigger_copies;
43
# Scale the object size and store it
44
my $scaled_bb = $bb->clone;
45
$scaled_bb->scale(1 / &Slic3r::SCALING_FACTOR);
46
$self->size($scaled_bb->size);
50
sub _build_fill_maker {
52
return Slic3r::Fill->new(bounding_box => $self->bounding_box);
58
return if !defined $self->_copies_shift;
60
# order copies with a nearest neighbor search and translate them by _copies_shift
61
$self->_shifted_copies([
64
$c->translate(@{ $self->_copies_shift });
66
} @{$self->copies}[@{chained_path($self->copies)}]
69
$self->print->_state->invalidate(STEP_SKIRT);
70
$self->print->_state->invalidate(STEP_BRIM);
73
# in unscaled coordinates
75
my ($self, $x, $y) = @_;
76
push @{$self->copies}, Slic3r::Point->new_scale($x, $y);
77
$self->_trigger_copies;
80
sub delete_last_copy {
83
$self->_trigger_copies;
86
sub delete_all_copies {
88
@{$self->copies} = ();
89
$self->_trigger_copies;
92
# this is the *total* layer count
93
# this value is not supposed to be compared with $layer->id
94
# since they have different semantics
97
return scalar @{ $self->layers } + scalar @{ $self->support_layers };
103
# since the object is aligned to origin, bounding box coincides with size
104
return Slic3r::Geometry::BoundingBox->new_from_points([
105
Slic3r::Point->new(0,0),
106
map Slic3r::Point->new($_->x, $_->y), $self->size #))
110
# this should be idempotent
117
@{$self->layers} = ();
119
# make layers taking custom heights into account
120
my $print_z = my $slice_z = my $height = my $id = 0;
121
my $first_object_layer_height = -1;
124
if ($self->config->raft_layers > 0) {
125
$id += $self->config->raft_layers;
127
# raise first object layer Z by the thickness of the raft itself
128
# plus the extra distance required by the support material logic
129
$print_z += $self->config->get_value('first_layer_height');
130
$print_z += $self->config->layer_height * ($self->config->raft_layers - 1);
132
# at this stage we don't know which nozzles are actually used for the first layer
133
# so we compute the average of all of them
134
my $nozzle_diameter = sum(@{$self->print->config->nozzle_diameter})/@{$self->print->config->nozzle_diameter};
135
my $distance = Slic3r::Print::SupportMaterial::contact_distance($nozzle_diameter);
137
# force first layer print_z according to the contact distance
138
# (the loop below will raise print_z by such height)
139
$first_object_layer_height = $distance;
142
# loop until we have at least one layer and the max slice_z reaches the object height
143
my $max_z = unscale($self->size->z);
144
while (($slice_z - $height) <= $max_z) {
145
# assign the default height to the layer according to the general settings
147
? $self->config->get_value('first_layer_height')
148
: $self->config->layer_height;
150
# look for an applicable custom range
151
if (my $range = first { $_->[0] <= $slice_z && $_->[1] > $slice_z } @{$self->layer_height_ranges}) {
152
$height = $range->[2];
154
# if user set custom height to zero we should just skip the range and resume slicing over it
156
$slice_z += $range->[1] - $range->[0];
161
if ($first_object_layer_height != -1 && !@{$self->layers}) {
162
$height = $first_object_layer_height;
166
$slice_z += $height/2;
168
### Slic3r::debugf "Layer %d: height = %s; slice_z = %s; print_z = %s\n", $id, $height, $slice_z, $print_z;
170
push @{$self->layers}, Slic3r::Layer->new(
177
if (@{$self->layers} >= 2) {
178
$self->layers->[-2]->upper_layer($self->layers->[-1]);
179
$self->layers->[-1]->lower_layer($self->layers->[-2]);
183
$slice_z += $height/2; # add the other half layer
187
# make sure all layers contain layer region objects for all regions
188
my $regions_count = $self->print->regions_count;
189
foreach my $layer (@{ $self->layers }) {
190
$layer->region($_) for 0 .. ($regions_count-1);
193
# get array of Z coordinates for slicing
194
my @z = map $_->slice_z, @{$self->layers};
196
# slice all non-modifier volumes
197
for my $region_id (0..$#{$self->region_volumes}) {
198
my $expolygons_by_layer = $self->_slice_region($region_id, \@z, 0);
199
for my $layer_id (0..$#$expolygons_by_layer) {
200
my $layerm = $self->layers->[$layer_id]->regions->[$region_id];
201
$layerm->slices->clear;
202
foreach my $expolygon (@{ $expolygons_by_layer->[$layer_id] }) {
203
$layerm->slices->append(Slic3r::Surface->new(
204
expolygon => $expolygon,
205
surface_type => S_TYPE_INTERNAL,
211
# then slice all modifier volumes
212
if (@{$self->region_volumes} > 1) {
213
for my $region_id (0..$#{$self->region_volumes}) {
214
my $expolygons_by_layer = $self->_slice_region($region_id, \@z, 1);
216
# loop through the other regions and 'steal' the slices belonging to this one
217
for my $other_region_id (0..$#{$self->region_volumes}) {
218
next if $other_region_id == $region_id;
220
for my $layer_id (0..$#$expolygons_by_layer) {
221
my $layerm = $self->layers->[$layer_id]->regions->[$region_id];
222
my $other_layerm = $self->layers->[$layer_id]->regions->[$other_region_id];
224
my $other_slices = [ map $_->p, @{$other_layerm->slices} ]; # Polygons
225
my $my_parts = intersection_ex(
227
[ map @$_, @{ $expolygons_by_layer->[$layer_id] } ],
231
# append new parts to our region
232
foreach my $expolygon (@$my_parts) {
233
$layerm->slices->append(Slic3r::Surface->new(
234
expolygon => $expolygon,
235
surface_type => S_TYPE_INTERNAL,
239
# remove such parts from original region
240
$other_layerm->slices->clear;
241
$other_layerm->slices->append(Slic3r::Surface->new(
243
surface_type => S_TYPE_INTERNAL,
244
)) for @{ diff_ex($other_slices, [ map @$_, @$my_parts ]) };
250
# remove last layer(s) if empty
251
pop @{$self->layers} while @{$self->layers} && (!map @{$_->slices}, @{$self->layers->[-1]->regions});
253
foreach my $layer (@{ $self->layers }) {
254
# merge all regions' slices to get islands
258
# detect slicing errors
259
my $warning_thrown = 0;
260
for my $i (0 .. $#{$self->layers}) {
261
my $layer = $self->layers->[$i];
262
next unless $layer->slicing_errors;
263
if (!$warning_thrown) {
264
warn "The model has overlapping or self-intersecting facets. I tried to repair it, "
265
. "however you might want to check the results or repair the input file and retry.\n";
269
# try to repair the layer surfaces by merging all contours and all holes from
271
Slic3r::debugf "Attempting to repair layer %d\n", $i;
273
foreach my $region_id (0 .. $#{$layer->regions}) {
274
my $layerm = $layer->region($region_id);
276
my (@upper_surfaces, @lower_surfaces);
277
for (my $j = $i+1; $j <= $#{$self->layers}; $j++) {
278
if (!$self->layers->[$j]->slicing_errors) {
279
@upper_surfaces = @{$self->layers->[$j]->region($region_id)->slices};
283
for (my $j = $i-1; $j >= 0; $j--) {
284
if (!$self->layers->[$j]->slicing_errors) {
285
@lower_surfaces = @{$self->layers->[$j]->region($region_id)->slices};
290
my $union = union_ex([
291
map $_->expolygon->contour, @upper_surfaces, @lower_surfaces,
294
[ map @$_, @$union ],
295
[ map @{$_->expolygon->holes}, @upper_surfaces, @lower_surfaces, ],
298
$layerm->slices->clear;
299
$layerm->slices->append(
300
map Slic3r::Surface->new
301
(expolygon => $_, surface_type => S_TYPE_INTERNAL),
306
# update layer slices after repairing the single regions
310
# remove empty layers from bottom
311
while (@{$self->layers} && !@{$self->layers->[0]->slices}) {
312
shift @{$self->layers};
313
for (my $i = 0; $i <= $#{$self->layers}; $i++) {
314
$self->layers->[$i]->id( $self->layers->[$i]->id-1 );
318
# simplify slices if required
319
if ($self->print->config->resolution) {
320
$self->_simplify_slices(scale($self->print->config->resolution));
325
my ($self, $region_id, $z, $modifier) = @_;
327
return [] if !defined $self->region_volumes->[$region_id];
331
foreach my $volume_id (@{$self->region_volumes->[$region_id]}) {
332
my $volume = $self->model_object->volumes->[$volume_id];
333
next if $volume->modifier && !$modifier;
334
next if !$volume->modifier && $modifier;
337
$mesh->merge($volume->mesh);
339
$mesh = $volume->mesh->clone;
342
return if !defined $mesh;
345
# we ignore the per-instance transformations currently and only
346
# consider the first one
347
$self->model_object->instances->[0]->transform_mesh($mesh, 1);
349
# align mesh to Z = 0 and apply XY shift
350
$mesh->translate((map unscale(-$_), @{$self->_copies_shift}), -$self->model_object->bounding_box->z_min);
352
# perform actual slicing
353
return $mesh->slice($z);
356
sub make_perimeters {
359
# compare each layer to the one below, and mark those slices needing
360
# one additional inner perimeter, like the top of domed objects-
362
# this algorithm makes sure that at least one perimeter is overlapping
363
# but we don't generate any extra perimeter if fill density is zero, as they would be floating
364
# inside the object - infill_only_where_needed should be the method of choice for printing
366
for my $region_id (0 .. ($self->print->regions_count-1)) {
367
my $region = $self->print->regions->[$region_id];
368
my $region_perimeters = $region->config->perimeters;
370
if ($region->config->extra_perimeters && $region_perimeters > 0 && $region->config->fill_density > 0) {
371
for my $i (0 .. $#{$self->layers}-1) {
372
my $layerm = $self->layers->[$i]->regions->[$region_id];
373
my $upper_layerm = $self->layers->[$i+1]->regions->[$region_id];
374
my $perimeter_spacing = $layerm->flow(FLOW_ROLE_PERIMETER)->scaled_spacing;
376
my $overlap = $perimeter_spacing; # one perimeter
379
offset([ map @{$_->expolygon}, @{$layerm->slices} ], -($region_perimeters * $perimeter_spacing)),
380
offset([ map @{$_->expolygon}, @{$upper_layerm->slices} ], -$overlap),
383
# if we need more perimeters, $diff should contain a narrow region that we can collapse
385
# we use a higher miterLimit here to handle areas with acute angles
386
# in those cases, the default miterLimit would cut the corner and we'd
387
# get a triangle that would trigger a non-needed extra perimeter
390
offset2($diff, -$perimeter_spacing, +$perimeter_spacing, CLIPPER_OFFSET_SCALE, JT_MITER, 5),
394
# diff contains the collapsed area
396
foreach my $slice (@{$layerm->slices}) {
397
my $extra_perimeters = 0;
399
# compute polygons representing the thickness of the hypotetical new internal perimeter
402
my $hypothetical_perimeter = diff(
403
offset($slice->expolygon->arrayref, -($perimeter_spacing * ($region_perimeters + $extra_perimeters-1))),
404
offset($slice->expolygon->arrayref, -($perimeter_spacing * ($region_perimeters + $extra_perimeters))),
406
last CYCLE if !@$hypothetical_perimeter; # no extra perimeter is possible
408
# only add the perimeter if there's an intersection with the collapsed area
409
last CYCLE if !@{ intersection($diff, $hypothetical_perimeter) };
410
Slic3r::debugf " adding one more perimeter at layer %d\n", $layerm->id;
411
$slice->extra_perimeters($extra_perimeters);
419
threads => $self->print->config->threads,
420
items => sub { 0 .. $#{$self->layers} },
423
while (defined (my $i = $q->dequeue)) {
424
$self->layers->[$i]->make_perimeters;
427
collect_cb => sub {},
428
no_threads_cb => sub {
429
$_->make_perimeters for @{$self->layers};
433
# simplify slices (both layer and region slices),
434
# we only need the max resolution for perimeters
435
### This makes this method not-idempotent, so we keep it disabled for now.
436
###$self->_simplify_slices(&Slic3r::SCALED_RESOLUTION);
439
sub detect_surfaces_type {
441
Slic3r::debugf "Detecting solid surfaces...\n";
443
for my $region_id (0 .. ($self->print->regions_count-1)) {
444
for my $i (0 .. $#{$self->layers}) {
445
my $layerm = $self->layers->[$i]->regions->[$region_id];
447
# prepare a reusable subroutine to make surface differences
448
my $difference = sub {
449
my ($subject, $clip, $result_type) = @_;
451
[ map @$_, @$subject ],
455
# collapse very narrow parts (using the safety offset in the diff is not enough)
456
my $offset = $layerm->flow(FLOW_ROLE_PERIMETER)->scaled_width / 10;
457
return map Slic3r::Surface->new(expolygon => $_, surface_type => $result_type),
458
@{ offset2_ex($diff, -$offset, +$offset) };
461
# comparison happens against the *full* slices (considering all regions)
462
# unless internal shells are requested
463
my $upper_layer = $self->layers->[$i+1];
464
my $lower_layer = $i > 0 ? $self->layers->[$i-1] : undef;
466
# find top surfaces (difference between current surfaces
467
# of current layer and upper one)
470
my $upper_slices = $self->config->interface_shells
471
? [ map $_->expolygon, @{$upper_layer->regions->[$region_id]->slices} ]
472
: $upper_layer->slices;
474
@top = $difference->(
475
[ map $_->expolygon, @{$layerm->slices} ],
480
# if no upper layer, all surfaces of this one are solid
481
# we clone surfaces because we're going to clear the slices collection
482
@top = map $_->clone, @{$layerm->slices};
483
$_->surface_type(S_TYPE_TOP) for @top;
486
# find bottom surfaces (difference between current surfaces
487
# of current layer and lower one)
490
# any surface lying on the void is a true bottom bridge
491
push @bottom, $difference->(
492
[ map $_->expolygon, @{$layerm->slices} ],
493
$lower_layer->slices,
497
# if user requested internal shells, we need to identify surfaces
498
# lying on other slices not belonging to this region
499
if ($self->config->interface_shells) {
500
# non-bridging bottom surfaces: any part of this layer lying
501
# on something else, excluding those lying on our own region
502
my $supported = intersection_ex(
503
[ map @{$_->expolygon}, @{$layerm->slices} ],
504
[ map @$_, @{$lower_layer->slices} ],
506
push @bottom, $difference->(
508
[ map $_->expolygon, @{$lower_layer->regions->[$region_id]->slices} ],
513
# if no lower layer, all surfaces of this one are solid
514
# we clone surfaces because we're going to clear the slices collection
515
@bottom = map $_->clone, @{$layerm->slices};
516
$_->surface_type(S_TYPE_BOTTOM) for @bottom;
519
# now, if the object contained a thin membrane, we could have overlapping bottom
520
# and top surfaces; let's do an intersection to discover them and consider them
521
# as bottom surfaces (to allow for bridge detection)
522
if (@top && @bottom) {
523
my $overlapping = intersection_ex([ map $_->p, @top ], [ map $_->p, @bottom ]);
524
Slic3r::debugf " layer %d contains %d membrane(s)\n", $layerm->id, scalar(@$overlapping)
526
@top = $difference->([map $_->expolygon, @top], $overlapping, S_TYPE_TOP);
529
# find internal surfaces (difference between top/bottom surfaces and others)
530
my @internal = $difference->(
531
[ map $_->expolygon, @{$layerm->slices} ],
532
[ map $_->expolygon, @top, @bottom ],
536
# save surfaces to layer
537
$layerm->slices->clear;
538
$layerm->slices->append(@bottom, @top, @internal);
540
Slic3r::debugf " layer %d has %d bottom, %d top and %d internal surfaces\n",
541
$layerm->id, scalar(@bottom), scalar(@top), scalar(@internal) if $Slic3r::debug;
544
# clip surfaces to the fill boundaries
545
foreach my $layer (@{$self->layers}) {
546
my $layerm = $layer->regions->[$region_id];
547
my $fill_boundaries = [ map $_->clone->p, @{$layerm->fill_surfaces} ];
548
$layerm->fill_surfaces->clear;
549
foreach my $surface (@{$layerm->slices}) {
550
my $intersection = intersection_ex(
554
$layerm->fill_surfaces->append(map Slic3r::Surface->new
555
(expolygon => $_, surface_type => $surface->surface_type),
562
sub clip_fill_surfaces {
564
return unless $self->config->infill_only_where_needed;
566
# We only want infill under ceilings; this is almost like an
567
# internal support material.
569
my $additional_margin = scale 3*0;
571
my $overhangs = []; # arrayref of polygons
572
for my $layer_id (reverse 0..$#{$self->layers}) {
573
my $layer = $self->layers->[$layer_id];
574
my @layer_internal = (); # arrayref of Surface objects
575
my @new_internal = (); # arrayref of Surface objects
577
# clip this layer's internal surfaces to @overhangs
578
foreach my $layerm (@{$layer->regions}) {
579
# we assume that this step is run before bridge_over_infill() and combine_infill()
580
# so these are the only internal types we might have
581
my (@internal, @other) = ();
582
foreach my $surface (map $_->clone, @{$layerm->fill_surfaces}) {
583
$surface->surface_type == S_TYPE_INTERNAL
584
? push @internal, $surface
585
: push @other, $surface;
588
# keep all the original internal surfaces to detect overhangs in this layer
589
push @layer_internal, @internal;
591
push @new_internal, my @new = map Slic3r::Surface->new(
593
surface_type => S_TYPE_INTERNAL,
597
[ map $_->p, @internal ],
600
$layerm->fill_surfaces->clear;
601
$layerm->fill_surfaces->append(@new, @other);
604
# get this layer's overhangs defined as the full slice minus the internal infill
605
# (thus we also consider perimeters)
608
[ map $_->p, map @{$_->fill_surfaces}, @{$layer->regions} ],
609
[ map $_->p, @layer_internal ],
611
$overhangs = offset($solid, +$additional_margin);
613
push @$overhangs, map $_->p, @new_internal; # propagate upper overhangs
618
sub bridge_over_infill {
621
for my $region_id (0..$#{$self->print->regions}) {
622
my $fill_density = $self->print->regions->[$region_id]->config->fill_density;
623
next if $fill_density == 100 || $fill_density == 0;
625
for my $layer_id (1..$#{$self->layers}) {
626
my $layer = $self->layers->[$layer_id];
627
my $layerm = $layer->regions->[$region_id];
628
my $lower_layer = $self->layers->[$layer_id-1];
630
# compute the areas needing bridge math
631
my @internal_solid = @{$layerm->fill_surfaces->filter_by_type(S_TYPE_INTERNALSOLID)};
632
my @lower_internal = map @{$_->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)}, @{$lower_layer->regions};
633
my $to_bridge = intersection_ex(
634
[ map $_->p, @internal_solid ],
635
[ map $_->p, @lower_internal ],
637
next unless @$to_bridge;
638
Slic3r::debugf "Bridging %d internal areas at layer %d\n", scalar(@$to_bridge), $layer_id;
640
# build the new collection of fill_surfaces
642
my @new_surfaces = map $_->clone, grep $_->surface_type != S_TYPE_INTERNALSOLID, @{$layerm->fill_surfaces};
643
push @new_surfaces, map Slic3r::Surface->new(
645
surface_type => S_TYPE_INTERNALBRIDGE,
647
push @new_surfaces, map Slic3r::Surface->new(
649
surface_type => S_TYPE_INTERNALSOLID,
651
[ map $_->p, @internal_solid ],
652
[ map @$_, @$to_bridge ],
655
$layerm->fill_surfaces->clear;
656
$layerm->fill_surfaces->append(@new_surfaces);
659
# exclude infill from the layers below if needed
660
# see discussion at https://github.com/alexrj/Slic3r/issues/240
661
# Update: do not exclude any infill. Sparse infill is able to absorb the excess material.
663
my $excess = $layerm->extruders->{infill}->bridge_flow->width - $layerm->height;
664
for (my $i = $layer_id-1; $excess >= $self->layers->[$i]->height; $i--) {
665
Slic3r::debugf " skipping infill below those areas at layer %d\n", $i;
666
foreach my $lower_layerm (@{$self->layers->[$i]->regions}) {
667
my @new_surfaces = ();
668
# subtract the area from all types of surfaces
669
foreach my $group (@{$lower_layerm->fill_surfaces->group}) {
670
push @new_surfaces, map $group->[0]->clone(expolygon => $_),
672
[ map $_->p, @$group ],
673
[ map @$_, @$to_bridge ],
675
push @new_surfaces, map Slic3r::Surface->new(
677
surface_type => S_TYPE_INTERNALVOID,
678
), @{intersection_ex(
679
[ map $_->p, @$group ],
680
[ map @$_, @$to_bridge ],
683
$lower_layerm->fill_surfaces->clear;
684
$lower_layerm->fill_surfaces->append(@new_surfaces);
687
$excess -= $self->layers->[$i]->height;
694
sub process_external_surfaces {
697
for my $region_id (0 .. ($self->print->regions_count-1)) {
698
$self->layers->[0]->regions->[$region_id]->process_external_surfaces(undef);
699
for my $i (1 .. $#{$self->layers}) {
700
$self->layers->[$i]->regions->[$region_id]->process_external_surfaces($self->layers->[$i-1]);
705
sub discover_horizontal_shells {
708
Slic3r::debugf "==> DISCOVERING HORIZONTAL SHELLS\n";
710
for my $region_id (0 .. ($self->print->regions_count-1)) {
711
for (my $i = 0; $i <= $#{$self->layers}; $i++) {
712
my $layerm = $self->layers->[$i]->regions->[$region_id];
714
if ($layerm->config->solid_infill_every_layers && $layerm->config->fill_density > 0
715
&& ($i % $layerm->config->solid_infill_every_layers) == 0) {
716
$_->surface_type(S_TYPE_INTERNALSOLID) for @{$layerm->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)};
719
EXTERNAL: foreach my $type (S_TYPE_TOP, S_TYPE_BOTTOM, S_TYPE_BOTTOMBRIDGE) {
720
# find slices of current type for current layer
721
# use slices instead of fill_surfaces because they also include the perimeter area
722
# which needs to be propagated in shells; we need to grow slices like we did for
723
# fill_surfaces though. Using both ungrown slices and grown fill_surfaces will
724
# not work in some situations, as there won't be any grown region in the perimeter
725
# area (this was seen in a model where the top layer had one extra perimeter, thus
726
# its fill_surfaces were thinner than the lower layer's infill), however it's the best
727
# solution so far. Growing the external slices by EXTERNAL_INFILL_MARGIN will put
728
# too much solid infill inside nearly-vertical slopes.
730
(map $_->p, @{$layerm->slices->filter_by_type($type)}),
731
(map $_->p, @{$layerm->fill_surfaces->filter_by_type($type)}),
734
Slic3r::debugf "Layer %d has %s surfaces\n", $i, ($type == S_TYPE_TOP) ? 'top' : 'bottom';
736
my $solid_layers = ($type == S_TYPE_TOP)
737
? $layerm->config->top_solid_layers
738
: $layerm->config->bottom_solid_layers;
739
NEIGHBOR: for (my $n = ($type == S_TYPE_TOP) ? $i-1 : $i+1;
740
abs($n - $i) <= $solid_layers-1;
741
($type == S_TYPE_TOP) ? $n-- : $n++) {
743
next if $n < 0 || $n > $#{$self->layers};
744
Slic3r::debugf " looking for neighbors on layer %d...\n", $n;
746
my $neighbor_layerm = $self->layers->[$n]->regions->[$region_id];
747
my $neighbor_fill_surfaces = $neighbor_layerm->fill_surfaces;
748
my @neighbor_fill_surfaces = map $_->clone, @$neighbor_fill_surfaces; # clone because we will use these surfaces even after clearing the collection
750
# find intersection between neighbor and current layer's surfaces
751
# intersections have contours and holes
752
# we update $solid so that we limit the next neighbor layer to the areas that were
753
# found on this one - in other words, solid shells on one layer (for a given external surface)
754
# are always a subset of the shells found on the previous shell layer
755
# this approach allows for DWIM in hollow sloping vases, where we want bottom
756
# shells to be generated in the base but not in the walls (where there are many
757
# narrow bottom surfaces): reassigning $solid will consider the 'shadow' of the
758
# upper perimeter as an obstacle and shell will not be propagated to more upper layers
759
my $new_internal_solid = $solid = intersection(
761
[ map $_->p, grep { ($_->surface_type == S_TYPE_INTERNAL) || ($_->surface_type == S_TYPE_INTERNALSOLID) } @neighbor_fill_surfaces ],
764
next EXTERNAL if !@$new_internal_solid;
766
if ($layerm->config->fill_density == 0) {
767
# if we're printing a hollow object we discard any solid shell thinner
768
# than a perimeter width, since it's probably just crossing a sloping wall
769
# and it's not wanted in a hollow print even if it would make sense when
770
# obeying the solid shell count option strictly (DWIM!)
771
my $margin = $neighbor_layerm->flow(FLOW_ROLE_PERIMETER)->scaled_width;
772
my $too_narrow = diff(
774
offset2($new_internal_solid, -$margin, +$margin, CLIPPER_OFFSET_SCALE, JT_MITER, 5),
777
$new_internal_solid = $solid = diff(
783
# make sure the new internal solid is wide enough, as it might get collapsed
784
# when spacing is added in Fill.pm
786
my $margin = 3 * $layerm->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width; # require at least this size
787
# we use a higher miterLimit here to handle areas with acute angles
788
# in those cases, the default miterLimit would cut the corner and we'd
789
# get a triangle in $too_narrow; if we grow it below then the shell
790
# would have a different shape from the external surface and we'd still
791
# have the same angle, so the next shell would be grown even more and so on.
792
my $too_narrow = diff(
794
offset2($new_internal_solid, -$margin, +$margin, CLIPPER_OFFSET_SCALE, JT_MITER, 5),
799
# grow the collapsing parts and add the extra area to the neighbor layer
800
# as well as to our original surfaces so that we support this
801
# additional area in the next shell too
803
# make sure our grown surfaces don't exceed the fill area
804
my @grown = @{intersection(
805
offset($too_narrow, +$margin),
806
[ map $_->p, @neighbor_fill_surfaces ],
808
$new_internal_solid = $solid = [ @grown, @$new_internal_solid ];
812
# internal-solid are the union of the existing internal-solid surfaces
814
my $internal_solid = union_ex([
815
( map $_->p, grep $_->surface_type == S_TYPE_INTERNALSOLID, @neighbor_fill_surfaces ),
816
@$new_internal_solid,
819
# subtract intersections from layer surfaces to get resulting internal surfaces
820
my $internal = diff_ex(
821
[ map $_->p, grep $_->surface_type == S_TYPE_INTERNAL, @neighbor_fill_surfaces ],
822
[ map @$_, @$internal_solid ],
825
Slic3r::debugf " %d internal-solid and %d internal surfaces found\n",
826
scalar(@$internal_solid), scalar(@$internal);
828
# assign resulting internal surfaces to layer
829
$neighbor_fill_surfaces->clear;
830
$neighbor_fill_surfaces->append(map Slic3r::Surface->new
831
(expolygon => $_, surface_type => S_TYPE_INTERNAL), @$internal);
833
# assign new internal-solid surfaces to layer
834
$neighbor_fill_surfaces->append(map Slic3r::Surface->new
835
(expolygon => $_, surface_type => S_TYPE_INTERNALSOLID), @$internal_solid);
837
# assign top and bottom surfaces to layer
838
foreach my $s (@{Slic3r::Surface::Collection->new(grep { ($_->surface_type == S_TYPE_TOP) || $_->is_bottom } @neighbor_fill_surfaces)->group}) {
839
my $solid_surfaces = diff_ex(
841
[ map @$_, @$internal_solid, @$internal ],
844
$neighbor_fill_surfaces->append(map $s->[0]->clone(expolygon => $_), @$solid_surfaces);
852
# combine fill surfaces across layers
856
return unless defined first { $_->config->infill_every_layers > 1 && $_->config->fill_density > 0 } @{$self->print->regions};
858
my @layer_heights = map $_->height, @{$self->layers};
860
for my $region_id (0 .. ($self->print->regions_count-1)) {
861
my $region = $self->print->regions->[$region_id];
862
my $every = $region->config->infill_every_layers;
864
# limit the number of combined layers to the maximum height allowed by this regions' nozzle
865
my $nozzle_diameter = $self->print->config->get_at('nozzle_diameter', $region->config->infill_extruder-1);
867
# define the combinations
868
my @combine = (); # layer_id => thickness in layers
870
my $current_height = my $layers = 0;
871
for my $layer_id (1 .. $#layer_heights) {
872
my $height = $self->layers->[$layer_id]->height;
874
if ($current_height + $height >= $nozzle_diameter || $layers >= $every) {
875
$combine[$layer_id-1] = $layers;
876
$current_height = $layers = 0;
879
$current_height += $height;
885
for my $layer_id (1 .. $#combine) {
886
next unless ($combine[$layer_id] // 1) > 1;
887
my @layerms = map $self->layers->[$_]->regions->[$region_id],
888
($layer_id - ($combine[$layer_id]-1) .. $layer_id);
890
# only combine internal infill
891
for my $type (S_TYPE_INTERNAL) {
892
# we need to perform a multi-layer intersection, so let's split it in pairs
894
# initialize the intersection with the candidates of the lowest layer
895
my $intersection = [ map $_->expolygon, @{$layerms[0]->fill_surfaces->filter_by_type($type)} ];
897
# start looping from the second layer and intersect the current intersection with it
898
for my $layerm (@layerms[1 .. $#layerms]) {
899
$intersection = intersection_ex(
900
[ map @$_, @$intersection ],
901
[ map @{$_->expolygon}, @{$layerm->fill_surfaces->filter_by_type($type)} ],
905
my $area_threshold = $layerms[0]->infill_area_threshold;
906
@$intersection = grep $_->area > $area_threshold, @$intersection;
907
next if !@$intersection;
908
Slic3r::debugf " combining %d %s regions from layers %d-%d\n",
909
scalar(@$intersection),
910
($type == S_TYPE_INTERNAL ? 'internal' : 'internal-solid'),
911
$layer_id-($every-1), $layer_id;
913
# $intersection now contains the regions that can be combined across the full amount of layers
914
# so let's remove those areas from all layers
916
my @intersection_with_clearance = map @{$_->offset(
917
$layerms[-1]->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width / 2
918
+ $layerms[-1]->flow(FLOW_ROLE_PERIMETER)->scaled_width / 2
919
# Because fill areas for rectilinear and honeycomb are grown
920
# later to overlap perimeters, we need to counteract that too.
921
+ (($type == S_TYPE_INTERNALSOLID || $region->config->fill_pattern =~ /(rectilinear|honeycomb)/)
922
? $layerms[-1]->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width * &Slic3r::INFILL_OVERLAP_OVER_SPACING
927
foreach my $layerm (@layerms) {
928
my @this_type = @{$layerm->fill_surfaces->filter_by_type($type)};
929
my @other_types = map $_->clone, grep $_->surface_type != $type, @{$layerm->fill_surfaces};
931
my @new_this_type = map Slic3r::Surface->new(expolygon => $_, surface_type => $type),
933
[ map $_->p, @this_type ],
934
[ @intersection_with_clearance ],
937
# apply surfaces back with adjusted depth to the uppermost layer
938
if ($layerm->id == $layer_id) {
940
map Slic3r::Surface->new(
942
surface_type => $type,
943
thickness => sum(map $_->height, @layerms),
944
thickness_layers => scalar(@layerms),
950
map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNALVOID),
952
[ map @{$_->expolygon}, @this_type ],
953
[ @intersection_with_clearance ],
957
$layerm->fill_surfaces->clear;
958
$layerm->fill_surfaces->append(@new_this_type, @other_types);
965
sub generate_support_material {
968
# TODO: make this method idempotent by removing all support layers
969
# before checking whether we need to generate support or not
970
return unless ($self->config->support_material || $self->config->raft_layers > 0)
971
&& scalar(@{$self->layers}) >= 2;
973
my $first_layer_flow = Slic3r::Flow->new_from_width(
974
width => ($self->config->first_layer_extrusion_width || $self->config->support_material_extrusion_width),
975
role => FLOW_ROLE_SUPPORT_MATERIAL,
976
nozzle_diameter => $self->print->config->nozzle_diameter->[ $self->config->support_material_extruder-1 ]
977
// $self->print->config->nozzle_diameter->[0],
978
layer_height => $self->config->get_abs_value('first_layer_height'),
979
bridge_flow_ratio => 0,
982
my $s = Slic3r::Print::SupportMaterial->new(
983
print_config => $self->print->config,
984
object_config => $self->config,
985
first_layer_flow => $first_layer_flow,
986
flow => $self->support_material_flow,
987
interface_flow => $self->support_material_flow(FLOW_ROLE_SUPPORT_MATERIAL_INTERFACE),
992
sub _simplify_slices {
993
my ($self, $distance) = @_;
995
foreach my $layer (@{$self->layers}) {
996
$layer->slices->simplify($distance);
997
$_->slices->simplify($distance) for @{$layer->regions};
1001
sub support_material_flow {
1002
my ($self, $role) = @_;
1004
$role //= FLOW_ROLE_SUPPORT_MATERIAL;
1005
my $extruder = ($role == FLOW_ROLE_SUPPORT_MATERIAL)
1006
? $self->config->support_material_extruder
1007
: $self->config->support_material_interface_extruder;
1009
# we use a bogus layer_height because we use the same flow for all
1010
# support material layers
1011
return Slic3r::Flow->new_from_width(
1012
width => $self->config->support_material_extrusion_width || $self->config->extrusion_width,
1014
nozzle_diameter => $self->print->config->nozzle_diameter->[$extruder-1] // $self->print->config->nozzle_diameter->[0],
1015
layer_height => $self->config->layer_height,
1016
bridge_flow_ratio => 0,