stl-to-amf.pl 1.8 KB

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