Print.pm 9.6 KB

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