stl-to-amf.pl 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. 'distinct-materials' => \$opt{distinct_materials},
  18. );
  19. GetOptions(%options) or usage(1);
  20. $ARGV[0] or usage(1);
  21. }
  22. {
  23. my @models = map Slic3r::Format::STL->read_file($_), @ARGV;
  24. my $output_file = $ARGV[0];
  25. $output_file =~ s/\.stl$/.amf.xml/i;
  26. my $new_model = Slic3r::Model->new;
  27. if ($opt{distinct_materials} && @models > 1) {
  28. my $new_object = $new_model->add_object;
  29. for my $m (0 .. $#models) {
  30. my $model = $models[$m];
  31. $new_model->set_material($m, { Name => basename($ARGV[$m]) });
  32. $new_object->add_volume(
  33. material_id => $m,
  34. facets => $model->objects->[0]->volumes->[0]->facets,
  35. vertices => $model->objects->[0]->vertices,
  36. );
  37. }
  38. } else {
  39. foreach my $model (@models) {
  40. $new_model->add_object(
  41. vertices => $model->objects->[0]->vertices,
  42. )->add_volume(
  43. facets => $model->objects->[0]->volumes->[0]->facets,
  44. );
  45. }
  46. }
  47. printf "Writing to %s\n", basename($output_file);
  48. Slic3r::Format::AMF->write_file($output_file, $new_model);
  49. }
  50. sub usage {
  51. my ($exit_code) = @_;
  52. print <<"EOF";
  53. Usage: amf-to-stl.pl [ OPTIONS ] file.stl [ file2.stl [ file3.stl ] ]
  54. --help Output this usage screen and exit
  55. --distinct-materials Assign each STL file to a different material
  56. EOF
  57. exit ($exit_code || 0);
  58. }
  59. __END__