2DBed.pm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package Slic3r::GUI::2DBed;
  2. use strict;
  3. use warnings;
  4. use List::Util qw(min max);
  5. use Slic3r::Geometry qw(PI X Y unscale deg2rad);
  6. use Slic3r::Geometry::Clipper qw(intersection_pl);
  7. use Wx qw(:misc :pen :brush :font wxTAB_TRAVERSAL);
  8. use Wx::Event qw(EVT_PAINT EVT_MOUSE_EVENTS);
  9. use base qw(Wx::Panel Class::Accessor);
  10. __PACKAGE__->mk_accessors(qw(bed_shape interactive pos _scale_factor _shift on_move));
  11. sub new {
  12. my ($class, $parent, $bed_shape) = @_;
  13. my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, [250,-1], wxTAB_TRAVERSAL);
  14. $self->bed_shape($bed_shape // []);
  15. EVT_PAINT($self, \&_repaint);
  16. EVT_MOUSE_EVENTS($self, \&_mouse_event);
  17. return $self;
  18. }
  19. sub _repaint {
  20. my ($self) = @_;
  21. my $dc = Wx::PaintDC->new($self);
  22. my ($cw, $ch) = $self->GetSizeWH;
  23. return if $cw == 0; # when canvas is not rendered yet, size is 0,0
  24. # turn $cw and $ch from sizes to max coordinates
  25. $cw--;
  26. $ch--;
  27. my $cbb = Slic3r::Geometry::BoundingBoxf->new_from_points([
  28. Slic3r::Pointf->new(0, 0),
  29. Slic3r::Pointf->new($cw, $ch),
  30. ]);
  31. # leave space for origin point
  32. $cbb->set_x_min($cbb->x_min + 2);
  33. $cbb->set_y_max($cbb->y_max - 2);
  34. # leave space for origin label
  35. $cbb->set_y_max($cbb->y_max - 10);
  36. # read new size
  37. ($cw, $ch) = @{$cbb->size};
  38. my $ccenter = $cbb->center;
  39. # get bounding box of bed shape in G-code coordinates
  40. my $bed_shape = $self->bed_shape;
  41. my $bed_polygon = Slic3r::Polygon->new_scale(@$bed_shape);
  42. my $bb = Slic3r::Geometry::BoundingBoxf->new_from_points($bed_shape);
  43. $bb->merge_point(Slic3r::Pointf->new(0,0)); # origin needs to be in the visible area
  44. my ($bw, $bh) = @{$bb->size};
  45. my $bcenter = $bb->center;
  46. # calculate the scaling factor for fitting bed shape in canvas area
  47. my $sfactor = min($cw/$bw, $ch/$bh);
  48. my $shift = Slic3r::Pointf->new(
  49. $ccenter->x - $bcenter->x * $sfactor,
  50. $ccenter->y - $bcenter->y * $sfactor, #-
  51. );
  52. $self->_scale_factor($sfactor);
  53. $self->_shift(Slic3r::Pointf->new(
  54. $shift->x + $cbb->x_min,
  55. $shift->y - ($cbb->y_max-$self->GetSize->GetHeight), #++
  56. ));
  57. # draw bed fill
  58. {
  59. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,0,0), 1, wxSOLID));
  60. $dc->SetBrush(Wx::Brush->new(Wx::Colour->new(255,255,255), wxSOLID));
  61. $dc->DrawPolygon([ map $self->to_pixels($_), @$bed_shape ], 0, 0);
  62. }
  63. # draw grid
  64. {
  65. my $step = 10; # 1cm grid
  66. my @polylines = ();
  67. for (my $x = $bb->x_min - ($bb->x_min % $step) + $step; $x < $bb->x_max; $x += $step) {
  68. push @polylines, Slic3r::Polyline->new_scale([$x, $bb->y_min], [$x, $bb->y_max]);
  69. }
  70. for (my $y = $bb->y_min - ($bb->y_min % $step) + $step; $y < $bb->y_max; $y += $step) {
  71. push @polylines, Slic3r::Polyline->new_scale([$bb->x_min, $y], [$bb->x_max, $y]);
  72. }
  73. @polylines = @{intersection_pl(\@polylines, [$bed_polygon])};
  74. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(230,230,230), 1, wxSOLID));
  75. $dc->DrawLine(map @{$self->to_pixels([map unscale($_), @$_])}, @$_[0,-1]) for @polylines;
  76. }
  77. # draw bed contour
  78. {
  79. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,0,0), 1, wxSOLID));
  80. $dc->SetBrush(Wx::Brush->new(Wx::Colour->new(255,255,255), wxTRANSPARENT));
  81. $dc->DrawPolygon([ map $self->to_pixels($_), @$bed_shape ], 0, 0);
  82. }
  83. my $origin_px = $self->to_pixels(Slic3r::Pointf->new(0,0));
  84. # draw axes
  85. {
  86. my $axes_len = 50;
  87. my $arrow_len = 6;
  88. my $arrow_angle = deg2rad(45);
  89. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(255,0,0), 2, wxSOLID)); # red
  90. my $x_end = Slic3r::Pointf->new($origin_px->[X] + $axes_len, $origin_px->[Y]);
  91. $dc->DrawLine(@$origin_px, @$x_end);
  92. foreach my $angle (-$arrow_angle, +$arrow_angle) {
  93. my $end = $x_end->clone;
  94. $end->translate(-$arrow_len, 0);
  95. $end->rotate($angle, $x_end);
  96. $dc->DrawLine(@$x_end, @$end);
  97. }
  98. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,255,0), 2, wxSOLID)); # green
  99. my $y_end = Slic3r::Pointf->new($origin_px->[X], $origin_px->[Y] - $axes_len);
  100. $dc->DrawLine(@$origin_px, @$y_end);
  101. foreach my $angle (-$arrow_angle, +$arrow_angle) {
  102. my $end = $y_end->clone;
  103. $end->translate(0, +$arrow_len);
  104. $end->rotate($angle, $y_end);
  105. $dc->DrawLine(@$y_end, @$end);
  106. }
  107. }
  108. # draw origin
  109. {
  110. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(0,0,0), 1, wxSOLID));
  111. $dc->SetBrush(Wx::Brush->new(Wx::Colour->new(0,0,0), wxSOLID));
  112. $dc->DrawCircle(@$origin_px, 3);
  113. $dc->SetTextForeground(Wx::Colour->new(0,0,0));
  114. $dc->SetFont(Wx::Font->new(10, wxDEFAULT, wxNORMAL, wxNORMAL));
  115. $dc->DrawText("(0,0)", $origin_px->[X] + 1, $origin_px->[Y] + 2);
  116. }
  117. # draw current position
  118. if (defined $self->pos) {
  119. my $pos_px = $self->to_pixels($self->pos);
  120. $dc->SetPen(Wx::Pen->new(Wx::Colour->new(200,0,0), 2, wxSOLID));
  121. $dc->SetBrush(Wx::Brush->new(Wx::Colour->new(200,0,0), wxTRANSPARENT));
  122. $dc->DrawCircle(@$pos_px, 5);
  123. $dc->DrawLine($pos_px->[X]-15, $pos_px->[Y], $pos_px->[X]+15, $pos_px->[Y]);
  124. $dc->DrawLine($pos_px->[X], $pos_px->[Y]-15, $pos_px->[X], $pos_px->[Y]+15);
  125. }
  126. }
  127. sub _mouse_event {
  128. my ($self, $event) = @_;
  129. return if !$self->interactive;
  130. my $pos = $event->GetPosition;
  131. my $point = $self->to_units([ $pos->x, $pos->y ]); #]]
  132. if ($event->LeftDown || $event->Dragging) {
  133. $self->on_move->($point) if $self->on_move;
  134. $self->Refresh;
  135. }
  136. }
  137. # convert G-code coordinates into pixels
  138. sub to_pixels {
  139. my ($self, $point) = @_;
  140. my $p = Slic3r::Pointf->new(@$point);
  141. $p->scale($self->_scale_factor);
  142. $p->translate(@{$self->_shift});
  143. return [$p->x, $self->GetSize->GetHeight - $p->y]; #]]
  144. }
  145. # convert pixels into G-code coordinates
  146. sub to_units {
  147. my ($self, $point) = @_;
  148. my $p = Slic3r::Pointf->new(
  149. $point->[X],
  150. $self->GetSize->GetHeight - $point->[Y],
  151. );
  152. $p->translate(@{$self->_shift->negative});
  153. $p->scale(1/$self->_scale_factor);
  154. return $p;
  155. }
  156. sub set_pos {
  157. my ($self, $pos) = @_;
  158. $self->pos($pos);
  159. $self->Refresh;
  160. }
  161. 1;