stl-to-amf.pl 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/perl
  2. # This script converts a STL file to AMF
  3. use strict;
  4. use warnings;
  5. BEGIN {
  6. use FindBin;
  7. use lib "$FindBin::Bin/../lib";
  8. }
  9. use File::Basename qw(basename);
  10. use Getopt::Long qw(:config no_auto_abbrev);
  11. use Slic3r;
  12. $|++;
  13. my %opt = ();
  14. {
  15. my %options = (
  16. 'help' => sub { usage() },
  17. );
  18. GetOptions(%options) or usage(1);
  19. $ARGV[0] or usage(1);
  20. }
  21. {
  22. my @meshes = map Slic3r::STL->read_file($_), @ARGV;
  23. my $output_file = $ARGV[0];
  24. $output_file =~ s/\.stl$/.amf.xml/i;
  25. my $materials = {};
  26. my $meshes_by_material = {};
  27. if (@meshes == 1) {
  28. $meshes_by_material->{_} = $meshes[0];
  29. } else {
  30. for (0..$#meshes) {
  31. $materials->{$_+1} = { Name => basename($ARGV[$_]) };
  32. $meshes_by_material->{$_+1} = $meshes[$_];
  33. }
  34. }
  35. printf "Writing to %s\n", basename($output_file);
  36. Slic3r::AMF->write_file($output_file, $materials, $meshes_by_material);
  37. }
  38. sub usage {
  39. my ($exit_code) = @_;
  40. print <<"EOF";
  41. Usage: amf-to-stl.pl [ OPTIONS ] file.stl [ file2.stl [ file3.stl ] ]
  42. --help Output this usage screen and exit
  43. EOF
  44. exit ($exit_code || 0);
  45. }
  46. __END__