Object.pm 31 KB

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