2018-01-25 12:46:04 +00:00
|
|
|
#include "BedShapeDialog.hpp"
|
|
|
|
|
|
|
|
#include <wx/wx.h>
|
|
|
|
#include <wx/numformatter.h>
|
2018-11-26 13:41:58 +00:00
|
|
|
#include <wx/sizer.h>
|
|
|
|
#include <wx/statbox.h>
|
|
|
|
|
|
|
|
#include "libslic3r/BoundingBox.hpp"
|
|
|
|
#include "libslic3r/Model.hpp"
|
|
|
|
#include "libslic3r/Polygon.hpp"
|
|
|
|
|
2018-01-30 11:10:12 +00:00
|
|
|
#include "boost/nowide/iostream.hpp"
|
2018-01-25 12:46:04 +00:00
|
|
|
|
2018-07-30 08:03:17 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-01-25 12:46:04 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
void BedShapeDialog::build_dialog(ConfigOptionPoints* default_pt)
|
|
|
|
{
|
|
|
|
m_panel = new BedShapePanel(this);
|
|
|
|
m_panel->build_panel(default_pt);
|
|
|
|
|
|
|
|
auto main_sizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
main_sizer->Add(m_panel, 1, wxEXPAND);
|
2018-01-30 11:10:12 +00:00
|
|
|
main_sizer->Add(CreateButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_CENTER_HORIZONTAL | wxBOTTOM, 10);
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
SetSizer(main_sizer);
|
|
|
|
SetMinSize(GetSize());
|
|
|
|
main_sizer->SetSizeHints(this);
|
|
|
|
|
|
|
|
// needed to actually free memory
|
2018-10-31 11:56:08 +00:00
|
|
|
this->Bind(wxEVT_CLOSE_WINDOW, ([this](wxCloseEvent e) {
|
2018-01-25 12:46:04 +00:00
|
|
|
EndModal(wxID_OK);
|
|
|
|
Destroy();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BedShapePanel::build_panel(ConfigOptionPoints* default_pt)
|
|
|
|
{
|
2018-02-28 15:04:56 +00:00
|
|
|
// on_change(nullptr);
|
2018-01-25 12:46:04 +00:00
|
|
|
|
2018-02-23 08:16:35 +00:00
|
|
|
auto box = new wxStaticBox(this, wxID_ANY, _(L("Shape")));
|
2018-01-25 12:46:04 +00:00
|
|
|
auto sbsizer = new wxStaticBoxSizer(box, wxVERTICAL);
|
|
|
|
|
|
|
|
// shape options
|
|
|
|
m_shape_options_book = new wxChoicebook(this, wxID_ANY, wxDefaultPosition, wxSize(300, -1), wxCHB_TOP);
|
|
|
|
sbsizer->Add(m_shape_options_book);
|
|
|
|
|
2018-02-23 08:16:35 +00:00
|
|
|
auto optgroup = init_shape_options_page(_(L("Rectangular")));
|
2018-01-25 12:46:04 +00:00
|
|
|
ConfigOptionDef def;
|
|
|
|
def.type = coPoints;
|
2018-08-21 19:05:24 +00:00
|
|
|
def.default_value = new ConfigOptionPoints{ Vec2d(200, 200) };
|
2018-02-23 08:16:35 +00:00
|
|
|
def.label = L("Size");
|
|
|
|
def.tooltip = L("Size in X and Y of the rectangular plate.");
|
2018-01-25 12:46:04 +00:00
|
|
|
Option option(def, "rect_size");
|
|
|
|
optgroup->append_single_option_line(option);
|
|
|
|
|
|
|
|
def.type = coPoints;
|
2018-08-21 19:05:24 +00:00
|
|
|
def.default_value = new ConfigOptionPoints{ Vec2d(0, 0) };
|
2018-02-23 08:16:35 +00:00
|
|
|
def.label = L("Origin");
|
|
|
|
def.tooltip = L("Distance of the 0,0 G-code coordinate from the front left corner of the rectangle.");
|
2018-01-25 12:46:04 +00:00
|
|
|
option = Option(def, "rect_origin");
|
|
|
|
optgroup->append_single_option_line(option);
|
|
|
|
|
2018-02-23 08:16:35 +00:00
|
|
|
optgroup = init_shape_options_page(_(L("Circular")));
|
2018-01-25 12:46:04 +00:00
|
|
|
def.type = coFloat;
|
|
|
|
def.default_value = new ConfigOptionFloat(200);
|
2018-02-23 08:16:35 +00:00
|
|
|
def.sidetext = L("mm");
|
|
|
|
def.label = L("Diameter");
|
|
|
|
def.tooltip = L("Diameter of the print bed. It is assumed that origin (0,0) is located in the center.");
|
2018-01-25 12:46:04 +00:00
|
|
|
option = Option(def, "diameter");
|
|
|
|
optgroup->append_single_option_line(option);
|
|
|
|
|
2018-02-23 08:16:35 +00:00
|
|
|
optgroup = init_shape_options_page(_(L("Custom")));
|
2018-01-25 12:46:04 +00:00
|
|
|
Line line{ "", "" };
|
|
|
|
line.full_width = 1;
|
|
|
|
line.widget = [this](wxWindow* parent) {
|
2018-02-23 08:16:35 +00:00
|
|
|
auto btn = new wxButton(parent, wxID_ANY, _(L("Load shape from STL...")), wxDefaultPosition, wxDefaultSize);
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
auto sizer = new wxBoxSizer(wxHORIZONTAL);
|
|
|
|
sizer->Add(btn);
|
|
|
|
|
|
|
|
btn->Bind(wxEVT_BUTTON, ([this](wxCommandEvent e)
|
|
|
|
{
|
|
|
|
load_stl();
|
|
|
|
}));
|
|
|
|
|
|
|
|
return sizer;
|
|
|
|
};
|
|
|
|
optgroup->append_line(line);
|
|
|
|
|
|
|
|
Bind(wxEVT_CHOICEBOOK_PAGE_CHANGED, ([this](wxCommandEvent e)
|
|
|
|
{
|
|
|
|
update_shape();
|
|
|
|
}));
|
|
|
|
|
|
|
|
// right pane with preview canvas
|
|
|
|
m_canvas = new Bed_2D(this);
|
|
|
|
m_canvas->m_bed_shape = default_pt->values;
|
|
|
|
|
|
|
|
// main sizer
|
|
|
|
auto top_sizer = new wxBoxSizer(wxHORIZONTAL);
|
|
|
|
top_sizer->Add(sbsizer, 0, wxEXPAND | wxLeft | wxTOP | wxBOTTOM, 10);
|
|
|
|
if (m_canvas)
|
|
|
|
top_sizer->Add(m_canvas, 1, wxEXPAND | wxALL, 10) ;
|
|
|
|
|
|
|
|
SetSizerAndFit(top_sizer);
|
|
|
|
|
|
|
|
set_shape(default_pt);
|
|
|
|
update_preview();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SHAPE_RECTANGULAR 0
|
|
|
|
#define SHAPE_CIRCULAR 1
|
|
|
|
#define SHAPE_CUSTOM 2
|
|
|
|
|
|
|
|
// Called from the constructor.
|
|
|
|
// Create a panel for a rectangular / circular / custom bed shape.
|
2018-10-31 11:56:08 +00:00
|
|
|
ConfigOptionsGroupShp BedShapePanel::init_shape_options_page(wxString title)
|
|
|
|
{
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
auto panel = new wxPanel(m_shape_options_book);
|
|
|
|
ConfigOptionsGroupShp optgroup;
|
2018-02-23 08:16:35 +00:00
|
|
|
optgroup = std::make_shared<ConfigOptionsGroup>(panel, _(L("Settings")));
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
optgroup->label_width = 100;
|
2018-10-31 11:56:08 +00:00
|
|
|
optgroup->m_on_change = [this](t_config_option_key opt_key, boost::any value) {
|
2018-01-25 12:46:04 +00:00
|
|
|
update_shape();
|
|
|
|
};
|
|
|
|
|
|
|
|
m_optgroups.push_back(optgroup);
|
|
|
|
panel->SetSizerAndFit(optgroup->sizer);
|
|
|
|
m_shape_options_book->AddPage(panel, title);
|
|
|
|
|
|
|
|
return optgroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from the constructor.
|
|
|
|
// Set the initial bed shape from a list of points.
|
|
|
|
// Deduce the bed shape type(rect, circle, custom)
|
|
|
|
// This routine shall be smart enough if the user messes up
|
|
|
|
// with the list of points in the ini file directly.
|
|
|
|
void BedShapePanel::set_shape(ConfigOptionPoints* points)
|
|
|
|
{
|
|
|
|
auto polygon = Polygon::new_scale(points->values);
|
|
|
|
|
|
|
|
// is this a rectangle ?
|
|
|
|
if (points->size() == 4) {
|
|
|
|
auto lines = polygon.lines();
|
|
|
|
if (lines[0].parallel_to(lines[2]) && lines[1].parallel_to(lines[3])) {
|
|
|
|
// okay, it's a rectangle
|
|
|
|
// find origin
|
2018-07-30 08:03:17 +00:00
|
|
|
coordf_t x_min, x_max, y_min, y_max;
|
2018-08-17 16:07:45 +00:00
|
|
|
x_max = x_min = points->values[0](0);
|
2018-08-17 13:53:43 +00:00
|
|
|
y_max = y_min = points->values[0](1);
|
2018-07-30 08:03:17 +00:00
|
|
|
for (auto pt : points->values)
|
|
|
|
{
|
2018-08-17 16:07:45 +00:00
|
|
|
x_min = std::min(x_min, pt(0));
|
|
|
|
x_max = std::max(x_max, pt(0));
|
|
|
|
y_min = std::min(y_min, pt(1));
|
|
|
|
y_max = std::max(y_max, pt(1));
|
2018-07-30 08:03:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 19:05:24 +00:00
|
|
|
auto origin = new ConfigOptionPoints{ Vec2d(-x_min, -y_min) };
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
m_shape_options_book->SetSelection(SHAPE_RECTANGULAR);
|
|
|
|
auto optgroup = m_optgroups[SHAPE_RECTANGULAR];
|
2018-08-21 19:05:24 +00:00
|
|
|
optgroup->set_value("rect_size", new ConfigOptionPoints{ Vec2d(x_max - x_min, y_max - y_min) });//[x_max - x_min, y_max - y_min]);
|
2018-01-25 12:46:04 +00:00
|
|
|
optgroup->set_value("rect_origin", origin);
|
|
|
|
update_shape();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// is this a circle ?
|
|
|
|
{
|
|
|
|
// Analyze the array of points.Do they reside on a circle ?
|
2018-01-30 11:10:12 +00:00
|
|
|
auto center = polygon.bounding_box().center();
|
|
|
|
std::vector<double> vertex_distances;
|
2018-01-25 12:46:04 +00:00
|
|
|
double avg_dist = 0;
|
|
|
|
for (auto pt: polygon.points)
|
|
|
|
{
|
Removed Point::scale(),translate(),coincides_with(),distance_to(),
distance_to_squared(),perp_distance_to(),negative(),vector_to(),
translate(), distance_to() etc,
replaced with the Eigen equivalents.
2018-08-17 12:14:24 +00:00
|
|
|
double distance = (pt - center).cast<double>().norm();
|
2018-01-25 12:46:04 +00:00
|
|
|
vertex_distances.push_back(distance);
|
|
|
|
avg_dist += distance;
|
|
|
|
}
|
2018-03-23 16:27:43 +00:00
|
|
|
|
|
|
|
avg_dist /= vertex_distances.size();
|
2018-01-25 12:46:04 +00:00
|
|
|
bool defined_value = true;
|
|
|
|
for (auto el: vertex_distances)
|
|
|
|
{
|
2018-01-30 11:10:12 +00:00
|
|
|
if (abs(el - avg_dist) > 10 * SCALED_EPSILON)
|
2018-01-25 12:46:04 +00:00
|
|
|
defined_value = false;
|
|
|
|
break;
|
|
|
|
}
|
2018-01-30 11:10:12 +00:00
|
|
|
if (defined_value) {
|
2018-01-25 12:46:04 +00:00
|
|
|
// all vertices are equidistant to center
|
|
|
|
m_shape_options_book->SetSelection(SHAPE_CIRCULAR);
|
|
|
|
auto optgroup = m_optgroups[SHAPE_CIRCULAR];
|
2018-08-21 15:43:05 +00:00
|
|
|
boost::any ret = wxNumberFormatter::ToString(unscale<double>(avg_dist * 2), 0);
|
2018-01-30 11:10:12 +00:00
|
|
|
optgroup->set_value("diameter", ret);
|
2018-01-25 12:46:04 +00:00
|
|
|
update_shape();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (points->size() < 3) {
|
|
|
|
// Invalid polygon.Revert to default bed dimensions.
|
|
|
|
m_shape_options_book->SetSelection(SHAPE_RECTANGULAR);
|
|
|
|
auto optgroup = m_optgroups[SHAPE_RECTANGULAR];
|
2018-08-21 19:05:24 +00:00
|
|
|
optgroup->set_value("rect_size", new ConfigOptionPoints{ Vec2d(200, 200) });
|
|
|
|
optgroup->set_value("rect_origin", new ConfigOptionPoints{ Vec2d(0, 0) });
|
2018-01-25 12:46:04 +00:00
|
|
|
update_shape();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a custom bed shape, use the polygon provided.
|
|
|
|
m_shape_options_book->SetSelection(SHAPE_CUSTOM);
|
|
|
|
// Copy the polygon to the canvas, make a copy of the array.
|
|
|
|
m_canvas->m_bed_shape = points->values;
|
|
|
|
update_shape();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BedShapePanel::update_preview()
|
|
|
|
{
|
2018-01-30 11:10:12 +00:00
|
|
|
if (m_canvas) m_canvas->Refresh();
|
2018-01-25 12:46:04 +00:00
|
|
|
Refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the bed shape from the dialog fields.
|
|
|
|
void BedShapePanel::update_shape()
|
|
|
|
{
|
|
|
|
auto page_idx = m_shape_options_book->GetSelection();
|
|
|
|
if (page_idx == SHAPE_RECTANGULAR) {
|
2018-08-21 19:05:24 +00:00
|
|
|
Vec2d rect_size(Vec2d::Zero());
|
|
|
|
Vec2d rect_origin(Vec2d::Zero());
|
2018-01-30 11:10:12 +00:00
|
|
|
try{
|
2018-08-21 19:05:24 +00:00
|
|
|
rect_size = boost::any_cast<Vec2d>(m_optgroups[SHAPE_RECTANGULAR]->get_value("rect_size")); }
|
2018-10-31 15:25:55 +00:00
|
|
|
catch (const std::exception & /* e */) {
|
2018-08-21 18:34:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-31 15:25:55 +00:00
|
|
|
try {
|
2018-08-21 19:05:24 +00:00
|
|
|
rect_origin = boost::any_cast<Vec2d>(m_optgroups[SHAPE_RECTANGULAR]->get_value("rect_origin"));
|
2018-01-30 11:10:12 +00:00
|
|
|
}
|
2018-10-31 15:25:55 +00:00
|
|
|
catch (const std::exception & /* e */) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-30 11:10:12 +00:00
|
|
|
|
2018-08-17 13:53:43 +00:00
|
|
|
auto x = rect_size(0);
|
|
|
|
auto y = rect_size(1);
|
2018-01-25 12:46:04 +00:00
|
|
|
// empty strings or '-' or other things
|
2018-01-30 11:10:12 +00:00
|
|
|
if (x == 0 || y == 0) return;
|
|
|
|
double x0 = 0.0;
|
|
|
|
double y0 = 0.0;
|
|
|
|
double x1 = x;
|
|
|
|
double y1 = y;
|
|
|
|
|
2018-08-17 13:53:43 +00:00
|
|
|
auto dx = rect_origin(0);
|
|
|
|
auto dy = rect_origin(1);
|
2018-01-30 11:10:12 +00:00
|
|
|
|
|
|
|
x0 -= dx;
|
|
|
|
x1 -= dx;
|
|
|
|
y0 -= dy;
|
|
|
|
y1 -= dy;
|
2018-08-21 19:05:24 +00:00
|
|
|
m_canvas->m_bed_shape = { Vec2d(x0, y0),
|
|
|
|
Vec2d(x1, y0),
|
|
|
|
Vec2d(x1, y1),
|
|
|
|
Vec2d(x0, y1)};
|
2018-01-25 12:46:04 +00:00
|
|
|
}
|
|
|
|
else if(page_idx == SHAPE_CIRCULAR) {
|
2018-01-30 11:10:12 +00:00
|
|
|
double diameter;
|
|
|
|
try{
|
|
|
|
diameter = boost::any_cast<double>(m_optgroups[SHAPE_CIRCULAR]->get_value("diameter"));
|
|
|
|
}
|
2018-10-31 15:25:55 +00:00
|
|
|
catch (const std::exception & /* e */) {
|
2018-01-30 11:10:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (diameter == 0.0) return ;
|
|
|
|
auto r = diameter / 2;
|
|
|
|
auto twopi = 2 * PI;
|
|
|
|
auto edges = 60;
|
2018-08-21 19:05:24 +00:00
|
|
|
std::vector<Vec2d> points;
|
2018-10-31 11:56:08 +00:00
|
|
|
for (size_t i = 1; i <= 60; ++i) {
|
2018-01-30 11:10:12 +00:00
|
|
|
auto angle = i * twopi / edges;
|
2018-08-21 19:05:24 +00:00
|
|
|
points.push_back(Vec2d(r*cos(angle), r*sin(angle)));
|
2018-01-30 11:10:12 +00:00
|
|
|
}
|
|
|
|
m_canvas->m_bed_shape = points;
|
|
|
|
}
|
2018-01-25 12:46:04 +00:00
|
|
|
|
|
|
|
// $self->{on_change}->();
|
|
|
|
update_preview();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loads an stl file, projects it to the XY plane and calculates a polygon.
|
|
|
|
void BedShapePanel::load_stl()
|
|
|
|
{
|
2018-02-23 14:32:13 +00:00
|
|
|
auto dialog = new wxFileDialog(this, _(L("Choose a file to import bed shape from (STL/OBJ/AMF/3MF/PRUSA):")), "", "",
|
2018-12-06 16:32:49 +00:00
|
|
|
file_wildcards(FT_MODEL), wxFD_OPEN | wxFD_FILE_MUST_EXIST);
|
2018-01-25 12:46:04 +00:00
|
|
|
if (dialog->ShowModal() != wxID_OK) {
|
|
|
|
dialog->Destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wxArrayString input_file;
|
|
|
|
dialog->GetPaths(input_file);
|
|
|
|
dialog->Destroy();
|
|
|
|
|
2019-01-03 13:34:53 +00:00
|
|
|
std::string file_name = input_file[0].ToUTF8().data();
|
2018-01-30 11:10:12 +00:00
|
|
|
|
|
|
|
Model model;
|
|
|
|
try {
|
|
|
|
model = Model::read_from_file(file_name);
|
|
|
|
}
|
|
|
|
catch (std::exception &e) {
|
2018-02-23 08:16:35 +00:00
|
|
|
auto msg = _(L("Error! ")) + file_name + " : " + e.what() + ".";
|
2018-01-30 11:10:12 +00:00
|
|
|
show_error(this, msg);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto mesh = model.mesh();
|
|
|
|
auto expolygons = mesh.horizontal_projection();
|
|
|
|
|
|
|
|
if (expolygons.size() == 0) {
|
2018-02-23 08:16:35 +00:00
|
|
|
show_error(this, _(L("The selected file contains no geometry.")));
|
2018-01-30 11:10:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (expolygons.size() > 1) {
|
2018-02-23 08:16:35 +00:00
|
|
|
show_error(this, _(L("The selected file contains several disjoint areas. This is not supported.")));
|
2018-01-30 11:10:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto polygon = expolygons[0].contour;
|
2018-08-21 19:05:24 +00:00
|
|
|
std::vector<Vec2d> points;
|
2018-01-30 11:10:12 +00:00
|
|
|
for (auto pt : polygon.points)
|
2018-08-21 15:43:05 +00:00
|
|
|
points.push_back(unscale(pt));
|
2018-01-30 11:10:12 +00:00
|
|
|
m_canvas->m_bed_shape = points;
|
2018-01-25 12:46:04 +00:00
|
|
|
update_preview();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // GUI
|
|
|
|
} // Slic3r
|