3D.pm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 qw(Slic3r::GUI::3DScene Class::Accessor);
  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->enable_moving(1);
  17. $self->select_by('object');
  18. $self->drag_by('instance');
  19. $self->{objects} = $objects;
  20. $self->{model} = $model;
  21. $self->{config} = $config;
  22. $self->{on_select_object} = sub {};
  23. $self->{on_instances_moved} = sub {};
  24. $self->on_select(sub {
  25. my ($volume_idx) = @_;
  26. my $obj_idx = undef;
  27. if ($volume_idx != -1) {
  28. $obj_idx = $self->object_idx($volume_idx);
  29. }
  30. $self->{on_select_object}->($obj_idx)
  31. if $self->{on_select_object};
  32. });
  33. $self->on_move(sub {
  34. my @volume_idxs = @_;
  35. my %done = (); # prevent moving instances twice
  36. foreach my $volume_idx (@volume_idxs) {
  37. my $volume = $self->volumes->[$volume_idx];
  38. my $obj_idx = $self->object_idx($volume_idx);
  39. my $instance_idx = $self->instance_idx($volume_idx);
  40. next if $done{"${obj_idx}_${instance_idx}"};
  41. $done{"${obj_idx}_${instance_idx}"} = 1;
  42. my $model_object = $self->{model}->get_object($obj_idx);
  43. $model_object
  44. ->instances->[$instance_idx]
  45. ->offset
  46. ->translate($volume->origin->x, $volume->origin->y); #))
  47. $model_object->invalidate_bounding_box;
  48. }
  49. $self->{on_instances_moved}->()
  50. if $self->{on_instances_moved};
  51. });
  52. return $self;
  53. }
  54. sub set_on_select_object {
  55. my ($self, $cb) = @_;
  56. $self->{on_select_object} = $cb;
  57. }
  58. sub set_on_double_click {
  59. my ($self, $cb) = @_;
  60. $self->on_double_click($cb);
  61. }
  62. sub set_on_right_click {
  63. my ($self, $cb) = @_;
  64. $self->on_right_click($cb);
  65. }
  66. sub set_on_instances_moved {
  67. my ($self, $cb) = @_;
  68. $self->{on_instances_moved} = $cb;
  69. }
  70. sub update {
  71. my ($self) = @_;
  72. $self->reset_objects;
  73. $self->update_bed_size;
  74. foreach my $obj_idx (0..$#{$self->{model}->objects}) {
  75. my @volume_idxs = $self->load_object($self->{model}, $obj_idx);
  76. if ($self->{objects}[$obj_idx]->selected) {
  77. $self->select_volume($_) for @volume_idxs;
  78. }
  79. }
  80. }
  81. sub update_bed_size {
  82. my ($self) = @_;
  83. $self->set_bed_shape($self->{config}->bed_shape);
  84. }
  85. 1;