pdf-slices.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. $config->set('print_center', [0,0]);
  29. # read model
  30. my $model = Slic3r::Model->read_from_file(my $input_file = $ARGV[0]);
  31. # init print object
  32. my $sprint = Slic3r::Print::Simple->new;
  33. $sprint->apply_config($config);
  34. $sprint->set_model($model);
  35. my $print = $sprint->_print;
  36. # compute sizes
  37. my $bb = $print->bounding_box;
  38. my $size = $bb->size;
  39. my $mediabox = [ map unscale($_)/mm, @{$size} ];
  40. # init PDF
  41. my $pdf = PDF::API2->new();
  42. my $color = $pdf->colorspace_separation('RDG_GLOSS', 'darkblue');
  43. # slice and build output geometry
  44. $_->slice for @{$print->objects};
  45. foreach my $object (@{ $print->objects }) {
  46. my $shift = $object->_shifted_copies->[0];
  47. $shift->translate(map $_/2, @$size);
  48. foreach my $layer (@{ $object->layers }) {
  49. my $page = $pdf->page();
  50. $page->mediabox(@$mediabox);
  51. my $content = $page->gfx;
  52. $content->fillcolor($color, 1);
  53. foreach my $expolygon (@{$layer->slices}) {
  54. $expolygon = $expolygon->clone;
  55. $expolygon->translate(@$shift);
  56. $content->poly(map { unscale($_->x)/mm, unscale($_->y)/mm } @{$expolygon->contour}); #)
  57. $content->close;
  58. foreach my $hole (@{$expolygon->holes}) {
  59. $content->poly(map { unscale($_->x)/mm, unscale($_->y)/mm } @$hole); #)
  60. $content->close;
  61. }
  62. $content->fill; # non-zero by default
  63. }
  64. }
  65. }
  66. # write output file
  67. my $output_file = $opt{output_file};
  68. if (!defined $output_file) {
  69. $output_file = $input_file;
  70. $output_file =~ s/\.(?:stl)$/.pdf/i;
  71. }
  72. $pdf->saveas($output_file);
  73. printf "PDF file written to %s\n", $output_file;
  74. }
  75. sub usage {
  76. my ($exit_code) = @_;
  77. print <<"EOF";
  78. Usage: pdf-slices.pl [ OPTIONS ] file.stl
  79. --help Output this usage screen and exit
  80. --output, -o Write to the specified file
  81. --layer-height, -h Use the specified layer height
  82. EOF
  83. exit ($exit_code || 0);
  84. }
  85. __END__