LambdaObjectDialog.pm 7.4 KB

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