~ubuntu-branches/ubuntu/utopic/slic3r/utopic

« back to all changes in this revision

Viewing changes to lib/Slic3r/GCode/Reader.pm

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2014-06-17 01:27:26 UTC
  • Revision ID: package-import@ubuntu.com-20140617012726-2wrs4zdo251nr4vg
Tags: upstream-1.1.4+dfsg
ImportĀ upstreamĀ versionĀ 1.1.4+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Slic3r::GCode::Reader;
 
2
use Moo;
 
3
 
 
4
has 'X' => (is => 'rw', default => sub {0});
 
5
has 'Y' => (is => 'rw', default => sub {0});
 
6
has 'Z' => (is => 'rw', default => sub {0});
 
7
has 'E' => (is => 'rw', default => sub {0});
 
8
has 'F' => (is => 'rw', default => sub {0});
 
9
 
 
10
our $Verbose = 0;
 
11
my @AXES = qw(X Y Z E);
 
12
 
 
13
sub clone {
 
14
    my $self = shift;
 
15
    return (ref $self)->new(
 
16
        map { $_ => $self->$_ } (@AXES, 'F'),
 
17
    );
 
18
}
 
19
 
 
20
sub parse {
 
21
    my $self = shift;
 
22
    my ($gcode, $cb) = @_;
 
23
    
 
24
    foreach my $raw_line (split /\R+/, $gcode) {
 
25
        print "$raw_line\n" if $Verbose || $ENV{SLIC3R_TESTS_GCODE};
 
26
        my $line = $raw_line;
 
27
        $line =~ s/\s*;(.*)//; # strip comment
 
28
        my %info = (comment => $1, raw => $raw_line);
 
29
        
 
30
        # parse command
 
31
        my ($command, @args) = split /\s+/, $line;
 
32
        $command //= '';
 
33
        my %args = map { /([A-Z])(.*)/; ($1 => $2) } @args;
 
34
        
 
35
        # check motion
 
36
        if ($command =~ /^G[01]$/) {
 
37
            foreach my $axis (@AXES) {
 
38
                if (exists $args{$axis}) {
 
39
                    $info{"dist_$axis"} = $args{$axis} - $self->$axis;
 
40
                    $info{"new_$axis"}  = $args{$axis};
 
41
                } else {
 
42
                    $info{"dist_$axis"} = 0;
 
43
                    $info{"new_$axis"}  = $self->$axis;
 
44
                }
 
45
            }
 
46
            $info{dist_XY} = Slic3r::Geometry::unscale(Slic3r::Line->new_scale([0,0], [@info{qw(dist_X dist_Y)}])->length);
 
47
            if (exists $args{E}) {
 
48
                if ($info{dist_E} > 0) {
 
49
                    $info{extruding} = 1;
 
50
                } elsif ($info{dist_E} < 0) {
 
51
                    $info{retracting} = 1
 
52
                }
 
53
            } else {
 
54
                $info{travel} = 1;
 
55
            }
 
56
        }
 
57
        
 
58
        # run callback
 
59
        $cb->($self, $command, \%args, \%info);
 
60
        
 
61
        # update coordinates
 
62
        if ($command =~ /^(?:G[01]|G92)$/) {
 
63
            for my $axis (@AXES, 'F') {
 
64
                $self->$axis($args{$axis}) if exists $args{$axis};
 
65
            }
 
66
        }
 
67
        
 
68
        # TODO: update temperatures
 
69
    }
 
70
}
 
71
 
 
72
1;