2013-05-13 18:14:33 +00:00
|
|
|
package Slic3r::GCode::Reader;
|
|
|
|
use Moo;
|
|
|
|
|
|
|
|
has 'X' => (is => 'rw', default => sub {0});
|
|
|
|
has 'Y' => (is => 'rw', default => sub {0});
|
|
|
|
has 'Z' => (is => 'rw', default => sub {0});
|
|
|
|
has 'E' => (is => 'rw', default => sub {0});
|
|
|
|
has 'F' => (is => 'rw', default => sub {0});
|
|
|
|
|
|
|
|
our $Verbose = 0;
|
|
|
|
my @AXES = qw(X Y Z E);
|
|
|
|
|
|
|
|
sub parse {
|
|
|
|
my $self = shift;
|
2013-08-28 14:51:58 +00:00
|
|
|
my ($gcode, $cb) = @_;
|
2013-05-13 18:14:33 +00:00
|
|
|
|
2013-08-28 14:51:58 +00:00
|
|
|
foreach my $raw_line (split /\R+/, $gcode) {
|
2013-05-13 18:14:33 +00:00
|
|
|
print "$raw_line\n" if $Verbose || $ENV{SLIC3R_TESTS_GCODE};
|
|
|
|
my $line = $raw_line;
|
|
|
|
$line =~ s/\s*;(.*)//; # strip comment
|
|
|
|
next if $line eq '';
|
|
|
|
my %info = (comment => $1, raw => $raw_line);
|
|
|
|
|
|
|
|
# parse command
|
|
|
|
my ($command, @args) = split /\s+/, $line;
|
|
|
|
my %args = map { /([A-Z])(.*)/; ($1 => $2) } @args;
|
|
|
|
|
2013-05-30 18:06:05 +00:00
|
|
|
# check motion
|
2013-05-13 18:14:33 +00:00
|
|
|
if ($command =~ /^G[01]$/) {
|
|
|
|
foreach my $axis (@AXES) {
|
|
|
|
if (exists $args{$axis}) {
|
|
|
|
$info{"dist_$axis"} = $args{$axis} - $self->$axis;
|
|
|
|
$info{"new_$axis"} = $args{$axis};
|
|
|
|
} else {
|
|
|
|
$info{"dist_$axis"} = 0;
|
|
|
|
$info{"new_$axis"} = $self->$axis;
|
|
|
|
}
|
|
|
|
}
|
2013-07-16 15:13:01 +00:00
|
|
|
$info{dist_XY} = Slic3r::Geometry::unscale(Slic3r::Line->new_scale([0,0], [@info{qw(dist_X dist_Y)}])->length);
|
2013-05-13 18:14:33 +00:00
|
|
|
if (exists $args{E}) {
|
|
|
|
if ($info{dist_E} > 0) {
|
|
|
|
$info{extruding} = 1;
|
|
|
|
} elsif ($info{dist_E} < 0) {
|
|
|
|
$info{retracting} = 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$info{travel} = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# run callback
|
2013-08-28 16:12:20 +00:00
|
|
|
$cb->($self, $command, \%args, \%info);
|
2013-05-13 18:14:33 +00:00
|
|
|
|
|
|
|
# update coordinates
|
|
|
|
if ($command =~ /^(?:G[01]|G92)$/) {
|
2013-08-28 14:51:58 +00:00
|
|
|
for my $axis (@AXES, 'F') {
|
|
|
|
$self->$axis($args{$axis}) if exists $args{$axis};
|
2013-05-13 18:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# TODO: update temperatures
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|