2011-09-18 17:28:12 +00:00
|
|
|
package Slic3r::SVG;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use SVG;
|
|
|
|
|
|
|
|
use constant X => 0;
|
|
|
|
use constant Y => 1;
|
|
|
|
|
|
|
|
sub factor {
|
2011-10-04 15:55:55 +00:00
|
|
|
return $Slic3r::resolution * 10;
|
2011-09-18 17:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub svg {
|
|
|
|
my ($print) = @_;
|
2011-10-01 12:26:54 +00:00
|
|
|
$print ||= Slic3r::Print->new(x_length => 200 / $Slic3r::resolution, y_length => 200 / $Slic3r::resolution);
|
2011-10-08 17:02:05 +00:00
|
|
|
my $svg = SVG->new(width => $print->max_length * factor(), height => $print->max_length * factor());
|
|
|
|
|
|
|
|
my $marker_end = $svg->marker(
|
|
|
|
id => "endArrow",
|
|
|
|
viewBox => "0 0 10 10",
|
|
|
|
refX => "1",
|
|
|
|
refY => "5",
|
|
|
|
markerUnits => "strokeWidth",
|
|
|
|
orient => "auto",
|
|
|
|
markerWidth => "10",
|
|
|
|
markerHeight => "8",
|
|
|
|
);
|
|
|
|
$marker_end->polyline(
|
|
|
|
points => "0,0 10,5 0,10 1,5",
|
|
|
|
fill => "darkblue",
|
|
|
|
);
|
|
|
|
|
|
|
|
return $svg;
|
2011-09-18 17:28:12 +00:00
|
|
|
}
|
|
|
|
|
2011-10-06 09:55:26 +00:00
|
|
|
sub output {
|
|
|
|
my ($print, $filename, %things) = @_;
|
2011-09-26 12:48:22 +00:00
|
|
|
|
|
|
|
my $svg = svg($print);
|
2011-10-06 09:55:26 +00:00
|
|
|
|
2011-10-09 17:47:21 +00:00
|
|
|
foreach my $type (qw(polygons polylines white_polygons green_polygons red_polygons red_polylines)) {
|
2011-10-06 09:55:26 +00:00
|
|
|
if ($things{$type}) {
|
2011-10-06 13:24:21 +00:00
|
|
|
my $method = $type =~ /polygons/ ? 'polygon' : 'polyline';
|
2011-10-09 17:47:21 +00:00
|
|
|
my ($colour) = $type =~ /^(red|green)_/;
|
2011-10-06 09:55:26 +00:00
|
|
|
my $g = $svg->group(
|
|
|
|
style => {
|
|
|
|
'stroke-width' => 2,
|
2011-10-10 14:31:37 +00:00
|
|
|
'stroke' => 'black' || $colour || 'black',
|
2011-10-09 17:47:21 +00:00
|
|
|
'fill' => ($type !~ /polygons/ ? 'none' : ($colour || 'grey')),
|
2011-10-06 09:55:26 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
foreach my $polygon (@{$things{$type}}) {
|
|
|
|
my $path = $svg->get_path(
|
|
|
|
'x' => [ map($_->[X] * factor(), @$polygon) ],
|
|
|
|
'y' => [ map($_->[Y] * factor(), @$polygon) ],
|
|
|
|
-type => 'polygon',
|
|
|
|
);
|
|
|
|
$g->$method(
|
|
|
|
%$path,
|
2011-10-11 13:51:08 +00:00
|
|
|
'marker-end' => $things{no_arrows} ? "" : "url(#endArrow)",
|
2011-10-06 09:55:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2011-09-26 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
2011-10-06 09:55:26 +00:00
|
|
|
foreach my $type (qw(points red_points)) {
|
|
|
|
if ($things{$type}) {
|
2011-10-09 20:18:06 +00:00
|
|
|
my ($colour, $r) = $type eq 'points' ? ('black', 5) : ('red', 3);
|
2011-10-06 09:55:26 +00:00
|
|
|
my $g = $svg->group(
|
|
|
|
style => {
|
|
|
|
'stroke-width' => 2,
|
2011-10-07 17:07:57 +00:00
|
|
|
'stroke' => $colour,
|
2011-10-06 09:55:26 +00:00
|
|
|
'fill' => $colour,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
foreach my $point (@{$things{$type}}) {
|
|
|
|
$g->circle(
|
|
|
|
cx => $point->[X] * factor(),
|
|
|
|
cy => $point->[Y] * factor(),
|
|
|
|
r => $r,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-08 17:02:05 +00:00
|
|
|
foreach my $type (qw(lines red_lines green_lines)) {
|
2011-10-06 09:55:26 +00:00
|
|
|
if ($things{$type}) {
|
2011-10-08 17:02:05 +00:00
|
|
|
my ($colour) = $type =~ /^(red|green)_/;
|
2011-10-06 09:55:26 +00:00
|
|
|
my $g = $svg->group(
|
|
|
|
style => {
|
|
|
|
'stroke-width' => 2,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
foreach my $line (@{$things{$type}}) {
|
|
|
|
$g->line(
|
|
|
|
x1 => $line->[0][X] * factor(),
|
|
|
|
y1 => $line->[0][Y] * factor(),
|
|
|
|
x2 => $line->[1][X] * factor(),
|
|
|
|
y2 => $line->[1][Y] * factor(),
|
|
|
|
style => {
|
2011-10-08 17:02:05 +00:00
|
|
|
'stroke' => $colour || 'black',
|
2011-10-06 09:55:26 +00:00
|
|
|
},
|
2011-10-11 13:51:08 +00:00
|
|
|
'marker-end' => $things{no_arrows} ? "" : "url(#endArrow)",
|
2011-10-06 09:55:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2011-09-26 12:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
write_svg($svg, $filename);
|
|
|
|
}
|
|
|
|
|
2011-10-06 09:55:26 +00:00
|
|
|
sub output_points {
|
|
|
|
my ($print, $filename, $points, $red_points) = @_;
|
|
|
|
return output($print, $filename, points => $points, red_points => $red_points);
|
|
|
|
}
|
|
|
|
|
2011-09-18 17:28:12 +00:00
|
|
|
sub output_polygons {
|
2011-10-06 09:55:26 +00:00
|
|
|
my ($print, $filename, $polygons) = @_;
|
|
|
|
return output($print, $filename, polygons => $polygons);
|
2011-09-18 17:28:12 +00:00
|
|
|
}
|
|
|
|
|
2011-10-01 12:26:54 +00:00
|
|
|
sub output_polylines {
|
2011-10-06 09:55:26 +00:00
|
|
|
my ($print, $filename, $polylines) = @_;
|
|
|
|
return output($print, $filename, polylines => $polylines);
|
2011-10-01 12:26:54 +00:00
|
|
|
}
|
|
|
|
|
2011-09-18 17:28:12 +00:00
|
|
|
sub output_lines {
|
|
|
|
my ($print, $filename, $lines) = @_;
|
2011-10-06 09:55:26 +00:00
|
|
|
return output($print, $filename, lines => $lines);
|
2011-09-18 17:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub write_svg {
|
|
|
|
my ($svg, $filename) = @_;
|
|
|
|
|
|
|
|
open my $fh, '>', $filename;
|
|
|
|
print $fh $svg->xmlify;
|
|
|
|
close $fh;
|
|
|
|
printf "SVG written to %s\n", $filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|