package Slic3r::Format::AMF; use Moo; use Slic3r::Geometry qw(X Y Z); sub read_file { my $self = shift; my ($file) = @_; eval "require Slic3r::Format::AMF::Parser; 1" or die "AMF parsing requires XML::SAX\n"; open my $fh, '<', $file or die "Failed to open $file\n"; my $vertices = []; my $materials = {}; my $meshes_by_material = {}; XML::SAX::PurePerl ->new(Handler => Slic3r::Format::AMF::Parser->new( _vertices => $vertices, _materials => $materials, _meshes_by_material => $meshes_by_material, )) ->parse_file($fh); close $fh; $_ = Slic3r::TriangleMesh->new(vertices => $vertices, facets => $_) for values %$meshes_by_material; return $materials, $meshes_by_material; } sub write_file { my $self = shift; my ($file, $materials, $meshes_by_material) = @_; my %vertices_offset = (); open my $fh, '>', $file; binmode $fh, ':utf8'; printf $fh qq{\n}; printf $fh qq{\n}; printf $fh qq{ Slic3r %s\n}, $Slic3r::VERSION; foreach my $material_id (keys %$materials) { printf $fh qq{ \n}, $material_id; for (keys %{$materials->{$material_id}}) { printf $fh qq{ %s\n}, $_, $materials->{$material_id}{$_}; } printf $fh qq{ \n}; } printf $fh qq{ \n}; printf $fh qq{ \n}; printf $fh qq{ \n}; my $vertices_count = 0; foreach my $mesh (values %$meshes_by_material) { $vertices_offset{$mesh} = $vertices_count; foreach my $vertex (@{$mesh->vertices}, ) { printf $fh qq{ \n}; printf $fh qq{ \n}; printf $fh qq{ %s\n}, $vertex->[X]; printf $fh qq{ %s\n}, $vertex->[Y]; printf $fh qq{ %s\n}, $vertex->[Z]; printf $fh qq{ \n}; printf $fh qq{ \n}; $vertices_count++; } } printf $fh qq{ \n}; foreach my $material_id (sort keys %$meshes_by_material) { my $mesh = $meshes_by_material->{$material_id}; printf $fh qq{ \n}, ($material_id eq '_') ? '' : " materialid=\"$material_id\""; foreach my $facet (@{$mesh->facets}) { printf $fh qq{ \n}; printf $fh qq{ %d\n}, $_, $facet->[$_] + $vertices_offset{$mesh}, $_ for -3..-1; printf $fh qq{ \n}; } printf $fh qq{ \n}; } printf $fh qq{ \n}; printf $fh qq{ \n}; printf $fh qq{\n}; close $fh; } 1;