Print.pm 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. system($^X, $script, $output_file);
  90. } else {
  91. system($script, $output_file);
  92. }
  93. }
  94. }
  95. }
  96. # Export SVG slices for the offline SLA printing.
  97. # The export_svg is expected to be executed inside an eval block.
  98. sub export_svg {
  99. my $self = shift;
  100. my %params = @_;
  101. $_->slice for @{$self->objects};
  102. my $fh = $params{output_fh};
  103. if (!$fh) {
  104. # The following line may die if the output_filename_format template substitution fails.
  105. my $output_file = $self->output_filepath($params{output_file});
  106. $output_file =~ s/\.[gG][cC][oO][dD][eE]$/.svg/;
  107. Slic3r::open(\$fh, ">", $output_file) or die "Failed to open $output_file for writing\n";
  108. print "Exporting to $output_file..." unless $params{quiet};
  109. }
  110. my $print_bb = $self->bounding_box;
  111. my $print_size = $print_bb->size;
  112. print $fh sprintf <<"EOF", unscale($print_size->[X]), unscale($print_size->[Y]);
  113. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  114. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
  115. <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">
  116. <!--
  117. Generated using Slic3r $Slic3r::VERSION
  118. http://slic3r.org/
  119. -->
  120. EOF
  121. my $print_polygon = sub {
  122. my ($polygon, $type) = @_;
  123. printf $fh qq{ <polygon slic3r:type="%s" points="%s" style="fill: %s" />\n},
  124. $type, (join ' ', map { join ',', map unscale $_, @$_ } @$polygon),
  125. ($type eq 'contour' ? 'white' : 'black');
  126. };
  127. my @layers = sort { $a->print_z <=> $b->print_z }
  128. map { @{$_->layers}, @{$_->support_layers} }
  129. @{$self->objects};
  130. my $layer_id = -1;
  131. my @previous_layer_slices = ();
  132. for my $layer (@layers) {
  133. $layer_id++;
  134. if ($layer->slice_z == -1) {
  135. printf $fh qq{ <g id="layer%d">\n}, $layer_id;
  136. } else {
  137. printf $fh qq{ <g id="layer%d" slic3r:z="%s">\n}, $layer_id, unscale($layer->slice_z);
  138. }
  139. my @current_layer_slices = ();
  140. # sort slices so that the outermost ones come first
  141. my @slices = sort { $a->contour->contains_point($b->contour->first_point) ? 0 : 1 } @{$layer->slices};
  142. foreach my $copy (@{$layer->object->_shifted_copies}) {
  143. foreach my $slice (@slices) {
  144. my $expolygon = $slice->clone;
  145. $expolygon->translate(@$copy);
  146. $expolygon->translate(-$print_bb->x_min, -$print_bb->y_min);
  147. $print_polygon->($expolygon->contour, 'contour');
  148. $print_polygon->($_, 'hole') for @{$expolygon->holes};
  149. push @current_layer_slices, $expolygon;
  150. }
  151. }
  152. # generate support material
  153. if ($self->has_support_material && $layer->id > 0) {
  154. my (@supported_slices, @unsupported_slices) = ();
  155. foreach my $expolygon (@current_layer_slices) {
  156. my $intersection = intersection_ex(
  157. [ map @$_, @previous_layer_slices ],
  158. [ @$expolygon ],
  159. );
  160. @$intersection
  161. ? push @supported_slices, $expolygon
  162. : push @unsupported_slices, $expolygon;
  163. }
  164. my @supported_points = map @$_, @$_, @supported_slices;
  165. foreach my $expolygon (@unsupported_slices) {
  166. # look for the nearest point to this island among all
  167. # supported points
  168. my $contour = $expolygon->contour;
  169. my $support_point = $contour->first_point->nearest_point(\@supported_points)
  170. or next;
  171. my $anchor_point = $support_point->nearest_point([ @$contour ]);
  172. printf $fh qq{ <line x1="%s" y1="%s" x2="%s" y2="%s" style="stroke-width: 2; stroke: white" />\n},
  173. map @$_, $support_point, $anchor_point;
  174. }
  175. }
  176. print $fh qq{ </g>\n};
  177. @previous_layer_slices = @current_layer_slices;
  178. }
  179. print $fh "</svg>\n";
  180. close $fh;
  181. print "Done.\n" unless $params{quiet};
  182. }
  183. sub make_skirt {
  184. my $self = shift;
  185. # prerequisites
  186. $_->make_perimeters for @{$self->objects};
  187. $_->infill for @{$self->objects};
  188. $_->generate_support_material for @{$self->objects};
  189. return if $self->step_done(STEP_SKIRT);
  190. $self->set_step_started(STEP_SKIRT);
  191. $self->skirt->clear;
  192. if ($self->has_skirt) {
  193. $self->status_cb->(88, "Generating skirt");
  194. $self->_make_skirt();
  195. }
  196. $self->set_step_done(STEP_SKIRT);
  197. }
  198. sub make_brim {
  199. my $self = shift;
  200. # prerequisites
  201. $_->make_perimeters for @{$self->objects};
  202. $_->infill for @{$self->objects};
  203. $_->generate_support_material for @{$self->objects};
  204. $self->make_skirt;
  205. return if $self->step_done(STEP_BRIM);
  206. $self->set_step_started(STEP_BRIM);
  207. # since this method must be idempotent, we clear brim paths *before*
  208. # checking whether we need to generate them
  209. $self->brim->clear;
  210. if ($self->config->brim_width > 0) {
  211. $self->status_cb->(88, "Generating brim");
  212. $self->_make_brim;
  213. }
  214. $self->set_step_done(STEP_BRIM);
  215. }
  216. sub make_wipe_tower {
  217. my $self = shift;
  218. # prerequisites
  219. $_->make_perimeters for @{$self->objects};
  220. $_->infill for @{$self->objects};
  221. $_->generate_support_material for @{$self->objects};
  222. $self->make_skirt;
  223. $self->make_brim;
  224. return if $self->step_done(STEP_WIPE_TOWER);
  225. $self->set_step_started(STEP_WIPE_TOWER);
  226. $self->_clear_wipe_tower;
  227. if ($self->has_wipe_tower) {
  228. # $self->status_cb->(95, "Generating wipe tower");
  229. $self->_make_wipe_tower;
  230. }
  231. $self->set_step_done(STEP_WIPE_TOWER);
  232. }
  233. 1;