2D.pm 15 KB

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