2012-02-25 16:35:25 +00:00
|
|
|
package Slic3r::Format::AMF::Parser;
|
2012-02-19 14:14:54 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2012-02-26 17:17:23 +00:00
|
|
|
use XML::SAX::PurePerl;
|
2012-02-20 15:43:45 +00:00
|
|
|
|
2012-02-19 14:14:54 +00:00
|
|
|
use base 'XML::SAX::Base';
|
|
|
|
|
|
|
|
my %xyz_index = (x => 0, y => 1, z => 2); #=
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
my $self = shift->SUPER::new(@_);
|
|
|
|
$self->{_tree} = [];
|
2012-08-29 16:23:34 +00:00
|
|
|
$self->{_objects_map} = {}; # this hash maps AMF object IDs to object indexes in $model->objects
|
|
|
|
$self->{_instances} = {}; # apply these lazily to make sure all objects have been parsed
|
2012-02-19 14:14:54 +00:00
|
|
|
$self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start_element {
|
|
|
|
my $self = shift;
|
|
|
|
my $data = shift;
|
|
|
|
|
2012-08-29 16:23:34 +00:00
|
|
|
if ($data->{LocalName} eq 'object') {
|
|
|
|
$self->{_object} = $self->{_model}->add_object;
|
|
|
|
$self->{_objects_map}{ $self->_get_attribute($data, 'id') } = $#{ $self->{_model}->objects };
|
|
|
|
} elsif ($data->{LocalName} eq 'vertex') {
|
2012-02-19 14:14:54 +00:00
|
|
|
$self->{_vertex} = ["", "", ""];
|
|
|
|
} elsif ($self->{_vertex} && $data->{LocalName} =~ /^[xyz]$/ && $self->{_tree}[-1] eq 'coordinates') {
|
|
|
|
$self->{_coordinate} = $data->{LocalName};
|
2012-02-20 15:43:45 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'volume') {
|
2012-08-29 16:23:34 +00:00
|
|
|
$self->{_volume} = $self->{_object}->add_volume(
|
|
|
|
material_id => $self->_get_attribute($data, 'materialid') || undef,
|
|
|
|
);
|
2012-02-19 14:14:54 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'triangle') {
|
2012-05-20 09:05:16 +00:00
|
|
|
$self->{_triangle} = ["", "", ""];
|
2012-02-19 14:14:54 +00:00
|
|
|
} elsif ($self->{_triangle} && $data->{LocalName} =~ /^v([123])$/ && $self->{_tree}[-1] eq 'triangle') {
|
2012-05-20 09:05:16 +00:00
|
|
|
$self->{_vertex_idx} = $1-1;
|
2012-02-20 15:43:45 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'material') {
|
2012-08-29 16:23:34 +00:00
|
|
|
my $material_id = $self->_get_attribute($data, 'id') || '_';
|
|
|
|
$self->{_material} = $self->{_model}->materials->{ $material_id } = {};
|
2012-02-20 15:43:45 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'metadata' && $self->{_tree}[-1] eq 'material') {
|
|
|
|
$self->{_material_metadata_type} = $self->_get_attribute($data, 'type');
|
|
|
|
$self->{_material}{ $self->{_material_metadata_type} } = "";
|
2012-08-29 16:23:34 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'constellation') {
|
|
|
|
$self->{_constellation} = 1; # we merge all constellations as we don't support more than one
|
|
|
|
} elsif ($data->{LocalName} eq 'instance' && $self->{_constellation}) {
|
|
|
|
my $object_id = $self->_get_attribute($data, 'objectid');
|
|
|
|
$self->{_instances}{$object_id} ||= [];
|
|
|
|
push @{ $self->{_instances}{$object_id} }, $self->{_instance} = {};
|
|
|
|
} elsif ($data->{LocalName} =~ /^(?:deltax|deltay|rz)$/ && $self->{_instance}) {
|
|
|
|
$self->{_instance_property} = $data->{LocalName};
|
2012-02-19 14:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
push @{$self->{_tree}}, $data->{LocalName};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub characters {
|
|
|
|
my $self = shift;
|
|
|
|
my $data = shift;
|
|
|
|
|
|
|
|
if ($self->{_vertex} && $self->{_coordinate}) {
|
|
|
|
$self->{_vertex}[ $xyz_index{$self->{_coordinate}} ] .= $data->{Data};
|
|
|
|
} elsif ($self->{_triangle} && defined $self->{_vertex_idx}) {
|
|
|
|
$self->{_triangle}[ $self->{_vertex_idx} ] .= $data->{Data};
|
2012-02-20 15:43:45 +00:00
|
|
|
} elsif ($self->{_material_metadata_type}) {
|
|
|
|
$self->{_material}{ $self->{_material_metadata_type} } .= $data->{Data};
|
2012-08-29 16:23:34 +00:00
|
|
|
} elsif ($self->{_instance_property}) {
|
|
|
|
$self->{_instance}{ $self->{_instance_property} } .= $data->{Data};
|
2012-02-19 14:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub end_element {
|
|
|
|
my $self = shift;
|
|
|
|
my $data = shift;
|
|
|
|
|
|
|
|
pop @{$self->{_tree}};
|
|
|
|
|
2012-08-29 16:23:34 +00:00
|
|
|
if ($data->{LocalName} eq 'object') {
|
|
|
|
$self->{_object} = undef;
|
|
|
|
} elsif ($data->{LocalName} eq 'vertex') {
|
|
|
|
push @{$self->{_object}->vertices}, $self->{_vertex};
|
2012-02-19 14:14:54 +00:00
|
|
|
$self->{_vertex} = undef;
|
|
|
|
} elsif ($self->{_coordinate} && $data->{LocalName} =~ /^[xyz]$/) {
|
|
|
|
$self->{_coordinate} = undef;
|
2012-02-20 15:43:45 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'volume') {
|
|
|
|
$self->{_volume} = undef;
|
2012-02-19 14:14:54 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'triangle') {
|
2012-08-29 16:23:34 +00:00
|
|
|
push @{$self->{_volume}->facets}, $self->{_triangle};
|
2012-02-19 14:14:54 +00:00
|
|
|
$self->{_triangle} = undef;
|
2012-08-29 14:49:38 +00:00
|
|
|
} elsif (defined $self->{_vertex_idx} && $data->{LocalName} =~ /^v[123]$/) {
|
2012-02-19 14:14:54 +00:00
|
|
|
$self->{_vertex_idx} = undef;
|
2012-02-20 15:43:45 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'material') {
|
|
|
|
$self->{_material} = undef;
|
|
|
|
} elsif ($data->{LocalName} eq 'metadata' && $self->{_material}) {
|
|
|
|
$self->{_material_metadata_type} = undef;
|
2012-08-29 16:23:34 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'constellation') {
|
|
|
|
$self->{_constellation} = undef;
|
|
|
|
} elsif ($data->{LocalName} eq 'instance') {
|
|
|
|
$self->{_instance} = undef;
|
|
|
|
} elsif ($data->{LocalName} =~ /^(?:deltax|deltay|rz)$/ && $self->{_instance}) {
|
|
|
|
$self->{_instance_property} = undef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub end_document {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
foreach my $object_id (keys %{ $self->{_instances} }) {
|
|
|
|
my $new_object_id = $self->{_objects_map}{$object_id};
|
2012-09-12 14:30:44 +00:00
|
|
|
if (!defined $new_object_id) {
|
2012-08-29 16:23:34 +00:00
|
|
|
warn "Undefined object $object_id referenced in constellation\n";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $instance (@{ $self->{_instances}{$object_id} }) {
|
|
|
|
$self->{_model}->objects->[$new_object_id]->add_instance(
|
|
|
|
rotation => $instance->{rz} || 0,
|
2012-09-12 14:30:44 +00:00
|
|
|
offset => [ $instance->{deltax} || 0, $instance->{deltay} || 0 ],
|
2012-08-29 16:23:34 +00:00
|
|
|
);
|
|
|
|
}
|
2012-02-19 14:14:54 +00:00
|
|
|
}
|
2012-02-20 15:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _get_attribute {
|
|
|
|
my $self = shift;
|
|
|
|
my ($data, $name) = @_;
|
2012-02-19 14:14:54 +00:00
|
|
|
|
2012-02-20 15:43:45 +00:00
|
|
|
return +(map $_->{Value}, grep $_->{Name} eq $name, values %{$data->{Attributes}})[0];
|
2012-02-19 14:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|