From 1998801a58d06517f4f0d8dc2837278a7f5f4fc1 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sun, 20 May 2012 11:40:37 +0200 Subject: [PATCH] Read OBJ files. #324 --- MANIFEST | 1 + README.markdown | 4 ++-- lib/Slic3r.pm | 1 + lib/Slic3r/Format/OBJ.pm | 23 +++++++++++++++++++++++ lib/Slic3r/GUI/Plater.pm | 2 +- lib/Slic3r/GUI/SkeinPanel.pm | 4 ++-- lib/Slic3r/Print.pm | 6 +++++- 7 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 lib/Slic3r/Format/OBJ.pm diff --git a/MANIFEST b/MANIFEST index 0c0afd33..3ef71cec 100644 --- a/MANIFEST +++ b/MANIFEST @@ -20,6 +20,7 @@ lib/Slic3r/Fill/PlanePath.pm lib/Slic3r/Fill/Rectilinear.pm lib/Slic3r/Format/AMF.pm lib/Slic3r/Format/AMF/Parser.pm +lib/Slic3r/Format/OBJ.pm lib/Slic3r/Format/STL.pm lib/Slic3r/Geometry.pm lib/Slic3r/Geometry/Clipper.pm diff --git a/README.markdown b/README.markdown index 0f462301..f4badfb9 100644 --- a/README.markdown +++ b/README.markdown @@ -23,9 +23,9 @@ Slic3r current key features are: * multi-platform (Linux/Mac/Win) and packaged as standalone-app with no dependencies required; * 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; -* easy GUI; +* easy GUI with plating and manipulation facilities; * multithreaded; * multiple infill patterns, with customizable density and angle; * retraction; diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index bef33e02..be2e6dca 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -28,6 +28,7 @@ use Slic3r::ExtrusionPath::Arc; use Slic3r::ExtrusionPath::Collection; use Slic3r::Fill; use Slic3r::Format::AMF; +use Slic3r::Format::OBJ; use Slic3r::Format::STL; use Slic3r::Geometry qw(PI); use Slic3r::Layer; diff --git a/lib/Slic3r/Format/OBJ.pm b/lib/Slic3r/Format/OBJ.pm new file mode 100644 index 00000000..21085302 --- /dev/null +++ b/lib/Slic3r/Format/OBJ.pm @@ -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; diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index 2ee2e951..645a8126 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -205,7 +205,7 @@ sub load { my $self = shift; 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) { $dialog->Destroy; return; diff --git a/lib/Slic3r/GUI/SkeinPanel.pm b/lib/Slic3r/GUI/SkeinPanel.pm index 9e302f87..45473018 100644 --- a/lib/Slic3r/GUI/SkeinPanel.pm +++ b/lib/Slic3r/GUI/SkeinPanel.pm @@ -161,7 +161,7 @@ sub new { 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 $gcode_wildcard = "G-code files *.gcode|*.gcode;*.GCODE"; @@ -188,7 +188,7 @@ sub do_slice { my $input_file; 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) { $dialog->Destroy; return; diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm index d3975ebb..77251d50 100644 --- a/lib/Slic3r/Print.pm +++ b/lib/Slic3r/Print.pm @@ -29,12 +29,16 @@ sub add_object_from_file { my $mesh = Slic3r::Format::STL->read_file($input_file); $mesh->check_manifoldness; $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) { my ($materials, $meshes_by_material) = Slic3r::Format::AMF->read_file($input_file); $_->check_manifoldness for values %$meshes_by_material; $object = $self->add_object_from_mesh($meshes_by_material->{_} || +(values %$meshes_by_material)[0]); } 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); return $object;