2018-04-09 14:24:34 +00:00
|
|
|
#include "2DBed.hpp"
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
#include <wx/dcbuffer.h>
|
|
|
|
#include "BoundingBox.hpp"
|
|
|
|
#include "Geometry.hpp"
|
2018-01-30 11:10:12 +00:00
|
|
|
#include "ClipperUtils.hpp"
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
namespace Slic3r {
|
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
void Bed_2D::repaint()
|
|
|
|
{
|
2018-01-30 11:10:12 +00:00
|
|
|
wxAutoBufferedPaintDC dc(this);
|
2018-01-25 12:46:04 +00:00
|
|
|
auto cw = GetSize().GetWidth();
|
|
|
|
auto ch = GetSize().GetHeight();
|
|
|
|
// when canvas is not rendered yet, size is 0, 0
|
|
|
|
if (cw == 0) return ;
|
|
|
|
|
|
|
|
if (m_user_drawn_background) {
|
|
|
|
// On all systems the AutoBufferedPaintDC() achieves double buffering.
|
|
|
|
// On MacOS the background is erased, on Windows the background is not erased
|
|
|
|
// and on Linux / GTK the background is erased to gray color.
|
|
|
|
// Fill DC with the background on Windows & Linux / GTK.
|
|
|
|
auto color = wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT); //GetSystemColour
|
2018-01-30 11:10:12 +00:00
|
|
|
dc.SetPen(*new wxPen(color, 1, wxPENSTYLE_SOLID));
|
2018-01-25 12:46:04 +00:00
|
|
|
dc.SetBrush(*new wxBrush(color, wxBRUSHSTYLE_SOLID));
|
|
|
|
auto rect = GetUpdateRegion().GetBox();
|
|
|
|
dc.DrawRectangle(rect.GetLeft(), rect.GetTop(), rect.GetWidth(), rect.GetHeight());
|
|
|
|
}
|
|
|
|
|
|
|
|
// turn cw and ch from sizes to max coordinates
|
2018-01-30 11:10:12 +00:00
|
|
|
cw--;
|
2018-01-25 12:46:04 +00:00
|
|
|
ch--;
|
|
|
|
|
|
|
|
auto cbb = BoundingBoxf(Pointf(0, 0),Pointf(cw, ch));
|
|
|
|
// leave space for origin point
|
2018-01-30 11:10:12 +00:00
|
|
|
cbb.min.translate(4, 0);
|
|
|
|
cbb.max.translate(-4, -4);
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
// leave space for origin label
|
2018-01-30 11:10:12 +00:00
|
|
|
cbb.max.translate(0, -13);
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
// read new size
|
2018-08-14 16:33:26 +00:00
|
|
|
cw = cbb.size().x();
|
|
|
|
ch = cbb.size().y();
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
auto ccenter = cbb.center();
|
|
|
|
|
|
|
|
// get bounding box of bed shape in G - code coordinates
|
|
|
|
auto bed_shape = m_bed_shape;
|
|
|
|
auto bed_polygon = Polygon::new_scale(m_bed_shape);
|
|
|
|
auto bb = BoundingBoxf(m_bed_shape);
|
|
|
|
bb.merge(Pointf(0, 0)); // origin needs to be in the visible area
|
2018-08-14 16:33:26 +00:00
|
|
|
auto bw = bb.size().x();
|
|
|
|
auto bh = bb.size().y();
|
2018-01-25 12:46:04 +00:00
|
|
|
auto bcenter = bb.center();
|
|
|
|
|
|
|
|
// calculate the scaling factor for fitting bed shape in canvas area
|
|
|
|
auto sfactor = std::min(cw/bw, ch/bh);
|
|
|
|
auto shift = Pointf(
|
2018-08-14 16:33:26 +00:00
|
|
|
ccenter.x() - bcenter.x() * sfactor,
|
|
|
|
ccenter.y() - bcenter.y() * sfactor
|
2018-01-25 12:46:04 +00:00
|
|
|
);
|
|
|
|
m_scale_factor = sfactor;
|
2018-08-14 16:33:26 +00:00
|
|
|
m_shift = Pointf(shift.x() + cbb.min.x(),
|
|
|
|
shift.y() - (cbb.max.y() - GetSize().GetHeight()));
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
// draw bed fill
|
2018-04-09 14:24:34 +00:00
|
|
|
dc.SetBrush(wxBrush(wxColour(255, 255, 255), wxSOLID));
|
2018-01-30 11:10:12 +00:00
|
|
|
wxPointList pt_list;
|
|
|
|
for (auto pt: m_bed_shape)
|
|
|
|
{
|
|
|
|
Point pt_pix = to_pixels(pt);
|
2018-08-14 16:33:26 +00:00
|
|
|
pt_list.push_back(new wxPoint(pt_pix.x(), pt_pix.y()));
|
2018-01-30 11:10:12 +00:00
|
|
|
}
|
|
|
|
dc.DrawPolygon(&pt_list, 0, 0);
|
|
|
|
|
2018-01-25 12:46:04 +00:00
|
|
|
// draw grid
|
2018-01-30 11:10:12 +00:00
|
|
|
auto step = 10; // 1cm grid
|
|
|
|
Polylines polylines;
|
2018-08-14 16:33:26 +00:00
|
|
|
for (auto x = bb.min.x() - fmod(bb.min.x(), step) + step; x < bb.max.x(); x += step) {
|
|
|
|
Polyline pl = Polyline::new_scale({ Pointf(x, bb.min.y()), Pointf(x, bb.max.y()) });
|
2018-01-30 11:10:12 +00:00
|
|
|
polylines.push_back(pl);
|
|
|
|
}
|
2018-08-14 16:33:26 +00:00
|
|
|
for (auto y = bb.min.y() - fmod(bb.min.y(), step) + step; y < bb.max.y(); y += step) {
|
|
|
|
polylines.push_back(Polyline::new_scale({ Pointf(bb.min.x(), y), Pointf(bb.max.x(), y) }));
|
2018-01-30 11:10:12 +00:00
|
|
|
}
|
|
|
|
polylines = intersection_pl(polylines, bed_polygon);
|
|
|
|
|
2018-04-09 14:24:34 +00:00
|
|
|
dc.SetPen(wxPen(wxColour(230, 230, 230), 1, wxSOLID));
|
2018-01-30 11:10:12 +00:00
|
|
|
for (auto pl : polylines)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < pl.points.size()-1; i++){
|
|
|
|
Point pt1 = to_pixels(Pointf::new_unscale(pl.points[i]));
|
|
|
|
Point pt2 = to_pixels(Pointf::new_unscale(pl.points[i+1]));
|
2018-08-14 16:33:26 +00:00
|
|
|
dc.DrawLine(pt1.x(), pt1.y(), pt2.x(), pt2.y());
|
2018-01-30 11:10:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw bed contour
|
2018-04-09 14:24:34 +00:00
|
|
|
dc.SetPen(wxPen(wxColour(0, 0, 0), 1, wxSOLID));
|
|
|
|
dc.SetBrush(wxBrush(wxColour(0, 0, 0), wxTRANSPARENT));
|
2018-01-30 11:10:12 +00:00
|
|
|
dc.DrawPolygon(&pt_list, 0, 0);
|
|
|
|
|
|
|
|
auto origin_px = to_pixels(Pointf(0, 0));
|
|
|
|
|
|
|
|
// draw axes
|
|
|
|
auto axes_len = 50;
|
|
|
|
auto arrow_len = 6;
|
|
|
|
auto arrow_angle = Geometry::deg2rad(45.0);
|
2018-04-09 14:24:34 +00:00
|
|
|
dc.SetPen(wxPen(wxColour(255, 0, 0), 2, wxSOLID)); // red
|
2018-08-14 16:33:26 +00:00
|
|
|
auto x_end = Pointf(origin_px.x() + axes_len, origin_px.y());
|
|
|
|
dc.DrawLine(wxPoint(origin_px.x(), origin_px.y()), wxPoint(x_end.x(), x_end.y()));
|
2018-01-30 11:10:12 +00:00
|
|
|
for (auto angle : { -arrow_angle, arrow_angle }){
|
|
|
|
auto end = x_end;
|
|
|
|
end.translate(-arrow_len, 0);
|
|
|
|
end.rotate(angle, x_end);
|
2018-08-14 16:33:26 +00:00
|
|
|
dc.DrawLine(wxPoint(x_end.x(), x_end.y()), wxPoint(end.x(), end.y()));
|
2018-01-30 11:10:12 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 14:24:34 +00:00
|
|
|
dc.SetPen(wxPen(wxColour(0, 255, 0), 2, wxSOLID)); // green
|
2018-08-14 16:33:26 +00:00
|
|
|
auto y_end = Pointf(origin_px.x(), origin_px.y() - axes_len);
|
|
|
|
dc.DrawLine(wxPoint(origin_px.x(), origin_px.y()), wxPoint(y_end.x(), y_end.y()));
|
2018-01-30 11:10:12 +00:00
|
|
|
for (auto angle : { -arrow_angle, arrow_angle }) {
|
|
|
|
auto end = y_end;
|
|
|
|
end.translate(0, +arrow_len);
|
|
|
|
end.rotate(angle, y_end);
|
2018-08-14 16:33:26 +00:00
|
|
|
dc.DrawLine(wxPoint(y_end.x(), y_end.y()), wxPoint(end.x(), end.y()));
|
2018-01-30 11:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw origin
|
2018-04-09 14:24:34 +00:00
|
|
|
dc.SetPen(wxPen(wxColour(0, 0, 0), 1, wxSOLID));
|
|
|
|
dc.SetBrush(wxBrush(wxColour(0, 0, 0), wxSOLID));
|
2018-08-14 16:33:26 +00:00
|
|
|
dc.DrawCircle(origin_px.x(), origin_px.y(), 3);
|
2018-01-30 11:10:12 +00:00
|
|
|
|
2018-04-09 14:24:34 +00:00
|
|
|
static const auto origin_label = wxString("(0,0)");
|
|
|
|
dc.SetTextForeground(wxColour(0, 0, 0));
|
|
|
|
dc.SetFont(wxFont(10, wxDEFAULT, wxNORMAL, wxNORMAL));
|
|
|
|
auto extent = dc.GetTextExtent(origin_label);
|
2018-08-14 16:33:26 +00:00
|
|
|
const auto origin_label_x = origin_px.x() <= cw / 2 ? origin_px.x() + 1 : origin_px.x() - 1 - extent.GetWidth();
|
|
|
|
const auto origin_label_y = origin_px.y() <= ch / 2 ? origin_px.y() + 1 : origin_px.y() - 1 - extent.GetHeight();
|
2018-04-09 14:24:34 +00:00
|
|
|
dc.DrawText(origin_label, origin_label_x, origin_label_y);
|
2018-01-30 11:10:12 +00:00
|
|
|
|
|
|
|
// draw current position
|
|
|
|
if (m_pos!= Pointf(0, 0)) {
|
|
|
|
auto pos_px = to_pixels(m_pos);
|
2018-04-09 14:24:34 +00:00
|
|
|
dc.SetPen(wxPen(wxColour(200, 0, 0), 2, wxSOLID));
|
|
|
|
dc.SetBrush(wxBrush(wxColour(200, 0, 0), wxTRANSPARENT));
|
2018-08-14 16:33:26 +00:00
|
|
|
dc.DrawCircle(pos_px.x(), pos_px.y(), 5);
|
2018-01-30 11:10:12 +00:00
|
|
|
|
2018-08-14 16:33:26 +00:00
|
|
|
dc.DrawLine(pos_px.x() - 15, pos_px.y(), pos_px.x() + 15, pos_px.y());
|
|
|
|
dc.DrawLine(pos_px.x(), pos_px.y() - 15, pos_px.x(), pos_px.y() + 15);
|
2018-01-30 11:10:12 +00:00
|
|
|
}
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
m_painted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert G - code coordinates into pixels
|
|
|
|
Point Bed_2D::to_pixels(Pointf point){
|
|
|
|
auto p = Pointf(point);
|
|
|
|
p.scale(m_scale_factor);
|
|
|
|
p.translate(m_shift);
|
2018-08-14 16:33:26 +00:00
|
|
|
return Point(p.x(), GetSize().GetHeight() - p.y());
|
2018-01-25 12:46:04 +00:00
|
|
|
}
|
|
|
|
|
2018-01-30 11:10:12 +00:00
|
|
|
void Bed_2D::mouse_event(wxMouseEvent event){
|
|
|
|
if (!m_interactive) return;
|
|
|
|
if (!m_painted) return;
|
|
|
|
|
|
|
|
auto pos = event.GetPosition();
|
2018-08-14 16:33:26 +00:00
|
|
|
auto point = to_units(Point(pos.x, pos.y));
|
2018-01-30 11:10:12 +00:00
|
|
|
if (event.LeftDown() || event.Dragging()) {
|
|
|
|
if (m_on_move)
|
|
|
|
m_on_move(point) ;
|
|
|
|
Refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert pixels into G - code coordinates
|
|
|
|
Pointf Bed_2D::to_units(Point point){
|
2018-08-14 16:33:26 +00:00
|
|
|
auto p = Pointf(point.x(), GetSize().GetHeight() - point.y());
|
2018-01-30 11:10:12 +00:00
|
|
|
p.translate(m_shift.negative());
|
|
|
|
p.scale(1 / m_scale_factor);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bed_2D::set_pos(Pointf pos){
|
|
|
|
m_pos = pos;
|
|
|
|
Refresh();
|
|
|
|
}
|
|
|
|
|
2018-01-25 12:46:04 +00:00
|
|
|
} // GUI
|
|
|
|
} // Slic3r
|