Refactoring: new Slic3r::Model class to represent files

This commit is contained in:
Alessandro Ranellucci 2012-08-29 16:49:38 +02:00
parent c0322ec703
commit f90520ed06
14 changed files with 255 additions and 88 deletions

View file

@ -296,7 +296,7 @@ sub load_file {
my $process_dialog = Wx::ProgressDialog->new('Loading…', "Processing input file…", 100, $self, 0);
$process_dialog->Pulse;
local $SIG{__WARN__} = Slic3r::GUI::warning_catcher($self);
$self->{print}->add_object_from_file($input_file);
$self->{print}->add_objects_from_file($input_file);
my $obj_idx = $#{$self->{print}->objects};
$process_dialog->Destroy;
@ -630,13 +630,11 @@ sub on_export_failed {
sub export_stl {
my $self = shift;
my $print = $self->{print};
# select output file
my $output_file = $main::opt{output};
{
$output_file = $print->expanded_output_filepath($output_file);
$output_file = $self->{print}->expanded_output_filepath($output_file);
$output_file =~ s/\.gcode$/.stl/i;
my $dlg = Wx::FileDialog->new($self, 'Save STL file as:', dirname($output_file),
basename($output_file), $Slic3r::GUI::SkeinPanel::model_wildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
@ -648,21 +646,26 @@ sub export_stl {
$dlg->Destroy;
}
my $mesh = Slic3r::TriangleMesh->new(facets => [], vertices => []);
for my $obj_idx (0 .. $#{$print->objects}) {
for my $copy (@{$print->copies->[$obj_idx]}) {
my $cloned_mesh = $print->objects->[$obj_idx]->mesh->clone;
$cloned_mesh->move(@$copy);
my $vertices_offset = scalar @{$mesh->vertices};
push @{$mesh->vertices}, @{$cloned_mesh->vertices};
push @{$mesh->facets}, map [ $_->[0], map $vertices_offset + $_, @$_[-3..-1] ], @{$cloned_mesh->facets};
Slic3r::Format::STL->write_file($output_file, $self->make_model, binary => 1);
$self->statusbar->SetStatusText("STL file exported to $output_file");
}
sub make_model {
my $self = shift;
my $model = Slic3r::Model->new;
for my $obj_idx (0 .. $#{$self->{print}->objects}) {
my $mesh = $self->{print}->objects->[$obj_idx]->mesh->clone;
$mesh->scale(&Slic3r::SCALING_FACTOR);
my $object = $model->add_object(vertices => $mesh->vertices);
$object->add_volume(facets => $mesh->facets);
for my $copy (@{$self->{print}->copies->[$obj_idx]}) {
$object->add_instance(rotation => 0, offset => [ map unscale $_, @$copy ]);
}
}
$mesh->scale(&Slic3r::SCALING_FACTOR);
$mesh->align_to_origin;
# TODO: $model->align_to_origin;
Slic3r::Format::STL->write_file($output_file, $mesh, 1);
$self->statusbar->SetStatusText("STL file exported to $output_file");
return $model;
}
sub make_thumbnail {