Print.pm 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # The slicing work horse.
  2. # Extends C++ class Slic3r::Print
  3. package Slic3r::Print;
  4. use strict;
  5. use warnings;
  6. use File::Basename qw(basename fileparse);
  7. use File::Spec;
  8. use List::Util qw(min max first sum);
  9. use Slic3r::ExtrusionLoop ':roles';
  10. use Slic3r::ExtrusionPath ':roles';
  11. use Slic3r::Flow ':roles';
  12. use Slic3r::Geometry qw(X Y unscale);
  13. use Slic3r::Geometry::Clipper qw(diff_ex union_ex intersection_ex intersection offset
  14. union JT_ROUND JT_SQUARE);
  15. use Slic3r::Print::State ':steps';
  16. our $status_cb;
  17. sub set_status_cb {
  18. my ($class, $cb) = @_;
  19. $status_cb = $cb;
  20. }
  21. sub status_cb {
  22. return $status_cb // sub {};
  23. }
  24. sub size {
  25. my $self = shift;
  26. return $self->bounding_box->size;
  27. }
  28. # Slicing process, running at a background thread.
  29. sub process {
  30. my ($self) = @_;
  31. Slic3r::trace(3, "Staring the slicing process.");
  32. $_->make_perimeters for @{$self->objects};
  33. $self->status_cb->(70, "Infilling layers");
  34. $_->infill for @{$self->objects};
  35. $_->generate_support_material for @{$self->objects};
  36. $self->make_skirt;
  37. $self->make_brim; # must come after make_skirt
  38. $self->make_wipe_tower;
  39. # time to make some statistics
  40. if (0) {
  41. eval "use Devel::Size";
  42. print "MEMORY USAGE:\n";
  43. printf " meshes = %.1fMb\n", List::Util::sum(map Devel::Size::total_size($_->meshes), @{$self->objects})/1024/1024;
  44. printf " layer slices = %.1fMb\n", List::Util::sum(map Devel::Size::total_size($_->slices), map @{$_->layers}, @{$self->objects})/1024/1024;
  45. printf " region slices = %.1fMb\n", List::Util::sum(map Devel::Size::total_size($_->slices), map @{$_->regions}, map @{$_->layers}, @{$self->objects})/1024/1024;
  46. printf " perimeters = %.1fMb\n", List::Util::sum(map Devel::Size::total_size($_->perimeters), map @{$_->regions}, map @{$_->layers}, @{$self->objects})/1024/1024;
  47. printf " fills = %.1fMb\n", List::Util::sum(map Devel::Size::total_size($_->fills), map @{$_->regions}, map @{$_->layers}, @{$self->objects})/1024/1024;
  48. printf " print object = %.1fMb\n", Devel::Size::total_size($self)/1024/1024;
  49. }
  50. if (0) {
  51. eval "use Slic3r::Test::SectionCut";
  52. Slic3r::Test::SectionCut->new(print => $self)->export_svg("section_cut.svg");
  53. }
  54. Slic3r::trace(3, "Slicing process finished.")
  55. }
  56. # G-code export process, running at a background thread.
  57. sub export_gcode {
  58. my $self = shift;
  59. my %params = @_;
  60. # prerequisites
  61. $self->process;
  62. # output everything to a G-code file
  63. my $output_file = $self->output_filepath($params{output_file} // '');
  64. $self->status_cb->(90, "Exporting G-code" . ($output_file ? " to $output_file" : ""));
  65. die "G-code export to " . $output_file . " failed\n"
  66. if ! Slic3r::GCode->new->do_export($self, $output_file);
  67. # run post-processing scripts
  68. if (@{$self->config->post_process}) {
  69. $self->status_cb->(95, "Running post-processing scripts");
  70. $self->config->setenv;
  71. for my $script (@{$self->config->post_process}) {
  72. Slic3r::debugf " '%s' '%s'\n", $script, $output_file;
  73. # -x doesn't return true on Windows except for .exe files
  74. if (($^O eq 'MSWin32') ? !(-e $script) : !(-x $script)) {
  75. die "The configured post-processing script is not executable: check permissions. ($script)\n";
  76. }
  77. if ($^O eq 'MSWin32' && $script =~ /\.[pP][lL]/) {
  78. system($^X, $script, $output_file);
  79. } else {
  80. system($script, $output_file);
  81. }
  82. }
  83. }
  84. }
  85. # Export SVG slices for the offline SLA printing.
  86. sub export_svg {
  87. my $self = shift;
  88. my %params = @_;
  89. $_->slice for @{$self->objects};
  90. my $fh = $params{output_fh};
  91. if (!$fh) {
  92. my $output_file = $self->output_filepath($params{output_file});
  93. $output_file =~ s/\.[gG][cC][oO][dD][eE]$/.svg/;
  94. Slic3r::open(\$fh, ">", $output_file) or die "Failed to open $output_file for writing\n";
  95. print "Exporting to $output_file..." unless $params{quiet};
  96. }
  97. my $print_bb = $self->bounding_box;
  98. my $print_size = $print_bb->size;
  99. print $fh sprintf <<"EOF", unscale($print_size->[X]), unscale($print_size->[Y]);
  100. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  101. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
  102. <svg width="%s" height="%s" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:slic3r="http://slic3r.org/namespaces/slic3r">
  103. <!--
  104. Generated using Slic3r $Slic3r::VERSION
  105. http://slic3r.org/
  106. -->
  107. EOF
  108. my $print_polygon = sub {
  109. my ($polygon, $type) = @_;
  110. printf $fh qq{ <polygon slic3r:type="%s" points="%s" style="fill: %s" />\n},
  111. $type, (join ' ', map { join ',', map unscale $_, @$_ } @$polygon),
  112. ($type eq 'contour' ? 'white' : 'black');
  113. };
  114. my @layers = sort { $a->print_z <=> $b->print_z }
  115. map { @{$_->layers}, @{$_->support_layers} }
  116. @{$self->objects};
  117. my $layer_id = -1;
  118. my @previous_layer_slices = ();
  119. for my $layer (@layers) {
  120. $layer_id++;
  121. if ($layer->slice_z == -1) {
  122. printf $fh qq{ <g id="layer%d">\n}, $layer_id;
  123. } else {
  124. printf $fh qq{ <g id="layer%d" slic3r:z="%s">\n}, $layer_id, unscale($layer->slice_z);
  125. }
  126. my @current_layer_slices = ();
  127. # sort slices so that the outermost ones come first
  128. my @slices = sort { $a->contour->contains_point($b->contour->first_point) ? 0 : 1 } @{$layer->slices};
  129. foreach my $copy (@{$layer->object->_shifted_copies}) {
  130. foreach my $slice (@slices) {
  131. my $expolygon = $slice->clone;
  132. $expolygon->translate(@$copy);
  133. $expolygon->translate(-$print_bb->x_min, -$print_bb->y_min);
  134. $print_polygon->($expolygon->contour, 'contour');
  135. $print_polygon->($_, 'hole') for @{$expolygon->holes};
  136. push @current_layer_slices, $expolygon;
  137. }
  138. }
  139. # generate support material
  140. if ($self->has_support_material && $layer->id > 0) {
  141. my (@supported_slices, @unsupported_slices) = ();
  142. foreach my $expolygon (@current_layer_slices) {
  143. my $intersection = intersection_ex(
  144. [ map @$_, @previous_layer_slices ],
  145. [ @$expolygon ],
  146. );
  147. @$intersection
  148. ? push @supported_slices, $expolygon
  149. : push @unsupported_slices, $expolygon;
  150. }
  151. my @supported_points = map @$_, @$_, @supported_slices;
  152. foreach my $expolygon (@unsupported_slices) {
  153. # look for the nearest point to this island among all
  154. # supported points
  155. my $contour = $expolygon->contour;
  156. my $support_point = $contour->first_point->nearest_point(\@supported_points)
  157. or next;
  158. my $anchor_point = $support_point->nearest_point([ @$contour ]);
  159. printf $fh qq{ <line x1="%s" y1="%s" x2="%s" y2="%s" style="stroke-width: 2; stroke: white" />\n},
  160. map @$_, $support_point, $anchor_point;
  161. }
  162. }
  163. print $fh qq{ </g>\n};
  164. @previous_layer_slices = @current_layer_slices;
  165. }
  166. print $fh "</svg>\n";
  167. close $fh;
  168. print "Done.\n" unless $params{quiet};
  169. }
  170. sub make_skirt {
  171. my $self = shift;
  172. # prerequisites
  173. $_->make_perimeters for @{$self->objects};
  174. $_->infill for @{$self->objects};
  175. $_->generate_support_material for @{$self->objects};
  176. return if $self->step_done(STEP_SKIRT);
  177. $self->set_step_started(STEP_SKIRT);
  178. $self->skirt->clear;
  179. if ($self->has_skirt) {
  180. $self->status_cb->(88, "Generating skirt");
  181. $self->_make_skirt();
  182. }
  183. $self->set_step_done(STEP_SKIRT);
  184. }
  185. sub make_brim {
  186. my $self = shift;
  187. # prerequisites
  188. $_->make_perimeters for @{$self->objects};
  189. $_->infill for @{$self->objects};
  190. $_->generate_support_material for @{$self->objects};
  191. $self->make_skirt;
  192. return if $self->step_done(STEP_BRIM);
  193. $self->set_step_started(STEP_BRIM);
  194. # since this method must be idempotent, we clear brim paths *before*
  195. # checking whether we need to generate them
  196. $self->brim->clear;
  197. if ($self->config->brim_width > 0) {
  198. $self->status_cb->(88, "Generating brim");
  199. $self->_make_brim;
  200. }
  201. $self->set_step_done(STEP_BRIM);
  202. }
  203. sub make_wipe_tower {
  204. my $self = shift;
  205. # prerequisites
  206. $_->make_perimeters for @{$self->objects};
  207. $_->infill for @{$self->objects};
  208. $_->generate_support_material for @{$self->objects};
  209. $self->make_skirt;
  210. $self->make_brim;
  211. return if $self->step_done(STEP_WIPE_TOWER);
  212. $self->set_step_started(STEP_WIPE_TOWER);
  213. $self->_clear_wipe_tower;
  214. if ($self->has_wipe_tower) {
  215. # $self->status_cb->(95, "Generating wipe tower");
  216. $self->_make_wipe_tower;
  217. }
  218. $self->set_step_done(STEP_WIPE_TOWER);
  219. }
  220. 1;