2D.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package Slic3r::GUI::Plater::2D;
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use List::Util qw(min max first);
  6. use Slic3r::Geometry qw(X Y scale unscale convex_hull);
  7. use Slic3r::Geometry::Clipper qw(offset JT_ROUND intersection_pl);
  8. use Wx qw(:misc :pen :brush :sizer :font :cursor wxTAB_TRAVERSAL);
  9. use Wx::Event qw(EVT_MOUSE_EVENTS EVT_PAINT EVT_SIZE);
  10. use base 'Wx::Panel';
  11. use constant CANVAS_TEXT => join('-', +(localtime)[3,4]) eq '13-8'
  12. ? 'What do you want to print today? ™' # Sept. 13, 2006. The first part ever printed by a RepRap to make another RepRap.
  13. : 'Drag your objects here';
  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. $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_instance_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. EVT_PAINT($self, \&repaint);
  35. EVT_MOUSE_EVENTS($self, \&mouse_event);
  36. EVT_SIZE($self, sub {
  37. $self->update_bed_size;
  38. $self->Refresh;
  39. });
  40. return $self;
  41. }
  42. sub on_select_object {
  43. my ($self, $cb) = @_;
  44. $self->{on_select_object} = $cb;
  45. }
  46. sub on_double_click {
  47. my ($self, $cb) = @_;
  48. $self->{on_double_click} = $cb;
  49. }
  50. sub on_right_click {
  51. my ($self, $cb) = @_;
  52. $self->{on_right_click} = $cb;
  53. }
  54. sub on_instance_moved {
  55. my ($self, $cb) = @_;
  56. $self->{on_instance_moved} = $cb;
  57. }
  58. sub repaint {
  59. my ($self, $event) = @_;
  60. my $dc = Wx::PaintDC->new($self);
  61. my $size = $self->GetSize;
  62. my @size = ($size->GetWidth, $size->GetHeight);
  63. # draw grid
  64. $dc->SetPen($self->{grid_pen});
  65. $dc->DrawLine(map @$_, @$_) for @{$self->{grid}};
  66. # draw bed
  67. {
  68. $dc->SetPen($self->{print_center_pen});
  69. $dc->SetBrush($self->{transparent_brush});
  70. $dc->DrawPolygon($self->scaled_points_to_pixel($self->{bed_polygon}, 1), 0, 0);
  71. }
  72. # draw print center
  73. if (@{$self->{objects}} && $Slic3r::GUI::Settings->{_}{autocenter}) {
  74. my $center = $self->unscaled_point_to_pixel($self->{print_center});
  75. $dc->SetPen($self->{print_center_pen});
  76. $dc->DrawLine($center->[X], 0, $center->[X], $size[Y]);
  77. $dc->DrawLine(0, $center->[Y], $size[X], $center->[Y]);
  78. $dc->SetTextForeground(Wx::Colour->new(0,0,0));
  79. $dc->SetFont(Wx::Font->new(10, wxDEFAULT, wxNORMAL, wxNORMAL));
  80. $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);
  81. $dc->DrawRotatedText("Y = " . sprintf('%.0f', $self->{print_center}->[Y]), 0, $center->[Y]+15, 90);
  82. }
  83. # draw frame
  84. if (0) {
  85. $dc->SetPen(wxBLACK_PEN);
  86. $dc->SetBrush($self->{transparent_brush});
  87. $dc->DrawRectangle(0, 0, @size);
  88. }
  89. # draw text if plate is empty
  90. if (!@{$self->{objects}}) {
  91. $dc->SetTextForeground(Wx::Colour->new(150,50,50));
  92. $dc->SetFont(Wx::Font->new(14, wxDEFAULT, wxNORMAL, wxNORMAL));
  93. $dc->DrawLabel(CANVAS_TEXT, Wx::Rect->new(0, 0, $self->GetSize->GetWidth, $self->GetSize->GetHeight), wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
  94. }
  95. # draw thumbnails
  96. $dc->SetPen(wxBLACK_PEN);
  97. $self->clean_instance_thumbnails;
  98. for my $obj_idx (0 .. $#{$self->{objects}}) {
  99. my $object = $self->{objects}[$obj_idx];
  100. my $model_object = $self->{model}->objects->[$obj_idx];
  101. next unless defined $object->thumbnail;
  102. for my $instance_idx (0 .. $#{$model_object->instances}) {
  103. my $instance = $model_object->instances->[$instance_idx];
  104. next if !defined $object->transformed_thumbnail;
  105. my $thumbnail = $object->transformed_thumbnail->clone; # in scaled model coordinates
  106. $thumbnail->translate(map scale($_), @{$instance->offset});
  107. $object->instance_thumbnails->[$instance_idx] = $thumbnail;
  108. if (defined $self->{drag_object} && $self->{drag_object}[0] == $obj_idx && $self->{drag_object}[1] == $instance_idx) {
  109. $dc->SetBrush($self->{dragged_brush});
  110. } elsif ($object->selected) {
  111. $dc->SetBrush($self->{selected_brush});
  112. } else {
  113. $dc->SetBrush($self->{objects_brush});
  114. }
  115. foreach my $expolygon (@$thumbnail) {
  116. foreach my $points (@{$expolygon->pp}) {
  117. $dc->DrawPolygon($self->scaled_points_to_pixel($points, 1), 0, 0);
  118. }
  119. }
  120. if (0) {
  121. # draw bounding box for debugging purposes
  122. my $bb = $model_object->instance_bounding_box($instance_idx);
  123. $bb->scale($self->{scaling_factor});
  124. # no need to translate by instance offset because instance_bounding_box() does that
  125. my $points = $bb->polygon->pp;
  126. $dc->SetPen($self->{clearance_pen});
  127. $dc->SetBrush($self->{transparent_brush});
  128. $dc->DrawPolygon($self->_y($points), 0, 0);
  129. }
  130. # if sequential printing is enabled and we have more than one object, draw clearance area
  131. if ($self->{config}->complete_objects && (map @{$_->instances}, @{$self->{model}->objects}) > 1) {
  132. my ($clearance) = @{offset([$thumbnail->convex_hull], (scale($self->{config}->extruder_clearance_radius) / 2), 1, JT_ROUND, scale(0.1))};
  133. $dc->SetPen($self->{clearance_pen});
  134. $dc->SetBrush($self->{transparent_brush});
  135. $dc->DrawPolygon($self->scaled_points_to_pixel($clearance, 1), 0, 0);
  136. }
  137. }
  138. }
  139. # draw skirt
  140. if (@{$self->{objects}} && $self->{config}->skirts) {
  141. my @points = map @{$_->contour}, map @$_, map @{$_->instance_thumbnails}, @{$self->{objects}};
  142. if (@points >= 3) {
  143. my ($convex_hull) = @{offset([convex_hull(\@points)], scale max($self->{config}->brim_width + $self->{config}->skirt_distance), 1, JT_ROUND, scale(0.1))};
  144. $dc->SetPen($self->{skirt_pen});
  145. $dc->SetBrush($self->{transparent_brush});
  146. $dc->DrawPolygon($self->scaled_points_to_pixel($convex_hull, 1), 0, 0);
  147. }
  148. }
  149. $event->Skip;
  150. }
  151. sub mouse_event {
  152. my ($self, $event) = @_;
  153. my $pos = $event->GetPosition;
  154. my $point = $self->point_to_model_units([ $pos->x, $pos->y ]); #]]
  155. if ($event->ButtonDown) {
  156. $self->{on_select_object}->(undef);
  157. # traverse objects and instances in reverse order, so that if they're overlapping
  158. # we get the one that gets drawn last, thus on top (as user expects that to move)
  159. OBJECTS: for my $obj_idx (reverse 0 .. $#{$self->{objects}}) {
  160. my $object = $self->{objects}->[$obj_idx];
  161. for my $instance_idx (reverse 0 .. $#{ $object->instance_thumbnails }) {
  162. my $thumbnail = $object->instance_thumbnails->[$instance_idx];
  163. if (defined first { $_->contour->contains_point($point) } @$thumbnail) {
  164. $self->{on_select_object}->($obj_idx);
  165. if ($event->LeftDown) {
  166. # start dragging
  167. my $instance = $self->{model}->objects->[$obj_idx]->instances->[$instance_idx];
  168. my $instance_origin = [ map scale($_), @{$instance->offset} ];
  169. $self->{drag_start_pos} = [ # displacement between the click and the instance origin in scaled model units
  170. $point->x - $instance_origin->[X],
  171. $point->y - $instance_origin->[Y], #-
  172. ];
  173. $self->{drag_object} = [ $obj_idx, $instance_idx ];
  174. } elsif ($event->RightDown) {
  175. $self->{on_right_click}->($pos);
  176. }
  177. last OBJECTS;
  178. }
  179. }
  180. }
  181. $self->Refresh;
  182. } elsif ($event->LeftUp) {
  183. $self->{on_instance_moved}->(@{ $self->{drag_object} })
  184. if $self->{drag_object};
  185. $self->{drag_start_pos} = undef;
  186. $self->{drag_object} = undef;
  187. $self->SetCursor(wxSTANDARD_CURSOR);
  188. } elsif ($event->LeftDClick) {
  189. $self->{on_double_click}->();
  190. } elsif ($event->Dragging) {
  191. return if !$self->{drag_start_pos}; # concurrency problems
  192. my ($obj_idx, $instance_idx) = @{ $self->{drag_object} };
  193. my $model_object = $self->{model}->objects->[$obj_idx];
  194. $model_object->instances->[$instance_idx]->set_offset(
  195. Slic3r::Pointf->new(
  196. unscale($point->[X] - $self->{drag_start_pos}[X]),
  197. unscale($point->[Y] - $self->{drag_start_pos}[Y]),
  198. ));
  199. $model_object->update_bounding_box;
  200. $self->Refresh;
  201. } elsif ($event->Moving) {
  202. my $cursor = wxSTANDARD_CURSOR;
  203. if (defined first { $_->contour->contains_point($point) } map @$_, map @{$_->instance_thumbnails}, @{ $self->{objects} }) {
  204. $cursor = Wx::Cursor->new(wxCURSOR_HAND);
  205. }
  206. $self->SetCursor($cursor);
  207. }
  208. }
  209. sub update_bed_size {
  210. my $self = shift;
  211. # when the canvas is not rendered yet, its GetSize() method returns 0,0
  212. my $canvas_size = $self->GetSize;
  213. my ($canvas_w, $canvas_h) = ($canvas_size->GetWidth, $canvas_size->GetHeight);
  214. return if $canvas_w == 0;
  215. # get bed shape polygon
  216. $self->{bed_polygon} = my $polygon = Slic3r::Polygon->new_scale(@{$self->{config}->bed_shape});
  217. my $bb = $polygon->bounding_box;
  218. my $size = $bb->size;
  219. # calculate the scaling factor needed for constraining print bed area inside preview
  220. # scaling_factor is expressed in pixel / mm
  221. $self->{scaling_factor} = min($canvas_w / unscale($size->x), $canvas_h / unscale($size->y)); #)
  222. # calculate the displacement needed to center bed
  223. $self->{bed_origin} = [
  224. $canvas_w/2 - (unscale($bb->x_max + $bb->x_min)/2 * $self->{scaling_factor}),
  225. $canvas_h - ($canvas_h/2 - (unscale($bb->y_max + $bb->y_min)/2 * $self->{scaling_factor})),
  226. ];
  227. # calculate print center
  228. my $center = $bb->center;
  229. $self->{print_center} = [ unscale($center->x), unscale($center->y) ]; #))
  230. # cache bed contours and grid
  231. {
  232. my $step = scale 10; # 1cm grid
  233. my @polylines = ();
  234. for (my $x = $bb->x_min - ($bb->x_min % $step) + $step; $x < $bb->x_max; $x += $step) {
  235. push @polylines, Slic3r::Polyline->new([$x, $bb->y_min], [$x, $bb->y_max]);
  236. }
  237. for (my $y = $bb->y_min - ($bb->y_min % $step) + $step; $y < $bb->y_max; $y += $step) {
  238. push @polylines, Slic3r::Polyline->new([$bb->x_min, $y], [$bb->x_max, $y]);
  239. }
  240. @polylines = @{intersection_pl(\@polylines, [$polygon])};
  241. $self->{grid} = [ map $self->scaled_points_to_pixel(\@$_, 1), @polylines ];
  242. }
  243. }
  244. sub clean_instance_thumbnails {
  245. my ($self) = @_;
  246. foreach my $object (@{ $self->{objects} }) {
  247. @{ $object->instance_thumbnails } = ();
  248. }
  249. }
  250. # convert a model coordinate into a pixel coordinate
  251. sub unscaled_point_to_pixel {
  252. my ($self, $point) = @_;
  253. my $canvas_height = $self->GetSize->GetHeight;
  254. my $zero = $self->{bed_origin};
  255. return [
  256. $point->[X] * $self->{scaling_factor} + $zero->[X],
  257. $canvas_height - $point->[Y] * $self->{scaling_factor} + ($zero->[Y] - $canvas_height),
  258. ];
  259. }
  260. sub scaled_points_to_pixel {
  261. my ($self, $points, $unscale) = @_;
  262. my $result = [];
  263. foreach my $point (@$points) {
  264. $point = [ map unscale($_), @$point ] if $unscale;
  265. push @$result, $self->unscaled_point_to_pixel($point);
  266. }
  267. return $result;
  268. }
  269. sub point_to_model_units {
  270. my ($self, $point) = @_;
  271. my $zero = $self->{bed_origin};
  272. return Slic3r::Point->new(
  273. scale ($point->[X] - $zero->[X]) / $self->{scaling_factor},
  274. scale ($zero->[Y] - $point->[Y]) / $self->{scaling_factor},
  275. );
  276. }
  277. 1;