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

« back to all changes in this revision

Viewing changes to t/gcode.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 => 11;
 
2
use strict;
 
3
use warnings;
 
4
 
 
5
BEGIN {
 
6
    use FindBin;
 
7
    use lib "$FindBin::Bin/../lib";
 
8
}
 
9
 
 
10
use List::Util qw(first);
 
11
use Slic3r;
 
12
use Slic3r::Geometry qw(scale convex_hull);
 
13
use Slic3r::Test;
 
14
 
 
15
{
 
16
    my $gcodegen = Slic3r::GCode->new(
 
17
        layer_count     => 1,
 
18
        extruders       => [],
 
19
    );
 
20
    $gcodegen->set_shift(10, 10);
 
21
    is_deeply $gcodegen->last_pos->arrayref, [scale -10, scale -10], 'last_pos is shifted correctly';
 
22
}
 
23
 
 
24
{
 
25
    my $config = Slic3r::Config->new_from_defaults;
 
26
    $config->set('wipe', [1]);
 
27
    
 
28
    my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
 
29
    my $have_wipe = 0;
 
30
    my @retract_speeds = ();
 
31
    Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
 
32
        my ($self, $cmd, $args, $info) = @_;
 
33
        if ($info->{retracting} && $info->{dist_XY} > 0) {
 
34
            $have_wipe = 1;
 
35
            my $move_time = $info->{dist_XY} / ($args->{F} // $self->F);
 
36
            push @retract_speeds, abs($info->{dist_E}) / $move_time;
 
37
        }
 
38
    });
 
39
    
 
40
    ok $have_wipe, "wipe";
 
41
    ok !defined (first { abs($_ - $config->retract_speed->[0]*60) < 5 } @retract_speeds), 'wipe moves don\'t retract faster than configured speed';
 
42
}
 
43
 
 
44
{
 
45
    # This tests the following behavior:
 
46
    # - complete objects does not crash
 
47
    # - no hard-coded "E" are generated
 
48
    # - Z moves are correctly generated for both objects
 
49
    # - no travel moves go outside skirt
 
50
    my $config = Slic3r::Config->new_from_defaults;
 
51
    $config->set('gcode_comments', 1);
 
52
    $config->set('complete_objects', 1);
 
53
    $config->set('extrusion_axis', 'A');
 
54
    $config->set('start_gcode', '');  # prevent any default extra Z move
 
55
    $config->set('layer_height', 0.4);
 
56
    $config->set('first_layer_height', 0.4);
 
57
    my $print = Slic3r::Test::init_print('20mm_cube', config => $config, duplicate => 2);
 
58
    ok my $gcode = Slic3r::Test::gcode($print), "complete_objects";
 
59
    my @z_moves = ();
 
60
    my @travel_moves = ();  # array of scaled points
 
61
    my @extrusions = ();    # array of scaled points
 
62
    Slic3r::GCode::Reader->new->parse($gcode, sub {
 
63
        my ($self, $cmd, $args, $info) = @_;
 
64
        fail 'unexpected E argument' if defined $args->{E};
 
65
        if (defined $args->{Z}) {
 
66
            push @z_moves, $args->{Z};
 
67
        }
 
68
        
 
69
        if ($info->{dist_XY}) {
 
70
            if ($info->{extruding} || $args->{A}) {
 
71
                push @extrusions, Slic3r::Point->new_scale($info->{new_X}, $info->{new_Y});
 
72
            } else {
 
73
                push @travel_moves, Slic3r::Point->new_scale($info->{new_X}, $info->{new_Y})
 
74
                    if @extrusions;  # skip initial travel move to first skirt point
 
75
            }
 
76
        }
 
77
    });
 
78
    my $layer_count = 20/0.4;  # cube is 20mm tall
 
79
    is scalar(@z_moves), 2*$layer_count, 'complete_objects generates the correct number of Z moves';
 
80
    is_deeply [ @z_moves[0..($layer_count-1)] ], [ @z_moves[$layer_count..$#z_moves] ], 'complete_objects generates the correct Z moves';
 
81
    
 
82
    my $convex_hull = convex_hull(\@extrusions);
 
83
    ok !(defined first { !$convex_hull->contains_point($_) } @travel_moves), 'all travel moves happen within skirt';
 
84
}
 
85
 
 
86
{
 
87
    my $config = Slic3r::Config->new_from_defaults;
 
88
    $config->set('retract_length', [1000000]);
 
89
    $config->set('use_relative_e_distances', 1);
 
90
    my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
 
91
    Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
 
92
        my ($self, $cmd, $args, $info) = @_;
 
93
        
 
94
        
 
95
    });
 
96
    ok $print->print->total_used_filament > 0, 'final retraction is not considered in total used filament';
 
97
}
 
98
 
 
99
{
 
100
    my $test = sub {
 
101
        my ($print, $comment) = @_;
 
102
        
 
103
        my @percent = ();
 
104
        Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
 
105
            my ($self, $cmd, $args, $info) = @_;
 
106
        
 
107
            if ($cmd eq 'M73') {
 
108
                push @percent, $args->{P};
 
109
            }
 
110
        });
 
111
        # the extruder heater is turned off when M73 P100 is reached
 
112
        ok !(defined first { $_ > 100 } @percent), "M73 is never given more than 100% ($comment)";
 
113
    };
 
114
    
 
115
    {
 
116
        my $config = Slic3r::Config->new_from_defaults;
 
117
        $config->set('gcode_flavor', 'sailfish');
 
118
        $config->set('raft_layers', 3);
 
119
        my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
 
120
        $test->($print, 'single object');
 
121
    }
 
122
    
 
123
    {
 
124
        my $config = Slic3r::Config->new_from_defaults;
 
125
        $config->set('gcode_flavor', 'sailfish');
 
126
        my $print = Slic3r::Test::init_print('20mm_cube', config => $config, duplicate => 2);
 
127
        $test->($print, 'two copies of single object');
 
128
    }
 
129
    
 
130
    {
 
131
        my $config = Slic3r::Config->new_from_defaults;
 
132
        $config->set('gcode_flavor', 'sailfish');
 
133
        my $print = Slic3r::Test::init_print(['20mm_cube','20mm_cube'], config => $config);
 
134
        $test->($print, 'two objects');
 
135
    }
 
136
}
 
137
 
 
138
__END__