view-mesh.pl 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/perl
  2. # This script displays 3D preview of a mesh
  3. use strict;
  4. use warnings;
  5. BEGIN {
  6. use FindBin;
  7. use lib "$FindBin::Bin/../lib";
  8. }
  9. use Getopt::Long qw(:config no_auto_abbrev);
  10. use Slic3r;
  11. use Slic3r::GUI;
  12. use Slic3r::GUI::3DScene;
  13. $|++;
  14. my %opt = ();
  15. {
  16. my %options = (
  17. 'help' => sub { usage() },
  18. 'cut=f' => \$opt{cut},
  19. 'enable-moving' => \$opt{enable_moving},
  20. );
  21. GetOptions(%options) or usage(1);
  22. $ARGV[0] or usage(1);
  23. }
  24. {
  25. my $model = Slic3r::Model->read_from_file($ARGV[0]);
  26. # make sure all objects have at least one defined instance
  27. $model->add_default_instances;
  28. $_->center_around_origin for @{$model->objects}; # and align to Z = 0
  29. my $app = Slic3r::ViewMesh->new;
  30. $app->{canvas}->enable_picking(1);
  31. $app->{canvas}->enable_moving($opt{enable_moving});
  32. $app->{canvas}->load_object($model, 0);
  33. $app->{canvas}->set_auto_bed_shape;
  34. $app->{canvas}->zoom_to_volumes;
  35. $app->{canvas}->SetCuttingPlane($opt{cut}) if defined $opt{cut};
  36. $app->MainLoop;
  37. }
  38. sub usage {
  39. my ($exit_code) = @_;
  40. print <<"EOF";
  41. Usage: view-mesh.pl [ OPTIONS ] file.stl
  42. --help Output this usage screen and exit
  43. --cut Z Display the cutting plane at the given Z
  44. EOF
  45. exit ($exit_code || 0);
  46. }
  47. package Slic3r::ViewMesh;
  48. use Wx qw(:sizer);
  49. use base qw(Wx::App);
  50. sub OnInit {
  51. my $self = shift;
  52. my $frame = Wx::Frame->new(undef, -1, 'Mesh Viewer', [-1, -1], [500, 400]);
  53. my $panel = Wx::Panel->new($frame, -1);
  54. $self->{canvas} = Slic3r::GUI::3DScene->new($panel);
  55. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  56. $sizer->Add($self->{canvas}, 1, wxEXPAND, 0);
  57. $panel->SetSizer($sizer);
  58. $sizer->SetSizeHints($panel);
  59. $frame->Show(1);
  60. }
  61. __END__