Slic3r/src/GUI/Plater.cpp
2018-05-07 22:36:40 -05:00

394 lines
15 KiB
C++

#include <memory>
#include <wx/progdlg.h>
#include <wx/window.h>
#include "Plater.hpp"
#include "ProgressStatusBar.hpp"
#include "Log.hpp"
#include "MainFrame.hpp"
#include "BoundingBox.hpp"
namespace Slic3r { namespace GUI {
const auto PROGRESS_BAR_EVENT = wxNewEventType();
Plater::Plater(wxWindow* parent, const wxString& title, std::shared_ptr<Settings> _settings) :
wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, title), settings(_settings)
{
// Set callback for status event for worker threads
/*
this->print->set_status_cb([=](std::string percent percent, std::wstring message) {
wxPostEvent(this, new wxPlThreadEvent(-1, PROGRESS_BAR_EVENT,
});
*/
auto on_select_object { [=](size_t& obj_idx) {
this->select_object(obj_idx);
} };
/*
# Initialize handlers for canvases
my $on_select_object = sub {
my ($obj_idx) = @_;
$self->select_object($obj_idx);
};
my $on_double_click = sub {
$self->object_settings_dialog if $self->selected_object;
};
my $on_right_click = sub {
my ($canvas, $click_pos) = @_;
my ($obj_idx, $object) = $self->selected_object;
return if !defined $obj_idx;
my $menu = $self->object_menu;
$canvas->PopupMenu($menu, $click_pos);
$menu->Destroy;
};
my $on_instances_moved = sub {
$self->on_model_change;
};
# Initialize 3D plater
if ($Slic3r::GUI::have_OpenGL) {
$self->{canvas3D} = Slic3r::GUI::Plater::3D->new($self->{preview_notebook}, $self->{objects}, $self->{model}, $self->{config});
$self->{preview_notebook}->AddPage($self->{canvas3D}, '3D');
$self->{canvas3D}->set_on_select_object($on_select_object);
$self->{canvas3D}->set_on_double_click($on_double_click);
$self->{canvas3D}->set_on_right_click(sub { $on_right_click->($self->{canvas3D}, @_); });
$self->{canvas3D}->set_on_instances_moved($on_instances_moved);
$self->{canvas3D}->on_viewport_changed(sub {
$self->{preview3D}->canvas->set_viewport_from_scene($self->{canvas3D});
});
}
*/
canvas2D = new Plate2D(preview_notebook, wxDefaultSize, objects, model, config, settings);
preview_notebook->AddPage(canvas2D, _("2D"));
/*
# Initialize 2D preview canvas
$self->{canvas} = Slic3r::GUI::Plater::2D->new($self->{preview_notebook}, wxDefaultSize, $self->{objects}, $self->{model}, $self->{config});
$self->{preview_notebook}->AddPage($self->{canvas}, '2D');
$self->{canvas}->on_select_object($on_select_object);
$self->{canvas}->on_double_click($on_double_click);
$self->{canvas}->on_right_click(sub { $on_right_click->($self->{canvas}, @_); });
$self->{canvas}->on_instances_moved($on_instances_moved);
# Initialize 3D toolpaths preview
$self->{preview3D_page_idx} = -1;
if ($Slic3r::GUI::have_OpenGL) {
$self->{preview3D} = Slic3r::GUI::Plater::3DPreview->new($self->{preview_notebook}, $self->{print});
$self->{preview3D}->canvas->on_viewport_changed(sub {
$self->{canvas3D}->set_viewport_from_scene($self->{preview3D}->canvas);
});
$self->{preview_notebook}->AddPage($self->{preview3D}, 'Preview');
$self->{preview3D_page_idx} = $self->{preview_notebook}->GetPageCount-1;
}
# Initialize toolpaths preview
$self->{toolpaths2D_page_idx} = -1;
if ($Slic3r::GUI::have_OpenGL) {
$self->{toolpaths2D} = Slic3r::GUI::Plater::2DToolpaths->new($self->{preview_notebook}, $self->{print});
$self->{preview_notebook}->AddPage($self->{toolpaths2D}, 'Layers');
$self->{toolpaths2D_page_idx} = $self->{preview_notebook}->GetPageCount-1;
}
EVT_NOTEBOOK_PAGE_CHANGED($self, $self->{preview_notebook}, sub {
wxTheApp->CallAfter(sub {
my $sel = $self->{preview_notebook}->GetSelection;
if ($sel == $self->{preview3D_page_idx} || $sel == $self->{toolpaths2D_page_idx}) {
if (!$Slic3r::GUI::Settings->{_}{background_processing} && !$self->{processed}) {
$self->statusbar->SetCancelCallback(sub {
$self->stop_background_process;
$self->statusbar->SetStatusText("Slicing cancelled");
$self->{preview_notebook}->SetSelection(0);
});
$self->start_background_process;
} else {
$self->{preview3D}->load_print
if $sel == $self->{preview3D_page_idx};
}
}
});
});
*/
this->canvas2D->update_bed_size();
// Finally assemble the sizers into the display.
// export/print/send/export buttons
// right panel sizer
auto right_sizer = this->right_sizer;
// $right_sizer->Add($presets, 0, wxEXPAND | wxTOP, 10) if defined $presets;
// $right_sizer->Add($buttons_sizer, 0, wxEXPAND | wxBOTTOM, 5);
// $right_sizer->Add($self->{settings_override_panel}, 1, wxEXPAND, 5);
// $right_sizer->Add($object_info_sizer, 0, wxEXPAND, 0);
// $right_sizer->Add($print_info_sizer, 0, wxEXPAND, 0);
// $right_sizer->Hide($print_info_sizer);
auto hsizer {new wxBoxSizer(wxHORIZONTAL)};
hsizer->Add(this->preview_notebook, 1, wxEXPAND | wxTOP, 1);
hsizer->Add(right_sizer, 0, wxEXPAND | wxLEFT | wxRIGHT, 3);
auto sizer {new wxBoxSizer(wxVERTICAL)};
if (this->htoolbar != nullptr) sizer->Add(this->htoolbar, 0, wxEXPAND, 0);
if (this->btoolbar != nullptr) sizer->Add(this->btoolbar, 0, wxEXPAND, 0);
sizer->Add(hsizer, 1, wxEXPAND,0);
sizer->SetSizeHints(this);
this->SetSizer(sizer);
}
void Plater::add() {
Log::info(LogChannel, L"Called Add function");
auto& start_object_id = this->object_identifier;
const auto& input_files{open_model(this, *(this->settings), wxTheApp->GetTopWindow())};
for (const auto& f : input_files) {
Log::info(LogChannel, (wxString(L"Calling Load File for ") + f).ToStdWstring());
this->load_file(f.ToStdString());
}
// abort if no objects actually added.
if (start_object_id == this->object_identifier) return;
// save the added objects
auto new_model {this->model};
// get newly added objects count
auto new_objects_count = this->object_identifier - start_object_id;
Slic3r::Log::info(LogChannel, (wxString("Obj id:") << object_identifier).ToStdWstring());
for (auto i = start_object_id; i < new_objects_count + start_object_id; i++) {
const auto& obj_idx {this->get_object_index(i)};
new_model->add_object(*(this->model->objects.at(obj_idx)));
}
Slic3r::Log::info(LogChannel, (wxString("Obj id:") << object_identifier).ToStdWstring());
// Prepare for undo
//this->add_undo_operation("ADD", nullptr, new_model, start_object_id);
}
std::vector<int> Plater::load_file(const std::string file, const int obj_idx_to_load) {
auto input_file {wxFileName(file)};
settings->skein_directory = input_file.GetPath();
settings->save_settings();
Slic3r::Model model;
bool valid_load {true};
auto obj_idx {std::vector<int>()};
auto progress_dialog {new wxProgressDialog(_(L"Loading…"), _(L"Processing input file…"), 100, this, 0)};
progress_dialog->Pulse();
//TODO: Add a std::wstring so we can handle non-roman characters as file names.
try {
model = Slic3r::Model::read_from_file(file);
} catch (std::runtime_error& e) {
show_error(this, e.what());
Slic3r::Log::error(LogChannel, LOG_WSTRING(file << " failed to load: " << e.what()));
valid_load = false;
}
Slic3r::Log::info(LogChannel, LOG_WSTRING("load_valid is " << valid_load));
if (valid_load) {
if (model.looks_like_multipart_object()) {
auto dialog {new wxMessageDialog(this,
_("This file contains several objects positioned at multiple heights. Instead of considering them as multiple objects, should I consider\n them this file as a single object having multiple parts?\n"), _("Multi-part object detected"), wxICON_WARNING | wxYES | wxNO)};
if (dialog->ShowModal() == wxID_YES) {
model.convert_multipart_object();
}
}
for (auto i = 0U; i < model.objects.size(); i++) {
auto object {model.objects[i]};
object->input_file = file;
for (auto j = 0U; j < object->volumes.size(); j++) {
auto volume {object->volumes.at(j)};
volume->input_file = file;
volume->input_file_obj_idx = i;
volume->input_file_vol_idx = j;
}
}
auto i {0U};
if (obj_idx_to_load > 0) {
Slic3r::Log::info(LogChannel, L"Loading model objects, obj_idx_to_load > 0");
const size_t idx_load = obj_idx_to_load;
if (idx_load >= model.objects.size()) return std::vector<int>();
obj_idx = this->load_model_objects(model.objects.at(idx_load));
i = idx_load;
} else {
Slic3r::Log::info(LogChannel, L"Loading model objects, obj_idx_to_load = 0");
obj_idx = this->load_model_objects(model.objects);
Slic3r::Log::info(LogChannel, LOG_WSTRING("obj_idx size: " << obj_idx.size()));
}
for (const auto &j : obj_idx) {
this->objects.at(j).input_file = file;
this->objects.at(j).input_file_obj_idx = i++;
}
GetFrame()->statusbar->SetStatusText(_("Loaded ") + input_file.GetName());
if (this->scaled_down) {
GetFrame()->statusbar->SetStatusText(_("Your object appears to be too large, so it was automatically scaled down to fit your print bed."));
}
if (this->outside_bounds) {
GetFrame()->statusbar->SetStatusText(_("Some of your object(s) appear to be outside the print bed. Use the arrange button to correct this."));
}
}
progress_dialog->Destroy();
this->redo = std::stack<UndoOperation>();
return obj_idx;
}
std::vector<int> Plater::load_model_objects(ModelObject* model_object) {
ModelObjectPtrs tmp {model_object}; // wrap in a std::vector
return load_model_objects(tmp);
}
std::vector<int> Plater::load_model_objects(ModelObjectPtrs model_objects) {
auto bed_center {this->bed_centerf()};
auto bed_shape {Slic3r::Polygon::new_scale(this->config->get<ConfigOptionPoints>("bed_shape").values)};
auto bed_size {bed_shape.bounding_box().size()};
bool need_arrange {false};
auto obj_idx {std::vector<int>()};
Slic3r::Log::info(LogChannel, LOG_WSTRING("Objects: " << model_objects.size()));
for (auto& obj : model_objects) {
auto o {this->model->add_object(*obj)};
o->repair();
auto tmpobj {PlaterObject()};
const auto objfile {wxFileName::FileName( obj->input_file )};
tmpobj.name = wxString(std::string() == obj->name ? obj->name : objfile.GetName());
tmpobj.identifier = (this->object_identifier)++;
this->objects.push_back(tmpobj);
obj_idx.push_back(this->objects.size() - 1);
Slic3r::Log::info(LogChannel, LOG_WSTRING("Object array new size: " << this->objects.size()));
Slic3r::Log::info(LogChannel, LOG_WSTRING("Instances: " << obj->instances.size()));
if (obj->instances.size() == 0) {
if (settings->autocenter) {
need_arrange = true;
o->center_around_origin();
o->add_instance();
o->instances.back()->offset = this->bed_centerf();
} else {
need_arrange = false;
if (settings->autoalignz) {
o->align_to_ground();
}
o->add_instance();
}
} else {
if (settings->autoalignz) {
o->align_to_ground();
}
}
{
// If the object is too large (more than 5x the bed) scale it down.
auto size {o->bounding_box().size()};
double ratio {0.0f};
if (ratio > 5) {
for (auto& instance : o->instances) {
instance->scaling_factor = (1.0f/ratio);
this->scaled_down = true;
}
}
}
{
// Provide a warning if downscaling by 5x still puts it over the bed size.
}
}
for (const auto& i : obj_idx) { this->make_thumbnail(i); }
if (need_arrange) this->arrange();
return obj_idx;
}
MainFrame* Plater::GetFrame() { return dynamic_cast<MainFrame*>(wxGetTopLevelParent(this)); }
int Plater::get_object_index(size_t object_id) {
for (size_t i = 0U; i < this->objects.size(); i++) {
if (this->objects.at(i).identifier == object_id) return static_cast<int>(i);
}
return -1;
}
void Plater::make_thumbnail(size_t idx) {
auto& plater_object {this->objects.at(idx)};
if (threaded) {
// spin off a thread to create the thumbnail and post an event when it is done.
} else {
plater_object.make_thumbnail(this->model, idx);
this->on_thumbnail_made(idx);
}
/*
my $plater_object = $self->{objects}[$obj_idx];
$plater_object->thumbnail(Slic3r::ExPolygon::Collection->new);
my $cb = sub {
$plater_object->make_thumbnail($self->{model}, $obj_idx);
if ($Slic3r::have_threads) {
Wx::PostEvent($self, Wx::PlThreadEvent->new(-1, $THUMBNAIL_DONE_EVENT, shared_clone([ $obj_idx ])));
Slic3r::thread_cleanup();
threads->exit;
} else {
$self->on_thumbnail_made($obj_idx);
}
};
@_ = ();
$Slic3r::have_threads
? threads->create(sub { $cb->(); Slic3r::thread_cleanup(); })->detach
: $cb->();
}
*/
}
void Plater::on_thumbnail_made(size_t idx) {
this->objects.at(idx).transform_thumbnail(this->model, idx);
this->refresh_canvases();
}
void Plater::refresh_canvases() {
if (this->canvas2D != nullptr)
this->canvas2D->Refresh();
if (this->canvas3D != nullptr)
this->canvas3D->update();
if (this->preview3D != nullptr)
this->preview3D->reload_print();
}
void Plater::arrange() {
// pause background process
Slic3r::Log::info(LogChannel, L"Called arrange()");
auto bb {Slic3r::BoundingBoxf(this->config->get<ConfigOptionPoints>("bed_shape").values)};
bool success {this->model->arrange_objects(5, &bb)};
GetFrame()->statusbar->SetStatusText(_("Objects were arranged."));
this->on_model_change(true);
}
void Plater::on_model_change(bool force_autocenter) {
if (force_autocenter || settings->autocenter) {
this->model->center_instances_around_point(this->bed_centerf());
}
this->refresh_canvases();
}
}} // Namespace Slic3r::GUI