From 4c96a520125241a212d159c63490a7e4f358f8cb Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Wed, 11 Sep 2013 14:46:38 +0200 Subject: [PATCH] Read OBJ and AMF --- lib/Slic3r/Format/AMF/Parser.pm | 13 ++++++++++--- lib/Slic3r/Format/OBJ.pm | 8 ++++++-- lib/Slic3r/Model.pm | 14 +++++++++++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/Slic3r/Format/AMF/Parser.pm b/lib/Slic3r/Format/AMF/Parser.pm index 19fc60e65..24732dbc8 100644 --- a/lib/Slic3r/Format/AMF/Parser.pm +++ b/lib/Slic3r/Format/AMF/Parser.pm @@ -20,6 +20,7 @@ sub start_element { if ($data->{LocalName} eq 'object') { $self->{_object} = $self->{_model}->add_object; + $self->{_object_vertices} = []; $self->{_objects_map}{ $self->_get_attribute($data, 'id') } = $#{ $self->{_model}->objects }; } elsif ($data->{LocalName} eq 'vertex') { $self->{_vertex} = ["", "", ""]; @@ -28,7 +29,9 @@ sub start_element { } elsif ($data->{LocalName} eq 'volume') { $self->{_volume} = $self->{_object}->add_volume( material_id => $self->_get_attribute($data, 'materialid') // undef, + mesh => Slic3r::TriangleMesh->new, ); + $self->{_volume_facets} = []; } elsif ($data->{LocalName} eq 'triangle') { $self->{_triangle} = ["", "", ""]; } elsif ($self->{_triangle} && $data->{LocalName} =~ /^v([123])$/ && $self->{_tree}[-1] eq 'triangle') { @@ -75,15 +78,19 @@ sub end_element { if ($data->{LocalName} eq 'object') { $self->{_object} = undef; + $self->{_object_vertices} = undef; } elsif ($data->{LocalName} eq 'vertex') { - push @{$self->{_object}->vertices}, $self->{_vertex}; + push @{$self->{_object_vertices}}, $self->{_vertex}; $self->{_vertex} = undef; } elsif ($self->{_coordinate} && $data->{LocalName} =~ /^[xyz]$/) { $self->{_coordinate} = undef; } elsif ($data->{LocalName} eq 'volume') { + $self->{_volume}->mesh->ReadFromPerl($self->{_object_vertices}, $self->{_volume_facets}); + $self->{_volume}->mesh->repair; $self->{_volume} = undef; + $self->{_volume_facets} = undef; } elsif ($data->{LocalName} eq 'triangle') { - push @{$self->{_volume}->facets}, $self->{_triangle}; + push @{$self->{_volume_facets}}, $self->{_triangle}; $self->{_triangle} = undef; } elsif (defined $self->{_vertex_idx} && $data->{LocalName} =~ /^v[123]$/) { $self->{_vertex_idx} = undef; @@ -113,7 +120,7 @@ sub end_document { foreach my $instance (@{ $self->{_instances}{$object_id} }) { $self->{_model}->objects->[$new_object_id]->add_instance( rotation => $instance->{rz} || 0, - offset => [ $instance->{deltax} || 0, $instance->{deltay} || 0 ], + offset => Slic3r::Point->new($instance->{deltax} || 0, $instance->{deltay} || 0), ); } } diff --git a/lib/Slic3r/Format/OBJ.pm b/lib/Slic3r/Format/OBJ.pm index 05a141db1..621fe24dc 100644 --- a/lib/Slic3r/Format/OBJ.pm +++ b/lib/Slic3r/Format/OBJ.pm @@ -17,9 +17,13 @@ sub read_file { } close $fh; + my $mesh = Slic3r::TriangleMesh->new; + $mesh->ReadFromPerl($vertices, $facets); + $mesh->repair; + my $model = Slic3r::Model->new; - my $object = $model->add_object(vertices => $vertices); - my $volume = $object->add_volume(facets => $facets); + my $object = $model->add_object; + my $volume = $object->add_volume(mesh => $mesh); return $model; } diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm index 2be84e227..d7c8f235e 100644 --- a/lib/Slic3r/Model.pm +++ b/lib/Slic3r/Model.pm @@ -25,7 +25,9 @@ sub merge { my $class = shift; my @models = @_; - my $new_model = $class->new; + my $new_model = ref($class) + ? $class + : $class->new; foreach my $model (@models) { # merge material attributes (should we rename them in case of duplicates?) $new_model->set_material($_, { %{$model->materials->{$_}}, %{$model->materials->{$_} || {}} }) @@ -34,14 +36,13 @@ sub merge { foreach my $object (@{$model->objects}) { my $new_object = $new_model->add_object( input_file => $object->input_file, - vertices => $object->vertices, config => $object->config, layer_height_ranges => $object->layer_height_ranges, ); $new_object->add_volume( material_id => $_->material_id, - facets => $_->facets, + mesh => $_->mesh->clone, ) for @{$object->volumes}; $new_object->add_instance( @@ -431,6 +432,13 @@ sub needed_repair { return (first { !$_->mesh->needed_repair } @{$self->volumes}) ? 0 : 1; } +sub mesh_stats { + my $self = shift; + + # TODO: sum values from all volumes + return $self->volumes->[0]->mesh->stats; +} + sub print_info { my $self = shift;