ObjectSettingsDialog.pm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # This dialog opens up when double clicked on an object line in the list at the right side of the platter.
  2. # One may load additional STLs and additional modifier STLs,
  3. # one may change the properties of the print per each modifier mesh or a Z-span.
  4. package Slic3r::GUI::Plater::ObjectSettingsDialog;
  5. use strict;
  6. use warnings;
  7. use utf8;
  8. use Wx qw(:dialog :id :misc :sizer :systemsettings :notebook wxTAB_TRAVERSAL wxTheApp);
  9. use Wx::Event qw(EVT_BUTTON);
  10. use base 'Wx::Dialog';
  11. # Called with
  12. # %params{object} of a Perl type Slic3r::GUI::Plater::Object
  13. # %params{model_object} of a C++ type Slic3r::ModelObject
  14. sub new {
  15. my ($class, $parent, %params) = @_;
  16. my $self = $class->SUPER::new($parent, -1, "Settings for " . $params{object}->name, wxDefaultPosition, [700,500], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
  17. $self->{$_} = $params{$_} for keys %params;
  18. $self->{tabpanel} = Wx::Notebook->new($self, -1, wxDefaultPosition, wxDefaultSize, wxNB_TOP | wxTAB_TRAVERSAL);
  19. $self->{tabpanel}->AddPage($self->{parts} = Slic3r::GUI::Plater::ObjectPartsPanel->new($self->{tabpanel}, model_object => $params{model_object}), "Parts");
  20. $self->{tabpanel}->AddPage($self->{layers} = Slic3r::GUI::Plater::ObjectDialog::LayersTab->new($self->{tabpanel}), "Layers");
  21. my $buttons = $self->CreateStdDialogButtonSizer(wxOK);
  22. EVT_BUTTON($self, wxID_OK, sub {
  23. # validate user input
  24. return if !$self->{parts}->CanClose;
  25. return if !$self->{layers}->CanClose;
  26. # notify tabs
  27. $self->{layers}->Closing;
  28. # save window size
  29. Slic3r::GUI::save_window_size($self, "object_settings");
  30. $self->EndModal(wxID_OK);
  31. $self->{parts}->Destroy;
  32. $self->Destroy;
  33. });
  34. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  35. $sizer->Add($self->{tabpanel}, 1, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 10);
  36. $sizer->Add($buttons, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  37. $self->SetSizer($sizer);
  38. $self->SetMinSize($self->GetSize);
  39. $self->Layout;
  40. Slic3r::GUI::restore_window_size($self, "object_settings");
  41. return $self;
  42. }
  43. sub PartsChanged {
  44. my ($self) = @_;
  45. return $self->{parts}->PartsChanged;
  46. }
  47. sub PartSettingsChanged {
  48. my ($self) = @_;
  49. return $self->{parts}->PartSettingsChanged || $self->{layers}->LayersChanged;
  50. }
  51. package Slic3r::GUI::Plater::ObjectDialog::BaseTab;
  52. use base 'Wx::Panel';
  53. sub model_object {
  54. my ($self) = @_;
  55. # $self->GetParent->GetParent is of type Slic3r::GUI::Plater::ObjectSettingsDialog
  56. return $self->GetParent->GetParent->{model_object};
  57. }
  58. package Slic3r::GUI::Plater::ObjectDialog::LayersTab;
  59. use Wx qw(:dialog :id :misc :sizer :systemsettings);
  60. use Wx::Grid;
  61. use Wx::Event qw(EVT_GRID_CELL_CHANGED);
  62. use base 'Slic3r::GUI::Plater::ObjectDialog::BaseTab';
  63. sub new {
  64. my $class = shift;
  65. my ($parent, %params) = @_;
  66. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize);
  67. my $sizer = Wx::BoxSizer->new(wxVERTICAL);
  68. {
  69. my $label = Wx::StaticText->new($self, -1, "You can use this section to override the default layer height for parts of this object.",
  70. wxDefaultPosition, [-1, 40]);
  71. $label->SetFont(Wx::SystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
  72. $sizer->Add($label, 0, wxEXPAND | wxALL, 10);
  73. }
  74. my $grid = $self->{grid} = Wx::Grid->new($self, -1, wxDefaultPosition, wxDefaultSize);
  75. $sizer->Add($grid, 1, wxEXPAND | wxALL, 10);
  76. $grid->CreateGrid(0, 3);
  77. $grid->DisableDragRowSize;
  78. $grid->HideRowLabels;
  79. $grid->SetColLabelValue(0, "Min Z (mm)");
  80. $grid->SetColLabelValue(1, "Max Z (mm)");
  81. $grid->SetColLabelValue(2, "Layer height (mm)");
  82. $grid->SetColSize($_, 135) for 0..2;
  83. $grid->SetDefaultCellAlignment(wxALIGN_CENTRE, wxALIGN_CENTRE);
  84. # load data
  85. foreach my $range (@{ $self->model_object->layer_height_ranges }) {
  86. $grid->AppendRows(1);
  87. my $i = $grid->GetNumberRows-1;
  88. $grid->SetCellValue($i, $_, $range->[$_]) for 0..2;
  89. }
  90. $grid->AppendRows(1); # append one empty row
  91. EVT_GRID_CELL_CHANGED($grid, sub {
  92. my ($grid, $event) = @_;
  93. # remove any non-numeric character
  94. my $value = $grid->GetCellValue($event->GetRow, $event->GetCol);
  95. $value =~ s/,/./g;
  96. $value =~ s/[^0-9.]//g;
  97. $grid->SetCellValue($event->GetRow, $event->GetCol, ($event->GetCol == 2) ? $self->_clamp_layer_height($value) : $value);
  98. # if there's no empty row, let's append one
  99. for my $i (0 .. $grid->GetNumberRows) {
  100. if ($i == $grid->GetNumberRows) {
  101. # if we're here then we found no empty row
  102. $grid->AppendRows(1);
  103. last;
  104. }
  105. if (!grep $grid->GetCellValue($i, $_), 0..2) {
  106. # exit loop if this row is empty
  107. last;
  108. }
  109. }
  110. $self->{layers_changed} = 1;
  111. });
  112. $self->SetSizer($sizer);
  113. $sizer->SetSizeHints($self);
  114. return $self;
  115. }
  116. sub _clamp_layer_height
  117. {
  118. my ($self, $value) = @_;
  119. # $self->GetParent->GetParent is of type Slic3r::GUI::Plater::ObjectSettingsDialog
  120. my $config = $self->GetParent->GetParent->{config};
  121. if ($value =~ /^[0-9,.E]+$/) {
  122. # Looks like a number. Validate the layer height.
  123. my $nozzle_dmrs = $config->get('nozzle_diameter');
  124. my $min_layer_heights = $config->get('min_layer_height');
  125. my $max_layer_heights = $config->get('max_layer_height');
  126. my $min_layer_height = 1000.;
  127. my $max_layer_height = 0.;
  128. my $max_nozzle_dmr = 0.;
  129. for (my $i = 0; $i < int(@{$nozzle_dmrs}); $i += 1) {
  130. $min_layer_height = $min_layer_heights->[$i] if ($min_layer_heights->[$i] < $min_layer_height);
  131. $max_layer_height = $max_layer_heights->[$i] if ($max_layer_heights->[$i] > $max_layer_height);
  132. $max_nozzle_dmr = $nozzle_dmrs ->[$i] if ($nozzle_dmrs ->[$i] > $max_nozzle_dmr );
  133. }
  134. $min_layer_height = 0.005 if ($min_layer_height < 0.005);
  135. $max_layer_height = $max_nozzle_dmr * 0.75 if ($max_layer_height == 0.);
  136. $max_layer_height = $max_nozzle_dmr if ($max_layer_height > $max_nozzle_dmr);
  137. return ($value < $min_layer_height) ? $min_layer_height :
  138. ($value > $max_layer_height) ? $max_layer_height : $value;
  139. } else {
  140. # If an invalid numeric value has been entered, use the default layer height.
  141. return $config->get('layer_height');
  142. }
  143. }
  144. sub CanClose {
  145. my $self = shift;
  146. # validate ranges before allowing user to dismiss the dialog
  147. foreach my $range ($self->_get_ranges) {
  148. my ($min, $max, $height) = @$range;
  149. if ($max <= $min) {
  150. Slic3r::GUI::show_error($self, "Invalid Z range $min-$max.");
  151. return 0;
  152. }
  153. if ($min < 0 || $max < 0) {
  154. Slic3r::GUI::show_error($self, "Invalid Z range $min-$max.");
  155. return 0;
  156. }
  157. if ($height < 0) {
  158. Slic3r::GUI::show_error($self, "Invalid layer height $height.");
  159. return 0;
  160. }
  161. # TODO: check for overlapping ranges
  162. }
  163. return 1;
  164. }
  165. sub Closing {
  166. my $self = shift;
  167. # save ranges into the plater object
  168. $self->model_object->set_layer_height_ranges([ $self->_get_ranges ]);
  169. }
  170. sub _get_ranges {
  171. my $self = shift;
  172. my @ranges = ();
  173. for my $i (0 .. $self->{grid}->GetNumberRows-1) {
  174. my ($min, $max, $height) = map $self->{grid}->GetCellValue($i, $_), 0..2;
  175. next if $min eq '' || $max eq '' || $height eq '';
  176. push @ranges, [ $min, $max, $height ];
  177. }
  178. return sort { $a->[0] <=> $b->[0] } @ranges;
  179. }
  180. sub LayersChanged {
  181. my ($self) = @_;
  182. return $self->{layers_changed};
  183. }
  184. 1;