2012-05-20 09:40:37 +00:00
|
|
|
package Slic3r::Format::OBJ;
|
|
|
|
use Moo;
|
|
|
|
|
2014-03-25 14:39:13 +00:00
|
|
|
use File::Basename qw(basename);
|
|
|
|
|
2012-05-20 09:40:37 +00:00
|
|
|
sub read_file {
|
|
|
|
my $self = shift;
|
|
|
|
my ($file) = @_;
|
|
|
|
|
2013-01-13 09:18:34 +00:00
|
|
|
Slic3r::open(\my $fh, '<', $file) or die "Failed to open $file\n";
|
2012-05-20 09:40:37 +00:00
|
|
|
my $vertices = [];
|
|
|
|
my $facets = [];
|
2013-06-06 08:46:58 +00:00
|
|
|
while (<$fh>) {
|
2012-05-20 09:40:37 +00:00
|
|
|
if (/^v ([^ ]+)\s+([^ ]+)\s+([^ ]+)/) {
|
|
|
|
push @$vertices, [$1, $2, $3];
|
|
|
|
} elsif (/^f (\d+).*? (\d+).*? (\d+).*?/) {
|
|
|
|
push @$facets, [ $1-1, $2-1, $3-1 ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
|
2013-09-11 12:46:38 +00:00
|
|
|
my $mesh = Slic3r::TriangleMesh->new;
|
|
|
|
$mesh->ReadFromPerl($vertices, $facets);
|
|
|
|
$mesh->repair;
|
|
|
|
|
2012-08-29 14:49:38 +00:00
|
|
|
my $model = Slic3r::Model->new;
|
2014-03-25 14:39:13 +00:00
|
|
|
|
|
|
|
my $material_id = basename($file);
|
|
|
|
$model->set_material($material_id);
|
|
|
|
|
2013-09-11 12:46:38 +00:00
|
|
|
my $object = $model->add_object;
|
2014-03-25 14:39:13 +00:00
|
|
|
my $volume = $object->add_volume(mesh => $mesh, material_id => $material_id);
|
2012-08-29 14:49:38 +00:00
|
|
|
return $model;
|
2012-05-20 09:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|