3DToolpaths.pm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package Slic3r::GUI::Plater::3DToolpaths;
  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. if ($event->HasModifiers) {
  45. $event->Skip;
  46. } else {
  47. my $key = $event->GetKeyCode;
  48. if ($key == 85 || $key == 315) {
  49. $slider->SetValue($slider->GetValue + 1);
  50. $self->set_z($self->{layers_z}[$slider->GetValue]);
  51. } elsif ($key == 68 || $key == 317) {
  52. $slider->SetValue($slider->GetValue - 1);
  53. $self->set_z($self->{layers_z}[$slider->GetValue]);
  54. } else {
  55. $event->Skip;
  56. }
  57. }
  58. });
  59. $self->SetSizer($sizer);
  60. $self->SetMinSize($self->GetSize);
  61. $sizer->SetSizeHints($self);
  62. # init canvas
  63. $self->print($print);
  64. $self->reload_print;
  65. return $self;
  66. }
  67. sub reload_print {
  68. my ($self) = @_;
  69. $self->canvas->reset_objects;
  70. $self->_loaded(0);
  71. $self->load_print;
  72. }
  73. sub load_print {
  74. my ($self) = @_;
  75. return if $self->_loaded;
  76. # we require that there's at least one object and the posSlice step
  77. # is performed on all of them (this ensures that _shifted_copies was
  78. # populated and we know the number of layers)
  79. if (!$self->print->object_step_done(STEP_SLICE)) {
  80. $self->enabled(0);
  81. $self->slider->Hide;
  82. $self->canvas->Refresh; # clears canvas
  83. return;
  84. }
  85. my $z_idx;
  86. {
  87. my %z = (); # z => 1
  88. foreach my $object (@{$self->{print}->objects}) {
  89. foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
  90. $z{$layer->print_z} = 1;
  91. }
  92. }
  93. $self->enabled(1);
  94. $self->{layers_z} = [ sort { $a <=> $b } keys %z ];
  95. $self->slider->SetRange(0, scalar(@{$self->{layers_z}})-1);
  96. if (($z_idx = $self->slider->GetValue) <= $#{$self->{layers_z}} && $self->slider->GetValue != 0) {
  97. # use $z_idx
  98. } else {
  99. $self->slider->SetValue(scalar(@{$self->{layers_z}})-1);
  100. $z_idx = @{$self->{layers_z}} ? -1 : undef;
  101. }
  102. $self->slider->Show;
  103. $self->Layout;
  104. }
  105. if ($self->IsShown) {
  106. # load skirt and brim
  107. $self->canvas->load_print_toolpaths($self->print);
  108. foreach my $object (@{$self->print->objects}) {
  109. $self->canvas->load_print_object_toolpaths($object);
  110. # Show the objects in very transparent color.
  111. #my @volume_ids = $self->canvas->load_object($object->model_object);
  112. #$self->canvas->volumes->[$_]->color->[3] = 0.2 for @volume_ids;
  113. }
  114. $self->canvas->zoom_to_volumes;
  115. $self->_loaded(1);
  116. }
  117. $self->set_z($self->{layers_z}[$z_idx]);
  118. }
  119. sub set_z {
  120. my ($self, $z) = @_;
  121. return if !$self->enabled;
  122. $self->{z_label}->SetLabel(sprintf '%.2f', $z);
  123. $self->canvas->set_toolpaths_range(0, $z);
  124. $self->canvas->Refresh if $self->IsShown;
  125. }
  126. sub set_bed_shape {
  127. my ($self, $bed_shape) = @_;
  128. $self->canvas->set_bed_shape($bed_shape);
  129. }
  130. 1;