Read OBJ files. #324

This commit is contained in:
Alessandro Ranellucci 2012-05-20 11:40:37 +02:00
parent fbe70ac15b
commit 1998801a58
7 changed files with 35 additions and 6 deletions

View File

@ -20,6 +20,7 @@ lib/Slic3r/Fill/PlanePath.pm
lib/Slic3r/Fill/Rectilinear.pm lib/Slic3r/Fill/Rectilinear.pm
lib/Slic3r/Format/AMF.pm lib/Slic3r/Format/AMF.pm
lib/Slic3r/Format/AMF/Parser.pm lib/Slic3r/Format/AMF/Parser.pm
lib/Slic3r/Format/OBJ.pm
lib/Slic3r/Format/STL.pm lib/Slic3r/Format/STL.pm
lib/Slic3r/Geometry.pm lib/Slic3r/Geometry.pm
lib/Slic3r/Geometry/Clipper.pm lib/Slic3r/Geometry/Clipper.pm

View File

@ -23,9 +23,9 @@ Slic3r current key features are:
* multi-platform (Linux/Mac/Win) and packaged as standalone-app with no dependencies required; * multi-platform (Linux/Mac/Win) and packaged as standalone-app with no dependencies required;
* easy configuration/calibration; * easy configuration/calibration;
* read binary and ASCII STL files as well as AMF; * read binary and ASCII STL files as well as OBJ and AMF;
* powerful command line interface; * powerful command line interface;
* easy GUI; * easy GUI with plating and manipulation facilities;
* multithreaded; * multithreaded;
* multiple infill patterns, with customizable density and angle; * multiple infill patterns, with customizable density and angle;
* retraction; * retraction;

View File

@ -28,6 +28,7 @@ use Slic3r::ExtrusionPath::Arc;
use Slic3r::ExtrusionPath::Collection; use Slic3r::ExtrusionPath::Collection;
use Slic3r::Fill; use Slic3r::Fill;
use Slic3r::Format::AMF; use Slic3r::Format::AMF;
use Slic3r::Format::OBJ;
use Slic3r::Format::STL; use Slic3r::Format::STL;
use Slic3r::Geometry qw(PI); use Slic3r::Geometry qw(PI);
use Slic3r::Layer; use Slic3r::Layer;

23
lib/Slic3r/Format/OBJ.pm Normal file
View File

@ -0,0 +1,23 @@
package Slic3r::Format::OBJ;
use Moo;
sub read_file {
my $self = shift;
my ($file) = @_;
open my $fh, '<', $file or die "Failed to open $file\n";
my $vertices = [];
my $facets = [];
while (my $_ = <$fh>) {
if (/^v ([^ ]+)\s+([^ ]+)\s+([^ ]+)/) {
push @$vertices, [$1, $2, $3];
} elsif (/^f (\d+).*? (\d+).*? (\d+).*?/) {
push @$facets, [ $1-1, $2-1, $3-1 ];
}
}
close $fh;
return Slic3r::TriangleMesh->new(vertices => $vertices, facets => $facets);
}
1;

View File

@ -205,7 +205,7 @@ sub load {
my $self = shift; my $self = shift;
my $dir = $Slic3r::GUI::SkeinPanel::last_skein_dir || $Slic3r::GUI::SkeinPanel::last_config_dir || ""; my $dir = $Slic3r::GUI::SkeinPanel::last_skein_dir || $Slic3r::GUI::SkeinPanel::last_config_dir || "";
my $dialog = Wx::FileDialog->new($self, 'Choose a STL or AMF file:', $dir, "", $Slic3r::GUI::SkeinPanel::model_wildcard, wxFD_OPEN); my $dialog = Wx::FileDialog->new($self, 'Choose a file (STL/OBJ/AMF):', $dir, "", $Slic3r::GUI::SkeinPanel::model_wildcard, wxFD_OPEN);
if ($dialog->ShowModal != wxID_OK) { if ($dialog->ShowModal != wxID_OK) {
$dialog->Destroy; $dialog->Destroy;
return; return;

View File

@ -161,7 +161,7 @@ sub new {
return $self; return $self;
} }
our $model_wildcard = "STL files (*.stl)|*.stl;*.STL|AMF files (*.amf)|*.amf;*.AMF;*.xml;*.XML"; our $model_wildcard = "STL files (*.stl)|*.stl;*.STL|OBJ files (*.obj)|*.obj;*.OBJ|AMF files (*.amf)|*.amf;*.AMF;*.xml;*.XML";
our $ini_wildcard = "INI files *.ini|*.ini;*.INI"; our $ini_wildcard = "INI files *.ini|*.ini;*.INI";
our $gcode_wildcard = "G-code files *.gcode|*.gcode;*.GCODE"; our $gcode_wildcard = "G-code files *.gcode|*.gcode;*.GCODE";
@ -188,7 +188,7 @@ sub do_slice {
my $input_file; my $input_file;
if (!$params{reslice}) { if (!$params{reslice}) {
my $dialog = Wx::FileDialog->new($self, 'Choose a STL or AMF file to slice:', $dir, "", $model_wildcard, wxFD_OPEN); my $dialog = Wx::FileDialog->new($self, 'Choose a file to slice (STL/OBJ/AMF):', $dir, "", $model_wildcard, wxFD_OPEN);
if ($dialog->ShowModal != wxID_OK) { if ($dialog->ShowModal != wxID_OK) {
$dialog->Destroy; $dialog->Destroy;
return; return;

View File

@ -29,12 +29,16 @@ sub add_object_from_file {
my $mesh = Slic3r::Format::STL->read_file($input_file); my $mesh = Slic3r::Format::STL->read_file($input_file);
$mesh->check_manifoldness; $mesh->check_manifoldness;
$object = $self->add_object_from_mesh($mesh); $object = $self->add_object_from_mesh($mesh);
} elsif ($input_file =~ /\.obj$/i) {
my $mesh = Slic3r::Format::OBJ->read_file($input_file);
$mesh->check_manifoldness;
$object = $self->add_object_from_mesh($mesh);
} elsif ( $input_file =~ /\.amf(\.xml)?$/i) { } elsif ( $input_file =~ /\.amf(\.xml)?$/i) {
my ($materials, $meshes_by_material) = Slic3r::Format::AMF->read_file($input_file); my ($materials, $meshes_by_material) = Slic3r::Format::AMF->read_file($input_file);
$_->check_manifoldness for values %$meshes_by_material; $_->check_manifoldness for values %$meshes_by_material;
$object = $self->add_object_from_mesh($meshes_by_material->{_} || +(values %$meshes_by_material)[0]); $object = $self->add_object_from_mesh($meshes_by_material->{_} || +(values %$meshes_by_material)[0]);
} else { } else {
die "Input file must have .stl or .amf(.xml) extension\n"; die "Input file must have .stl, .obj or .amf(.xml) extension\n";
} }
$object->input_file($input_file); $object->input_file($input_file);
return $object; return $object;