From f9661b02a638a0146a46015871bbc20e8ae7667e Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 11 Apr 2014 19:55:15 +0200 Subject: [PATCH] Bugfix in utils/pdf-slices.pl, wrong shift caused slices to be outside the media box --- lib/Slic3r/Model.pm | 11 +++++++++++ utils/pdf-slices.pl | 40 ++++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm index efd331f1f..d4faaf508 100644 --- a/lib/Slic3r/Model.pm +++ b/lib/Slic3r/Model.pm @@ -218,6 +218,17 @@ sub center_instances_around_point { } } +sub align_instances_to_origin { + my ($self) = @_; + + my $bb = $self->bounding_box; + return if !defined $bb; + + my $new_center = $bb->size; + $new_center->translate(-$new_center->x/2, -$new_center->y/2); #// + $self->center_instances_around_point($new_center); +} + sub translate { my $self = shift; my @shift = @_; diff --git a/utils/pdf-slices.pl b/utils/pdf-slices.pl index 7bace695e..3931dceac 100755 --- a/utils/pdf-slices.pl +++ b/utils/pdf-slices.pl @@ -12,7 +12,7 @@ BEGIN { use Getopt::Long qw(:config no_auto_abbrev); use PDF::API2; use Slic3r; -use Slic3r::Geometry qw(unscale X Y); +use Slic3r::Geometry qw(scale unscale X Y); use constant mm => 25.4 / 72; @@ -31,16 +31,21 @@ my %opt = (); # prepare config my $config = Slic3r::Config->new; $config->set('layer_height', $opt{layer_height}) if $opt{layer_height}; + $config->set('print_center', [0,0]); + + # read model + my $model = Slic3r::Model->read_from_file(my $input_file = $ARGV[0]); # init print object my $sprint = Slic3r::Print::Simple->new; $sprint->apply_config($config); - $sprint->set_model(Slic3r::Model->read_from_file(my $input_file = $ARGV[0])); + $sprint->set_model($model); my $print = $sprint->_print; # compute sizes my $bb = $print->bounding_box; - my $mediabox = [ map unscale($_)/mm, @{$bb->size} ]; + my $size = $bb->size; + my $mediabox = [ map unscale($_)/mm, @{$size} ]; # init PDF my $pdf = PDF::API2->new(); @@ -48,20 +53,27 @@ my %opt = (); # slice and build output geometry $_->slice for @{$print->objects}; - foreach my $layer (@{ $print->objects->[0]->layers }) { - my $page = $pdf->page(); - $page->mediabox(@$mediabox); - my $content = $page->gfx; - $content->fillcolor($color, 1); + foreach my $object (@{ $print->objects }) { + my $shift = $object->_shifted_copies->[0]; + $shift->translate(map $_/2, @$size); - foreach my $expolygon (@{$layer->slices}) { - $content->poly(map { unscale($_->x)/mm, unscale($_->y)/mm } @{$expolygon->contour}); #) - $content->close; - foreach my $hole (@{$expolygon->holes}) { - $content->poly(map { unscale($_->x)/mm, unscale($_->y)/mm } @$hole); #) + foreach my $layer (@{ $object->layers }) { + my $page = $pdf->page(); + $page->mediabox(@$mediabox); + my $content = $page->gfx; + $content->fillcolor($color, 1); + + foreach my $expolygon (@{$layer->slices}) { + $expolygon = $expolygon->clone; + $expolygon->translate(@$shift); + $content->poly(map { unscale($_->x)/mm, unscale($_->y)/mm } @{$expolygon->contour}); #) $content->close; + foreach my $hole (@{$expolygon->holes}) { + $content->poly(map { unscale($_->x)/mm, unscale($_->y)/mm } @$hole); #) + $content->close; + } + $content->fill; # non-zero by default } - $content->fill; # non-zero by default } }