Object.pm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. package Slic3r::Print::Object;
  2. # extends c++ class Slic3r::PrintObject (Print.xsp)
  3. use strict;
  4. use warnings;
  5. use List::Util qw(min max sum first any);
  6. use Slic3r::Flow ':roles';
  7. use Slic3r::Geometry qw(X Y Z PI scale unscale chained_path epsilon);
  8. use Slic3r::Geometry::Clipper qw(diff diff_ex intersection intersection_ex union union_ex
  9. offset offset_ex offset2 offset2_ex intersection_ppl CLIPPER_OFFSET_SCALE JT_MITER);
  10. use Slic3r::Print::State ':steps';
  11. use Slic3r::Surface ':types';
  12. # TODO: lazy
  13. sub fill_maker {
  14. my $self = shift;
  15. return Slic3r::Fill->new(bounding_box => $self->bounding_box);
  16. }
  17. sub region_volumes {
  18. my $self = shift;
  19. return [ map $self->get_region_volumes($_), 0..($self->region_count - 1) ];
  20. }
  21. sub layers {
  22. my $self = shift;
  23. return [ map $self->get_layer($_), 0..($self->layer_count - 1) ];
  24. }
  25. sub support_layers {
  26. my $self = shift;
  27. return [ map $self->get_support_layer($_), 0..($self->support_layer_count - 1) ];
  28. }
  29. # 1) Decides Z positions of the layers,
  30. # 2) Initializes layers and their regions
  31. # 3) Slices the object meshes
  32. # 4) Slices the modifier meshes and reclassifies the slices of the object meshes by the slices of the modifier meshes
  33. # 5) Applies size compensation (offsets the slices in XY plane)
  34. # 6) Replaces bad slices by the slices reconstructed from the upper/lower layer
  35. # Resulting expolygons of layer regions are marked as Internal.
  36. #
  37. # this should be idempotent
  38. sub slice {
  39. my $self = shift;
  40. return if $self->step_done(STEP_SLICE);
  41. $self->set_step_started(STEP_SLICE);
  42. $self->print->status_cb->(10, "Processing triangulated mesh");
  43. $self->_slice;
  44. # detect slicing errors
  45. my $warning_thrown = 0;
  46. for my $i (0 .. ($self->layer_count - 1)) {
  47. my $layer = $self->get_layer($i);
  48. next unless $layer->slicing_errors;
  49. if (!$warning_thrown) {
  50. warn "The model has overlapping or self-intersecting facets. I tried to repair it, "
  51. . "however you might want to check the results or repair the input file and retry.\n";
  52. $warning_thrown = 1;
  53. }
  54. # try to repair the layer surfaces by merging all contours and all holes from
  55. # neighbor layers
  56. Slic3r::debugf "Attempting to repair layer %d\n", $i;
  57. foreach my $region_id (0 .. ($layer->region_count - 1)) {
  58. my $layerm = $layer->region($region_id);
  59. my (@upper_surfaces, @lower_surfaces);
  60. for (my $j = $i+1; $j < $self->layer_count; $j++) {
  61. if (!$self->get_layer($j)->slicing_errors) {
  62. @upper_surfaces = @{$self->get_layer($j)->region($region_id)->slices};
  63. last;
  64. }
  65. }
  66. for (my $j = $i-1; $j >= 0; $j--) {
  67. if (!$self->get_layer($j)->slicing_errors) {
  68. @lower_surfaces = @{$self->get_layer($j)->region($region_id)->slices};
  69. last;
  70. }
  71. }
  72. my $union = union_ex([
  73. map $_->expolygon->contour, @upper_surfaces, @lower_surfaces,
  74. ]);
  75. my $diff = diff_ex(
  76. [ map @$_, @$union ],
  77. [ map @{$_->expolygon->holes}, @upper_surfaces, @lower_surfaces, ],
  78. );
  79. $layerm->slices->clear;
  80. $layerm->slices->append($_)
  81. for map Slic3r::Surface->new
  82. (expolygon => $_, surface_type => S_TYPE_INTERNAL),
  83. @$diff;
  84. }
  85. # update layer slices after repairing the single regions
  86. $layer->make_slices;
  87. }
  88. # remove empty layers from bottom
  89. while (@{$self->layers} && !@{$self->get_layer(0)->slices}) {
  90. $self->delete_layer(0);
  91. for (my $i = 0; $i <= $#{$self->layers}; $i++) {
  92. $self->get_layer($i)->set_id( $self->get_layer($i)->id-1 );
  93. }
  94. }
  95. # simplify slices if required
  96. if ($self->print->config->resolution) {
  97. $self->_simplify_slices(scale($self->print->config->resolution));
  98. }
  99. die "No layers were detected. You might want to repair your STL file(s) or check their size or thickness and retry.\n"
  100. if !@{$self->layers};
  101. $self->set_typed_slices(0);
  102. $self->set_step_done(STEP_SLICE);
  103. }
  104. sub make_perimeters {
  105. my ($self) = @_;
  106. return if $self->step_done(STEP_PERIMETERS);
  107. # Temporary workaround for detect_surfaces_type() not being idempotent (see #3764).
  108. # We can remove this when idempotence is restored. This make_perimeters() method
  109. # will just call merge_slices() to undo the typed slices and invalidate posDetectSurfaces.
  110. if ($self->typed_slices) {
  111. $self->invalidate_step(STEP_SLICE);
  112. }
  113. # prerequisites
  114. $self->slice;
  115. $self->_make_perimeters;
  116. }
  117. # This will assign a type (top/bottom/internal) to $layerm->slices
  118. # and transform $layerm->fill_surfaces from expolygon
  119. # to typed top/bottom/internal surfaces;
  120. sub detect_surfaces_type {
  121. my ($self) = @_;
  122. # prerequisites
  123. $self->slice;
  124. $self->_detect_surfaces_type;
  125. }
  126. sub prepare_infill {
  127. my ($self) = @_;
  128. return if $self->step_done(STEP_PREPARE_INFILL);
  129. # This prepare_infill() is not really idempotent.
  130. # TODO: It should clear and regenerate fill_surfaces at every run
  131. # instead of modifying it in place.
  132. $self->invalidate_step(STEP_PERIMETERS);
  133. $self->make_perimeters;
  134. # Do this after invalidating STEP_PERIMETERS because that would re-invalidate STEP_PREPARE_INFILL
  135. $self->set_step_started(STEP_PREPARE_INFILL);
  136. # prerequisites
  137. $self->detect_surfaces_type;
  138. $self->print->status_cb->(30, "Preparing infill");
  139. # decide what surfaces are to be filled
  140. $_->prepare_fill_surfaces for map @{$_->regions}, @{$self->layers};
  141. # this will detect bridges and reverse bridges
  142. # and rearrange top/bottom/internal surfaces
  143. $self->process_external_surfaces;
  144. # detect which fill surfaces are near external layers
  145. # they will be split in internal and internal-solid surfaces
  146. $self->discover_horizontal_shells;
  147. $self->clip_fill_surfaces;
  148. # the following step needs to be done before combination because it may need
  149. # to remove only half of the combined infill
  150. $self->bridge_over_infill;
  151. # combine fill surfaces to honor the "infill every N layers" option
  152. $self->combine_infill;
  153. $self->set_step_done(STEP_PREPARE_INFILL);
  154. }
  155. sub infill {
  156. my ($self) = @_;
  157. # prerequisites
  158. $self->prepare_infill;
  159. $self->_infill;
  160. }
  161. sub generate_support_material {
  162. my $self = shift;
  163. # prerequisites
  164. $self->slice;
  165. return if $self->step_done(STEP_SUPPORTMATERIAL);
  166. $self->set_step_started(STEP_SUPPORTMATERIAL);
  167. $self->clear_support_layers;
  168. if ((!$self->config->support_material
  169. && $self->config->raft_layers == 0
  170. && $self->config->support_material_enforce_layers == 0)
  171. || scalar(@{$self->layers}) < 2
  172. ) {
  173. $self->set_step_done(STEP_SUPPORTMATERIAL);
  174. return;
  175. }
  176. $self->print->status_cb->(85, "Generating support material");
  177. $self->_support_material->generate($self);
  178. $self->set_step_done(STEP_SUPPORTMATERIAL);
  179. my $stats = sprintf "Weight: %.1fg, Cost: %.1f" , $self->print->total_weight, $self->print->total_cost;
  180. $self->print->status_cb->(85, $stats);
  181. }
  182. sub _support_material {
  183. my ($self) = @_;
  184. my $first_layer_flow = Slic3r::Flow->new_from_width(
  185. width => ($self->print->config->first_layer_extrusion_width || $self->config->support_material_extrusion_width),
  186. role => FLOW_ROLE_SUPPORT_MATERIAL,
  187. nozzle_diameter => $self->print->config->nozzle_diameter->[ $self->config->support_material_extruder-1 ]
  188. // $self->print->config->nozzle_diameter->[0],
  189. layer_height => $self->config->get_abs_value('first_layer_height'),
  190. bridge_flow_ratio => 0,
  191. );
  192. return Slic3r::Print::SupportMaterial->new(
  193. print_config => $self->print->config,
  194. object_config => $self->config,
  195. first_layer_flow => $first_layer_flow,
  196. flow => $self->support_material_flow,
  197. interface_flow => $self->support_material_flow(FLOW_ROLE_SUPPORT_MATERIAL_INTERFACE),
  198. );
  199. }
  200. # Idempotence of this method is guaranteed by the fact that we don't remove things from
  201. # fill_surfaces but we only turn them into VOID surfaces, thus preserving the boundaries.
  202. sub clip_fill_surfaces {
  203. my $self = shift;
  204. return unless $self->config->infill_only_where_needed
  205. && any { $_->config->fill_density > 0 } @{$self->print->regions};
  206. # We only want infill under ceilings; this is almost like an
  207. # internal support material.
  208. # proceed top-down skipping bottom layer
  209. my $upper_internal = [];
  210. for my $layer_id (reverse 1..($self->layer_count - 1)) {
  211. my $layer = $self->get_layer($layer_id);
  212. my $lower_layer = $self->get_layer($layer_id-1);
  213. # detect things that we need to support
  214. my $overhangs = []; # Polygons
  215. # we need to support any solid surface
  216. push @$overhangs, map $_->p,
  217. grep $_->is_solid, map @{$_->fill_surfaces}, @{$layer->regions};
  218. # we also need to support perimeters when there's at least one full
  219. # unsupported loop
  220. {
  221. # get perimeters area as the difference between slices and fill_surfaces
  222. my $perimeters = diff(
  223. [ map @$_, @{$layer->slices} ],
  224. [ map $_->p, map @{$_->fill_surfaces}, @{$layer->regions} ],
  225. );
  226. # only consider the area that is not supported by lower perimeters
  227. $perimeters = intersection(
  228. $perimeters,
  229. [ map $_->p, map @{$_->fill_surfaces}, @{$lower_layer->regions} ],
  230. 1,
  231. );
  232. # only consider perimeter areas that are at least one extrusion width thick
  233. my $pw = min(map $_->flow(FLOW_ROLE_PERIMETER)->scaled_width, @{$layer->regions});
  234. $perimeters = offset2($perimeters, -$pw, +$pw);
  235. # append such thick perimeters to the areas that need support
  236. push @$overhangs, @$perimeters;
  237. }
  238. # find new internal infill
  239. $upper_internal = my $new_internal = intersection(
  240. [
  241. @$overhangs,
  242. @$upper_internal,
  243. ],
  244. [
  245. # our current internal fill boundaries
  246. map $_->p,
  247. grep $_->surface_type == S_TYPE_INTERNAL || $_->surface_type == S_TYPE_INTERNALVOID,
  248. map @{$_->fill_surfaces}, @{$lower_layer->regions}
  249. ],
  250. );
  251. # apply new internal infill to regions
  252. foreach my $layerm (@{$lower_layer->regions}) {
  253. next if $layerm->region->config->fill_density == 0;
  254. my (@internal, @other) = ();
  255. foreach my $surface (map $_->clone, @{$layerm->fill_surfaces}) {
  256. if ($surface->surface_type == S_TYPE_INTERNAL || $surface->surface_type == S_TYPE_INTERNALVOID) {
  257. push @internal, $surface;
  258. } else {
  259. push @other, $surface;
  260. }
  261. }
  262. my @new = map Slic3r::Surface->new(
  263. expolygon => $_,
  264. surface_type => S_TYPE_INTERNAL,
  265. ),
  266. @{intersection_ex(
  267. [ map $_->p, @internal ],
  268. $new_internal,
  269. 1,
  270. )};
  271. push @other, map Slic3r::Surface->new(
  272. expolygon => $_,
  273. surface_type => S_TYPE_INTERNALVOID,
  274. ),
  275. @{diff_ex(
  276. [ map $_->p, @internal ],
  277. $new_internal,
  278. 1,
  279. )};
  280. # If there are voids it means that our internal infill is not adjacent to
  281. # perimeters. In this case it would be nice to add a loop around infill to
  282. # make it more robust and nicer. TODO.
  283. $layerm->fill_surfaces->clear;
  284. $layerm->fill_surfaces->append($_) for (@new, @other);
  285. }
  286. }
  287. }
  288. sub discover_horizontal_shells {
  289. my $self = shift;
  290. Slic3r::debugf "==> DISCOVERING HORIZONTAL SHELLS\n";
  291. for my $region_id (0 .. ($self->print->region_count-1)) {
  292. for (my $i = 0; $i < $self->layer_count; $i++) {
  293. my $layerm = $self->get_layer($i)->regions->[$region_id];
  294. if ($layerm->region->config->solid_infill_every_layers && $layerm->region->config->fill_density > 0
  295. && ($i % $layerm->region->config->solid_infill_every_layers) == 0) {
  296. my $type = $layerm->region->config->fill_density == 100 ? S_TYPE_INTERNALSOLID : S_TYPE_INTERNALBRIDGE;
  297. $_->surface_type($type) for @{$layerm->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)};
  298. }
  299. EXTERNAL: foreach my $type (S_TYPE_TOP, S_TYPE_BOTTOM, S_TYPE_BOTTOMBRIDGE) {
  300. # find slices of current type for current layer
  301. # use slices instead of fill_surfaces because they also include the perimeter area
  302. # which needs to be propagated in shells; we need to grow slices like we did for
  303. # fill_surfaces though. Using both ungrown slices and grown fill_surfaces will
  304. # not work in some situations, as there won't be any grown region in the perimeter
  305. # area (this was seen in a model where the top layer had one extra perimeter, thus
  306. # its fill_surfaces were thinner than the lower layer's infill), however it's the best
  307. # solution so far. Growing the external slices by EXTERNAL_INFILL_MARGIN will put
  308. # too much solid infill inside nearly-vertical slopes.
  309. my $solid = [
  310. (map $_->p, @{$layerm->slices->filter_by_type($type)}),
  311. (map $_->p, @{$layerm->fill_surfaces->filter_by_type($type)}),
  312. ];
  313. next if !@$solid;
  314. Slic3r::debugf "Layer %d has %s surfaces\n", $i, ($type == S_TYPE_TOP) ? 'top' : 'bottom';
  315. my $solid_layers = ($type == S_TYPE_TOP)
  316. ? $layerm->region->config->top_solid_layers
  317. : $layerm->region->config->bottom_solid_layers;
  318. NEIGHBOR: for (my $n = ($type == S_TYPE_TOP) ? $i-1 : $i+1;
  319. abs($n - $i) <= $solid_layers-1;
  320. ($type == S_TYPE_TOP) ? $n-- : $n++) {
  321. next if $n < 0 || $n >= $self->layer_count;
  322. Slic3r::debugf " looking for neighbors on layer %d...\n", $n;
  323. my $neighbor_layerm = $self->get_layer($n)->regions->[$region_id];
  324. my $neighbor_fill_surfaces = $neighbor_layerm->fill_surfaces;
  325. my @neighbor_fill_surfaces = map $_->clone, @$neighbor_fill_surfaces; # clone because we will use these surfaces even after clearing the collection
  326. # find intersection between neighbor and current layer's surfaces
  327. # intersections have contours and holes
  328. my $new_internal_solid = intersection(
  329. $solid,
  330. [ map $_->p, grep { ($_->surface_type == S_TYPE_INTERNAL) || ($_->surface_type == S_TYPE_INTERNALSOLID) } @neighbor_fill_surfaces ],
  331. 1,
  332. );
  333. if (!@$new_internal_solid) {
  334. # No internal solid needed on this layer. In order to decide whether to continue
  335. # searching on the next neighbor (thus enforcing the configured number of solid
  336. # layers, use different strategies according to configured infill density:
  337. if ($layerm->region->config->fill_density == 0) {
  338. # If user expects the object to be void (for example a hollow sloping vase),
  339. # don't continue the search. In this case, we only generate the external solid
  340. # shell if the object would otherwise show a hole (gap between perimeters of
  341. # the two layers), and internal solid shells are a subset of the shells found
  342. # on each previous layer.
  343. next EXTERNAL;
  344. } else {
  345. # If we have internal infill, we can generate internal solid shells freely.
  346. next NEIGHBOR;
  347. }
  348. }
  349. if ($layerm->region->config->fill_density == 0) {
  350. # if we're printing a hollow object we discard any solid shell thinner
  351. # than a perimeter width, since it's probably just crossing a sloping wall
  352. # and it's not wanted in a hollow print even if it would make sense when
  353. # obeying the solid shell count option strictly (DWIM!)
  354. my $margin = $neighbor_layerm->flow(FLOW_ROLE_EXTERNAL_PERIMETER)->scaled_width;
  355. my $too_narrow = diff(
  356. $new_internal_solid,
  357. offset2($new_internal_solid, -$margin, +$margin, CLIPPER_OFFSET_SCALE, JT_MITER, 5),
  358. 1,
  359. );
  360. $new_internal_solid = $solid = diff(
  361. $new_internal_solid,
  362. $too_narrow,
  363. ) if @$too_narrow;
  364. }
  365. # make sure the new internal solid is wide enough, as it might get collapsed
  366. # when spacing is added in Fill.pm
  367. {
  368. my $margin = 3 * $layerm->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width; # require at least this size
  369. # we use a higher miterLimit here to handle areas with acute angles
  370. # in those cases, the default miterLimit would cut the corner and we'd
  371. # get a triangle in $too_narrow; if we grow it below then the shell
  372. # would have a different shape from the external surface and we'd still
  373. # have the same angle, so the next shell would be grown even more and so on.
  374. my $too_narrow = diff(
  375. $new_internal_solid,
  376. offset2($new_internal_solid, -$margin, +$margin, CLIPPER_OFFSET_SCALE, JT_MITER, 5),
  377. 1,
  378. );
  379. if (@$too_narrow) {
  380. # grow the collapsing parts and add the extra area to the neighbor layer
  381. # as well as to our original surfaces so that we support this
  382. # additional area in the next shell too
  383. # make sure our grown surfaces don't exceed the fill area
  384. my @grown = @{intersection(
  385. offset($too_narrow, +$margin),
  386. # Discard bridges as they are grown for anchoring and we can't
  387. # remove such anchors. (This may happen when a bridge is being
  388. # anchored onto a wall where little space remains after the bridge
  389. # is grown, and that little space is an internal solid shell so
  390. # it triggers this too_narrow logic.)
  391. [ map $_->p, grep { $_->is_internal && !$_->is_bridge } @neighbor_fill_surfaces ],
  392. )};
  393. $new_internal_solid = $solid = [ @grown, @$new_internal_solid ];
  394. }
  395. }
  396. # internal-solid are the union of the existing internal-solid surfaces
  397. # and new ones
  398. my $internal_solid = union_ex([
  399. ( map $_->p, grep $_->surface_type == S_TYPE_INTERNALSOLID, @neighbor_fill_surfaces ),
  400. @$new_internal_solid,
  401. ]);
  402. # subtract intersections from layer surfaces to get resulting internal surfaces
  403. my $internal = diff_ex(
  404. [ map $_->p, grep $_->surface_type == S_TYPE_INTERNAL, @neighbor_fill_surfaces ],
  405. [ map @$_, @$internal_solid ],
  406. 1,
  407. );
  408. Slic3r::debugf " %d internal-solid and %d internal surfaces found\n",
  409. scalar(@$internal_solid), scalar(@$internal);
  410. # assign resulting internal surfaces to layer
  411. $neighbor_fill_surfaces->clear;
  412. $neighbor_fill_surfaces->append($_)
  413. for map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNAL),
  414. @$internal;
  415. # assign new internal-solid surfaces to layer
  416. $neighbor_fill_surfaces->append($_)
  417. for map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNALSOLID),
  418. @$internal_solid;
  419. # assign top and bottom surfaces to layer
  420. foreach my $s (@{Slic3r::Surface::Collection->new(grep { ($_->surface_type == S_TYPE_TOP) || $_->is_bottom } @neighbor_fill_surfaces)->group}) {
  421. my $solid_surfaces = diff_ex(
  422. [ map $_->p, @$s ],
  423. [ map @$_, @$internal_solid, @$internal ],
  424. 1,
  425. );
  426. $neighbor_fill_surfaces->append($_)
  427. for map $s->[0]->clone(expolygon => $_), @$solid_surfaces;
  428. }
  429. }
  430. }
  431. }
  432. }
  433. }
  434. # combine fill surfaces across layers
  435. # Idempotence of this method is guaranteed by the fact that we don't remove things from
  436. # fill_surfaces but we only turn them into VOID surfaces, thus preserving the boundaries.
  437. sub combine_infill {
  438. my $self = shift;
  439. # define the type used for voids
  440. my %voidtype = (
  441. &S_TYPE_INTERNAL() => S_TYPE_INTERNALVOID,
  442. );
  443. # work on each region separately
  444. for my $region_id (0 .. ($self->print->region_count-1)) {
  445. my $region = $self->print->get_region($region_id);
  446. my $every = $region->config->infill_every_layers;
  447. next unless $every > 1 && $region->config->fill_density > 0;
  448. # limit the number of combined layers to the maximum height allowed by this regions' nozzle
  449. my $nozzle_diameter = min(
  450. $self->print->config->get_at('nozzle_diameter', $region->config->infill_extruder-1),
  451. $self->print->config->get_at('nozzle_diameter', $region->config->solid_infill_extruder-1),
  452. );
  453. # define the combinations
  454. my %combine = (); # layer_idx => number of additional combined lower layers
  455. {
  456. my $current_height = my $layers = 0;
  457. for my $layer_idx (0 .. ($self->layer_count-1)) {
  458. my $layer = $self->get_layer($layer_idx);
  459. next if $layer->id == 0; # skip first print layer (which may not be first layer in array because of raft)
  460. my $height = $layer->height;
  461. # check whether the combination of this layer with the lower layers' buffer
  462. # would exceed max layer height or max combined layer count
  463. if ($current_height + $height >= $nozzle_diameter + epsilon || $layers >= $every) {
  464. # append combination to lower layer
  465. $combine{$layer_idx-1} = $layers;
  466. $current_height = $layers = 0;
  467. }
  468. $current_height += $height;
  469. $layers++;
  470. }
  471. # append lower layers (if any) to uppermost layer
  472. $combine{$self->layer_count-1} = $layers;
  473. }
  474. # loop through layers to which we have assigned layers to combine
  475. for my $layer_idx (sort keys %combine) {
  476. next unless $combine{$layer_idx} > 1;
  477. # get all the LayerRegion objects to be combined
  478. my @layerms = map $self->get_layer($_)->get_region($region_id),
  479. ($layer_idx - ($combine{$layer_idx}-1) .. $layer_idx);
  480. # only combine internal infill
  481. for my $type (S_TYPE_INTERNAL) {
  482. # we need to perform a multi-layer intersection, so let's split it in pairs
  483. # initialize the intersection with the candidates of the lowest layer
  484. my $intersection = [ map $_->expolygon, @{$layerms[0]->fill_surfaces->filter_by_type($type)} ];
  485. # start looping from the second layer and intersect the current intersection with it
  486. for my $layerm (@layerms[1 .. $#layerms]) {
  487. $intersection = intersection_ex(
  488. [ map @$_, @$intersection ],
  489. [ map @{$_->expolygon}, @{$layerm->fill_surfaces->filter_by_type($type)} ],
  490. );
  491. }
  492. my $area_threshold = $layerms[0]->infill_area_threshold;
  493. @$intersection = grep $_->area > $area_threshold, @$intersection;
  494. next if !@$intersection;
  495. Slic3r::debugf " combining %d %s regions from layers %d-%d\n",
  496. scalar(@$intersection),
  497. ($type == S_TYPE_INTERNAL ? 'internal' : 'internal-solid'),
  498. $layer_idx-($every-1), $layer_idx;
  499. # $intersection now contains the regions that can be combined across the full amount of layers
  500. # so let's remove those areas from all layers
  501. my @intersection_with_clearance = map @{$_->offset(
  502. $layerms[-1]->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width / 2
  503. + $layerms[-1]->flow(FLOW_ROLE_PERIMETER)->scaled_width / 2
  504. # Because fill areas for rectilinear and honeycomb are grown
  505. # later to overlap perimeters, we need to counteract that too.
  506. + (($type == S_TYPE_INTERNALSOLID || $region->config->fill_pattern =~ /(rectilinear|grid|line|honeycomb)/)
  507. ? $layerms[-1]->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width
  508. : 0)
  509. )}, @$intersection;
  510. foreach my $layerm (@layerms) {
  511. my @this_type = @{$layerm->fill_surfaces->filter_by_type($type)};
  512. my @other_types = map $_->clone, grep $_->surface_type != $type, @{$layerm->fill_surfaces};
  513. my @new_this_type = map Slic3r::Surface->new(expolygon => $_, surface_type => $type),
  514. @{diff_ex(
  515. [ map $_->p, @this_type ],
  516. [ @intersection_with_clearance ],
  517. )};
  518. # apply surfaces back with adjusted depth to the uppermost layer
  519. if ($layerm->layer->id == $self->get_layer($layer_idx)->id) {
  520. push @new_this_type,
  521. map Slic3r::Surface->new(
  522. expolygon => $_,
  523. surface_type => $type,
  524. thickness => sum(map $_->layer->height, @layerms),
  525. thickness_layers => scalar(@layerms),
  526. ),
  527. @$intersection;
  528. } else {
  529. # save void surfaces
  530. push @new_this_type,
  531. map Slic3r::Surface->new(expolygon => $_, surface_type => $voidtype{$type}),
  532. @{intersection_ex(
  533. [ map @{$_->expolygon}, @this_type ],
  534. [ @intersection_with_clearance ],
  535. )};
  536. }
  537. $layerm->fill_surfaces->clear;
  538. $layerm->fill_surfaces->append($_) for (@new_this_type, @other_types);
  539. }
  540. }
  541. }
  542. }
  543. }
  544. # Simplify the sliced model, if "resolution" configuration parameter > 0.
  545. # The simplification is problematic, because it simplifies the slices independent from each other,
  546. # which makes the simplified discretization visible on the object surface.
  547. sub _simplify_slices {
  548. my ($self, $distance) = @_;
  549. foreach my $layer (@{$self->layers}) {
  550. $layer->slices->simplify($distance);
  551. $_->slices->simplify($distance) for @{$layer->regions};
  552. }
  553. }
  554. sub support_material_flow {
  555. my ($self, $role) = @_;
  556. $role //= FLOW_ROLE_SUPPORT_MATERIAL;
  557. my $extruder = ($role == FLOW_ROLE_SUPPORT_MATERIAL)
  558. ? $self->config->support_material_extruder
  559. : $self->config->support_material_interface_extruder;
  560. my $width = $self->config->support_material_extrusion_width || $self->config->extrusion_width;
  561. if ($role == FLOW_ROLE_SUPPORT_MATERIAL_INTERFACE) {
  562. $width = $self->config->support_material_interface_extrusion_width || $width;
  563. }
  564. # we use a bogus layer_height because we use the same flow for all
  565. # support material layers
  566. return Slic3r::Flow->new_from_width(
  567. width => $width,
  568. role => $role,
  569. nozzle_diameter => $self->print->config->nozzle_diameter->[$extruder-1] // $self->print->config->nozzle_diameter->[0],
  570. layer_height => $self->config->layer_height,
  571. bridge_flow_ratio => 0,
  572. );
  573. }
  574. 1;