BedShapeDialog.pm 11 KB

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