3DPreview.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 :keycode wxWHITE);
  7. use Wx::Event qw(EVT_SLIDER EVT_KEY_DOWN EVT_CHECKBOX);
  8. use base qw(Wx::Panel Class::Accessor);
  9. __PACKAGE__->mk_accessors(qw(print enabled _loaded canvas slider_low slider_high single_layer));
  10. sub new {
  11. my $class = shift;
  12. my ($parent, $print, $config) = @_;
  13. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition);
  14. $self->{config} = $config;
  15. $self->{number_extruders} = 1;
  16. $self->{preferred_color_mode} = 'feature';
  17. # init GUI elements
  18. my $canvas = Slic3r::GUI::3DScene->new($self);
  19. $canvas->use_plain_shader(1);
  20. $self->canvas($canvas);
  21. my $slider_low = Wx::Slider->new(
  22. $self, -1,
  23. 0, # default
  24. 0, # min
  25. # we set max to a bogus non-zero value because the MSW implementation of wxSlider
  26. # will skip drawing the slider if max <= min:
  27. 1, # max
  28. wxDefaultPosition,
  29. wxDefaultSize,
  30. wxVERTICAL | wxSL_INVERSE,
  31. );
  32. $self->slider_low($slider_low);
  33. my $slider_high = Wx::Slider->new(
  34. $self, -1,
  35. 0, # default
  36. 0, # min
  37. # we set max to a bogus non-zero value because the MSW implementation of wxSlider
  38. # will skip drawing the slider if max <= min:
  39. 1, # max
  40. wxDefaultPosition,
  41. wxDefaultSize,
  42. wxVERTICAL | wxSL_INVERSE,
  43. );
  44. $self->slider_high($slider_high);
  45. my $z_label_low = $self->{z_label_low} = Wx::StaticText->new($self, -1, "", wxDefaultPosition,
  46. [40,-1], wxALIGN_CENTRE_HORIZONTAL);
  47. $z_label_low->SetFont($Slic3r::GUI::small_font);
  48. my $z_label_high = $self->{z_label_high} = Wx::StaticText->new($self, -1, "", wxDefaultPosition,
  49. [40,-1], wxALIGN_CENTRE_HORIZONTAL);
  50. $z_label_high->SetFont($Slic3r::GUI::small_font);
  51. $self->single_layer(0);
  52. $self->{color_by_extruder} = 0;
  53. my $checkbox_singlelayer = $self->{checkbox_singlelayer} = Wx::CheckBox->new($self, -1, "1 Layer");
  54. my $checkbox_color_by_extruder = $self->{checkbox_color_by_extruder} = Wx::CheckBox->new($self, -1, "Tool");
  55. my $hsizer = Wx::BoxSizer->new(wxHORIZONTAL);
  56. my $vsizer = Wx::BoxSizer->new(wxVERTICAL);
  57. my $vsizer_outer = Wx::BoxSizer->new(wxVERTICAL);
  58. $vsizer->Add($slider_low, 3, 0, 0);
  59. $vsizer->Add($z_label_low, 0, 0, 0);
  60. $hsizer->Add($vsizer, 0, wxEXPAND, 0);
  61. $vsizer = Wx::BoxSizer->new(wxVERTICAL);
  62. $vsizer->Add($slider_high, 3, 0, 0);
  63. $vsizer->Add($z_label_high, 0, 0, 0);
  64. $hsizer->Add($vsizer, 0, wxEXPAND, 0);
  65. $vsizer_outer->Add($hsizer, 3, wxALIGN_CENTER_HORIZONTAL, 0);
  66. $vsizer_outer->Add($checkbox_singlelayer, 0, wxTOP | wxALIGN_CENTER_HORIZONTAL, 5);
  67. $vsizer_outer->Add($checkbox_color_by_extruder, 0, wxTOP | wxALIGN_CENTER_HORIZONTAL, 5);
  68. my $sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  69. $sizer->Add($canvas, 1, wxALL | wxEXPAND, 0);
  70. $sizer->Add($vsizer_outer, 0, wxTOP | wxBOTTOM | wxEXPAND, 5);
  71. EVT_SLIDER($self, $slider_low, sub {
  72. $slider_high->SetValue($slider_low->GetValue) if $self->single_layer;
  73. $self->set_z_idx_low ($slider_low ->GetValue)
  74. });
  75. EVT_SLIDER($self, $slider_high, sub {
  76. $slider_low->SetValue($slider_high->GetValue) if $self->single_layer;
  77. $self->set_z_idx_high($slider_high->GetValue)
  78. });
  79. EVT_KEY_DOWN($canvas, sub {
  80. my ($s, $event) = @_;
  81. my $key = $event->GetKeyCode;
  82. if ($event->HasModifiers) {
  83. $event->Skip;
  84. } else {
  85. if ($key == ord('U')) {
  86. $slider_high->SetValue($slider_high->GetValue + 1);
  87. $slider_low->SetValue($slider_high->GetValue) if ($event->ShiftDown());
  88. $self->set_z_idx_high($slider_high->GetValue);
  89. } elsif ($key == ord('D')) {
  90. $slider_high->SetValue($slider_high->GetValue - 1);
  91. $slider_low->SetValue($slider_high->GetValue) if ($event->ShiftDown());
  92. $self->set_z_idx_high($slider_high->GetValue);
  93. } elsif ($key == ord('S')) {
  94. $checkbox_singlelayer->SetValue(! $checkbox_singlelayer->GetValue());
  95. $self->single_layer($checkbox_singlelayer->GetValue());
  96. if ($self->single_layer) {
  97. $slider_low->SetValue($slider_high->GetValue);
  98. $self->set_z_idx_high($slider_high->GetValue);
  99. }
  100. } else {
  101. $event->Skip;
  102. }
  103. }
  104. });
  105. EVT_KEY_DOWN($slider_low, sub {
  106. my ($s, $event) = @_;
  107. my $key = $event->GetKeyCode;
  108. if ($event->HasModifiers) {
  109. $event->Skip;
  110. } else {
  111. if ($key == WXK_LEFT) {
  112. } elsif ($key == WXK_RIGHT) {
  113. $slider_high->SetFocus;
  114. } else {
  115. $event->Skip;
  116. }
  117. }
  118. });
  119. EVT_KEY_DOWN($slider_high, sub {
  120. my ($s, $event) = @_;
  121. my $key = $event->GetKeyCode;
  122. if ($event->HasModifiers) {
  123. $event->Skip;
  124. } else {
  125. if ($key == WXK_LEFT) {
  126. $slider_low->SetFocus;
  127. } elsif ($key == WXK_RIGHT) {
  128. } else {
  129. $event->Skip;
  130. }
  131. }
  132. });
  133. EVT_CHECKBOX($self, $checkbox_singlelayer, sub {
  134. $self->single_layer($checkbox_singlelayer->GetValue());
  135. if ($self->single_layer) {
  136. $slider_low->SetValue($slider_high->GetValue);
  137. $self->set_z_idx_high($slider_high->GetValue);
  138. }
  139. });
  140. EVT_CHECKBOX($self, $checkbox_color_by_extruder, sub {
  141. $self->{color_by_extruder} = $checkbox_color_by_extruder->GetValue();
  142. $self->{preferred_color_mode} = $self->{color_by_extruder} ? 'tool' : 'feature';
  143. $self->reload_print;
  144. });
  145. $self->SetSizer($sizer);
  146. $self->SetMinSize($self->GetSize);
  147. $sizer->SetSizeHints($self);
  148. # init canvas
  149. $self->print($print);
  150. $self->reload_print;
  151. return $self;
  152. }
  153. sub reload_print {
  154. my ($self, $force) = @_;
  155. $self->canvas->reset_objects;
  156. $self->_loaded(0);
  157. if (! $self->IsShown && ! $force) {
  158. $self->{reload_delayed} = 1;
  159. return;
  160. }
  161. $self->load_print;
  162. }
  163. sub load_print {
  164. my ($self) = @_;
  165. return if $self->_loaded;
  166. # we require that there's at least one object and the posSlice step
  167. # is performed on all of them (this ensures that _shifted_copies was
  168. # populated and we know the number of layers)
  169. my $n_layers = 0;
  170. if ($self->print->object_step_done(STEP_SLICE)) {
  171. my %z = (); # z => 1
  172. foreach my $object (@{$self->{print}->objects}) {
  173. foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
  174. $z{$layer->print_z} = 1;
  175. }
  176. }
  177. $self->{layers_z} = [ sort { $a <=> $b } keys %z ];
  178. $n_layers = scalar(@{$self->{layers_z}});
  179. }
  180. if ($n_layers == 0) {
  181. $self->enabled(0);
  182. $self->set_z_range(0,0);
  183. $self->slider_low->Hide;
  184. $self->slider_high->Hide;
  185. $self->canvas->Refresh; # clears canvas
  186. return;
  187. }
  188. my $z_idx_low = $self->slider_low->GetValue;
  189. my $z_idx_high = $self->slider_high->GetValue;
  190. $self->enabled(1);
  191. $self->slider_low->SetRange(0, $n_layers - 1);
  192. $self->slider_high->SetRange(0, $n_layers - 1);
  193. if ($z_idx_high < $n_layers && ($self->single_layer || $z_idx_high != 0)) {
  194. # use $z_idx
  195. } else {
  196. # Out of range. Disable 'single layer' view.
  197. $self->single_layer(0);
  198. $self->{checkbox_singlelayer}->SetValue(0);
  199. $z_idx_low = 0;
  200. $z_idx_high = $n_layers - 1;
  201. }
  202. if ($self->single_layer) {
  203. $z_idx_low = $z_idx_high;
  204. } elsif ($z_idx_low > $z_idx_high) {
  205. $z_idx_low = 0;
  206. }
  207. $self->slider_low->SetValue($z_idx_low);
  208. $self->slider_high->SetValue($z_idx_high);
  209. $self->slider_low->Show;
  210. $self->slider_high->Show;
  211. $self->Layout;
  212. my $by_tool = $self->{color_by_extruder};
  213. if ($self->{preferred_color_mode} eq 'tool_or_feature') {
  214. # It is left to Slic3r to decide whether the print shall be colored by the tool or by the feature.
  215. # Color by feature if it is a single extruder print.
  216. my $extruders = $self->{print}->extruders;
  217. $by_tool = scalar(@{$extruders}) > 1;
  218. $self->{color_by_extruder} = $by_tool;
  219. $self->{checkbox_color_by_extruder}->SetValue($by_tool);
  220. $self->{preferred_color_mode} = 'tool_or_feature';
  221. }
  222. # Collect colors per extruder.
  223. # Leave it empty, if the print should be colored by a feature.
  224. my @colors = ();
  225. if ($by_tool) {
  226. my @extruder_colors = @{$self->{config}->extruder_colour};
  227. my @filament_colors = @{$self->{config}->filament_colour};
  228. for (my $i = 0; $i <= $#extruder_colors; $i += 1) {
  229. my $color = $extruder_colors[$i];
  230. $color = $filament_colors[$i] if (! defined($color) || $color !~ m/^#[[:xdigit:]]{6}/);
  231. $color = '#FFFFFF' if (! defined($color) || $color !~ m/^#[[:xdigit:]]{6}/);
  232. push @colors, $color;
  233. }
  234. }
  235. if ($self->IsShown) {
  236. # load skirt and brim
  237. $self->canvas->load_print_toolpaths($self->print, \@colors);
  238. $self->canvas->load_wipe_tower_toolpaths($self->print, \@colors);
  239. foreach my $object (@{$self->print->objects}) {
  240. $self->canvas->load_print_object_toolpaths($object, \@colors);
  241. # Show the objects in very transparent color.
  242. #my @volume_ids = $self->canvas->load_object($object->model_object);
  243. #$self->canvas->volumes->[$_]->color->[3] = 0.2 for @volume_ids;
  244. }
  245. $self->canvas->zoom_to_volumes;
  246. $self->_loaded(1);
  247. }
  248. $self->set_z_range($self->{layers_z}[$z_idx_low], $self->{layers_z}[$z_idx_high]);
  249. }
  250. sub set_z_range
  251. {
  252. my ($self, $z_low, $z_high) = @_;
  253. return if !$self->enabled;
  254. $self->{z_label_low}->SetLabel(sprintf '%.2f', $z_low);
  255. $self->{z_label_high}->SetLabel(sprintf '%.2f', $z_high);
  256. $self->canvas->set_toolpaths_range($z_low - 1e-6, $z_high + 1e-6);
  257. $self->canvas->Refresh if $self->IsShown;
  258. }
  259. sub set_z_idx_low
  260. {
  261. my ($self, $idx_low) = @_;
  262. if ($self->enabled) {
  263. my $idx_high = $self->slider_high->GetValue;
  264. if ($idx_low >= $idx_high) {
  265. $idx_high = $idx_low;
  266. $self->slider_high->SetValue($idx_high);
  267. }
  268. $self->set_z_range($self->{layers_z}[$idx_low], $self->{layers_z}[$idx_high]);
  269. }
  270. }
  271. sub set_z_idx_high
  272. {
  273. my ($self, $idx_high) = @_;
  274. if ($self->enabled) {
  275. my $idx_low = $self->slider_low->GetValue;
  276. if ($idx_low > $idx_high) {
  277. $idx_low = $idx_high;
  278. $self->slider_low->SetValue($idx_low);
  279. }
  280. $self->set_z_range($self->{layers_z}[$idx_low], $self->{layers_z}[$idx_high]);
  281. }
  282. }
  283. sub set_bed_shape {
  284. my ($self, $bed_shape) = @_;
  285. $self->canvas->set_bed_shape($bed_shape);
  286. }
  287. sub set_number_extruders {
  288. my ($self, $number_extruders) = @_;
  289. if ($self->{number_extruders} != $number_extruders) {
  290. $self->{number_extruders} = $number_extruders;
  291. my $by_tool = $number_extruders > 1;
  292. $self->{color_by_extruder} = $by_tool;
  293. $self->{checkbox_color_by_extruder}->SetValue($by_tool);
  294. $self->{preferred_color_mode} = $by_tool ? 'tool_or_feature' : 'feature';
  295. }
  296. }
  297. # Called by the Platter wxNotebook when this page is activated.
  298. sub OnActivate {
  299. my ($self) = @_;
  300. $self->reload_print(1) if ($self->{reload_delayed});
  301. }
  302. 1;