Restore functionality of Test::SectionCut

This commit is contained in:
Alessandro Ranellucci 2014-12-27 13:04:28 +01:00
parent b5b0df2426
commit bbb47e087a

View file

@ -1,72 +1,66 @@
package Slic3r::Test::SectionCut; package Slic3r::Test::SectionCut;
use Moo; use Moo;
use List::Util qw(first max); use List::Util qw(first min max);
use Slic3r::Geometry qw(X Y A B X1 Y1 X2 Y2 unscale); use Slic3r::Geometry qw(unscale);
use Slic3r::Geometry::Clipper qw(union_ex intersection_pl); use Slic3r::Geometry::Clipper qw(intersection_pl);
use SVG; use SVG;
use Slic3r::SVG; use Slic3r::SVG;
has 'scale' => (is => 'ro', default => sub {30}); has 'print' => (is => 'ro', required => 1);
has 'print' => (is => 'ro', required => 1); has 'scale' => (is => 'ro', default => sub { 30 });
has 'y_percent' => (is => 'ro', default => sub {0.5}); has 'y_percent' => (is => 'ro', default => sub { 0.5 }); # Y coord of section line expressed as factor
has 'line' => (is => 'rw'); has 'line' => (is => 'rw');
has 'height' => (is => 'rw'); has '_height' => (is => 'rw');
has '_svg' => (is => 'rw');
has '_svg_style' => (is => 'rw', default => sub { {} });
sub BUILD { sub BUILD {
my $self = shift; my $self = shift;
# calculate the Y coordinate of the section line
my $bb = $self->print->bounding_box; my $bb = $self->print->bounding_box;
my $y = ($bb->y_min + $bb->y_max) * $self->y_percent; my $y = ($bb->y_min + $bb->y_max) * $self->y_percent;
my $line = Slic3r::Line->new([ $bb->x_min, $y ], [ $bb->x_max, $y ]);
$self->line($line); # store our section line
$self->line(Slic3r::Line->new([ $bb->x_min, $y ], [ $bb->x_max, $y ]));
} }
sub export_svg { sub export_svg {
my $self = shift; my $self = shift;
my ($filename) = @_; my ($filename) = @_;
my $print_size = $self->print->size; # get bounding box of print and its height
$self->height(max(map $_->print_z, map @{$_->layers}, @{$self->print->objects})); # (Print should return a BoundingBox3 object instead)
my $svg = SVG->new( my $bb = $self->print->bounding_box;
width => $self->scale * unscale($print_size->[X]), my $print_size = $bb->size;
height => $self->scale * $self->height, $self->_height(max(map $_->print_z, map @{$_->layers}, @{$self->print->objects}));
);
my $group = sub { # initialize the SVG canvas
my %p = @_; $self->_svg(my $svg = SVG->new(
my $g = $svg->group(style => $p{style}); width => $self->scale * unscale($print_size->x),
my $items = $self->_plot($p{filter}); height => $self->scale * $self->_height,
$g->rectangle(%$_) for @{ $items->{rectangles} }; ));
$g->circle(%$_) for @{ $items->{circles} };
};
$group->( # set default styles
filter => sub { map @{$_->perimeters}, @{$_[0]->regions} }, $self->_svg_style->{'stroke-width'} = 1;
style => { $self->_svg_style->{'fill-opacity'} = 0.5;
'stroke-width' => 1, $self->_svg_style->{'stroke-opacity'} = 0.2;
'stroke' => 'grey',
'fill' => 'red',
},
);
$group->( # plot perimeters
filter => sub { map @{$_->fills}, @{$_[0]->regions} }, $self->_svg_style->{'stroke'} = '#EE0000';
style => { $self->_svg_style->{'fill'} = '#FF0000';
'stroke-width' => 1, $self->_plot_group(sub { map @{$_->perimeters}, @{$_[0]->regions} });
'stroke' => '#444444',
'fill' => 'grey',
},
);
$group->( # plot infill
filter => sub { $_[0]->isa('Slic3r::Layer::Support') ? ($_[0]->support_fills, $_[0]->support_interface_fills) : () }, $self->_svg_style->{'stroke'} = '#444444';
style => { $self->_svg_style->{'fill'} = '#454545';
'stroke-width' => 1, $self->_plot_group(sub { map @{$_->fills}, @{$_[0]->regions} });
'stroke' => '#444444',
'fill' => '#22FF00', # plot support material
}, $self->_svg_style->{'stroke'} = '#12EF00';
); $self->_svg_style->{'fill'} = '#22FF00';
$self->_plot_group(sub { $_[0]->isa('Slic3r::Layer::Support') ? ($_[0]->support_fills, $_[0]->support_interface_fills) : () });
Slic3r::open(\my $fh, '>', $filename); Slic3r::open(\my $fh, '>', $filename);
print $fh $svg->xmlify; print $fh $svg->xmlify;
@ -74,11 +68,12 @@ sub export_svg {
printf "Section cut SVG written to %s\n", $filename; printf "Section cut SVG written to %s\n", $filename;
} }
sub _plot { sub _plot_group {
my $self = shift; my $self = shift;
my ($filter) = @_; my ($filter) = @_;
my (@rectangles, @circles) = (); my $bb = $self->print->bounding_box;
my $g = $self->_svg->group(style => { %{$self->_svg_style} });
foreach my $object (@{$self->print->objects}) { foreach my $object (@{$self->print->objects}) {
foreach my $copy (@{$object->_shifted_copies}) { foreach my $copy (@{$object->_shifted_copies}) {
@ -89,17 +84,17 @@ sub _plot {
grep defined $_, grep defined $_,
$filter->($layer); $filter->($layer);
# move paths to location of copy
$_->polyline->translate(@$copy) for @paths; $_->polyline->translate(@$copy) for @paths;
if (0) { if (0) {
# export plan with section line and exit
require "Slic3r/SVG.pm"; require "Slic3r/SVG.pm";
Slic3r::SVG::output( Slic3r::SVG::output(
"line.svg", "line.svg",
no_arrows => 1, no_arrows => 1,
#polygon => $line->grow(Slic3r::Geometry::scale $path->width/2), lines => [ $self->line ],
polygons => [ $object->bounding_box->polygon ], red_polylines => [ map $_->polyline, @paths ],
lines => [ $self->line ],
red_polylines => [ map $_->polyline, @paths ],
); );
exit; exit;
} }
@ -111,61 +106,69 @@ sub _plot {
$line->grow(Slic3r::Geometry::scale $path->width/2), $line->grow(Slic3r::Geometry::scale $path->width/2),
)}; )};
die "Intersection has more than two points!\n" if first { @$_ > 2 } @intersections; die "Intersection has more than two points!\n"
if defined first { @$_ > 2 } @intersections;
# turn intersections to lines
my @lines = map Slic3r::Line->new(@$_), @intersections;
# align intersections to canvas
$_->translate(-$bb->x_min, 0) for @lines;
# we want lines oriented from left to right in order to draw
# rectangles correctly
foreach my $line (@lines) {
$line->reverse if $line->a->x > $line->b->x;
}
if ($path->is_bridge) { if ($path->is_bridge) {
foreach my $line (@intersections) { foreach my $line (@lines) {
my $radius = $path->width / 2; my $radius = $path->width / 2;
my $width = unscale abs($line->[B][X] - $line->[A][X]); my $width = unscale abs($line->b->x - $line->a->x);
if ((10 * Slic3r::Geometry::scale $radius) < $width) { if ((10 * $radius) < $width) {
# we're cutting the path in the longitudinal direction, so we've got a rectangle # we're cutting the path in the longitudinal direction, so we've got a rectangle
push @rectangles, { $g->rectangle(
'x' => $self->scale * unscale $line->[A][X], 'x' => $self->scale * unscale($line->a->x),
'y' => $self->scale * $self->_y($layer->print_z), 'y' => $self->scale * $self->_y($layer->print_z),
'width' => $self->scale * $width, 'width' => $self->scale * $width,
'height' => $self->scale * $radius * 2, 'height' => $self->scale * $radius * 2,
'rx' => $self->scale * $radius * 0.35, 'rx' => $self->scale * $radius * 0.35,
'ry' => $self->scale * $radius * 0.35, 'ry' => $self->scale * $radius * 0.35,
}; );
} else { } else {
push @circles, { $g->circle(
'cx' => $self->scale * (unscale($line->[A][X]) + $radius), 'cx' => $self->scale * (unscale($line->a->x) + $radius),
'cy' => $self->scale * $self->_y($layer->print_z - $radius), 'cy' => $self->scale * $self->_y($layer->print_z - $radius),
'r' => $self->scale * $radius, 'r' => $self->scale * $radius,
}; );
} }
} }
} else { } else {
push @rectangles, map { foreach my $line (@lines) {
my $height = $path->height; my $height = $path->height;
$height = $layer->height if $height == -1; $height = $layer->height if $height == -1;
{ $g->rectangle(
'x' => $self->scale * unscale $_->[A][X], 'x' => $self->scale * unscale($line->a->x),
'y' => $self->scale * $self->_y($layer->print_z), 'y' => $self->scale * $self->_y($layer->print_z),
'width' => $self->scale * unscale(abs($_->[B][X] - $_->[A][X])), 'width' => $self->scale * unscale($line->b->x - $line->a->x),
'height' => $self->scale * $height, 'height' => $self->scale * $height,
'rx' => $self->scale * $height * 0.35, 'rx' => $self->scale * $height * 0.5,
'ry' => $self->scale * $height * 0.35, 'ry' => $self->scale * $height * 0.5,
}; );
} @intersections; }
} }
} }
} }
} }
} }
} }
return {
rectangles => \@rectangles,
circles => \@circles,
};
} }
sub _y { sub _y {
my $self = shift; my $self = shift;
my ($y) = @_; my ($y) = @_;
return $y;
return $self->height - $y; return $self->_height - $y;
} }
1; 1;