split_stl.pl 1.5 KB

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