Print.pm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. # The export_gcode may die for various reasons (fails to process output_filename_format,
  58. # write error into the G-code, cannot execute post-processing scripts).
  59. # It is up to the caller to show an error message.
  60. sub export_gcode {
  61. my $self = shift;
  62. my %params = @_;
  63. # prerequisites
  64. $self->process;
  65. # output everything to a G-code file
  66. # The following call may die if the output_filename_format template substitution fails.
  67. my $output_file = $self->output_filepath($params{output_file} // '');
  68. $self->status_cb->(90, "Exporting G-code" . ($output_file ? " to $output_file" : ""));
  69. # The following line may die for multiple reasons.
  70. my $gcode = Slic3r::GCode->new;
  71. if (defined $params{gcode_preview_data}) {
  72. $gcode->do_export_w_preview($self, $output_file, $params{gcode_preview_data});
  73. } else {
  74. $gcode->do_export($self, $output_file);
  75. }
  76. # run post-processing scripts
  77. if (@{$self->config->post_process}) {
  78. $self->status_cb->(95, "Running post-processing scripts");
  79. $self->config->setenv;
  80. for my $script (@{$self->config->post_process}) {
  81. # Ignore empty post processing script lines.
  82. next if $script =~ /^\s*$/;
  83. Slic3r::debugf " '%s' '%s'\n", $script, $output_file;
  84. # -x doesn't return true on Windows except for .exe files
  85. if (($^O eq 'MSWin32') ? !(-e $script) : !(-x $script)) {
  86. die "The configured post-processing script is not executable: check permissions. ($script)\n";
  87. }
  88. if ($^O eq 'MSWin32' && $script =~ /\.[pP][lL]/) {
  89. # The current process (^X) may be slic3r.exe or slic3r-console.exe.
  90. # Replace it with the current perl interpreter.
  91. my($filename, $directories, $suffix) = fileparse($^X);
  92. $filename =~ s/^slic3r.*$/perl5\.24\.0\.exe/;
  93. my $interpreter = $directories . $filename;
  94. system($interpreter, $script, $output_file);
  95. } else {
  96. system($script, $output_file);
  97. }
  98. }
  99. }
  100. }
  101. # Export SVG slices for the offline SLA printing.
  102. # The export_svg is expected to be executed inside an eval block.
  103. sub export_svg {
  104. my $self = shift;
  105. my %params = @_;
  106. $_->slice for @{$self->objects};
  107. my $fh = $params{output_fh};
  108. if (!$fh) {
  109. # The following line may die if the output_filename_format template substitution fails.
  110. my $output_file = $self->output_filepath($params{output_file});
  111. $output_file =~ s/\.[gG][cC][oO][dD][eE]$/.svg/;
  112. Slic3r::open(\$fh, ">", $output_file) or die "Failed to open $output_file for writing\n";
  113. print "Exporting to $output_file..." unless $params{quiet};
  114. }
  115. my $print_bb = $self->bounding_box;
  116. my $print_size = $print_bb->size;
  117. print $fh sprintf <<"EOF", unscale($print_size->[X]), unscale($print_size->[Y]);
  118. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  119. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
  120. <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">
  121. <!--
  122. Generated using Slic3r $Slic3r::VERSION
  123. http://slic3r.org/
  124. -->
  125. EOF
  126. my $print_polygon = sub {
  127. my ($polygon, $type) = @_;
  128. printf $fh qq{ <polygon slic3r:type="%s" points="%s" style="fill: %s" />\n},
  129. $type, (join ' ', map { join ',', map unscale $_, @$_ } @$polygon),
  130. ($type eq 'contour' ? 'white' : 'black');
  131. };
  132. my @layers = sort { $a->print_z <=> $b->print_z }
  133. map { @{$_->layers}, @{$_->support_layers} }
  134. @{$self->objects};
  135. my $layer_id = -1;
  136. my @previous_layer_slices = ();
  137. for my $layer (@layers) {
  138. $layer_id++;
  139. if ($layer->slice_z == -1) {
  140. printf $fh qq{ <g id="layer%d">\n}, $layer_id;
  141. } else {
  142. printf $fh qq{ <g id="layer%d" slic3r:z="%s">\n}, $layer_id, unscale($layer->slice_z);
  143. }
  144. my @current_layer_slices = ();
  145. # sort slices so that the outermost ones come first
  146. my @slices = sort { $a->contour->contains_point($b->contour->first_point) ? 0 : 1 } @{$layer->slices};
  147. foreach my $copy (@{$layer->object->_shifted_copies}) {
  148. foreach my $slice (@slices) {
  149. my $expolygon = $slice->clone;
  150. $expolygon->translate(@$copy);
  151. $expolygon->translate(-$print_bb->x_min, -$print_bb->y_min);
  152. $print_polygon->($expolygon->contour, 'contour');
  153. $print_polygon->($_, 'hole') for @{$expolygon->holes};
  154. push @current_layer_slices, $expolygon;
  155. }
  156. }
  157. # generate support material
  158. if ($self->has_support_material && $layer->id > 0) {
  159. my (@supported_slices, @unsupported_slices) = ();
  160. foreach my $expolygon (@current_layer_slices) {
  161. my $intersection = intersection_ex(
  162. [ map @$_, @previous_layer_slices ],
  163. [ @$expolygon ],
  164. );
  165. @$intersection
  166. ? push @supported_slices, $expolygon
  167. : push @unsupported_slices, $expolygon;
  168. }
  169. my @supported_points = map @$_, @$_, @supported_slices;
  170. foreach my $expolygon (@unsupported_slices) {
  171. # look for the nearest point to this island among all
  172. # supported points
  173. my $contour = $expolygon->contour;
  174. my $support_point = $contour->first_point->nearest_point(\@supported_points)
  175. or next;
  176. my $anchor_point = $support_point->nearest_point([ @$contour ]);
  177. printf $fh qq{ <line x1="%s" y1="%s" x2="%s" y2="%s" style="stroke-width: 2; stroke: white" />\n},
  178. map @$_, $support_point, $anchor_point;
  179. }
  180. }
  181. print $fh qq{ </g>\n};
  182. @previous_layer_slices = @current_layer_slices;
  183. }
  184. print $fh "</svg>\n";
  185. close $fh;
  186. print "Done.\n" unless $params{quiet};
  187. }
  188. sub make_skirt {
  189. my $self = shift;
  190. # prerequisites
  191. $_->make_perimeters for @{$self->objects};
  192. $_->infill for @{$self->objects};
  193. $_->generate_support_material for @{$self->objects};
  194. return if $self->step_done(STEP_SKIRT);
  195. $self->set_step_started(STEP_SKIRT);
  196. $self->skirt->clear;
  197. if ($self->has_skirt) {
  198. $self->status_cb->(88, "Generating skirt");
  199. $self->_make_skirt();
  200. }
  201. $self->set_step_done(STEP_SKIRT);
  202. }
  203. sub make_brim {
  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. return if $self->step_done(STEP_BRIM);
  211. $self->set_step_started(STEP_BRIM);
  212. # since this method must be idempotent, we clear brim paths *before*
  213. # checking whether we need to generate them
  214. $self->brim->clear;
  215. if ($self->config->brim_width > 0) {
  216. $self->status_cb->(88, "Generating brim");
  217. $self->_make_brim;
  218. }
  219. $self->set_step_done(STEP_BRIM);
  220. }
  221. sub make_wipe_tower {
  222. my $self = shift;
  223. # prerequisites
  224. $_->make_perimeters for @{$self->objects};
  225. $_->infill for @{$self->objects};
  226. $_->generate_support_material for @{$self->objects};
  227. $self->make_skirt;
  228. $self->make_brim;
  229. return if $self->step_done(STEP_WIPE_TOWER);
  230. $self->set_step_started(STEP_WIPE_TOWER);
  231. $self->_clear_wipe_tower;
  232. if ($self->has_wipe_tower) {
  233. # $self->status_cb->(95, "Generating wipe tower");
  234. $self->_make_wipe_tower;
  235. }
  236. $self->set_step_done(STEP_WIPE_TOWER);
  237. }
  238. 1;