2D.pm 17 KB

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