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

« back to all changes in this revision

Viewing changes to t/retraction.t

  • 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
use Test::More tests => 17;
 
2
use strict;
 
3
use warnings;
 
4
 
 
5
BEGIN {
 
6
    use FindBin;
 
7
    use lib "$FindBin::Bin/../lib";
 
8
}
 
9
 
 
10
use Slic3r;
 
11
use Slic3r::Test qw(_eq);
 
12
 
 
13
{
 
14
    my $config = Slic3r::Config->new_from_defaults;
 
15
    my $duplicate = 1;
 
16
 
 
17
    my $test = sub {
 
18
        my ($conf) = @_;
 
19
        $conf ||= $config;
 
20
    
 
21
        my $print = Slic3r::Test::init_print('20mm_cube', config => $conf, duplicate => $duplicate);
 
22
    
 
23
        my $tool = 0;
 
24
        my @toolchange_count = (); # track first usages so that we don't expect retract_length_toolchange when extruders are used for the first time
 
25
        my @retracted = (1);  # ignore the first travel move from home to first point
 
26
        my @retracted_length = (0);
 
27
        my $lifted = 0;
 
28
        my $changed_tool = 0;
 
29
        my $wait_for_toolchange = 0;
 
30
        Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
 
31
            my ($self, $cmd, $args, $info) = @_;
 
32
        
 
33
            if ($cmd =~ /^T(\d+)/) {
 
34
                $tool = $1;
 
35
                $changed_tool = 1;
 
36
                $wait_for_toolchange = 0;
 
37
                $toolchange_count[$tool] //= 0;
 
38
                $toolchange_count[$tool]++;
 
39
            } elsif ($cmd =~ /^G[01]$/ && !$args->{Z}) { # ignore lift taking place after retraction
 
40
                fail 'toolchange happens right after retraction' if $wait_for_toolchange;
 
41
            }
 
42
        
 
43
            if ($info->{dist_Z}) {
 
44
                # lift move or lift + change layer
 
45
                if (_eq($info->{dist_Z}, $print->print->config->get_at('retract_lift', $tool))
 
46
                    || (_eq($info->{dist_Z}, $conf->layer_height + $print->print->config->get_at('retract_lift', $tool)) && $print->print->config->get_at('retract_lift', $tool) > 0)) {
 
47
                    fail 'only lifting while retracted' if !$retracted[$tool] && !($conf->g0 && $info->{retracting});
 
48
                    fail 'double lift' if $lifted;
 
49
                    $lifted = 1;
 
50
                }
 
51
                if ($info->{dist_Z} < 0) {
 
52
                    fail 'going down only after lifting' if !$lifted;
 
53
                    fail 'going down by the same amount of the lift or by the amount needed to get to next layer'
 
54
                        if !_eq($info->{dist_Z}, -$print->print->config->get_at('retract_lift', $tool))
 
55
                            && !_eq($info->{dist_Z}, -$print->print->config->get_at('retract_lift', $tool) + $conf->layer_height);
 
56
                    $lifted = 0;
 
57
                }
 
58
                fail 'move Z at travel speed' if ($args->{F} // $self->F) != $conf->travel_speed * 60;
 
59
            }
 
60
            if ($info->{retracting}) {
 
61
                $retracted[$tool] = 1;
 
62
                $retracted_length[$tool] += -$info->{dist_E};
 
63
                if (_eq($retracted_length[$tool], $print->print->config->get_at('retract_length', $tool))) {
 
64
                    # okay
 
65
                } elsif (_eq($retracted_length[$tool], $print->print->config->get_at('retract_length_toolchange', $tool))) {
 
66
                    $wait_for_toolchange = 1;
 
67
                } else {
 
68
                    fail 'retracted by the correct amount';
 
69
                }
 
70
                fail 'combining retraction and travel with G0'
 
71
                    if $cmd ne 'G0' && $conf->g0 && ($info->{dist_Z} || $info->{dist_XY});
 
72
            }
 
73
            if ($info->{extruding}) {
 
74
                fail 'only extruding while not lifted' if $lifted;
 
75
                if ($retracted[$tool]) {
 
76
                    my $expected_amount = $retracted_length[$tool] + $print->print->config->get_at('retract_restart_extra', $tool);
 
77
                    if ($changed_tool && $toolchange_count[$tool] > 1) {
 
78
                        $expected_amount = $print->print->config->get_at('retract_length_toolchange', $tool) + $print->print->config->get_at('retract_restart_extra_toolchange', $tool);
 
79
                        $changed_tool = 0;
 
80
                    }
 
81
                    fail 'unretracted by the correct amount'
 
82
                        if !_eq($info->{dist_E}, $expected_amount);
 
83
                    $retracted[$tool] = 0;
 
84
                    $retracted_length[$tool] = 0;
 
85
                }
 
86
            }
 
87
            if ($info->{travel} && $info->{dist_XY} >= $print->print->config->get_at('retract_before_travel', $tool)) {
 
88
                fail 'retracted before long travel move' if !$retracted[$tool];
 
89
            }
 
90
        });
 
91
    
 
92
        1;
 
93
    };
 
94
 
 
95
    $config->set('first_layer_height',      $config->layer_height);
 
96
    $config->set('first_layer_speed',       '100%');
 
97
    $config->set('start_gcode',             '');  # to avoid dealing with the nozzle lift in start G-code
 
98
    $config->set('retract_length',          [1.5]);
 
99
    $config->set('retract_before_travel',   [3]);
 
100
    $config->set('only_retract_when_crossing_perimeters', 0);
 
101
 
 
102
    my $retract_tests = sub {
 
103
        my ($descr) = @_;
 
104
    
 
105
        ok $test->(), "retraction$descr";
 
106
    
 
107
        my $conf = $config->clone;
 
108
        $conf->set('retract_restart_extra',   [1]);
 
109
        ok $test->($conf), "restart extra length$descr";
 
110
    
 
111
        $conf->set('retract_restart_extra',   [-1]);
 
112
        ok $test->($conf), "negative restart extra length$descr";
 
113
    
 
114
        $conf->set('retract_lift',            [1]);
 
115
        ok $test->($conf), "lift$descr";
 
116
    };
 
117
 
 
118
    $retract_tests->('');
 
119
 
 
120
    $duplicate = 2;
 
121
    $retract_tests->(' (duplicate)');
 
122
 
 
123
    $config->set('g0', 1);
 
124
    $retract_tests->(' (G0 and duplicate)');
 
125
 
 
126
    $duplicate = 1;
 
127
    $config->set('g0', 0);
 
128
    $config->set('infill_extruder', 2);
 
129
    $config->set('skirts', 4);
 
130
    $config->set('skirt_height', 3);
 
131
    $retract_tests->(' (dual extruder with multiple skirt layers)');
 
132
}
 
133
 
 
134
{
 
135
    my $config = Slic3r::Config->new_from_defaults;
 
136
    $config->set('retract_layer_change', [0]);
 
137
    
 
138
    my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
 
139
    my $retracted = 0;
 
140
    my $layer_changes_with_retraction = 0;
 
141
    Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
 
142
        my ($self, $cmd, $args, $info) = @_;
 
143
        
 
144
        if ($info->{retracting}) {
 
145
            $retracted = 1;
 
146
        } elsif ($info->{extruding} && $retracted) {
 
147
            $retracted = 0;
 
148
        }
 
149
        
 
150
        if ($info->{dist_Z} && $retracted) {
 
151
            $layer_changes_with_retraction++;
 
152
        }
 
153
    });
 
154
    
 
155
    is $layer_changes_with_retraction, 0, 'no retraction on layer change';
 
156
}
 
157
 
 
158
__END__