Refactoring: move split logic in a single place (ModelObject class)

This commit is contained in:
Alessandro Ranellucci 2014-11-12 22:36:03 +01:00
parent e5cce32302
commit 9a4e8f39af
2 changed files with 49 additions and 75 deletions

View file

@ -158,46 +158,6 @@ sub _arrange {
);
}
# this method splits objects into multiple distinct objects by walking their meshes
sub split_meshes {
my $self = shift;
my @objects = @{$self->objects};
@{$self->objects} = ();
foreach my $object (@objects) {
if (@{$object->volumes} > 1) {
# We can't split meshes if there's more than one material, because
# we can't group the resulting meshes by object afterwards
$self->_add_object($object);
next;
}
my $volume = $object->volumes->[0];
foreach my $mesh (@{$volume->mesh->split}) {
my $new_object = $self->add_object(
input_file => $object->input_file,
config => $object->config->clone,
layer_height_ranges => $object->layer_height_ranges, # TODO: this needs to be cloned
origin_translation => $object->origin_translation,
);
$new_object->add_volume(
mesh => $mesh,
name => $volume->name,
material_id => $volume->material_id,
config => $volume->config,
);
# add one instance per original instance
$new_object->add_instance(
offset => Slic3r::Pointf->new(@{$_->offset}),
rotation => $_->rotation,
scaling_factor => $_->scaling_factor,
) for @{ $object->instances // [] };
}
}
}
sub print_info {
my $self = shift;
$_->print_info for @{$self->objects};
@ -299,6 +259,45 @@ sub add_instance {
}
}
sub split_object {
my ($self) = @_;
if (@{$self->volumes} > 1) {
# We can't split meshes if there's more than one volume, because
# we can't group the resulting meshes by object afterwards
my $o = $self->model->_add_object($self);
return [$o];
}
my @new_objects = ();
my $volume = $self->volumes->[0];
foreach my $mesh (@{$volume->mesh->split}) {
$mesh->repair;
push @new_objects, my $new_object = $self->model->add_object(
input_file => $self->input_file,
config => $self->config->clone,
layer_height_ranges => $self->layer_height_ranges, # TODO: this needs to be cloned
origin_translation => $self->origin_translation,
);
$new_object->add_volume(
mesh => $mesh,
name => $volume->name,
material_id => $volume->material_id,
config => $volume->config,
);
# add one instance per original instance
$new_object->add_instance(
offset => Slic3r::Pointf->new(@{$_->offset}),
rotation => $_->rotation,
scaling_factor => $_->scaling_factor,
) for @{ $self->instances };
}
return [@new_objects];
}
sub rotate {
my ($self, $angle, $axis) = @_;