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

« back to all changes in this revision

Viewing changes to lib/Slic3r/Config.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::Config;
 
2
use strict;
 
3
use warnings;
 
4
use utf8;
 
5
 
 
6
use List::Util qw(first max);
 
7
 
 
8
# cemetery of old config settings
 
9
our @Ignore = qw(duplicate_x duplicate_y multiply_x multiply_y support_material_tool acceleration
 
10
    adjust_overhang_flow standby_temperature scale rotate duplicate duplicate_grid
 
11
    rotate scale duplicate_grid start_perimeters_at_concave_points start_perimeters_at_non_overhang
 
12
    randomize_start seal_position);
 
13
 
 
14
our $Options = print_config_def();
 
15
 
 
16
# overwrite the hard-coded readonly value (this information is not available in XS)
 
17
$Options->{threads}{readonly} = !$Slic3r::have_threads;
 
18
 
 
19
# generate accessors
 
20
{
 
21
    no strict 'refs';
 
22
    for my $opt_key (keys %$Options) {
 
23
        *{$opt_key} = sub { $_[0]->get($opt_key) };
 
24
    }
 
25
}
 
26
 
 
27
sub new_from_defaults {
 
28
    my $class = shift;
 
29
    my (@opt_keys) = @_;
 
30
    
 
31
    my $self = $class->new;
 
32
    my $defaults = Slic3r::Config::Full->new;
 
33
    if (@opt_keys) {
 
34
        $self->set($_, $defaults->get($_)) for @opt_keys;
 
35
    } else {
 
36
        $self->apply_static($defaults);
 
37
    }
 
38
    return $self;
 
39
}
 
40
 
 
41
sub new_from_cli {
 
42
    my $class = shift;
 
43
    my %args = @_;
 
44
    
 
45
    delete $args{$_} for grep !defined $args{$_}, keys %args;
 
46
    
 
47
    for (qw(start end layer toolchange)) {
 
48
        my $opt_key = "${_}_gcode";
 
49
        if ($args{$opt_key}) {
 
50
            if (-e $args{$opt_key}) {
 
51
                Slic3r::open(\my $fh, "<", $args{$opt_key})
 
52
                    or die "Failed to open $args{$opt_key}\n";
 
53
                binmode $fh, ':utf8';
 
54
                $args{$opt_key} = do { local $/; <$fh> };
 
55
                close $fh;
 
56
            }
 
57
        }
 
58
    }
 
59
    
 
60
    my $self = $class->new;
 
61
    foreach my $opt_key (keys %args) {
 
62
        my $opt_def = $Options->{$opt_key};
 
63
        
 
64
        # we use set_deserialize() for bool options since GetOpt::Long doesn't handle 
 
65
        # arrays of boolean values
 
66
        if ($opt_key =~ /^(?:print_center|bed_size|duplicate_grid|extruder_offset)$/ || $opt_def->{type} eq 'bool') {
 
67
            $self->set_deserialize($opt_key, $args{$opt_key});
 
68
        } elsif (my $shortcut = $opt_def->{shortcut}) {
 
69
            $self->set($_, $args{$opt_key}) for @$shortcut;
 
70
        } else {
 
71
            $self->set($opt_key, $args{$opt_key});
 
72
        }
 
73
    }
 
74
    
 
75
    return $self;
 
76
}
 
77
 
 
78
sub merge {
 
79
    my $class = shift;
 
80
    my $config = $class->new;
 
81
    $config->apply($_) for @_;
 
82
    return $config;
 
83
}
 
84
 
 
85
sub load {
 
86
    my $class = shift;
 
87
    my ($file) = @_;
 
88
    
 
89
    my $ini = __PACKAGE__->read_ini($file);
 
90
    return $class->load_ini_hash($ini->{_});
 
91
}
 
92
 
 
93
sub load_ini_hash {
 
94
    my $class = shift;
 
95
    my ($ini_hash) = @_;
 
96
    
 
97
    my $config = $class->new;
 
98
    foreach my $opt_key (keys %$ini_hash) {
 
99
        ($opt_key, my $value) = _handle_legacy($opt_key, $ini_hash->{$opt_key});
 
100
        next if !defined $opt_key;
 
101
        $config->set_deserialize($opt_key, $value);
 
102
    }
 
103
    return $config;
 
104
}
 
105
 
 
106
sub clone {
 
107
    my $self = shift;
 
108
    
 
109
    my $new = (ref $self)->new;
 
110
    $new->apply($self);
 
111
    return $new;
 
112
}
 
113
 
 
114
sub get_value {
 
115
    my $self = shift;
 
116
    my ($opt_key) = @_;
 
117
    
 
118
    return $Options->{$opt_key}{ratio_over}
 
119
        ? $self->get_abs_value($opt_key)
 
120
        : $self->get($opt_key);
 
121
}
 
122
 
 
123
sub _handle_legacy {
 
124
    my ($opt_key, $value) = @_;
 
125
    
 
126
    # handle legacy options
 
127
    return () if first { $_ eq $opt_key } @Ignore;
 
128
    if ($opt_key =~ /^(extrusion_width|bottom_layer_speed|first_layer_height)_ratio$/) {
 
129
        $opt_key = $1;
 
130
        $opt_key =~ s/^bottom_layer_speed$/first_layer_speed/;
 
131
        $value = $value =~ /^\d+(?:\.\d+)?$/ && $value != 0 ? ($value*100) . "%" : 0;
 
132
    }
 
133
    if ($opt_key eq 'threads' && !$Slic3r::have_threads) {
 
134
        $value = 1;
 
135
    }
 
136
    if ($opt_key eq 'gcode_flavor' && $value eq 'makerbot') {
 
137
        $value = 'makerware';
 
138
    }
 
139
    if ($opt_key eq 'fill_density' && defined($value) && $value !~ /%/ && $value <= 1) {
 
140
        # fill_density was turned into a percent value
 
141
        $value *= 100;
 
142
        $value = "$value";  # force update of the PV value, workaround for bug https://rt.cpan.org/Ticket/Display.html?id=94110
 
143
    }
 
144
    if ($opt_key eq 'randomize_start' && $value) {
 
145
        $opt_key = 'seam_position';
 
146
        $value = 'random';
 
147
    }
 
148
    
 
149
    # For historical reasons, the world's full of configs having these very low values;
 
150
    # to avoid unexpected behavior we need to ignore them.  Banning these two hard-coded
 
151
    # values is a dirty hack and will need to be removed sometime in the future, but it
 
152
    # will avoid lots of complaints for now.
 
153
    if ($opt_key eq 'perimeter_acceleration' && $value == '25') {
 
154
        $value = 0;
 
155
    }
 
156
    if ($opt_key eq 'infill_acceleration' && $value == '50') {
 
157
        $value = 0;
 
158
    }
 
159
    
 
160
    if (!exists $Options->{$opt_key}) {
 
161
        my @keys = grep { $Options->{$_}{aliases} && grep $_ eq $opt_key, @{$Options->{$_}{aliases}} } keys %$Options;
 
162
        if (!@keys) {
 
163
            warn "Unknown option $opt_key\n";
 
164
            return ();
 
165
        }
 
166
        $opt_key = $keys[0];
 
167
    }
 
168
    
 
169
    return ($opt_key, $value);
 
170
}
 
171
 
 
172
sub set_ifndef {
 
173
    my $self = shift;
 
174
    my ($opt_key, $value, $deserialize) = @_;
 
175
    
 
176
    if (!$self->has($opt_key)) {
 
177
        if ($deserialize) {
 
178
            $self->set_deserialize($opt_key, $value);
 
179
        } else {
 
180
            $self->set($opt_key, $value);
 
181
        }
 
182
    }
 
183
}
 
184
 
 
185
sub as_ini {
 
186
    my ($self) = @_;
 
187
    
 
188
    my $ini = { _ => {} };
 
189
    foreach my $opt_key (sort @{$self->get_keys}) {
 
190
        next if $Options->{$opt_key}{shortcut};
 
191
        $ini->{_}{$opt_key} = $self->serialize($opt_key);
 
192
    }
 
193
    return $ini;
 
194
}
 
195
 
 
196
sub save {
 
197
    my $self = shift;
 
198
    my ($file) = @_;
 
199
    
 
200
    __PACKAGE__->write_ini($file, $self->as_ini);
 
201
}
 
202
 
 
203
sub setenv {
 
204
    my $self = shift;
 
205
    
 
206
    foreach my $opt_key (@{$self->get_keys}) {
 
207
        $ENV{"SLIC3R_" . uc $opt_key} = $self->serialize($opt_key);
 
208
    }
 
209
}
 
210
 
 
211
sub equals {
 
212
    my ($self, $other) = @_;
 
213
    return @{ $self->diff($other) } == 0;
 
214
}
 
215
 
 
216
# this will *ignore* options not present in both configs
 
217
sub diff {
 
218
    my ($self, $other) = @_;
 
219
    
 
220
    my @diff = ();
 
221
    foreach my $opt_key (sort @{$self->get_keys}) {
 
222
        push @diff, $opt_key
 
223
            if $other->has($opt_key) && $other->serialize($opt_key) ne $self->serialize($opt_key);
 
224
    }
 
225
    return [@diff];
 
226
}
 
227
 
 
228
# this method is idempotent by design and only applies to ::DynamicConfig or ::Full
 
229
# objects because it performs cross checks
 
230
sub validate {
 
231
    my $self = shift;
 
232
    
 
233
    # -j, --threads
 
234
    die "Invalid value for --threads\n"
 
235
        if $self->threads < 1;
 
236
 
 
237
    # --layer-height
 
238
    die "Invalid value for --layer-height\n"
 
239
        if $self->layer_height <= 0;
 
240
    die "--layer-height must be a multiple of print resolution\n"
 
241
        if $self->layer_height / &Slic3r::SCALING_FACTOR % 1 != 0;
 
242
    
 
243
    # --first-layer-height
 
244
    die "Invalid value for --first-layer-height\n"
 
245
        if $self->first_layer_height !~ /^(?:\d*(?:\.\d+)?)%?$/;
 
246
    
 
247
    # --filament-diameter
 
248
    die "Invalid value for --filament-diameter\n"
 
249
        if grep $_ < 1, @{$self->filament_diameter};
 
250
    
 
251
    # --nozzle-diameter
 
252
    die "Invalid value for --nozzle-diameter\n"
 
253
        if grep $_ < 0, @{$self->nozzle_diameter};
 
254
    die "--layer-height can't be greater than --nozzle-diameter\n"
 
255
        if grep $self->layer_height > $_, @{$self->nozzle_diameter};
 
256
    die "First layer height can't be greater than --nozzle-diameter\n"
 
257
        if grep $self->get_value('first_layer_height') > $_, @{$self->nozzle_diameter};
 
258
    
 
259
    # --perimeters
 
260
    die "Invalid value for --perimeters\n"
 
261
        if $self->perimeters < 0;
 
262
    
 
263
    # --solid-layers
 
264
    die "Invalid value for --solid-layers\n" if defined $self->solid_layers && $self->solid_layers < 0;
 
265
    die "Invalid value for --top-solid-layers\n"    if $self->top_solid_layers      < 0;
 
266
    die "Invalid value for --bottom-solid-layers\n" if $self->bottom_solid_layers   < 0;
 
267
    
 
268
    # --gcode-flavor
 
269
    die "Invalid value for --gcode-flavor\n"
 
270
        if !first { $_ eq $self->gcode_flavor } @{$Options->{gcode_flavor}{values}};
 
271
    
 
272
    die "--use-firmware-retraction is only supported by Marlin firmware\n"
 
273
        if $self->use_firmware_retraction && $self->gcode_flavor ne 'reprap';
 
274
    
 
275
    die "--use-firmware-retraction is not compatible with --wipe\n"
 
276
        if $self->use_firmware_retraction && first {$_} @{$self->wipe};
 
277
    
 
278
    # --print-center
 
279
    die "Invalid value for --print-center\n"
 
280
        if !ref $self->print_center 
 
281
            && (!$self->print_center || $self->print_center !~ /^\d+,\d+$/);
 
282
    
 
283
    # --fill-pattern
 
284
    die "Invalid value for --fill-pattern\n"
 
285
        if !first { $_ eq $self->fill_pattern } @{$Options->{fill_pattern}{values}};
 
286
    
 
287
    # --solid-fill-pattern
 
288
    die "Invalid value for --solid-fill-pattern\n"
 
289
        if !first { $_ eq $self->solid_fill_pattern } @{$Options->{solid_fill_pattern}{values}};
 
290
    
 
291
    # --fill-density
 
292
    die "The selected fill pattern is not supposed to work at 100% density\n"
 
293
        if $self->fill_density == 100
 
294
            && !first { $_ eq $self->fill_pattern } @{$Options->{solid_fill_pattern}{values}};
 
295
    
 
296
    # --infill-every-layers
 
297
    die "Invalid value for --infill-every-layers\n"
 
298
        if $self->infill_every_layers !~ /^\d+$/ || $self->infill_every_layers < 1;
 
299
    
 
300
    # --bed-size
 
301
    die "Invalid value for --bed-size\n"
 
302
        if !ref $self->bed_size 
 
303
            && (!$self->bed_size || $self->bed_size !~ /^\d+,\d+$/);
 
304
    
 
305
    # --skirt-height
 
306
    die "Invalid value for --skirt-height\n"
 
307
        if $self->skirt_height < -1;  # -1 means as tall as the object
 
308
    
 
309
    # --bridge-flow-ratio
 
310
    die "Invalid value for --bridge-flow-ratio\n"
 
311
        if $self->bridge_flow_ratio <= 0;
 
312
    
 
313
    # extruder clearance
 
314
    die "Invalid value for --extruder-clearance-radius\n"
 
315
        if $self->extruder_clearance_radius <= 0;
 
316
    die "Invalid value for --extruder-clearance-height\n"
 
317
        if $self->extruder_clearance_height <= 0;
 
318
    
 
319
    # --extrusion-multiplier
 
320
    die "Invalid value for --extrusion-multiplier\n"
 
321
        if defined first { $_ <= 0 } @{$self->extrusion_multiplier};
 
322
    
 
323
    # --default-acceleration
 
324
    die "Invalid zero value for --default-acceleration when using other acceleration settings\n"
 
325
        if ($self->perimeter_acceleration || $self->infill_acceleration || $self->bridge_acceleration || $self->first_layer_acceleration)
 
326
            && !$self->default_acceleration;
 
327
    
 
328
    # --spiral-vase
 
329
    if ($self->spiral_vase) {
 
330
        # Note that we might want to have more than one perimeter on the bottom
 
331
        # solid layers.
 
332
        die "Can't make more than one perimeter when spiral vase mode is enabled\n"
 
333
            if $self->perimeters > 1;
 
334
        
 
335
        die "Can't make less than one perimeter when spiral vase mode is enabled\n"
 
336
            if $self->perimeters < 1;
 
337
        
 
338
        die "Spiral vase mode can only print hollow objects, so you need to set Fill density to 0\n"
 
339
            if $self->fill_density > 0;
 
340
        
 
341
        die "Spiral vase mode is not compatible with top solid layers\n"
 
342
            if $self->top_solid_layers > 0;
 
343
        
 
344
        die "Spiral vase mode is not compatible with support material\n"
 
345
            if $self->support_material || $self->support_material_enforce_layers > 0;
 
346
    }
 
347
    
 
348
    # extrusion widths
 
349
    {
 
350
        my $max_nozzle_diameter = max(@{ $self->nozzle_diameter });
 
351
        die "Invalid extrusion width (too large)\n"
 
352
            if defined first { $_ > 10 * $max_nozzle_diameter }
 
353
                map $self->get_abs_value_over("${_}_extrusion_width", $self->layer_height),
 
354
                qw(perimeter infill solid_infill top_infill support_material first_layer);
 
355
    }
 
356
    
 
357
    # general validation, quick and dirty
 
358
    foreach my $opt_key (@{$self->get_keys}) {
 
359
        my $opt = $Options->{$opt_key};
 
360
        next unless defined $self->$opt_key;
 
361
        next unless defined $opt->{cli} && $opt->{cli} =~ /=(.+)$/;
 
362
        my $type = $1;
 
363
        my @values = ();
 
364
        if ($type =~ s/\@$//) {
 
365
            die "Invalid value for $opt_key\n" if ref($self->$opt_key) ne 'ARRAY';
 
366
            @values = @{ $self->$opt_key };
 
367
        } else {
 
368
            @values = ($self->$opt_key);
 
369
        }
 
370
        foreach my $value (@values) {
 
371
            if ($type eq 'i' || $type eq 'f' || $opt->{type} eq 'percent') {
 
372
                $value =~ s/%$// if $opt->{type} eq 'percent';
 
373
                die "Invalid value for $opt_key\n"
 
374
                    if ($type eq 'i' && $value !~ /^-?\d+$/)
 
375
                    || (($type eq 'f' || $opt->{type} eq 'percent') && $value !~ /^-?(?:\d+|\d*\.\d+)$/)
 
376
                    || (defined $opt->{min} && $value < $opt->{min})
 
377
                    || (defined $opt->{max} && $value > $opt->{max});
 
378
            } elsif ($type eq 's' && $opt->{type} eq 'select') {
 
379
                die "Invalid value for $opt_key\n"
 
380
                    unless first { $_ eq $value } @{ $opt->{values} };
 
381
            }
 
382
        }
 
383
    }
 
384
    
 
385
    return 1;
 
386
}
 
387
 
 
388
# min object distance is max(duplicate_distance, clearance_radius)
 
389
sub min_object_distance {
 
390
    my $self = shift;
 
391
    
 
392
    return ($self->complete_objects && $self->extruder_clearance_radius > $self->duplicate_distance)
 
393
        ? $self->extruder_clearance_radius
 
394
        : $self->duplicate_distance;
 
395
}
 
396
 
 
397
# CLASS METHODS:
 
398
 
 
399
sub write_ini {
 
400
    my $class = shift;
 
401
    my ($file, $ini) = @_;
 
402
    
 
403
    Slic3r::open(\my $fh, '>', $file);
 
404
    binmode $fh, ':utf8';
 
405
    my $localtime = localtime;
 
406
    printf $fh "# generated by Slic3r $Slic3r::VERSION on %s\n", "$localtime";
 
407
    # make sure the _ category is the first one written
 
408
    foreach my $category (sort { ($a eq '_') ? -1 : ($a cmp $b) } keys %$ini) {
 
409
        printf $fh "\n[%s]\n", $category if $category ne '_';
 
410
        foreach my $key (sort keys %{$ini->{$category}}) {
 
411
            printf $fh "%s = %s\n", $key, $ini->{$category}{$key};
 
412
        }
 
413
    }
 
414
    close $fh;
 
415
}
 
416
 
 
417
sub read_ini {
 
418
    my $class = shift;
 
419
    my ($file) = @_;
 
420
    
 
421
    local $/ = "\n";
 
422
    Slic3r::open(\my $fh, '<', $file);
 
423
    binmode $fh, ':utf8';
 
424
    
 
425
    my $ini = { _ => {} };
 
426
    my $category = '_';
 
427
    while (<$fh>) {
 
428
        s/\R+$//;
 
429
        next if /^\s+/;
 
430
        next if /^$/;
 
431
        next if /^\s*#/;
 
432
        if (/^\[(.+?)\]$/) {
 
433
            $category = $1;
 
434
            next;
 
435
        }
 
436
        /^(\w+) = (.*)/ or die "Unreadable configuration file (invalid data at line $.)\n";
 
437
        $ini->{$category}{$1} = $2;
 
438
    }
 
439
    close $fh;
 
440
    
 
441
    return $ini;
 
442
}
 
443
 
 
444
package Slic3r::Config::Print;
 
445
use parent 'Slic3r::Config';
 
446
 
 
447
package Slic3r::Config::PrintObject;
 
448
use parent 'Slic3r::Config';
 
449
 
 
450
package Slic3r::Config::PrintRegion;
 
451
use parent 'Slic3r::Config';
 
452
 
 
453
package Slic3r::Config::Full;
 
454
use parent 'Slic3r::Config';
 
455
 
 
456
1;