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;
|
|
|
|
|
|
|
|
use XML::SAX::ExpatXS;
|
2012-02-20 15:43:45 +00:00
|
|
|
use XXX;
|
|
|
|
|
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} = [];
|
|
|
|
$self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start_element {
|
|
|
|
my $self = shift;
|
|
|
|
my $data = shift;
|
|
|
|
|
|
|
|
if ($data->{LocalName} eq 'vertex') {
|
|
|
|
$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-02-26 15:22:45 +00:00
|
|
|
$self->{_volume_materialid} = $self->_get_attribute($data, 'materialid') || '_';
|
2012-02-20 15:43:45 +00:00
|
|
|
$self->{_volume} = [];
|
2012-02-19 14:14:54 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'triangle') {
|
|
|
|
$self->{_triangle} = [[], "", "", ""]; # empty normal
|
|
|
|
} elsif ($self->{_triangle} && $data->{LocalName} =~ /^v([123])$/ && $self->{_tree}[-1] eq 'triangle') {
|
|
|
|
$self->{_vertex_idx} = $1;
|
2012-02-20 15:43:45 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'material') {
|
|
|
|
$self->{_material_id} = $self->_get_attribute($data, 'id') || '_';
|
|
|
|
$self->{_material} = {};
|
|
|
|
} 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-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-02-19 14:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub end_element {
|
|
|
|
my $self = shift;
|
|
|
|
my $data = shift;
|
|
|
|
|
|
|
|
pop @{$self->{_tree}};
|
|
|
|
|
|
|
|
if ($data->{LocalName} eq 'vertex') {
|
|
|
|
push @{$self->{_vertices}}, $self->{_vertex};
|
|
|
|
$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->{_meshes_by_material}{ $self->{_volume_materialid} } ||= [];
|
|
|
|
push @{ $self->{_meshes_by_material}{ $self->{_volume_materialid} } }, @{$self->{_volume}};
|
|
|
|
$self->{_volume} = undef;
|
2012-02-19 14:14:54 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'triangle') {
|
2012-02-20 15:43:45 +00:00
|
|
|
push @{$self->{_volume}}, $self->{_triangle};
|
2012-02-19 14:14:54 +00:00
|
|
|
$self->{_triangle} = undef;
|
|
|
|
} elsif ($self->{_vertex_idx} && $data->{LocalName} =~ /^v[123]$/) {
|
|
|
|
$self->{_vertex_idx} = undef;
|
2012-02-20 15:43:45 +00:00
|
|
|
} elsif ($data->{LocalName} eq 'material') {
|
|
|
|
$self->{_materials}{ $self->{_material_id} } = $self->{_material};
|
|
|
|
$self->{_material_id} = undef;
|
|
|
|
$self->{_material} = undef;
|
|
|
|
} elsif ($data->{LocalName} eq 'metadata' && $self->{_material}) {
|
|
|
|
$self->{_material_metadata_type} = undef;
|
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;
|