LambdaObjectDialog.pm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # Generate an anonymous or "lambda" 3D object. This gets used with the Create Modifier option in Settings.
  2. #
  3. package Slic3r::GUI::Plater::LambdaObjectDialog;
  4. use strict;
  5. use warnings;
  6. use utf8;
  7. use Slic3r::Geometry qw(PI X);
  8. use Wx qw(wxTheApp :dialog :id :misc :sizer wxTAB_TRAVERSAL wxCB_READONLY wxTE_PROCESS_TAB);
  9. use Wx::Event qw(EVT_CLOSE EVT_BUTTON EVT_COMBOBOX EVT_TEXT);
  10. use Scalar::Util qw(looks_like_number);
  11. use base 'Wx::Dialog';
  12. sub new {
  13. my $class = shift;
  14. my ($parent, %params) = @_;
  15. my $self = $class->SUPER::new($parent, -1, "Create Modifier", wxDefaultPosition, [500,500],
  16. wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
  17. # Note whether the window was already closed, so a pending update is not executed.
  18. $self->{already_closed} = 0;
  19. $self->{object_parameters} = {
  20. type => 'slab',
  21. dim => [1, 1, 1],
  22. cyl_r => 1,
  23. cyl_h => 1,
  24. sph_rho => 1.0,
  25. slab_h => 1.0,
  26. };
  27. $self->{sizer} = Wx::BoxSizer->new(wxVERTICAL);
  28. my $buttons = $self->CreateStdDialogButtonSizer(wxOK | wxCANCEL);
  29. EVT_BUTTON($self, wxID_OK, sub {
  30. $self->EndModal(wxID_OK);
  31. $self->Destroy;
  32. });
  33. EVT_BUTTON($self, wxID_CANCEL, sub {
  34. $self->EndModal(wxID_CANCEL);
  35. $self->Destroy;
  36. });
  37. $self->{type} = Wx::ComboBox->new($self, 1, $self->{object_parameters}{type},
  38. wxDefaultPosition, wxDefaultSize,
  39. [qw(slab box cylinder sphere)], wxCB_READONLY);
  40. my $optgroup_box;
  41. $optgroup_box = $self->{optgroup_box} = Slic3r::GUI::OptionsGroup->new(
  42. parent => $self,
  43. title => 'Add Cube...',
  44. on_change => sub {
  45. # Do validation
  46. my ($opt_id) = @_;
  47. if ($opt_id == 0 || $opt_id == 1 || $opt_id == 2) {
  48. if (!looks_like_number($optgroup_box->get_value($opt_id))) {
  49. return 0;
  50. }
  51. }
  52. $self->{object_parameters}->{dim}[$opt_id] = $optgroup_box->get_value($opt_id);
  53. },
  54. label_width => 100,
  55. );
  56. $optgroup_box->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  57. opt_id => 0,
  58. label => 'L (x)',
  59. type => 'f',
  60. default => $self->{object_parameters}{dim}[0],
  61. sidetext => 'mm',
  62. ));
  63. $optgroup_box->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  64. opt_id => 1,
  65. label => 'W (y)',
  66. type => 'f',
  67. default => $self->{object_parameters}{dim}[1],
  68. sidetext => 'mm',
  69. ));
  70. $optgroup_box->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  71. opt_id => 2,
  72. label => 'H (z)',
  73. type => 'f',
  74. default => $self->{object_parameters}{dim}[2],
  75. sidetext => 'mm',
  76. ));
  77. my $optgroup_cylinder;
  78. $optgroup_cylinder = $self->{optgroup_cylinder} = Slic3r::GUI::OptionsGroup->new(
  79. parent => $self,
  80. title => 'Add Cylinder...',
  81. on_change => sub {
  82. # Do validation
  83. my ($opt_id) = @_;
  84. if ($opt_id eq 'cyl_r' || $opt_id eq 'cyl_h') {
  85. if (!looks_like_number($optgroup_cylinder->get_value($opt_id))) {
  86. return 0;
  87. }
  88. }
  89. $self->{object_parameters}->{$opt_id} = $optgroup_cylinder->get_value($opt_id);
  90. },
  91. label_width => 100,
  92. );
  93. $optgroup_cylinder->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  94. opt_id => "cyl_r",
  95. label => 'Radius',
  96. type => 'f',
  97. default => $self->{object_parameters}{cyl_r},
  98. sidetext => 'mm',
  99. ));
  100. $optgroup_cylinder->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  101. opt_id => "cyl_h",
  102. label => 'Height',
  103. type => 'f',
  104. default => $self->{object_parameters}{cyl_h},
  105. sidetext => 'mm',
  106. ));
  107. my $optgroup_sphere;
  108. $optgroup_sphere = $self->{optgroup_sphere} = Slic3r::GUI::OptionsGroup->new(
  109. parent => $self,
  110. title => 'Add Sphere...',
  111. on_change => sub {
  112. # Do validation
  113. my ($opt_id) = @_;
  114. if ($opt_id eq 'sph_rho') {
  115. if (!looks_like_number($optgroup_sphere->get_value($opt_id))) {
  116. return 0;
  117. }
  118. }
  119. $self->{object_parameters}->{$opt_id} = $optgroup_sphere->get_value($opt_id);
  120. },
  121. label_width => 100,
  122. );
  123. $optgroup_sphere->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  124. opt_id => "sph_rho",
  125. label => 'Radius',
  126. type => 'f',
  127. default => $self->{object_parameters}{sph_rho},
  128. sidetext => 'mm',
  129. ));
  130. my $optgroup_slab;
  131. $optgroup_slab = $self->{optgroup_slab} = Slic3r::GUI::OptionsGroup->new(
  132. parent => $self,
  133. title => 'Add Slab...',
  134. on_change => sub {
  135. # Do validation
  136. my ($opt_id) = @_;
  137. if ($opt_id eq 'slab_h') {
  138. if (!looks_like_number($optgroup_slab->get_value($opt_id))) {
  139. return 0;
  140. }
  141. }
  142. $self->{object_parameters}->{$opt_id} = $optgroup_slab->get_value($opt_id);
  143. },
  144. label_width => 100,
  145. );
  146. $optgroup_slab->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  147. opt_id => "slab_h",
  148. label => 'Thickness',
  149. type => 'f',
  150. default => $self->{object_parameters}{slab_h},
  151. sidetext => 'mm',
  152. ));
  153. EVT_COMBOBOX($self, 1, sub{
  154. $self->{object_parameters}->{type} = $self->{type}->GetValue();
  155. $self->_update_ui;
  156. });
  157. $self->{sizer}->Add($self->{type}, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  158. $self->{sizer}->Add($optgroup_box->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  159. $self->{sizer}->Add($optgroup_cylinder->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  160. $self->{sizer}->Add($optgroup_sphere->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  161. $self->{sizer}->Add($optgroup_slab->sizer, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  162. $self->{sizer}->Add($buttons,0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 10);
  163. $self->_update_ui;
  164. $self->SetSizer($self->{sizer});
  165. $self->{sizer}->Fit($self);
  166. $self->{sizer}->SetSizeHints($self);
  167. return $self;
  168. }
  169. sub ObjectParameter {
  170. my ($self) = @_;
  171. return $self->{object_parameters};
  172. }
  173. sub _update_ui {
  174. my ($self) = @_;
  175. $self->{sizer}->Hide($self->{optgroup_cylinder}->sizer);
  176. $self->{sizer}->Hide($self->{optgroup_slab}->sizer);
  177. $self->{sizer}->Hide($self->{optgroup_box}->sizer);
  178. $self->{sizer}->Hide($self->{optgroup_sphere}->sizer);
  179. if ($self->{type}->GetValue eq "box") {
  180. $self->{sizer}->Show($self->{optgroup_box}->sizer);
  181. } elsif ($self->{type}->GetValue eq "cylinder") {
  182. $self->{sizer}->Show($self->{optgroup_cylinder}->sizer);
  183. } elsif ($self->{type}->GetValue eq "slab") {
  184. $self->{sizer}->Show($self->{optgroup_slab}->sizer);
  185. } elsif ($self->{type}->GetValue eq "sphere") {
  186. $self->{sizer}->Show($self->{optgroup_sphere}->sizer);
  187. }
  188. $self->{sizer}->Fit($self);
  189. $self->{sizer}->SetSizeHints($self);
  190. }
  191. 1;