Reader.pm 2.6 KB

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