PrusaSlicer-NonPlainar/xs/src/slic3r/AppController.cpp

343 lines
8.7 KiB
C++
Raw Normal View History

2018-06-28 16:47:18 +00:00
#include "AppController.hpp"
#include <future>
#include <chrono>
2018-06-28 16:47:18 +00:00
#include <sstream>
#include <cstdarg>
#include <thread>
#include <unordered_map>
#include <slic3r/GUI/GUI.hpp>
#include <slic3r/GUI/PresetBundle.hpp>
#include <Geometry.hpp>
2018-06-28 16:47:18 +00:00
#include <PrintConfig.hpp>
#include <Print.hpp>
#include <Model.hpp>
#include <Utils.hpp>
namespace Slic3r {
class AppControllerBoilerplate::PriData {
2018-06-28 16:47:18 +00:00
public:
std::mutex m;
std::thread::id ui_thread;
inline explicit PriData(std::thread::id uit): ui_thread(uit) {}
2018-06-28 16:47:18 +00:00
};
AppControllerBoilerplate::AppControllerBoilerplate()
:pri_data_(new PriData(std::this_thread::get_id())) {}
2018-06-28 16:47:18 +00:00
AppControllerBoilerplate::~AppControllerBoilerplate() {
pri_data_.reset();
2018-06-28 16:47:18 +00:00
}
bool AppControllerBoilerplate::is_main_thread() const
{
return pri_data_->ui_thread == std::this_thread::get_id();
2018-06-28 16:47:18 +00:00
}
namespace GUI {
PresetBundle* get_preset_bundle();
}
static const PrintObjectStep STEP_SLICE = posSlice;
static const PrintObjectStep STEP_PERIMETERS = posPerimeters;
static const PrintObjectStep STEP_PREPARE_INFILL = posPrepareInfill;
static const PrintObjectStep STEP_INFILL = posInfill;
static const PrintObjectStep STEP_SUPPORTMATERIAL = posSupportMaterial;
static const PrintStep STEP_SKIRT = psSkirt;
static const PrintStep STEP_BRIM = psBrim;
static const PrintStep STEP_WIPE_TOWER = psWipeTower;
AppControllerBoilerplate::ProgresIndicatorPtr
AppControllerBoilerplate::global_progress_indicator() {
2018-06-28 16:47:18 +00:00
ProgresIndicatorPtr ret;
pri_data_->m.lock();
ret = global_progressind_;
pri_data_->m.unlock();
2018-06-28 16:47:18 +00:00
return ret;
}
void AppControllerBoilerplate::global_progress_indicator(
AppControllerBoilerplate::ProgresIndicatorPtr gpri)
{
pri_data_->m.lock();
global_progressind_ = gpri;
pri_data_->m.unlock();
}
2018-06-28 16:47:18 +00:00
void PrintController::make_skirt()
{
assert(print_ != nullptr);
// prerequisites
for(auto obj : print_->objects) make_perimeters(obj);
for(auto obj : print_->objects) infill(obj);
for(auto obj : print_->objects) gen_support_material(obj);
if(!print_->state.is_done(STEP_SKIRT)) {
print_->state.set_started(STEP_SKIRT);
print_->skirt.clear();
if(print_->has_skirt()) print_->_make_skirt();
print_->state.set_done(STEP_SKIRT);
}
}
void PrintController::make_brim()
{
assert(print_ != nullptr);
// prerequisites
for(auto obj : print_->objects) make_perimeters(obj);
for(auto obj : print_->objects) infill(obj);
for(auto obj : print_->objects) gen_support_material(obj);
make_skirt();
if(!print_->state.is_done(STEP_BRIM)) {
print_->state.set_started(STEP_BRIM);
// since this method must be idempotent, we clear brim paths *before*
// checking whether we need to generate them
print_->brim.clear();
if(print_->config.brim_width > 0) print_->_make_brim();
print_->state.set_done(STEP_BRIM);
}
}
void PrintController::make_wipe_tower()
{
assert(print_ != nullptr);
// prerequisites
for(auto obj : print_->objects) make_perimeters(obj);
for(auto obj : print_->objects) infill(obj);
for(auto obj : print_->objects) gen_support_material(obj);
make_skirt();
make_brim();
if(!print_->state.is_done(STEP_WIPE_TOWER)) {
print_->state.set_started(STEP_WIPE_TOWER);
// since this method must be idempotent, we clear brim paths *before*
// checking whether we need to generate them
print_->brim.clear();
if(print_->has_wipe_tower()) print_->_make_wipe_tower();
print_->state.set_done(STEP_WIPE_TOWER);
}
}
void PrintController::slice(PrintObject *pobj)
{
assert(pobj != nullptr && print_ != nullptr);
if(pobj->state.is_done(STEP_SLICE)) return;
pobj->state.set_started(STEP_SLICE);
pobj->_slice();
auto msg = pobj->_fix_slicing_errors();
if(!msg.empty()) report_issue(IssueType::WARN, msg);
// simplify slices if required
if (print_->config.resolution)
pobj->_simplify_slices(scale_(print_->config.resolution));
if(pobj->layers.empty())
report_issue(IssueType::ERR,
2018-07-03 08:22:55 +00:00
_(L("No layers were detected. You might want to repair your "
"STL file(s) or check their size or thickness and retry"))
2018-06-28 16:47:18 +00:00
);
pobj->state.set_done(STEP_SLICE);
}
void PrintController::make_perimeters(PrintObject *pobj)
{
assert(pobj != nullptr);
slice(pobj);
if (!pobj->state.is_done(STEP_PERIMETERS)) {
pobj->_make_perimeters();
}
}
void PrintController::infill(PrintObject *pobj)
{
assert(pobj != nullptr);
make_perimeters(pobj);
if (!pobj->state.is_done(STEP_PREPARE_INFILL)) {
pobj->state.set_started(STEP_PREPARE_INFILL);
pobj->_prepare_infill();
pobj->state.set_done(STEP_PREPARE_INFILL);
}
pobj->_infill();
}
void PrintController::gen_support_material(PrintObject *pobj)
{
assert(pobj != nullptr);
// prerequisites
slice(pobj);
if(!pobj->state.is_done(STEP_SUPPORTMATERIAL)) {
pobj->state.set_started(STEP_SUPPORTMATERIAL);
pobj->clear_support_layers();
if((pobj->config.support_material || pobj->config.raft_layers > 0)
&& pobj->layers.size() > 1) {
pobj->_generate_support_material();
}
pobj->state.set_done(STEP_SUPPORTMATERIAL);
}
}
2018-07-03 08:22:55 +00:00
void PrintController::slice(AppControllerBoilerplate::ProgresIndicatorPtr pri)
2018-06-28 16:47:18 +00:00
{
2018-07-03 08:22:55 +00:00
auto st = pri->state();
2018-06-28 16:47:18 +00:00
Slic3r::trace(3, "Starting the slicing process.");
2018-07-03 08:22:55 +00:00
pri->update(st+20, _(L("Generating perimeters")));
2018-06-28 16:47:18 +00:00
for(auto obj : print_->objects) make_perimeters(obj);
2018-07-03 08:22:55 +00:00
pri->update(st+60, _(L("Infilling layers")));
2018-06-28 16:47:18 +00:00
for(auto obj : print_->objects) infill(obj);
2018-07-03 08:22:55 +00:00
pri->update(st+70, _(L("Generating support material")));
2018-06-28 16:47:18 +00:00
for(auto obj : print_->objects) gen_support_material(obj);
2018-07-03 08:22:55 +00:00
pri->message_fmt(_(L("Weight: %.1fg, Cost: %.1f")),
print_->total_weight, print_->total_cost);
pri->state(st+85);
2018-06-28 16:47:18 +00:00
2018-07-03 08:22:55 +00:00
pri->update(st+88, _(L("Generating skirt")));
2018-06-28 16:47:18 +00:00
make_skirt();
2018-07-03 08:22:55 +00:00
pri->update(st+90, _(L("Generating brim")));
2018-06-28 16:47:18 +00:00
make_brim();
2018-07-03 08:22:55 +00:00
pri->update(st+95, _(L("Generating wipe tower")));
2018-06-28 16:47:18 +00:00
make_wipe_tower();
2018-07-03 08:22:55 +00:00
pri->update(st+100, _(L("Done")));
2018-06-28 16:47:18 +00:00
// time to make some statistics..
2018-07-03 08:22:55 +00:00
Slic3r::trace(3, _(L("Slicing process finished.")));
2018-06-28 16:47:18 +00:00
}
2018-07-03 08:22:55 +00:00
void PrintController::slice()
{
auto pri = global_progress_indicator();
if(!pri) pri = create_progress_indicator(100, L("Slicing"));
2018-07-03 08:22:55 +00:00
slice(pri);
}
2018-06-28 16:47:18 +00:00
void IProgressIndicator::message_fmt(
2018-07-03 08:22:55 +00:00
const string &fmtstr, ...) {
2018-06-28 16:47:18 +00:00
std::stringstream ss;
va_list args;
va_start(args, fmtstr);
auto fmt = fmtstr.begin();
while (*fmt != '\0') {
if (*fmt == 'd') {
int i = va_arg(args, int);
ss << i << '\n';
} else if (*fmt == 'c') {
// note automatic conversion to integral type
int c = va_arg(args, int);
ss << static_cast<char>(c) << '\n';
} else if (*fmt == 'f') {
double d = va_arg(args, double);
ss << d << '\n';
}
++fmt;
}
va_end(args);
message(ss.str());
}
2018-07-03 08:22:55 +00:00
const PrintConfig &PrintController::config() const
{
return print_->config;
}
2018-06-28 16:47:18 +00:00
void AppController::arrange_model()
{
auto ftr = std::async(
supports_asynch()? std::launch::async : std::launch::deferred,
[this]()
{
unsigned count = 0;
for(auto obj : model_->objects) count += obj->instances.size();
auto pind = global_progress_indicator();
2018-06-28 16:47:18 +00:00
2018-07-02 09:22:47 +00:00
float pmax = 1.0;
if(pind) {
pmax = pind->max();
// Set the range of the progress to the object count
pind->max(count);
}
auto dist = print_ctl()->config().min_object_distance();
2018-06-28 16:47:18 +00:00
BoundingBoxf bb(print_ctl()->config().bed_shape.values);
2018-07-03 08:22:55 +00:00
if(pind) pind->update(0, _(L("Arranging objects...")));
try {
model_->arrange_objects(dist, &bb, [pind, count](unsigned rem){
2018-07-03 08:22:55 +00:00
if(pind) pind->update(count - rem, _(L("Arranging objects...")));
});
} catch(std::exception& e) {
std::cerr << e.what() << std::endl;
report_issue(IssueType::ERR,
2018-07-03 08:22:55 +00:00
_(L("Could not arrange model objects! "
"Some geometries may be invalid.")),
_(L("Exception occurred")));
}
// Restore previous max value
2018-07-02 09:22:47 +00:00
if(pind) {
pind->max(pmax);
2018-07-03 08:22:55 +00:00
pind->update(0, _(L("Arranging done.")));
2018-07-02 09:22:47 +00:00
}
2018-06-28 16:47:18 +00:00
});
while( ftr.wait_for(std::chrono::milliseconds(10))
!= std::future_status::ready) {
process_events();
}
2018-06-28 16:47:18 +00:00
}
}