2
# This script generates section cuts from a given G-Code file
9
use lib "$FindBin::Bin/../lib";
12
use Getopt::Long qw(:config no_auto_abbrev);
14
use List::Util qw(max);
16
use Slic3r::Geometry qw(X Y A B X1 Y1 X2 Y2);
17
use Slic3r::Geometry::Clipper qw(JT_SQUARE);
23
extrusion_width => 0.5,
28
'help' => sub { usage() },
29
'layer-height|h=f' => \$opt{layer_height},
30
'extrusion-width|w=f' => \$opt{extrusion_width},
31
'scale|s=i' => \$opt{scale},
33
GetOptions(%options) or usage(1);
38
my $input_file = $ARGV[0];
39
my $output_file = $input_file;
40
$output_file =~ s/\.(?:gcode|gco|ngc|g)$/.svg/;
43
my %paths = (); # z => [ path, path ... ]
44
Slic3r::GCode::Reader->new->parse(io($input_file)->all, sub {
45
my ($self, $cmd, $args, $info) = @_;
47
if ($cmd eq 'G1' && $info->{extruding}) {
48
$paths{ $self->Z } ||= [];
49
push @{ $paths{ $self->Z } }, Slic3r::Line->new(
50
[ $self->X, $self->Y ],
51
[ $info->{new_X}, $info->{new_Y} ],
56
# calculate print extents
57
my $bounding_box = Slic3r::Geometry::BoundingBox->new_from_points([ map @$_, map @$_, values %paths ]);
59
# calculate section line
60
my $section_y = $bounding_box->center->[Y];
62
[ $bounding_box->x_min, $section_y ],
63
[ $bounding_box->x_max, $section_y ],
67
my $max_z = max(keys %paths);
69
width => $opt{scale} * $bounding_box->size->[X],
70
height => $opt{scale} * $max_z,
73
# put everything into a group
74
my $g = $svg->group(style => {
76
'stroke' => '#444444',
81
foreach my $z (sort keys %paths) {
82
foreach my $line (@{ $paths{$z} }) {
83
my @intersections = @{intersection_pl(
85
[ _grow($line, $opt{extrusion_width}/2) ],
89
'x' => $opt{scale} * ($_->[A][X] - $bounding_box->x_min),
90
'y' => $opt{scale} * ($max_z - $z),
91
'width' => $opt{scale} * abs($_->[B][X] - $_->[A][X]),
92
'height' => $opt{scale} * $opt{layer_height},
93
'rx' => $opt{scale} * $opt{layer_height} * 0.35,
94
'ry' => $opt{scale} * $opt{layer_height} * 0.35,
100
Slic3r::open(\my $fh, '>', $output_file);
101
print $fh $svg->xmlify;
103
printf "Section cut SVG written to %s\n", $output_file;
106
# replace built-in Line->grow method which relies on int_offset()
108
my ($line, $distance) = @_;
110
my $polygon = [ @$line, CORE::reverse @$line[1..($#$line-1)] ];
111
return @{Math::Clipper::offset([$polygon], $distance, 100000, JT_SQUARE, 2)};
115
my ($exit_code) = @_;
118
Usage: gcode_sectioncut.pl [ OPTIONS ] file.gcode
120
--help Output this usage screen and exit
121
--layer-height, -h Use the specified layer height
122
--extrusion-width, -w Use the specified extrusion width
123
--scale Factor for converting G-code units to SVG units
126
exit ($exit_code || 0);