pdf-slices.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/perl
  2. # This script exports model slices to a PDF file as solid fills, one per page
  3. use strict;
  4. use warnings;
  5. BEGIN {
  6. use FindBin;
  7. use lib "$FindBin::Bin/../lib";
  8. }
  9. use Getopt::Long qw(:config no_auto_abbrev);
  10. use PDF::API2;
  11. use Slic3r;
  12. use Slic3r::Geometry qw(scale unscale X Y);
  13. use constant mm => 25.4 / 72;
  14. my %opt = ();
  15. {
  16. my %options = (
  17. 'help' => sub { usage() },
  18. 'output|o=s' => \$opt{output_file},
  19. 'layer-height|h=f' => \$opt{layer_height},
  20. );
  21. GetOptions(%options) or usage(1);
  22. $ARGV[0] or usage(1);
  23. }
  24. {
  25. # prepare config
  26. my $config = Slic3r::Config->new;
  27. $config->set('layer_height', $opt{layer_height}) if $opt{layer_height};
  28. # read model
  29. my $model = Slic3r::Model->read_from_file(my $input_file = $ARGV[0]);
  30. # init print object
  31. my $sprint = Slic3r::Print::Simple->new(
  32. print_center => [0,0],
  33. );
  34. $sprint->apply_config($config);
  35. $sprint->set_model($model);
  36. my $print = $sprint->_print;
  37. # compute sizes
  38. my $bb = $print->bounding_box;
  39. my $size = $bb->size;
  40. my $mediabox = [ map unscale($_)/mm, @{$size} ];
  41. # init PDF
  42. my $pdf = PDF::API2->new();
  43. my $color = $pdf->colorspace_separation('RDG_GLOSS', 'darkblue');
  44. # slice and build output geometry
  45. $_->slice for @{$print->objects};
  46. foreach my $object (@{ $print->objects }) {
  47. my $shift = $object->_shifted_copies->[0];
  48. $shift->translate(map $_/2, @$size);
  49. foreach my $layer (@{ $object->layers }) {
  50. my $page = $pdf->page();
  51. $page->mediabox(@$mediabox);
  52. my $content = $page->gfx;
  53. $content->fillcolor($color, 1);
  54. foreach my $expolygon (@{$layer->slices}) {
  55. $expolygon = $expolygon->clone;
  56. $expolygon->translate(@$shift);
  57. $content->poly(map { unscale($_->x)/mm, unscale($_->y)/mm } @{$expolygon->contour}); #)
  58. $content->close;
  59. foreach my $hole (@{$expolygon->holes}) {
  60. $content->poly(map { unscale($_->x)/mm, unscale($_->y)/mm } @$hole); #)
  61. $content->close;
  62. }
  63. $content->fill; # non-zero by default
  64. }
  65. }
  66. }
  67. # write output file
  68. my $output_file = $opt{output_file};
  69. if (!defined $output_file) {
  70. $output_file = $input_file;
  71. $output_file =~ s/\.(?:stl)$/.pdf/i;
  72. }
  73. $pdf->saveas($output_file);
  74. printf "PDF file written to %s\n", $output_file;
  75. }
  76. sub usage {
  77. my ($exit_code) = @_;
  78. print <<"EOF";
  79. Usage: pdf-slices.pl [ OPTIONS ] file.stl
  80. --help Output this usage screen and exit
  81. --output, -o Write to the specified file
  82. --layer-height, -h Use the specified layer height
  83. EOF
  84. exit ($exit_code || 0);
  85. }
  86. __END__