2D.pm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. # 2D preview on the platter.
  2. # 3D objects are visualized by their convex hulls.
  3. package Slic3r::GUI::Plater::2D;
  4. use strict;
  5. use warnings;
  6. use utf8;
  7. use List::Util qw(min max first);
  8. use Slic3r::Geometry qw(X Y scale unscale convex_hull);
  9. use Slic3r::Geometry::Clipper qw(offset JT_ROUND intersection_pl);
  10. use Wx qw(:misc :pen :brush :sizer :font :cursor wxTAB_TRAVERSAL);
  11. use Wx::Event qw(EVT_MOUSE_EVENTS EVT_KEY_DOWN EVT_PAINT EVT_ERASE_BACKGROUND EVT_SIZE);
  12. use base 'Wx::Panel';
  13. # Color Scheme
  14. use Slic3r::GUI::ColorScheme;
  15. use constant CANVAS_TEXT => join('-', +(localtime)[3,4]) eq '13-8'
  16. ? 'What do you want to print today? ™' # Sept. 13, 2006. The first part ever printed by a RepRap to make another RepRap.
  17. : 'Drag your objects here';
  18. sub new {
  19. my $class = shift;
  20. my ($parent, $size, $objects, $model, $config) = @_;
  21. if ( ( defined $Slic3r::GUI::Settings->{_}{colorscheme} ) && ( Slic3r::GUI::ColorScheme->can($Slic3r::GUI::Settings->{_}{colorscheme}) ) ) {
  22. my $myGetSchemeName = \&{"Slic3r::GUI::ColorScheme::$Slic3r::GUI::Settings->{_}{colorscheme}"};
  23. $myGetSchemeName->();
  24. } else {
  25. Slic3r::GUI::ColorScheme->getDefault();
  26. }
  27. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, $size, wxTAB_TRAVERSAL);
  28. # This has only effect on MacOS. On Windows and Linux/GTK, the background is painted by $self->repaint().
  29. $self->SetBackgroundColour(Wx::Colour->new(@BACKGROUND255));
  30. $self->{objects} = $objects;
  31. $self->{model} = $model;
  32. $self->{config} = $config;
  33. $self->{on_select_object} = sub {};
  34. $self->{on_double_click} = sub {};
  35. $self->{on_right_click} = sub {};
  36. $self->{on_instances_moved} = sub {};
  37. $self->{objects_brush} = Wx::Brush->new(Wx::Colour->new(@BED_OBJECTS), wxSOLID);
  38. $self->{instance_brush} = Wx::Brush->new(Wx::Colour->new(@BED_INSTANCE), wxSOLID);
  39. $self->{selected_brush} = Wx::Brush->new(Wx::Colour->new(@BED_SELECTED), wxSOLID);
  40. $self->{dragged_brush} = Wx::Brush->new(Wx::Colour->new(@BED_DRAGGED), wxSOLID);
  41. $self->{bed_brush} = Wx::Brush->new(Wx::Colour->new(@BED_COLOR), wxSOLID);
  42. $self->{transparent_brush} = Wx::Brush->new(Wx::Colour->new(0,0,0), wxTRANSPARENT);
  43. $self->{grid_pen} = Wx::Pen->new(Wx::Colour->new(@BED_GRID), 1, wxSOLID);
  44. $self->{print_center_pen} = Wx::Pen->new(Wx::Colour->new(@BED_CENTER), 1, wxSOLID);
  45. $self->{clearance_pen} = Wx::Pen->new(Wx::Colour->new(@BED_CLEARANCE), 1, wxSOLID);
  46. $self->{skirt_pen} = Wx::Pen->new(Wx::Colour->new(@BED_SKIRT), 1, wxSOLID);
  47. $self->{dark_pen} = Wx::Pen->new(Wx::Colour->new(@BED_DARK), 1, wxSOLID);
  48. $self->{user_drawn_background} = $^O ne 'darwin';
  49. $self->{selected_instance} = undef;
  50. EVT_PAINT($self, \&repaint);
  51. EVT_ERASE_BACKGROUND($self, sub {}) if $self->{user_drawn_background};
  52. EVT_MOUSE_EVENTS($self, \&mouse_event);
  53. EVT_SIZE($self, sub {
  54. $self->update_bed_size;
  55. $self->Refresh;
  56. });
  57. EVT_KEY_DOWN($self, sub {
  58. my ($s, $event) = @_;
  59. my $key = $event->GetKeyCode;
  60. if ($key == 65 || $key == 314) {
  61. $self->nudge_instance('left');
  62. } elsif ($key == 87 || $key == 315) {
  63. $self->nudge_instance('up');
  64. } elsif ($key == 68 || $key == 316) {
  65. $self->nudge_instance('right');
  66. } elsif ($key == 83 || $key == 317) {
  67. $self->nudge_instance('down');
  68. } else {
  69. $event->Skip;
  70. }
  71. });
  72. return $self;
  73. }
  74. sub on_select_object {
  75. my ($self, $cb) = @_;
  76. $self->{on_select_object} = $cb;
  77. }
  78. sub on_double_click {
  79. my ($self, $cb) = @_;
  80. $self->{on_double_click} = $cb;
  81. }
  82. sub on_right_click {
  83. my ($self, $cb) = @_;
  84. $self->{on_right_click} = $cb;
  85. }
  86. sub on_instances_moved {
  87. my ($self, $cb) = @_;
  88. $self->{on_instances_moved} = $cb;
  89. }
  90. sub repaint {
  91. my ($self, $event) = @_;
  92. # Focus is needed in order to catch keyboard events.
  93. $self->SetFocus;
  94. my $dc = Wx::AutoBufferedPaintDC->new($self);
  95. my $size = $self->GetSize;
  96. my @size = ($size->GetWidth, $size->GetHeight);
  97. if ($self->{user_drawn_background}) {
  98. # On all systems the AutoBufferedPaintDC() achieves double buffering.
  99. # On MacOS the background is erased, on Windows the background is not erased
  100. # and on Linux/GTK the background is erased to gray color.
  101. # Fill DC with the background on Windows & Linux/GTK.
  102. my $brush_background = Wx::Brush->new(Wx::Colour->new(@BACKGROUND255), wxSOLID);
  103. my $pen_background = Wx::Pen->new(Wx::Colour->new(@BACKGROUND255), 1, wxSOLID);
  104. $dc->SetPen($pen_background);
  105. $dc->SetBrush($brush_background);
  106. my $rect = $self->GetUpdateRegion()->GetBox();
  107. $dc->DrawRectangle($rect->GetLeft(), $rect->GetTop(), $rect->GetWidth(), $rect->GetHeight());
  108. }
  109. # draw bed
  110. {
  111. $dc->SetPen($self->{print_center_pen});
  112. $dc->SetBrush($self->{bed_brush});
  113. $dc->DrawPolygon($self->scaled_points_to_pixel($self->{bed_polygon}, 1), 0, 0);
  114. }
  115. # draw print center
  116. if (@{$self->{objects}} && $Slic3r::GUI::Settings->{_}{autocenter}) {
  117. my $center = $self->unscaled_point_to_pixel($self->{print_center});
  118. $dc->SetPen($self->{print_center_pen});
  119. $dc->DrawLine($center->[X], 0, $center->[X], $size[Y]);
  120. $dc->DrawLine(0, $center->[Y], $size[X], $center->[Y]);
  121. $dc->SetTextForeground(Wx::Colour->new(0,0,0));
  122. $dc->SetFont(Wx::Font->new(10, wxDEFAULT, wxNORMAL, wxNORMAL));
  123. $dc->DrawLabel("X = " . sprintf('%.0f', $self->{print_center}->[X]), Wx::Rect->new(0, 0, $center->[X]*2, $self->GetSize->GetHeight), wxALIGN_CENTER_HORIZONTAL | wxALIGN_BOTTOM);
  124. $dc->DrawRotatedText("Y = " . sprintf('%.0f', $self->{print_center}->[Y]), 0, $center->[Y]+15, 90);
  125. }
  126. # draw frame
  127. if (0) {
  128. $dc->SetPen($self->{dark_pen});
  129. $dc->SetBrush($self->{transparent_brush});
  130. $dc->DrawRectangle(0, 0, @size);
  131. }
  132. # draw text if plate is empty
  133. if (!@{$self->{objects}}) {
  134. $dc->SetTextForeground(Wx::Colour->new(@BED_OBJECTS));
  135. $dc->SetFont(Wx::Font->new(14, wxDEFAULT, wxNORMAL, wxNORMAL));
  136. $dc->DrawLabel(CANVAS_TEXT, Wx::Rect->new(0, 0, $self->GetSize->GetWidth, $self->GetSize->GetHeight), wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
  137. } else {
  138. # draw grid
  139. $dc->SetPen($self->{grid_pen});
  140. $dc->DrawLine(map @$_, @$_) for @{$self->{grid}};
  141. }
  142. # draw thumbnails
  143. $dc->SetPen($self->{dark_pen});
  144. $self->clean_instance_thumbnails;
  145. for my $obj_idx (0 .. $#{$self->{objects}}) {
  146. my $object = $self->{objects}[$obj_idx];
  147. my $model_object = $self->{model}->objects->[$obj_idx];
  148. next unless defined $object->thumbnail;
  149. for my $instance_idx (0 .. $#{$model_object->instances}) {
  150. my $instance = $model_object->instances->[$instance_idx];
  151. next if !defined $object->transformed_thumbnail;
  152. my $thumbnail = $object->transformed_thumbnail->clone; # in scaled model coordinates
  153. $thumbnail->translate(map scale($_), @{$instance->offset});
  154. $object->instance_thumbnails->[$instance_idx] = $thumbnail;
  155. if (defined $self->{drag_object} && $self->{drag_object}[0] == $obj_idx && $self->{drag_object}[1] == $instance_idx) {
  156. $dc->SetBrush($self->{dragged_brush});
  157. } elsif ($object->selected && $object->selected_instance == $instance_idx) {
  158. $dc->SetBrush($self->{instance_brush});
  159. } elsif ($object->selected) {
  160. $dc->SetBrush($self->{selected_brush});
  161. } else {
  162. $dc->SetBrush($self->{objects_brush});
  163. }
  164. foreach my $expolygon (@$thumbnail) {
  165. foreach my $points (@{$expolygon->pp}) {
  166. $dc->DrawPolygon($self->scaled_points_to_pixel($points, 1), 0, 0);
  167. }
  168. }
  169. if (0) {
  170. # draw bounding box for debugging purposes
  171. my $bb = $model_object->instance_bounding_box($instance_idx);
  172. $bb->scale($self->{scaling_factor});
  173. # no need to translate by instance offset because instance_bounding_box() does that
  174. my $points = $bb->polygon->pp;
  175. $dc->SetPen($self->{clearance_pen});
  176. $dc->SetBrush($self->{transparent_brush});
  177. $dc->DrawPolygon($self->_y($points), 0, 0);
  178. }
  179. # if sequential printing is enabled and we have more than one object, draw clearance area
  180. if ($self->{config}->complete_objects && (map @{$_->instances}, @{$self->{model}->objects}) > 1) {
  181. my ($clearance) = @{offset([$thumbnail->convex_hull], (scale($self->{config}->extruder_clearance_radius) / 2), 1, JT_ROUND, scale(0.1))};
  182. $dc->SetPen($self->{clearance_pen});
  183. $dc->SetBrush($self->{transparent_brush});
  184. $dc->DrawPolygon($self->scaled_points_to_pixel($clearance, 1), 0, 0);
  185. }
  186. }
  187. }
  188. # draw skirt
  189. if (@{$self->{objects}} && $self->{config}->skirts) {
  190. my @points = map @{$_->contour}, map @$_, map @{$_->instance_thumbnails}, @{$self->{objects}};
  191. if (@points >= 3) {
  192. my ($convex_hull) = @{offset([convex_hull(\@points)], scale max($self->{config}->brim_width + $self->{config}->skirt_distance), 1, JT_ROUND, scale(0.1))};
  193. $dc->SetPen($self->{skirt_pen});
  194. $dc->SetBrush($self->{transparent_brush});
  195. $dc->DrawPolygon($self->scaled_points_to_pixel($convex_hull, 1), 0, 0);
  196. }
  197. }
  198. $event->Skip;
  199. }
  200. sub mouse_event {
  201. my ($self, $event) = @_;
  202. my $pos = $event->GetPosition;
  203. my $point = $self->point_to_model_units([ $pos->x, $pos->y ]); #]]
  204. if ($event->ButtonDown) {
  205. # On Linux, Focus is needed in order to move selected instance using keyboard arrows.
  206. $self->SetFocus;
  207. $self->{on_select_object}->(undef);
  208. $self->{selected_instance} = undef;
  209. # traverse objects and instances in reverse order, so that if they're overlapping
  210. # we get the one that gets drawn last, thus on top (as user expects that to move)
  211. OBJECTS: for my $obj_idx (reverse 0 .. $#{$self->{objects}}) {
  212. my $object = $self->{objects}->[$obj_idx];
  213. for my $instance_idx (reverse 0 .. $#{ $object->instance_thumbnails }) {
  214. my $thumbnail = $object->instance_thumbnails->[$instance_idx];
  215. if (defined first { $_->contour->contains_point($point) } @$thumbnail) {
  216. $self->{on_select_object}->($obj_idx);
  217. if ($event->LeftDown) {
  218. # start dragging
  219. my $instance = $self->{model}->objects->[$obj_idx]->instances->[$instance_idx];
  220. my $instance_origin = [ map scale($_), @{$instance->offset} ];
  221. $self->{drag_start_pos} = [ # displacement between the click and the instance origin in scaled model units
  222. $point->x - $instance_origin->[X],
  223. $point->y - $instance_origin->[Y], #-
  224. ];
  225. $self->{drag_object} = [ $obj_idx, $instance_idx ];
  226. $self->{objects}->[$obj_idx]->selected_instance($instance_idx);
  227. $self->{selected_instance} = $self->{drag_object};
  228. } elsif ($event->RightDown) {
  229. $self->{on_right_click}->($pos);
  230. }
  231. last OBJECTS;
  232. }
  233. }
  234. }
  235. $self->Refresh;
  236. } elsif ($event->LeftUp) {
  237. $self->{on_instances_moved}->()
  238. if $self->{drag_object};
  239. $self->{drag_start_pos} = undef;
  240. $self->{drag_object} = undef;
  241. $self->SetCursor(wxSTANDARD_CURSOR);
  242. } elsif ($event->LeftDClick) {
  243. $self->{on_double_click}->();
  244. } elsif ($event->Dragging) {
  245. return if !$self->{drag_start_pos}; # concurrency problems
  246. my ($obj_idx, $instance_idx) = @{ $self->{drag_object} };
  247. my $model_object = $self->{model}->objects->[$obj_idx];
  248. $model_object->instances->[$instance_idx]->set_offset(
  249. Slic3r::Pointf->new(
  250. unscale($point->[X] - $self->{drag_start_pos}[X]),
  251. unscale($point->[Y] - $self->{drag_start_pos}[Y]),
  252. ));
  253. $model_object->update_bounding_box;
  254. $self->Refresh;
  255. } elsif ($event->Moving) {
  256. my $cursor = wxSTANDARD_CURSOR;
  257. if (defined first { $_->contour->contains_point($point) } map @$_, map @{$_->instance_thumbnails}, @{ $self->{objects} }) {
  258. $cursor = Wx::Cursor->new(wxCURSOR_HAND);
  259. }
  260. $self->SetCursor($cursor);
  261. }
  262. }
  263. sub nudge_instance{
  264. my ($self, $direction) = @_;
  265. # Get the selected instance of an object.
  266. if (!defined $self->{selected_instance}) {
  267. # Check if an object is selected.
  268. for my $obj_idx (0 .. $#{$self->{objects}}) {
  269. if ($self->{objects}->[$obj_idx]->selected) {
  270. if ($self->{objects}->[$obj_idx]->selected_instance != -1) {
  271. $self->{selected_instance} = [$obj_idx, $self->{objects}->[$obj_idx]->selected_instance];
  272. }
  273. }
  274. }
  275. }
  276. return if not defined ($self->{selected_instance});
  277. my ($obj_idx, $instance_idx) = @{ $self->{selected_instance} };
  278. my $object = $self->{model}->objects->[$obj_idx];
  279. my $instance = $object->instances->[$instance_idx];
  280. # Get the nudge values.
  281. my $x_nudge = 0;
  282. my $y_nudge = 0;
  283. $self->{nudge_value} = ($Slic3r::GUI::Settings->{_}{nudge_val} < 0.1 ? 0.1 : $Slic3r::GUI::Settings->{_}{nudge_val}) / &Slic3r::SCALING_FACTOR;
  284. if ($direction eq 'right'){
  285. $x_nudge = $self->{nudge_value};
  286. } elsif ($direction eq 'left'){
  287. $x_nudge = -1 * $self->{nudge_value};
  288. } elsif ($direction eq 'up'){
  289. $y_nudge = $self->{nudge_value};
  290. } elsif ($direction eq 'down'){
  291. $y_nudge = -$self->{nudge_value};
  292. }
  293. my $point = Slic3r::Pointf->new($x_nudge, $y_nudge);
  294. my $instance_origin = [ map scale($_), @{$instance->offset} ];
  295. $point = [ map scale($_), @{$point} ];
  296. $instance->set_offset(
  297. Slic3r::Pointf->new(
  298. unscale( $instance_origin->[X] + $x_nudge),
  299. unscale( $instance_origin->[Y] + $y_nudge),
  300. ));
  301. $object->update_bounding_box;
  302. $self->Refresh;
  303. $self->{on_instances_moved}->();
  304. }
  305. sub update_bed_size {
  306. my $self = shift;
  307. # when the canvas is not rendered yet, its GetSize() method returns 0,0
  308. my $canvas_size = $self->GetSize;
  309. my ($canvas_w, $canvas_h) = ($canvas_size->GetWidth, $canvas_size->GetHeight);
  310. return if $canvas_w == 0;
  311. # get bed shape polygon
  312. $self->{bed_polygon} = my $polygon = Slic3r::Polygon->new_scale(@{$self->{config}->bed_shape});
  313. my $bb = $polygon->bounding_box;
  314. my $size = $bb->size;
  315. # calculate the scaling factor needed for constraining print bed area inside preview
  316. # scaling_factor is expressed in pixel / mm
  317. $self->{scaling_factor} = min($canvas_w / unscale($size->x), $canvas_h / unscale($size->y)); #)
  318. # calculate the displacement needed to center bed
  319. $self->{bed_origin} = [
  320. $canvas_w/2 - (unscale($bb->x_max + $bb->x_min)/2 * $self->{scaling_factor}),
  321. $canvas_h - ($canvas_h/2 - (unscale($bb->y_max + $bb->y_min)/2 * $self->{scaling_factor})),
  322. ];
  323. # calculate print center
  324. my $center = $bb->center;
  325. $self->{print_center} = [ unscale($center->x), unscale($center->y) ]; #))
  326. # cache bed contours and grid
  327. {
  328. my $step = scale 10; # 1cm grid
  329. my @polylines = ();
  330. for (my $x = $bb->x_min - ($bb->x_min % $step) + $step; $x < $bb->x_max; $x += $step) {
  331. push @polylines, Slic3r::Polyline->new([$x, $bb->y_min], [$x, $bb->y_max]);
  332. }
  333. for (my $y = $bb->y_min - ($bb->y_min % $step) + $step; $y < $bb->y_max; $y += $step) {
  334. push @polylines, Slic3r::Polyline->new([$bb->x_min, $y], [$bb->x_max, $y]);
  335. }
  336. @polylines = @{intersection_pl(\@polylines, [$polygon])};
  337. $self->{grid} = [ map $self->scaled_points_to_pixel([ @$_[0,-1] ], 1), @polylines ];
  338. }
  339. }
  340. sub clean_instance_thumbnails {
  341. my ($self) = @_;
  342. foreach my $object (@{ $self->{objects} }) {
  343. @{ $object->instance_thumbnails } = ();
  344. }
  345. }
  346. # convert a model coordinate into a pixel coordinate
  347. sub unscaled_point_to_pixel {
  348. my ($self, $point) = @_;
  349. my $canvas_height = $self->GetSize->GetHeight;
  350. my $zero = $self->{bed_origin};
  351. return [
  352. $point->[X] * $self->{scaling_factor} + $zero->[X],
  353. $canvas_height - $point->[Y] * $self->{scaling_factor} + ($zero->[Y] - $canvas_height),
  354. ];
  355. }
  356. sub scaled_points_to_pixel {
  357. my ($self, $points, $unscale) = @_;
  358. my $result = [];
  359. foreach my $point (@$points) {
  360. $point = [ map unscale($_), @$point ] if $unscale;
  361. push @$result, $self->unscaled_point_to_pixel($point);
  362. }
  363. return $result;
  364. }
  365. sub point_to_model_units {
  366. my ($self, $point) = @_;
  367. my $zero = $self->{bed_origin};
  368. return Slic3r::Point->new(
  369. scale ($point->[X] - $zero->[X]) / $self->{scaling_factor},
  370. scale ($zero->[Y] - $point->[Y]) / $self->{scaling_factor},
  371. );
  372. }
  373. 1;