3DPreview.pm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package Slic3r::GUI::Plater::3DPreview;
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use Slic3r::Print::State ':steps';
  6. use Wx qw(:misc :sizer :slider :statictext wxWHITE);
  7. use Wx::Event qw(EVT_SLIDER EVT_KEY_DOWN);
  8. use base qw(Wx::Panel Class::Accessor);
  9. __PACKAGE__->mk_accessors(qw(print enabled _loaded canvas slider));
  10. sub new {
  11. my $class = shift;
  12. my ($parent, $print) = @_;
  13. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition);
  14. # init GUI elements
  15. my $canvas = Slic3r::GUI::3DScene->new($self);
  16. $self->canvas($canvas);
  17. my $slider = Wx::Slider->new(
  18. $self, -1,
  19. 0, # default
  20. 0, # min
  21. # we set max to a bogus non-zero value because the MSW implementation of wxSlider
  22. # will skip drawing the slider if max <= min:
  23. 1, # max
  24. wxDefaultPosition,
  25. wxDefaultSize,
  26. wxVERTICAL | wxSL_INVERSE,
  27. );
  28. $self->slider($slider);
  29. my $z_label = $self->{z_label} = Wx::StaticText->new($self, -1, "", wxDefaultPosition,
  30. [40,-1], wxALIGN_CENTRE_HORIZONTAL);
  31. $z_label->SetFont($Slic3r::GUI::small_font);
  32. my $vsizer = Wx::BoxSizer->new(wxVERTICAL);
  33. $vsizer->Add($slider, 1, wxALL | wxEXPAND | wxALIGN_CENTER, 3);
  34. $vsizer->Add($z_label, 0, wxALL | wxEXPAND | wxALIGN_CENTER, 3);
  35. my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  36. $sizer->Add($canvas, 1, wxALL | wxEXPAND, 0);
  37. $sizer->Add($vsizer, 0, wxTOP | wxBOTTOM | wxEXPAND, 5);
  38. EVT_SLIDER($self, $slider, sub {
  39. $self->set_z($self->{layers_z}[$slider->GetValue])
  40. if $self->enabled;
  41. });
  42. EVT_KEY_DOWN($canvas, sub {
  43. my ($s, $event) = @_;
  44. my $key = $event->GetKeyCode;
  45. if ($key == 85 || $key == 315) {
  46. $slider->SetValue($slider->GetValue + 1);
  47. $self->set_z($self->{layers_z}[$slider->GetValue]);
  48. } elsif ($key == 68 || $key == 317) {
  49. $slider->SetValue($slider->GetValue - 1);
  50. $self->set_z($self->{layers_z}[$slider->GetValue]);
  51. } else {
  52. $event->Skip;
  53. }
  54. });
  55. $self->SetSizer($sizer);
  56. $self->SetMinSize($self->GetSize);
  57. $sizer->SetSizeHints($self);
  58. # init canvas
  59. $self->print($print);
  60. $self->reload_print;
  61. return $self;
  62. }
  63. sub reload_print {
  64. my ($self, $obj_idx) = @_;
  65. $self->canvas->reset_objects;
  66. $self->_loaded(0);
  67. $self->load_print($obj_idx);
  68. }
  69. sub load_print {
  70. my ($self, $obj_idx) = @_;
  71. return if $self->_loaded;
  72. # we require that there's at least one object and the posSlice step
  73. # is performed on all of them (this ensures that _shifted_copies was
  74. # populated and we know the number of layers)
  75. if (!$self->print->object_step_done(STEP_SLICE)) {
  76. $self->enabled(0);
  77. $self->slider->Hide;
  78. $self->canvas->Refresh; # clears canvas
  79. return;
  80. }
  81. my $z_idx;
  82. {
  83. my %z = (); # z => 1
  84. if(defined $obj_idx) { # Load only given object
  85. foreach my $layer (@{$self->{print}->get_object($obj_idx)->layers}) {
  86. $z{$layer->print_z} = 1;
  87. }
  88. }else{ # Load all objects on the plater + support material
  89. foreach my $object (@{$self->{print}->objects}) {
  90. foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
  91. $z{$layer->print_z} = 1;
  92. }
  93. }
  94. }
  95. $self->enabled(1);
  96. $self->{layers_z} = [ sort { $a <=> $b } keys %z ];
  97. $self->slider->SetRange(0, scalar(@{$self->{layers_z}})-1);
  98. if (($z_idx = $self->slider->GetValue) <= $#{$self->{layers_z}} && $self->slider->GetValue != 0) {
  99. # use $z_idx
  100. } else {
  101. $self->slider->SetValue(scalar(@{$self->{layers_z}})-1);
  102. $z_idx = @{$self->{layers_z}} ? -1 : undef;
  103. }
  104. $self->slider->Show;
  105. $self->Layout;
  106. }
  107. if ($self->IsShown) {
  108. # set colors
  109. $self->canvas->color_toolpaths_by($Slic3r::GUI::Settings->{_}{color_toolpaths_by});
  110. if ($self->canvas->color_toolpaths_by eq 'extruder') {
  111. my @filament_colors = map { s/^#//; [ map $_/255, (unpack 'C*', pack 'H*', $_), 255 ] }
  112. @{$self->print->config->filament_colour};
  113. $self->canvas->colors->[$_] = $filament_colors[$_] for 0..$#filament_colors;
  114. } else {
  115. $self->canvas->colors([ $self->canvas->default_colors ]);
  116. }
  117. if(defined $obj_idx) { # Load only one object
  118. $self->canvas->load_print_object_toolpaths($self->{print}->get_object($obj_idx));
  119. }else{ # load all objects
  120. # load skirt and brim
  121. $self->canvas->load_print_toolpaths($self->print);
  122. foreach my $object (@{$self->print->objects}) {
  123. $self->canvas->load_print_object_toolpaths($object);
  124. #my @volume_ids = $self->canvas->load_object($object->model_object);
  125. #$self->canvas->volumes->[$_]->color->[3] = 0.2 for @volume_ids;
  126. }
  127. }
  128. $self->_loaded(1);
  129. }
  130. $self->set_z($self->{layers_z}[$z_idx]);
  131. }
  132. sub set_z {
  133. my ($self, $z) = @_;
  134. return if !$self->enabled;
  135. $self->{z_label}->SetLabel(sprintf '%.2f', $z);
  136. $self->canvas->set_toolpaths_range(0, $z);
  137. $self->canvas->Refresh if $self->IsShown;
  138. }
  139. sub set_bed_shape {
  140. my ($self, $bed_shape) = @_;
  141. $self->canvas->set_bed_shape($bed_shape);
  142. }
  143. 1;