split_stl.pl 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/perl
  2. # This script splits a STL plate into individual files
  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. 'ascii' => \$opt{ascii},
  19. );
  20. GetOptions(%options) or usage(1);
  21. $ARGV[0] or usage(1);
  22. }
  23. {
  24. my $model = Slic3r::Model->load_stl($ARGV[0], basename($ARGV[0]));
  25. my $basename = $ARGV[0];
  26. $basename =~ s/\.[sS][tT][lL]$//;
  27. my $part_count = 0;
  28. my $mesh = $model->objects->[0]->volumes->[0]->mesh;
  29. foreach my $new_mesh (@{$mesh->split}) {
  30. $new_mesh->repair;
  31. my $new_model = Slic3r::Model->new;
  32. $new_model
  33. ->add_object()
  34. ->add_volume(mesh => $new_mesh);
  35. $new_model->add_default_instances;
  36. my $output_file = sprintf '%s_%02d.stl', $basename, ++$part_count;
  37. printf "Writing to %s\n", basename($output_file);
  38. $new_model->store_stl($output_file, !$opt{ascii});
  39. }
  40. }
  41. sub usage {
  42. my ($exit_code) = @_;
  43. print <<"EOF";
  44. Usage: split_stl.pl [ OPTIONS ] file.stl
  45. --help Output this usage screen and exit
  46. --ascii Generate ASCII STL files (default: binary)
  47. EOF
  48. exit ($exit_code || 0);
  49. }
  50. __END__