pdf-slices.pl 2.9 KB

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