Adapt plater to the new split config

This commit is contained in:
Alessandro Ranellucci 2014-01-02 22:06:58 +01:00
parent bfa2ee2770
commit 81663215c5
11 changed files with 153 additions and 65 deletions

View file

@ -76,7 +76,7 @@ sub load {
my ($file) = @_;
my $ini = __PACKAGE__->read_ini($file);
my $config = __PACKAGE__->new;
my $config = $class->new;
foreach my $opt_key (keys %{$ini->{_}}) {
($opt_key, my $value) = _handle_legacy($opt_key, $ini->{_}{$opt_key});
next if !defined $opt_key;
@ -88,7 +88,7 @@ sub load {
sub clone {
my $self = shift;
my $new = __PACKAGE__->new;
my $new = (ref $self)->new;
$new->apply($self);
return $new;
}
@ -180,13 +180,14 @@ sub equals {
return @{ $self->diff($other) } == 0;
}
# this will *ignore* options not present in both configs
sub diff {
my ($self, $other) = @_;
my @diff = ();
foreach my $opt_key (sort @{$self->get_keys}) {
push @diff, $opt_key
if !$other->has($opt_key) || $other->serialize($opt_key) ne $self->serialize($opt_key);
if $other->has($opt_key) && $other->serialize($opt_key) ne $self->serialize($opt_key);
}
return [@diff];
}
@ -265,27 +266,11 @@ sub validate {
die "Invalid value for --infill-every-layers\n"
if $self->infill_every_layers !~ /^\d+$/ || $self->infill_every_layers < 1;
# --scale
die "Invalid value for --scale\n"
if $self->scale <= 0;
# --bed-size
die "Invalid value for --bed-size\n"
if !ref $self->bed_size
&& (!$self->bed_size || $self->bed_size !~ /^\d+,\d+$/);
# --duplicate-grid
die "Invalid value for --duplicate-grid\n"
if !ref $self->duplicate_grid
&& (!$self->duplicate_grid || $self->duplicate_grid !~ /^\d+,\d+$/);
# --duplicate
die "Invalid value for --duplicate or --duplicate-grid\n"
if !$self->duplicate || $self->duplicate < 1 || !$self->duplicate_grid
|| (grep !$_, @{$self->duplicate_grid});
die "Use either --duplicate or --duplicate-grid (using both doesn't make sense)\n"
if $self->duplicate > 1 && $self->duplicate_grid && (grep $_ && $_ > 1, @{$self->duplicate_grid});
# --skirt-height
die "Invalid value for --skirt-height\n"
if $self->skirt_height < -1; # -1 means as tall as the object

View file

@ -754,11 +754,15 @@ sub export_gcode2 {
} if $Slic3r::have_threads;
my $print = $self->{print};
$print->apply_config($config);
$print->apply_extra_variables($extra_variables);
eval {
$print->config->validate;
# this will throw errors if config is not valid
$config->validate;
$print->apply_config($config);
$print->apply_extra_variables($extra_variables);
$print->validate;
{

View file

@ -114,7 +114,7 @@ sub update_optgroup {
my $config = $self->model_object->config;
my %categories = ();
foreach my $opt_key (keys %$config) {
foreach my $opt_key (@{$config->get_keys}) {
my $category = $Slic3r::Config::Options->{$opt_key}{category};
$categories{$category} ||= [];
push @{$categories{$category}}, $opt_key;
@ -288,31 +288,41 @@ sub new {
# get unique materials used in this object
$self->{materials} = [ $self->model_object->unique_materials ];
# build an OptionsGroup
$self->{mapping} = {
(map { $self->{materials}[$_] => $_+1 } 0..$#{ $self->{materials} }), # defaults
%{$self->model_object->material_mapping},
};
my $optgroup = Slic3r::GUI::OptionsGroup->new(
parent => $self,
title => 'Extruders',
label_width => 300,
options => [
map {
my $i = $_;
my $material_id = $self->{materials}[$i];
{
opt_key => "material_extruder_$_",
type => 'i',
label => $self->model_object->model->get_material_name($material_id),
min => 1,
default => $self->{mapping}{$material_id},
on_change => sub { $self->{mapping}{$material_id} = $_[0] },
}
} 0..$#{ $self->{materials} }
],
);
$self->{sizer}->Add($optgroup->sizer, 0, wxEXPAND | wxALL, 10);
# get the current mapping
$self->{mapping} = {};
foreach my $material_id (@{ $self->{materials}}) {
my $config = $self->model_object->model->materials->{ $material_id }->config;
$self->{mapping}{$material_id} = ($config->perimeter_extruder // 0) + 1;
}
if (@{$self->{materials}} > 0) {
# build an OptionsGroup
my $optgroup = Slic3r::GUI::OptionsGroup->new(
parent => $self,
title => 'Extruders',
label_width => 300,
options => [
map {
my $i = $_;
my $material_id = $self->{materials}[$i];
{
opt_key => "material_extruder_$_",
type => 'i',
label => $self->model_object->model->get_material_name($material_id),
min => 1,
default => $self->{mapping}{$material_id} // 1,
on_change => sub { $self->{mapping}{$material_id} = $_[0] },
}
} 0..$#{ $self->{materials} }
],
);
$self->{sizer}->Add($optgroup->sizer, 0, wxEXPAND | wxALL, 10);
} else {
my $label = Wx::StaticText->new($self, -1, "This object does not contain named materials.",
wxDefaultPosition, [-1, 25]);
$label->SetFont(Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
$self->{sizer}->Add($label, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 10);
}
$self->SetSizer($self->{sizer});
$self->{sizer}->SetSizeHints($self);
@ -324,10 +334,10 @@ sub Closing {
my $self = shift;
# save mappings into the plater object
foreach my $volume (@{$self->model_object}) {
foreach my $volume (@{$self->model_object->volumes}) {
if (defined $volume->material_id) {
my $config = $self->model_object->model->materials->{ $volume->material_id }->config;
$config->set('extruder', $self->{mapping}{ $volume->material_id });
$config->set('extruder', $self->{mapping}{ $volume->material_id }-1);
}
}
}

View file

@ -54,10 +54,11 @@ sub add_object {
if (defined $volume->material_id) {
# merge material attributes (should we rename materials in case of duplicates?)
$self->set_material($volume->material_id, {
%{ $object->model->materials->{$volume->material_id} },
%{ $self->materials->{$volume->material_id} || {} },
});
my %attributes = %{ $object->model->materials->{$volume->material_id}->attributes };
if (exists $self->materials->{$volume->material_id}) {
%attributes = (%attributes, %{ $self->materials->{$volume->material_id}->attributes });
}
$self->set_material($volume->material_id, {%attributes});
}
}
@ -478,7 +479,8 @@ sub unique_materials {
my $self = shift;
my %materials = ();
$materials{ $_->material_id // '_' } = 1 for @{$self->volumes};
$materials{ $_->material_id } = 1
for grep { defined $_->material_id } @{$self->volumes};
return sort keys %materials;
}

View file

@ -31,9 +31,87 @@ has 'brim' => (is => 'rw', default => sub { Slic3r::ExtrusionPath::Collection->n
sub apply_config {
my ($self, $config) = @_;
$self->config->apply_dynamic($config);
# handle changes to print config
my $print_diff = $self->config->diff($config);
if (@$print_diff) {
$self->config->apply_dynamic($config);
# TODO: only invalidate changed steps
$self->_state->invalidate_all;
}
# handle changes to object config defaults
$self->default_object_config->apply_dynamic($config);
foreach my $object (@{$self->objects}) {
# we don't assume that $config contains a full ObjectConfig,
# so we base it on the current print-wise default
my $new = $self->default_object_config->clone;
# we override the new config with object-specific options
$new->apply_dynamic($object->model_object->config);
# check whether the new config is different from the current one
my $diff = $object->config->diff($new);
if (@$diff) {
$object->config->apply($new);
# TODO: only invalidate changed steps
$object->_state->invalidate_all;
}
}
# handle changes to regions config defaults
$self->default_region_config->apply_dynamic($config);
# check whether after applying the new region config defaults to all existing regions
# they still have distinct configs; if not we need to re-add objects in order to
# merge the now-equal regions
# first compute the transformed region configs
my @new_region_configs = ();
foreach my $region_id (0..$#{$self->regions}) {
my $new = $self->default_region_config->clone;
foreach my $object (@{$self->objects}) {
foreach my $volume_id (@{ $object->region_volumes->[$region_id] }) {
my $volume = $object->model_object->volumes->[$volume_id];
next if !defined $volume->material_id;
my $material = $object->model_object->model->materials->{$volume->material_id};
$new->apply_dynamic($material->config);
}
}
push @new_region_configs, $new;
}
# then find the first pair of identical configs
my $have_identical_configs = 0;
my $region_diff = [];
for my $i (0..$#new_region_configs) {
for my $j (($i+1)..$#new_region_configs) {
if ($new_region_configs[$i]->equals($new_region_configs[$j])) {
$have_identical_configs = 1;
}
}
my $diff = $self->regions->[$i]->config->diff($new_region_configs[$i]);
push @$region_diff, @$diff;
}
if ($have_identical_configs) {
# okay, the current subdivision of regions does not make sense anymore.
# we need to remove all objects and re-add them
my @model_objects = map $_->model_object, @{$self->object};
$self->delete_all_objects;
$self->add_model_object($_) for @model_objects;
} elsif (@$region_diff > 0) {
# if there are no identical regions even after applying the change in
# region config defaults, but at least one region config option changed,
# store the new region configs and invalidate
# the affected step(s)
foreach my $region_id (0..$#{$self->regions}) {
$self->regions->[$region_id]->config->apply($new_region_configs[$region_id]);
}
# TODO: only invalidate changed steps
$_->_state->invalidate_all for @{$self->objects};
}
}
sub has_support_material {
@ -991,7 +1069,7 @@ sub auto_assign_extruders {
if (defined $volume->material_id) {
my $material = $model_object->model->materials->{ $volume->material_id };
my $config = $material->config;
$config->set_ifndef('perimeters_extruder', $i);
$config->set_ifndef('perimeter_extruder', $i);
$config->set_ifndef('infill_extruder', $i);
$config->set_ifndef('support_material_extruder', $i);
$config->set_ifndef('support_material_interface_extruder', $i);