2D.pm 13 KB

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