2012-02-25 17:35:25 +01:00
|
|
|
package Slic3r::Format::STL;
|
2011-09-06 11:50:43 +02:00
|
|
|
use Moo;
|
2011-09-01 21:06:28 +02:00
|
|
|
|
2014-02-14 22:24:30 +01:00
|
|
|
use File::Basename qw(basename);
|
|
|
|
|
2011-10-12 10:47:26 +02:00
|
|
|
sub read_file {
|
|
|
|
my $self = shift;
|
|
|
|
my ($file) = @_;
|
|
|
|
|
2014-03-25 19:06:51 +01:00
|
|
|
my $path = Slic3r::encode_path($file);
|
|
|
|
die "Failed to open $file\n" if !-e $path;
|
|
|
|
|
2013-09-11 09:49:28 +02:00
|
|
|
my $mesh = Slic3r::TriangleMesh->new;
|
2014-03-25 19:06:51 +01:00
|
|
|
$mesh->ReadSTLFile($path);
|
2013-09-11 09:49:28 +02:00
|
|
|
$mesh->repair;
|
2012-02-17 13:49:33 +01:00
|
|
|
|
2015-01-18 12:12:10 +01:00
|
|
|
die "This STL file couldn't be read because it's empty.\n"
|
|
|
|
if $mesh->facets_count == 0;
|
|
|
|
|
2012-08-29 16:49:38 +02:00
|
|
|
my $model = Slic3r::Model->new;
|
2014-02-14 22:24:30 +01:00
|
|
|
|
2014-07-12 11:20:57 +02:00
|
|
|
my $basename = basename($file);
|
|
|
|
my $object = $model->add_object(input_file => $file, name => $basename);
|
|
|
|
my $volume = $object->add_volume(mesh => $mesh, name => $basename);
|
2012-08-29 16:49:38 +02:00
|
|
|
return $model;
|
2011-10-12 10:47:26 +02:00
|
|
|
}
|
|
|
|
|
2012-01-28 15:05:42 +01:00
|
|
|
sub write_file {
|
|
|
|
my $self = shift;
|
2014-04-25 10:20:30 +02:00
|
|
|
my ($file, $mesh, %params) = @_;
|
|
|
|
|
|
|
|
$mesh = $mesh->mesh if $mesh->isa('Slic3r::Model');
|
2012-01-28 15:05:42 +01:00
|
|
|
|
2013-09-11 09:49:28 +02:00
|
|
|
my $path = Slic3r::encode_path($file);
|
2012-01-28 15:05:42 +01:00
|
|
|
|
2012-08-29 16:49:38 +02:00
|
|
|
$params{binary}
|
2014-04-25 10:20:30 +02:00
|
|
|
? $mesh->write_binary($path)
|
|
|
|
: $mesh->write_ascii($path);
|
2012-02-19 15:45:27 +01:00
|
|
|
}
|
|
|
|
|
2011-09-01 21:06:28 +02:00
|
|
|
1;
|