3D.pm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package Slic3r::GUI::Plater::3D;
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use List::Util qw();
  6. use Slic3r::Geometry qw();
  7. use Slic3r::Geometry::Clipper qw();
  8. use Wx qw(:misc :pen :brush :sizer :font :cursor wxTAB_TRAVERSAL);
  9. use Wx::Event qw();
  10. use base 'Slic3r::GUI::PreviewCanvas';
  11. sub new {
  12. my $class = shift;
  13. my ($parent, $objects, $model, $config) = @_;
  14. my $self = $class->SUPER::new($parent);
  15. $self->enable_picking(1);
  16. $self->{objects} = $objects;
  17. $self->{model} = $model;
  18. $self->{config} = $config;
  19. $self->{on_select_object} = sub {};
  20. $self->{on_double_click} = sub {};
  21. $self->{on_right_click} = sub {};
  22. $self->{on_instance_moved} = sub {};
  23. return $self;
  24. }
  25. sub set_on_select_object {
  26. my ($self, $cb) = @_;
  27. $self->on_select_object(sub {
  28. my ($volume_idx) = @_;
  29. return $cb->(undef) if $volume_idx == -1;
  30. my $obj_idx = $self->{_volumes_inv}{$volume_idx};
  31. return $cb->($obj_idx);
  32. });
  33. }
  34. sub set_on_double_click {
  35. my ($self, $cb) = @_;
  36. $self->on_double_click($cb);
  37. }
  38. sub set_on_right_click {
  39. my ($self, $cb) = @_;
  40. $self->on_right_click($cb);
  41. }
  42. sub set_on_instance_moved {
  43. my ($self, $cb) = @_;
  44. $self->on_instance_moved(sub {
  45. my ($volume_idx, $instance_idx) = @_;
  46. my $obj_idx = $self->{_volumes_inv}{$volume_idx};
  47. return $cb->($obj_idx, $instance_idx);
  48. });
  49. }
  50. sub update {
  51. my ($self) = @_;
  52. $self->{_volumes} = {}; # obj_idx => [ volume_idx, volume_idx ]
  53. $self->{_volumes_inv} = {}; # volume_idx => obj_idx
  54. $self->reset_objects;
  55. return if $self->{model}->objects_count == 0;
  56. $self->set_bounding_box($self->{model}->bounding_box);
  57. $self->set_bed_shape($self->{config}->bed_shape);
  58. foreach my $obj_idx (0..$#{$self->{model}->objects}) {
  59. my $model_object = $self->{model}->get_object($obj_idx);
  60. my @volume_idxs = $self->load_object($model_object, 1);
  61. # store mapping between canvas volumes and model objects
  62. $self->{_volumes}{$obj_idx} = [ @volume_idxs ];
  63. $self->{_volumes_inv}{$_} = $obj_idx for @volume_idxs;
  64. if ($self->{objects}[$obj_idx]{selected}) {
  65. $self->select_volume($_) for @volume_idxs;
  66. }
  67. }
  68. }
  69. 1;