2016-09-13 09:24:55 +00:00
|
|
|
# Extends C++ class Slic3r::DynamicPrintConfig
|
|
|
|
# This perl class does not keep any perl class variables,
|
|
|
|
# all the storage is handled by the underlying C++ code.
|
2011-10-03 09:55:32 +00:00
|
|
|
package Slic3r::Config;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
2011-10-09 20:29:13 +00:00
|
|
|
use utf8;
|
2011-10-03 09:55:32 +00:00
|
|
|
|
2014-01-11 13:30:34 +00:00
|
|
|
use List::Util qw(first max);
|
2012-09-28 12:13:06 +00:00
|
|
|
|
2016-09-13 09:24:55 +00:00
|
|
|
# C++ Slic3r::PrintConfigDef exported as a Perl hash of hashes.
|
|
|
|
# The C++ counterpart is a constant singleton.
|
2013-12-21 23:39:03 +00:00
|
|
|
our $Options = print_config_def();
|
2013-12-21 20:06:45 +00:00
|
|
|
|
2014-04-05 08:58:03 +00:00
|
|
|
# overwrite the hard-coded readonly value (this information is not available in XS)
|
|
|
|
$Options->{threads}{readonly} = !$Slic3r::have_threads;
|
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
# generate accessors
|
2013-12-21 20:06:45 +00:00
|
|
|
{
|
2011-10-05 16:13:47 +00:00
|
|
|
no strict 'refs';
|
2012-07-27 19:13:03 +00:00
|
|
|
for my $opt_key (keys %$Options) {
|
2014-01-02 16:24:23 +00:00
|
|
|
*{$opt_key} = sub { $_[0]->get($opt_key) };
|
2012-07-27 19:13:03 +00:00
|
|
|
}
|
2011-10-05 16:13:47 +00:00
|
|
|
}
|
2014-06-16 13:18:39 +00:00
|
|
|
|
2017-10-17 14:01:18 +00:00
|
|
|
# From command line parameters, used by slic3r.pl
|
2012-07-27 19:13:03 +00:00
|
|
|
sub new_from_cli {
|
2011-10-05 16:13:47 +00:00
|
|
|
my $class = shift;
|
2012-07-27 19:13:03 +00:00
|
|
|
my %args = @_;
|
2011-10-05 16:13:47 +00:00
|
|
|
|
2016-09-13 09:24:55 +00:00
|
|
|
# Delete hash keys with undefined value.
|
2012-07-27 19:13:03 +00:00
|
|
|
delete $args{$_} for grep !defined $args{$_}, keys %args;
|
|
|
|
|
2016-09-13 09:24:55 +00:00
|
|
|
# Replace the start_gcode, end_gcode ... hash values
|
|
|
|
# with the content of the files they reference.
|
2012-12-23 15:29:08 +00:00
|
|
|
for (qw(start end layer toolchange)) {
|
2012-07-27 19:13:03 +00:00
|
|
|
my $opt_key = "${_}_gcode";
|
|
|
|
if ($args{$opt_key}) {
|
2013-06-23 17:25:02 +00:00
|
|
|
if (-e $args{$opt_key}) {
|
|
|
|
Slic3r::open(\my $fh, "<", $args{$opt_key})
|
|
|
|
or die "Failed to open $args{$opt_key}\n";
|
|
|
|
binmode $fh, ':utf8';
|
|
|
|
$args{$opt_key} = do { local $/; <$fh> };
|
|
|
|
close $fh;
|
|
|
|
}
|
2012-06-19 16:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-13 09:24:55 +00:00
|
|
|
|
2013-12-21 20:06:45 +00:00
|
|
|
my $self = $class->new;
|
|
|
|
foreach my $opt_key (keys %args) {
|
2014-04-19 17:14:41 +00:00
|
|
|
my $opt_def = $Options->{$opt_key};
|
|
|
|
|
|
|
|
# we use set_deserialize() for bool options since GetOpt::Long doesn't handle
|
|
|
|
# arrays of boolean values
|
2014-06-16 21:58:45 +00:00
|
|
|
if ($opt_key =~ /^(?:bed_shape|duplicate_grid|extruder_offset)$/ || $opt_def->{type} eq 'bool') {
|
2013-12-21 20:06:45 +00:00
|
|
|
$self->set_deserialize($opt_key, $args{$opt_key});
|
2014-04-19 17:14:41 +00:00
|
|
|
} elsif (my $shortcut = $opt_def->{shortcut}) {
|
2014-03-25 23:08:15 +00:00
|
|
|
$self->set($_, $args{$opt_key}) for @$shortcut;
|
2013-12-21 20:06:45 +00:00
|
|
|
} else {
|
|
|
|
$self->set($opt_key, $args{$opt_key});
|
|
|
|
}
|
|
|
|
}
|
2013-12-21 23:39:03 +00:00
|
|
|
|
|
|
|
return $self;
|
2012-06-19 16:11:51 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 19:13:03 +00:00
|
|
|
# CLASS METHODS:
|
|
|
|
|
2016-09-13 09:24:55 +00:00
|
|
|
# Write a "Windows" style ini file with categories enclosed in squre brackets.
|
2017-10-17 14:01:18 +00:00
|
|
|
# Used by config-bundle-to-config.pl and to save slic3r.ini.
|
2012-07-27 19:13:03 +00:00
|
|
|
sub write_ini {
|
|
|
|
my $class = shift;
|
|
|
|
my ($file, $ini) = @_;
|
|
|
|
|
2013-01-13 09:18:34 +00:00
|
|
|
Slic3r::open(\my $fh, '>', $file);
|
2012-07-27 19:13:03 +00:00
|
|
|
binmode $fh, ':utf8';
|
|
|
|
my $localtime = localtime;
|
|
|
|
printf $fh "# generated by Slic3r $Slic3r::VERSION on %s\n", "$localtime";
|
2014-03-25 13:04:01 +00:00
|
|
|
# make sure the _ category is the first one written
|
|
|
|
foreach my $category (sort { ($a eq '_') ? -1 : ($a cmp $b) } keys %$ini) {
|
2012-07-27 19:13:03 +00:00
|
|
|
printf $fh "\n[%s]\n", $category if $category ne '_';
|
|
|
|
foreach my $key (sort keys %{$ini->{$category}}) {
|
|
|
|
printf $fh "%s = %s\n", $key, $ini->{$category}{$key};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
2016-09-13 09:24:55 +00:00
|
|
|
# Parse a "Windows" style ini file with categories enclosed in squre brackets.
|
|
|
|
# Returns a hash of hashes over strings.
|
|
|
|
# {category}{name}=value
|
|
|
|
# Non-categorized entries are stored under a category '_'.
|
2017-10-17 14:01:18 +00:00
|
|
|
# Used by config-bundle-to-config.pl and to read slic3r.ini.
|
2012-07-27 19:13:03 +00:00
|
|
|
sub read_ini {
|
|
|
|
my $class = shift;
|
|
|
|
my ($file) = @_;
|
|
|
|
|
|
|
|
local $/ = "\n";
|
2014-11-22 20:55:45 +00:00
|
|
|
Slic3r::open(\my $fh, '<', $file)
|
|
|
|
or die "Unable to open $file: $!\n";
|
2012-07-27 19:13:03 +00:00
|
|
|
binmode $fh, ':utf8';
|
|
|
|
|
|
|
|
my $ini = { _ => {} };
|
|
|
|
my $category = '_';
|
2013-06-06 08:46:58 +00:00
|
|
|
while (<$fh>) {
|
2012-07-27 19:13:03 +00:00
|
|
|
s/\R+$//;
|
|
|
|
next if /^\s+/;
|
|
|
|
next if /^$/;
|
|
|
|
next if /^\s*#/;
|
2014-03-25 13:04:01 +00:00
|
|
|
if (/^\[(.+?)\]$/) {
|
2012-07-27 19:13:03 +00:00
|
|
|
$category = $1;
|
|
|
|
next;
|
|
|
|
}
|
2014-11-06 20:08:55 +00:00
|
|
|
/^(\w+) *= *(.*)/ or die "Unreadable configuration file (invalid data at line $.)\n";
|
2012-07-27 19:13:03 +00:00
|
|
|
$ini->{$category}{$1} = $2;
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
|
|
|
|
return $ini;
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:33:19 +00:00
|
|
|
package Slic3r::Config::Static;
|
2014-10-18 15:41:21 +00:00
|
|
|
use parent 'Slic3r::Config';
|
|
|
|
|
2015-12-16 11:33:19 +00:00
|
|
|
sub Slic3r::Config::GCode::new { Slic3r::Config::Static::new_GCodeConfig }
|
|
|
|
sub Slic3r::Config::Print::new { Slic3r::Config::Static::new_PrintConfig }
|
|
|
|
sub Slic3r::Config::PrintObject::new { Slic3r::Config::Static::new_PrintObjectConfig }
|
|
|
|
sub Slic3r::Config::PrintRegion::new { Slic3r::Config::Static::new_PrintRegionConfig }
|
|
|
|
sub Slic3r::Config::Full::new { Slic3r::Config::Static::new_FullPrintConfig }
|
2014-01-02 09:44:54 +00:00
|
|
|
|
2011-10-03 09:55:32 +00:00
|
|
|
1;
|