2
# This script exports model slices to a PDF file as solid fills, one per page
9
use lib "$FindBin::Bin/../lib";
12
use Getopt::Long qw(:config no_auto_abbrev);
15
use Slic3r::Geometry qw(scale unscale X Y);
17
use constant mm => 25.4 / 72;
22
'help' => sub { usage() },
23
'output|o=s' => \$opt{output_file},
24
'layer-height|h=f' => \$opt{layer_height},
26
GetOptions(%options) or usage(1);
32
my $config = Slic3r::Config->new;
33
$config->set('layer_height', $opt{layer_height}) if $opt{layer_height};
34
$config->set('print_center', [0,0]);
37
my $model = Slic3r::Model->read_from_file(my $input_file = $ARGV[0]);
40
my $sprint = Slic3r::Print::Simple->new;
41
$sprint->apply_config($config);
42
$sprint->set_model($model);
43
my $print = $sprint->_print;
46
my $bb = $print->bounding_box;
48
my $mediabox = [ map unscale($_)/mm, @{$size} ];
51
my $pdf = PDF::API2->new();
52
my $color = $pdf->colorspace_separation('RDG_GLOSS', 'darkblue');
54
# slice and build output geometry
55
$_->slice for @{$print->objects};
56
foreach my $object (@{ $print->objects }) {
57
my $shift = $object->_shifted_copies->[0];
58
$shift->translate(map $_/2, @$size);
60
foreach my $layer (@{ $object->layers }) {
61
my $page = $pdf->page();
62
$page->mediabox(@$mediabox);
63
my $content = $page->gfx;
64
$content->fillcolor($color, 1);
66
foreach my $expolygon (@{$layer->slices}) {
67
$expolygon = $expolygon->clone;
68
$expolygon->translate(@$shift);
69
$content->poly(map { unscale($_->x)/mm, unscale($_->y)/mm } @{$expolygon->contour}); #)
71
foreach my $hole (@{$expolygon->holes}) {
72
$content->poly(map { unscale($_->x)/mm, unscale($_->y)/mm } @$hole); #)
75
$content->fill; # non-zero by default
81
my $output_file = $opt{output_file};
82
if (!defined $output_file) {
83
$output_file = $input_file;
84
$output_file =~ s/\.(?:stl)$/.pdf/i;
86
$pdf->saveas($output_file);
87
printf "PDF file written to %s\n", $output_file;
94
Usage: pdf-slices.pl [ OPTIONS ] file.stl
96
--help Output this usage screen and exit
97
--output, -o Write to the specified file
98
--layer-height, -h Use the specified layer height
101
exit ($exit_code || 0);