view-mesh.pl 1.9 KB

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