Read OBJ and AMF

This commit is contained in:
Alessandro Ranellucci 2013-09-11 14:46:38 +02:00
parent a821eb7f3c
commit 4c96a52012
3 changed files with 27 additions and 8 deletions

View File

@ -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),
);
}
}

View File

@ -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;
}

View File

@ -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;