2012-02-25 16:35:25 +00:00
|
|
|
package Slic3r::Format::AMF;
|
2012-02-19 14:14:54 +00:00
|
|
|
use Moo;
|
2012-02-19 14:45:27 +00:00
|
|
|
|
|
|
|
use Slic3r::Geometry qw(X Y Z);
|
2012-02-19 14:14:54 +00:00
|
|
|
|
|
|
|
sub read_file {
|
|
|
|
my $self = shift;
|
|
|
|
my ($file) = @_;
|
|
|
|
|
2012-02-25 16:35:25 +00:00
|
|
|
eval "require Slic3r::Format::AMF::Parser; 1"
|
2012-02-26 17:17:23 +00:00
|
|
|
or die "AMF parsing requires XML::SAX\n";
|
2012-02-19 14:14:54 +00:00
|
|
|
|
|
|
|
open my $fh, '<', $file or die "Failed to open $file\n";
|
|
|
|
|
|
|
|
my $vertices = [];
|
2012-02-20 15:43:45 +00:00
|
|
|
my $materials = {};
|
|
|
|
my $meshes_by_material = {};
|
2012-02-26 17:17:23 +00:00
|
|
|
XML::SAX::PurePerl
|
2012-02-25 16:35:25 +00:00
|
|
|
->new(Handler => Slic3r::Format::AMF::Parser->new(
|
2012-02-20 15:43:45 +00:00
|
|
|
_vertices => $vertices,
|
|
|
|
_materials => $materials,
|
|
|
|
_meshes_by_material => $meshes_by_material,
|
2012-02-19 14:14:54 +00:00
|
|
|
))
|
|
|
|
->parse_file($fh);
|
|
|
|
|
|
|
|
close $fh;
|
|
|
|
|
2012-08-29 14:49:38 +00:00
|
|
|
my $model = Slic3r::Model->new;
|
|
|
|
my $object = $model->add_object(vertices => $vertices);
|
|
|
|
foreach my $material (keys %$meshes_by_material) {
|
|
|
|
push @{$model->materials}, $material; # TODO: we should not add duplicate materials
|
|
|
|
$object->add_volume(
|
|
|
|
material_id => $#{$model->materials},
|
|
|
|
facets => $meshes_by_material->{$material},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $model;
|
2012-02-19 14:14:54 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 14:45:27 +00:00
|
|
|
sub write_file {
|
|
|
|
my $self = shift;
|
2012-08-29 14:49:38 +00:00
|
|
|
my ($file, $model, %params) = @_;
|
2012-02-20 15:43:45 +00:00
|
|
|
|
|
|
|
my %vertices_offset = ();
|
2012-02-19 14:45:27 +00:00
|
|
|
|
|
|
|
open my $fh, '>', $file;
|
|
|
|
binmode $fh, ':utf8';
|
|
|
|
printf $fh qq{<?xml version="1.0" encoding="UTF-8"?>\n};
|
|
|
|
printf $fh qq{<amf unit="millimeter">\n};
|
|
|
|
printf $fh qq{ <metadata type="cad">Slic3r %s</metadata>\n}, $Slic3r::VERSION;
|
2012-08-29 14:49:38 +00:00
|
|
|
for my $material_id (0 .. $#{ $model->materials }) {
|
|
|
|
my $material = $model->materials->[$material_id];
|
|
|
|
printf $fh qq{ <material id="%d">\n}, $material_id;
|
|
|
|
for (keys %$material) {
|
|
|
|
printf $fh qq{ <metadata type=\"%s\">%s</metadata>\n}, $_, $material->{$_};
|
2012-02-20 15:43:45 +00:00
|
|
|
}
|
|
|
|
printf $fh qq{ </material>\n};
|
|
|
|
}
|
2012-08-29 14:49:38 +00:00
|
|
|
for my $object_id (0 .. $#{ $model->objects }) {
|
|
|
|
my $object = $model->objects->[$object_id];
|
|
|
|
printf $fh qq{ <object id="%d">\n}, $object_id;
|
|
|
|
printf $fh qq{ <mesh>\n};
|
|
|
|
printf $fh qq{ <vertices>\n};
|
|
|
|
foreach my $vertex (@{$object->vertices}, ) {
|
2012-02-20 15:43:45 +00:00
|
|
|
printf $fh qq{ <vertex>\n};
|
|
|
|
printf $fh qq{ <coordinates>\n};
|
|
|
|
printf $fh qq{ <x>%s</x>\n}, $vertex->[X];
|
|
|
|
printf $fh qq{ <y>%s</y>\n}, $vertex->[Y];
|
|
|
|
printf $fh qq{ <z>%s</z>\n}, $vertex->[Z];
|
|
|
|
printf $fh qq{ </coordinates>\n};
|
|
|
|
printf $fh qq{ </vertex>\n};
|
|
|
|
}
|
2012-08-29 14:49:38 +00:00
|
|
|
printf $fh qq{ </vertices>\n};
|
|
|
|
foreach my $volume (@{ $object->volumes }) {
|
|
|
|
printf $fh qq{ <volume%s>\n},
|
|
|
|
(!defined $volume->material_id) ? '' : (sprintf ' materialid="%s"', $volume->material_id);
|
|
|
|
foreach my $facet (@{$volume->facets}) {
|
|
|
|
printf $fh qq{ <triangle>\n};
|
|
|
|
printf $fh qq{ <v%d>%d</v%d>\n}, $_, $facet->[$_], $_ for -3..-1;
|
|
|
|
printf $fh qq{ </triangle>\n};
|
|
|
|
}
|
|
|
|
printf $fh qq{ </volume>\n};
|
2012-02-20 15:43:45 +00:00
|
|
|
}
|
2012-08-29 14:49:38 +00:00
|
|
|
printf $fh qq{ </mesh>\n};
|
|
|
|
printf $fh qq{ </object>\n};
|
2012-02-19 14:45:27 +00:00
|
|
|
}
|
|
|
|
printf $fh qq{</amf>\n};
|
|
|
|
close $fh;
|
|
|
|
}
|
|
|
|
|
2012-02-19 14:14:54 +00:00
|
|
|
1;
|