BedShapeDialog.pm 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package Slic3r::GUI::BedShapeDialog;
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use List::Util qw(min max);
  6. use Slic3r::Geometry qw(PI X Y unscale);
  7. use Wx qw(:dialog :id :misc :sizer :choicebook wxTAB_TRAVERSAL);
  8. use Wx::Event qw(EVT_CLOSE);
  9. use base 'Wx::Dialog';
  10. sub new {
  11. my $class = shift;
  12. my ($parent, $default) = @_;
  13. my $self = $class->SUPER::new($parent, -1, "Bed Shape", wxDefaultPosition, [350,700], wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
  14. $self->{panel} = my $panel = Slic3r::GUI::BedShapePanel->new($self, $default);
  15. my $main_sizer = Wx::BoxSizer->new(wxVERTICAL);
  16. $main_sizer->Add($panel, 1, wxEXPAND);
  17. $main_sizer->Add($self->CreateButtonSizer(wxOK | wxCANCEL), 0, wxEXPAND);
  18. $self->SetSizer($main_sizer);
  19. $self->SetMinSize($self->GetSize);
  20. $main_sizer->SetSizeHints($self);
  21. # needed to actually free memory
  22. EVT_CLOSE($self, sub {
  23. $self->EndModal(wxID_OK);
  24. $self->Destroy;
  25. });
  26. return $self;
  27. }
  28. sub GetValue {
  29. my ($self) = @_;
  30. return $self->{panel}->GetValue;
  31. }
  32. package Slic3r::GUI::BedShapePanel;
  33. use List::Util qw(min max sum first);
  34. use Slic3r::Geometry qw(PI X Y scale unscale scaled_epsilon deg2rad);
  35. use Wx qw(:font :id :misc :sizer :choicebook :filedialog :pen :brush wxTAB_TRAVERSAL);
  36. use Wx::Event qw(EVT_CLOSE EVT_CHOICEBOOK_PAGE_CHANGED EVT_BUTTON);
  37. use base 'Wx::Panel';
  38. use constant SHAPE_RECTANGULAR => 0;
  39. use constant SHAPE_CIRCULAR => 1;
  40. use constant SHAPE_CUSTOM => 2;
  41. sub new {
  42. my $class = shift;
  43. my ($parent, $default) = @_;
  44. my $self = $class->SUPER::new($parent, -1);
  45. $self->on_change(undef);
  46. my $box = Wx::StaticBox->new($self, -1, "Shape");
  47. my $sbsizer = Wx::StaticBoxSizer->new($box, wxVERTICAL);
  48. # shape options
  49. $self->{shape_options_book} = Wx::Choicebook->new($self, -1, wxDefaultPosition, [300,-1], wxCHB_TOP);
  50. $sbsizer->Add($self->{shape_options_book});
  51. $self->{optgroups} = [];
  52. {
  53. my $optgroup = $self->_init_shape_options_page('Rectangular');
  54. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  55. opt_id => 'rect_size',
  56. type => 'point',
  57. label => 'Size',
  58. tooltip => 'Size in X and Y of the rectangular plate.',
  59. default => [200,200],
  60. ));
  61. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  62. opt_id => 'rect_origin',
  63. type => 'point',
  64. label => 'Origin',
  65. tooltip => 'Distance of the 0,0 G-code coordinate from the front left corner of the rectangle.',
  66. default => [0,0],
  67. ));
  68. }
  69. {
  70. my $optgroup = $self->_init_shape_options_page('Circular');
  71. $optgroup->append_single_option_line(Slic3r::GUI::OptionsGroup::Option->new(
  72. opt_id => 'diameter',
  73. type => 'f',
  74. label => 'Diameter',
  75. tooltip => 'Diameter of the print bed. It is assumed that origin (0,0) is located in the center.',
  76. sidetext => 'mm',
  77. default => 200,
  78. ));
  79. }
  80. {
  81. my $optgroup = $self->_init_shape_options_page('Custom');
  82. $optgroup->append_line(Slic3r::GUI::OptionsGroup::Line->new(
  83. full_width => 1,
  84. widget => sub {
  85. my ($parent) = @_;
  86. my $btn = Wx::Button->new($parent, -1, "Load shape from STL...", wxDefaultPosition, wxDefaultSize);
  87. EVT_BUTTON($self, $btn, sub { $self->_load_stl });
  88. return $btn;
  89. }
  90. ));
  91. }
  92. EVT_CHOICEBOOK_PAGE_CHANGED($self, -1, sub {
  93. $self->_update_shape;
  94. });
  95. # right pane with preview canvas
  96. my $canvas = $self->{canvas} = Slic3r::GUI::2DBed->new($self);
  97. # main sizer
  98. my $top_sizer = Wx::BoxSizer->new(wxHORIZONTAL);
  99. $top_sizer->Add($sbsizer, 0, wxEXPAND | wxTOP | wxBOTTOM, 10);
  100. $top_sizer->Add($canvas, 1, wxEXPAND | wxALL, 10) if $canvas;
  101. $self->SetSizerAndFit($top_sizer);
  102. $self->_set_shape($default);
  103. $self->_update_preview;
  104. return $self;
  105. }
  106. sub on_change {
  107. my ($self, $cb) = @_;
  108. $self->{on_change} = $cb // sub {};
  109. }
  110. sub _set_shape {
  111. my ($self, $points) = @_;
  112. $self->{bed_shape} = $points;
  113. # is this a rectangle?
  114. if (@$points == 4) {
  115. my $polygon = Slic3r::Polygon->new_scale(@$points);
  116. my $lines = $polygon->lines;
  117. if ($lines->[0]->parallel_to_line($lines->[2]) && $lines->[1]->parallel_to_line($lines->[3])) {
  118. # okay, it's a rectangle
  119. # find origin
  120. # the || 0 hack prevents "-0" which might confuse the user
  121. my $x_min = min(map $_->[X], @$points) || 0;
  122. my $x_max = max(map $_->[X], @$points) || 0;
  123. my $y_min = min(map $_->[Y], @$points) || 0;
  124. my $y_max = max(map $_->[Y], @$points) || 0;
  125. my $origin = [-$x_min, -$y_min];
  126. $self->{shape_options_book}->SetSelection(SHAPE_RECTANGULAR);
  127. my $optgroup = $self->{optgroups}[SHAPE_RECTANGULAR];
  128. $optgroup->set_value('rect_size', [ $x_max-$x_min, $y_max-$y_min ]);
  129. $optgroup->set_value('rect_origin', $origin);
  130. $self->_update_shape;
  131. return;
  132. }
  133. }
  134. # is this a circle?
  135. {
  136. my $polygon = Slic3r::Polygon->new_scale(@$points);
  137. my $center = $polygon->bounding_box->center;
  138. my @vertex_distances = map $center->distance_to($_), @$polygon;
  139. my $avg_dist = sum(@vertex_distances)/@vertex_distances;
  140. if (!defined first { abs($_ - $avg_dist) > 10*scaled_epsilon } @vertex_distances) {
  141. # all vertices are equidistant to center
  142. $self->{shape_options_book}->SetSelection(SHAPE_CIRCULAR);
  143. my $optgroup = $self->{optgroups}[SHAPE_CIRCULAR];
  144. $optgroup->set_value('diameter', sprintf("%.0f", unscale($avg_dist*2)));
  145. $self->_update_shape;
  146. return;
  147. }
  148. }
  149. $self->{shape_options_book}->SetSelection(SHAPE_CUSTOM);
  150. $self->_update_shape;
  151. }
  152. sub _update_shape {
  153. my ($self) = @_;
  154. my $page_idx = $self->{shape_options_book}->GetSelection;
  155. if ($page_idx == SHAPE_RECTANGULAR) {
  156. my $rect_size = $self->{optgroups}[SHAPE_RECTANGULAR]->get_value('rect_size');
  157. my $rect_origin = $self->{optgroups}[SHAPE_RECTANGULAR]->get_value('rect_origin');
  158. my ($x, $y) = @$rect_size;
  159. return if !$x || !$y; # empty strings
  160. my ($x0, $y0) = (0,0);
  161. my ($x1, $y1) = ($x,$y);
  162. {
  163. my ($dx, $dy) = @$rect_origin;
  164. return if $dx eq '' || $dy eq ''; # empty strings
  165. $x0 -= $dx;
  166. $x1 -= $dx;
  167. $y0 -= $dy;
  168. $y1 -= $dy;
  169. }
  170. $self->{canvas}->bed_shape([
  171. [$x0,$y0],
  172. [$x1,$y0],
  173. [$x1,$y1],
  174. [$x0,$y1],
  175. ]);
  176. } elsif ($page_idx == SHAPE_CIRCULAR) {
  177. my $diameter = $self->{optgroups}[SHAPE_CIRCULAR]->get_value('diameter');
  178. return if !$diameter;
  179. my $r = $diameter/2;
  180. my $twopi = 2*PI;
  181. my $edges = 60;
  182. my $polygon = Slic3r::Polygon->new_scale(
  183. map [ $r * cos $_, $r * sin $_ ],
  184. map { $twopi/$edges*$_ } 1..$edges
  185. );
  186. $self->{canvas}->bed_shape([
  187. map [ unscale($_->x), unscale($_->y) ], @$polygon #))
  188. ]);
  189. }
  190. $self->{on_change}->();
  191. $self->_update_preview;
  192. }
  193. sub _update_preview {
  194. my ($self) = @_;
  195. $self->{canvas}->Refresh if $self->{canvas};
  196. }
  197. sub _init_shape_options_page {
  198. my ($self, $title) = @_;
  199. my $panel = Wx::Panel->new($self->{shape_options_book});
  200. my $optgroup;
  201. push @{$self->{optgroups}}, $optgroup = Slic3r::GUI::OptionsGroup->new(
  202. parent => $panel,
  203. title => 'Settings',
  204. label_width => 100,
  205. on_change => sub {
  206. my ($opt_id) = @_;
  207. #$self->{"_$opt_id"} = $optgroup->get_value($opt_id);
  208. $self->_update_shape;
  209. },
  210. );
  211. $panel->SetSizerAndFit($optgroup->sizer);
  212. $self->{shape_options_book}->AddPage($panel, $title);
  213. return $optgroup;
  214. }
  215. sub _load_stl {
  216. my ($self) = @_;
  217. my $dialog = Wx::FileDialog->new($self, 'Choose a file to import bed shape from (STL/OBJ/AMF):', "", "", &Slic3r::GUI::MODEL_WILDCARD, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
  218. if ($dialog->ShowModal != wxID_OK) {
  219. $dialog->Destroy;
  220. return;
  221. }
  222. my $input_file = Slic3r::decode_path($dialog->GetPaths);
  223. $dialog->Destroy;
  224. my $model = Slic3r::Model->read_from_file($input_file);
  225. my $mesh = $model->raw_mesh;
  226. my $expolygons = $mesh->horizontal_projection;
  227. if (@$expolygons == 0) {
  228. Slic3r::GUI::show_error($self, "The selected file contains no geometry.");
  229. return;
  230. }
  231. if (@$expolygons > 1) {
  232. Slic3r::GUI::show_error($self, "The selected file contains several disjoint areas. This is not supported.");
  233. return;
  234. }
  235. my $polygon = $expolygons->[0]->contour;
  236. $self->{canvas}->bed_shape([ map [ unscale($_->x), unscale($_->y) ], @$polygon ]); #))
  237. }
  238. sub GetValue {
  239. my ($self) = @_;
  240. return $self->{canvas}->bed_shape;
  241. }
  242. 1;