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