2D.pm 15 KB

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