2012-11-21 19:41:14 +00:00
|
|
|
package Slic3r::Test;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2012-12-21 12:25:03 +00:00
|
|
|
require Exporter;
|
|
|
|
our @ISA = qw(Exporter);
|
|
|
|
our @EXPORT_OK = qw(_eq);
|
|
|
|
|
2012-11-21 19:41:14 +00:00
|
|
|
use IO::Scalar;
|
2012-12-30 15:27:20 +00:00
|
|
|
use List::Util qw(first);
|
|
|
|
use Slic3r::Geometry qw(epsilon X Y Z);
|
2012-11-21 19:41:14 +00:00
|
|
|
|
2012-12-09 17:33:25 +00:00
|
|
|
my %cuboids = (
|
|
|
|
'20mm_cube' => [20,20,20],
|
|
|
|
'2x20x10' => [2, 20,10],
|
|
|
|
);
|
|
|
|
|
2013-02-23 16:40:38 +00:00
|
|
|
sub model {
|
|
|
|
my ($model_name) = @_;
|
2012-11-21 19:41:14 +00:00
|
|
|
|
2013-02-23 16:40:38 +00:00
|
|
|
my ($vertices, $facets);
|
|
|
|
if ($cuboids{$model_name}) {
|
|
|
|
my ($x, $y, $z) = @{ $cuboids{$model_name} };
|
|
|
|
$vertices = [
|
|
|
|
[$x,$y,0], [$x,0,0], [0,0,0], [0,$y,0], [$x,$y,$z], [0,$y,$z], [0,0,$z], [$x,0,$z],
|
|
|
|
];
|
|
|
|
$facets = [
|
|
|
|
[0,1,2], [0,2,3], [4,5,6], [4,6,7], [0,4,7], [0,7,1], [1,7,6], [1,6,2], [2,6,5], [2,5,3], [4,0,3], [4,3,5],
|
|
|
|
],
|
2012-11-21 19:41:14 +00:00
|
|
|
}
|
|
|
|
|
2013-02-23 16:40:38 +00:00
|
|
|
my $model = Slic3r::Model->new;
|
|
|
|
$model->add_object(vertices => $vertices)->add_volume(facets => $facets);
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub init_print {
|
|
|
|
my ($model_name, %params) = @_;
|
|
|
|
|
2012-11-21 19:41:14 +00:00
|
|
|
my $config = Slic3r::Config->new_from_defaults;
|
|
|
|
$config->apply($params{config}) if $params{config};
|
2012-12-21 14:14:44 +00:00
|
|
|
$config->set('gcode_comments', 1) if $ENV{SLIC3R_TESTS_GCODE};
|
2012-11-21 19:41:14 +00:00
|
|
|
|
|
|
|
my $print = Slic3r::Print->new(config => $config);
|
2013-02-23 16:40:38 +00:00
|
|
|
$print->add_model(model($model_name));
|
2012-11-21 19:41:14 +00:00
|
|
|
$print->validate;
|
|
|
|
|
|
|
|
return $print;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub gcode {
|
|
|
|
my ($print) = @_;
|
|
|
|
|
|
|
|
my $fh = IO::Scalar->new(\my $gcode);
|
|
|
|
$print->export_gcode(output_fh => $fh, quiet => 1);
|
|
|
|
$fh->close;
|
|
|
|
|
|
|
|
return $gcode;
|
|
|
|
}
|
|
|
|
|
2012-12-21 12:25:03 +00:00
|
|
|
sub _eq {
|
2012-11-21 19:41:14 +00:00
|
|
|
my ($a, $b) = @_;
|
|
|
|
return abs($a - $b) < epsilon;
|
|
|
|
}
|
|
|
|
|
2012-12-30 15:27:20 +00:00
|
|
|
sub add_facet {
|
|
|
|
my ($facet, $vertices, $facets) = @_;
|
|
|
|
|
|
|
|
push @$facets, [];
|
|
|
|
for my $i (0..2) {
|
|
|
|
my $v = first { $vertices->[$_][X] == $facet->[$i][X] && $vertices->[$_][Y] == $facet->[$i][Y] && $vertices->[$_][Z] == $facet->[$i][Z] } 0..$#$vertices;
|
|
|
|
if (!defined $v) {
|
|
|
|
push @$vertices, [ @{$facet->[$i]}[X,Y,Z] ];
|
|
|
|
$v = $#$vertices;
|
|
|
|
}
|
|
|
|
$facets->[-1][$i] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-21 19:41:14 +00:00
|
|
|
package Slic3r::Test::GCodeReader;
|
|
|
|
use Moo;
|
|
|
|
|
|
|
|
has 'gcode' => (is => 'ro', required => 1);
|
|
|
|
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;
|
2012-12-05 00:12:50 +00:00
|
|
|
my @AXES = qw(X Y Z E);
|
2012-11-21 19:41:14 +00:00
|
|
|
|
|
|
|
sub parse {
|
|
|
|
my $self = shift;
|
|
|
|
my ($cb) = @_;
|
|
|
|
|
2013-05-02 09:42:51 +00:00
|
|
|
foreach my $line (split /\R+/, $self->gcode) {
|
2012-12-09 17:33:25 +00:00
|
|
|
print "$line\n" if $Verbose || $ENV{SLIC3R_TESTS_GCODE};
|
2012-11-21 19:41:14 +00:00
|
|
|
$line =~ s/\s*;(.*)//; # strip comment
|
|
|
|
next if $line eq '';
|
|
|
|
my $comment = $1;
|
|
|
|
|
|
|
|
# parse command
|
|
|
|
my ($command, @args) = split /\s+/, $line;
|
|
|
|
my %args = map { /([A-Z])(.*)/; ($1 => $2) } @args;
|
|
|
|
my %info = ();
|
|
|
|
|
|
|
|
# check retraction
|
|
|
|
if ($command =~ /^G[01]$/) {
|
|
|
|
foreach my $axis (@AXES) {
|
2013-05-02 09:42:51 +00:00
|
|
|
if (exists $args{$axis}) {
|
|
|
|
$info{"dist_$axis"} = $args{$axis} - $self->$axis;
|
|
|
|
$info{"new_$axis"} = $args{$axis};
|
|
|
|
} else {
|
2012-11-21 19:41:14 +00:00
|
|
|
$info{"dist_$axis"} = 0;
|
2013-05-02 09:42:51 +00:00
|
|
|
$info{"new_$axis"} = $self->$axis;
|
2012-11-21 19:41:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$info{dist_XY} = Slic3r::Line->new([0,0], [@info{qw(dist_X dist_Y)}])->length;
|
|
|
|
if (exists $args{E}) {
|
2013-03-18 17:01:01 +00:00
|
|
|
if ($info{dist_E} > 0) {
|
|
|
|
$info{extruding} = 1;
|
|
|
|
} elsif ($info{dist_E} < 0) {
|
|
|
|
$info{retracting} = 1
|
|
|
|
}
|
2013-05-02 09:42:51 +00:00
|
|
|
} else {
|
|
|
|
$info{travel} = 1;
|
2012-11-21 19:41:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# run callback
|
|
|
|
$cb->($self, $command, \%args, \%info);
|
|
|
|
|
|
|
|
# update coordinates
|
|
|
|
if ($command =~ /^(?:G[01]|G92)$/) {
|
2012-12-05 00:12:50 +00:00
|
|
|
for (@AXES, 'F') {
|
2012-11-21 19:41:14 +00:00
|
|
|
$self->$_($args{$_}) if exists $args{$_};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# TODO: update temperatures
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|