~ubuntu-branches/ubuntu/utopic/slic3r/utopic

« back to all changes in this revision

Viewing changes to utils/pdf-slices.pl

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2014-06-17 01:27:26 UTC
  • Revision ID: package-import@ubuntu.com-20140617012726-2wrs4zdo251nr4vg
Tags: upstream-1.1.4+dfsg
ImportĀ upstreamĀ versionĀ 1.1.4+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
# This script exports model slices to a PDF file as solid fills, one per page
 
3
 
 
4
use strict;
 
5
use warnings;
 
6
 
 
7
BEGIN {
 
8
    use FindBin;
 
9
    use lib "$FindBin::Bin/../lib";
 
10
}
 
11
 
 
12
use Getopt::Long qw(:config no_auto_abbrev);
 
13
use PDF::API2;
 
14
use Slic3r;
 
15
use Slic3r::Geometry qw(scale unscale X Y);
 
16
 
 
17
use constant mm => 25.4 / 72;
 
18
 
 
19
my %opt = ();
 
20
{
 
21
    my %options = (
 
22
        'help'                  => sub { usage() },
 
23
        'output|o=s'            => \$opt{output_file},
 
24
        'layer-height|h=f'      => \$opt{layer_height},
 
25
    );
 
26
    GetOptions(%options) or usage(1);
 
27
    $ARGV[0] or usage(1);
 
28
}
 
29
 
 
30
{
 
31
    # prepare config
 
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]);
 
35
    
 
36
    # read model
 
37
    my $model = Slic3r::Model->read_from_file(my $input_file = $ARGV[0]);
 
38
    
 
39
    # init print object
 
40
    my $sprint = Slic3r::Print::Simple->new;
 
41
    $sprint->apply_config($config);
 
42
    $sprint->set_model($model);
 
43
    my $print = $sprint->_print;
 
44
    
 
45
    # compute sizes
 
46
    my $bb = $print->bounding_box;
 
47
    my $size = $bb->size;
 
48
    my $mediabox = [ map unscale($_)/mm, @{$size} ];
 
49
    
 
50
    # init PDF
 
51
    my $pdf = PDF::API2->new();
 
52
    my $color = $pdf->colorspace_separation('RDG_GLOSS', 'darkblue');
 
53
    
 
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);
 
59
        
 
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);
 
65
        
 
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});  #)
 
70
                $content->close;
 
71
                foreach my $hole (@{$expolygon->holes}) {
 
72
                    $content->poly(map { unscale($_->x)/mm, unscale($_->y)/mm } @$hole);  #)
 
73
                    $content->close;
 
74
                }
 
75
                $content->fill;  # non-zero by default
 
76
            }
 
77
        }
 
78
    }
 
79
    
 
80
    # write output file
 
81
    my $output_file = $opt{output_file};
 
82
    if (!defined $output_file) {
 
83
        $output_file = $input_file;
 
84
        $output_file =~ s/\.(?:stl)$/.pdf/i;
 
85
    }
 
86
    $pdf->saveas($output_file);
 
87
    printf "PDF file written to %s\n", $output_file;
 
88
}
 
89
 
 
90
sub usage {
 
91
    my ($exit_code) = @_;
 
92
    
 
93
    print <<"EOF";
 
94
Usage: pdf-slices.pl [ OPTIONS ] file.stl
 
95
 
 
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
 
99
    
 
100
EOF
 
101
    exit ($exit_code || 0);
 
102
}
 
103
 
 
104
__END__