2013-04-27 13:02:13 +00:00
|
|
|
|
package Slic3r::Test::SectionCut;
|
|
|
|
|
use Moo;
|
|
|
|
|
|
2014-12-27 12:04:28 +00:00
|
|
|
|
use List::Util qw(first min max);
|
|
|
|
|
use Slic3r::Geometry qw(unscale);
|
|
|
|
|
use Slic3r::Geometry::Clipper qw(intersection_pl);
|
2013-04-27 13:02:13 +00:00
|
|
|
|
use SVG;
|
2014-12-27 12:04:28 +00:00
|
|
|
|
use Slic3r::SVG;
|
2013-04-27 13:02:13 +00:00
|
|
|
|
|
2014-12-27 12:04:28 +00:00
|
|
|
|
has 'print' => (is => 'ro', required => 1);
|
|
|
|
|
has 'scale' => (is => 'ro', default => sub { 30 });
|
|
|
|
|
has 'y_percent' => (is => 'ro', default => sub { 0.5 }); # Y coord of section line expressed as factor
|
|
|
|
|
has 'line' => (is => 'rw');
|
|
|
|
|
has '_height' => (is => 'rw');
|
|
|
|
|
has '_svg' => (is => 'rw');
|
|
|
|
|
has '_svg_style' => (is => 'rw', default => sub { {} });
|
2013-04-27 13:02:13 +00:00
|
|
|
|
|
2014-04-29 21:15:36 +00:00
|
|
|
|
sub BUILD {
|
2013-04-27 13:02:13 +00:00
|
|
|
|
my $self = shift;
|
|
|
|
|
|
2014-12-27 12:04:28 +00:00
|
|
|
|
# calculate the Y coordinate of the section line
|
2013-06-16 10:21:25 +00:00
|
|
|
|
my $bb = $self->print->bounding_box;
|
2014-04-29 21:15:36 +00:00
|
|
|
|
my $y = ($bb->y_min + $bb->y_max) * $self->y_percent;
|
2014-12-27 12:04:28 +00:00
|
|
|
|
|
|
|
|
|
# store our section line
|
|
|
|
|
$self->line(Slic3r::Line->new([ $bb->x_min, $y ], [ $bb->x_max, $y ]));
|
2013-04-27 13:02:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub export_svg {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
my ($filename) = @_;
|
|
|
|
|
|
2014-12-27 12:04:28 +00:00
|
|
|
|
# get bounding box of print and its height
|
|
|
|
|
# (Print should return a BoundingBox3 object instead)
|
|
|
|
|
my $bb = $self->print->bounding_box;
|
|
|
|
|
my $print_size = $bb->size;
|
|
|
|
|
$self->_height(max(map $_->print_z, map @{$_->layers}, @{$self->print->objects}));
|
|
|
|
|
|
|
|
|
|
# initialize the SVG canvas
|
|
|
|
|
$self->_svg(my $svg = SVG->new(
|
|
|
|
|
width => $self->scale * unscale($print_size->x),
|
|
|
|
|
height => $self->scale * $self->_height,
|
|
|
|
|
));
|
2013-04-27 13:02:13 +00:00
|
|
|
|
|
2014-12-27 12:04:28 +00:00
|
|
|
|
# set default styles
|
|
|
|
|
$self->_svg_style->{'stroke-width'} = 1;
|
|
|
|
|
$self->_svg_style->{'fill-opacity'} = 0.5;
|
|
|
|
|
$self->_svg_style->{'stroke-opacity'} = 0.2;
|
2013-04-27 13:02:13 +00:00
|
|
|
|
|
2014-12-27 12:04:28 +00:00
|
|
|
|
# plot perimeters
|
|
|
|
|
$self->_svg_style->{'stroke'} = '#EE0000';
|
|
|
|
|
$self->_svg_style->{'fill'} = '#FF0000';
|
|
|
|
|
$self->_plot_group(sub { map @{$_->perimeters}, @{$_[0]->regions} });
|
2013-04-27 13:02:13 +00:00
|
|
|
|
|
2014-12-27 12:04:28 +00:00
|
|
|
|
# plot infill
|
|
|
|
|
$self->_svg_style->{'stroke'} = '#444444';
|
|
|
|
|
$self->_svg_style->{'fill'} = '#454545';
|
|
|
|
|
$self->_plot_group(sub { map @{$_->fills}, @{$_[0]->regions} });
|
2013-04-27 13:02:13 +00:00
|
|
|
|
|
2014-12-27 12:04:28 +00:00
|
|
|
|
# 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) : () });
|
2013-04-27 13:02:13 +00:00
|
|
|
|
|
|
|
|
|
Slic3r::open(\my $fh, '>', $filename);
|
|
|
|
|
print $fh $svg->xmlify;
|
|
|
|
|
close $fh;
|
|
|
|
|
printf "Section cut SVG written to %s\n", $filename;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-27 12:04:28 +00:00
|
|
|
|
sub _plot_group {
|
2013-04-27 13:02:13 +00:00
|
|
|
|
my $self = shift;
|
|
|
|
|
my ($filter) = @_;
|
|
|
|
|
|
2014-12-27 12:04:28 +00:00
|
|
|
|
my $bb = $self->print->bounding_box;
|
|
|
|
|
my $g = $self->_svg->group(style => { %{$self->_svg_style} });
|
2013-04-27 13:02:13 +00:00
|
|
|
|
|
|
|
|
|
foreach my $object (@{$self->print->objects}) {
|
2014-04-29 21:15:36 +00:00
|
|
|
|
foreach my $copy (@{$object->_shifted_copies}) {
|
2013-07-29 18:49:54 +00:00
|
|
|
|
foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
|
2013-04-27 13:02:13 +00:00
|
|
|
|
# get all ExtrusionPath objects
|
2014-12-26 17:57:21 +00:00
|
|
|
|
my @paths = map $_->clone,
|
|
|
|
|
map { ($_->isa('Slic3r::ExtrusionLoop') || $_->isa('Slic3r::ExtrusionPath::Collection')) ? @$_ : $_ }
|
2013-04-27 13:02:13 +00:00
|
|
|
|
grep defined $_,
|
|
|
|
|
$filter->($layer);
|
|
|
|
|
|
2014-12-27 12:04:28 +00:00
|
|
|
|
# move paths to location of copy
|
2014-04-29 21:15:36 +00:00
|
|
|
|
$_->polyline->translate(@$copy) for @paths;
|
|
|
|
|
|
2014-12-26 17:57:21 +00:00
|
|
|
|
if (0) {
|
2014-12-27 12:04:28 +00:00
|
|
|
|
# export plan with section line and exit
|
2014-12-26 17:57:21 +00:00
|
|
|
|
require "Slic3r/SVG.pm";
|
|
|
|
|
Slic3r::SVG::output(
|
|
|
|
|
"line.svg",
|
2014-12-27 12:04:28 +00:00
|
|
|
|
no_arrows => 1,
|
|
|
|
|
lines => [ $self->line ],
|
|
|
|
|
red_polylines => [ map $_->polyline, @paths ],
|
2014-12-26 17:57:21 +00:00
|
|
|
|
);
|
|
|
|
|
exit;
|
|
|
|
|
}
|
2014-04-29 21:15:36 +00:00
|
|
|
|
|
2013-04-27 13:02:13 +00:00
|
|
|
|
foreach my $path (@paths) {
|
2013-08-08 00:10:34 +00:00
|
|
|
|
foreach my $line (@{$path->lines}) {
|
2013-11-21 17:47:25 +00:00
|
|
|
|
my @intersections = @{intersection_pl(
|
|
|
|
|
[ $self->line->as_polyline ],
|
2014-04-29 21:15:36 +00:00
|
|
|
|
$line->grow(Slic3r::Geometry::scale $path->width/2),
|
2013-11-21 17:47:25 +00:00
|
|
|
|
)};
|
2014-04-29 21:15:36 +00:00
|
|
|
|
|
2014-12-27 12:04:28 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2013-04-27 13:02:13 +00:00
|
|
|
|
|
2013-05-10 18:53:49 +00:00
|
|
|
|
if ($path->is_bridge) {
|
2014-12-27 12:04:28 +00:00
|
|
|
|
foreach my $line (@lines) {
|
2014-04-29 21:15:36 +00:00
|
|
|
|
my $radius = $path->width / 2;
|
2014-12-27 12:04:28 +00:00
|
|
|
|
my $width = unscale abs($line->b->x - $line->a->x);
|
|
|
|
|
if ((10 * $radius) < $width) {
|
2013-05-10 18:53:49 +00:00
|
|
|
|
# we're cutting the path in the longitudinal direction, so we've got a rectangle
|
2014-12-27 12:04:28 +00:00
|
|
|
|
$g->rectangle(
|
|
|
|
|
'x' => $self->scale * unscale($line->a->x),
|
2013-07-28 22:27:53 +00:00
|
|
|
|
'y' => $self->scale * $self->_y($layer->print_z),
|
2013-05-13 13:21:26 +00:00
|
|
|
|
'width' => $self->scale * $width,
|
2013-05-10 18:53:49 +00:00
|
|
|
|
'height' => $self->scale * $radius * 2,
|
|
|
|
|
'rx' => $self->scale * $radius * 0.35,
|
|
|
|
|
'ry' => $self->scale * $radius * 0.35,
|
2014-12-27 12:04:28 +00:00
|
|
|
|
);
|
2013-05-10 18:53:49 +00:00
|
|
|
|
} else {
|
2014-12-27 12:04:28 +00:00
|
|
|
|
$g->circle(
|
|
|
|
|
'cx' => $self->scale * (unscale($line->a->x) + $radius),
|
2013-07-28 22:27:53 +00:00
|
|
|
|
'cy' => $self->scale * $self->_y($layer->print_z - $radius),
|
2013-05-13 13:21:26 +00:00
|
|
|
|
'r' => $self->scale * $radius,
|
2014-12-27 12:04:28 +00:00
|
|
|
|
);
|
2013-05-10 18:53:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-12-27 12:04:28 +00:00
|
|
|
|
foreach my $line (@lines) {
|
2013-09-17 12:16:29 +00:00
|
|
|
|
my $height = $path->height;
|
|
|
|
|
$height = $layer->height if $height == -1;
|
2014-12-27 12:04:28 +00:00
|
|
|
|
$g->rectangle(
|
|
|
|
|
'x' => $self->scale * unscale($line->a->x),
|
2013-07-28 22:27:53 +00:00
|
|
|
|
'y' => $self->scale * $self->_y($layer->print_z),
|
2014-12-27 12:04:28 +00:00
|
|
|
|
'width' => $self->scale * unscale($line->b->x - $line->a->x),
|
2013-05-10 18:53:49 +00:00
|
|
|
|
'height' => $self->scale * $height,
|
2014-12-27 12:04:28 +00:00
|
|
|
|
'rx' => $self->scale * $height * 0.5,
|
|
|
|
|
'ry' => $self->scale * $height * 0.5,
|
|
|
|
|
);
|
|
|
|
|
}
|
2013-05-10 18:53:49 +00:00
|
|
|
|
}
|
2013-04-27 13:02:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub _y {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
my ($y) = @_;
|
2014-12-27 12:04:28 +00:00
|
|
|
|
|
|
|
|
|
return $self->_height - $y;
|
2013-04-27 13:02:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
1;
|