Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AppController.cpp « slic3r « src « xs - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c9029f2be00ccb54ac8dd11b330adafead39b67a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "AppController.hpp"

#include <future>
#include <chrono>
#include <sstream>
#include <cstdarg>
#include <thread>
#include <unordered_map>

#include <slic3r/GUI/GUI.hpp>
#include <ModelArrange.hpp>
#include <slic3r/GUI/PresetBundle.hpp>

#include <PrintConfig.hpp>
#include <Print.hpp>
#include <PrintExport.hpp>
#include <Geometry.hpp>
#include <Model.hpp>
#include <Utils.hpp>


namespace Slic3r {

class AppControllerBoilerplate::PriData {
public:
    std::mutex m;
    std::thread::id ui_thread;

    inline explicit PriData(std::thread::id uit): ui_thread(uit) {}
};

AppControllerBoilerplate::AppControllerBoilerplate()
    :pri_data_(new PriData(std::this_thread::get_id())) {}

AppControllerBoilerplate::~AppControllerBoilerplate() {
    pri_data_.reset();
}

bool AppControllerBoilerplate::is_main_thread() const
{
    return pri_data_->ui_thread == std::this_thread::get_id();
}

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() {
    ProgresIndicatorPtr ret;

    pri_data_->m.lock();
    ret = global_progressind_;
    pri_data_->m.unlock();

    return ret;
}

void AppControllerBoilerplate::global_progress_indicator(
        AppControllerBoilerplate::ProgresIndicatorPtr gpri)
{
    pri_data_->m.lock();
    global_progressind_ = gpri;
    pri_data_->m.unlock();
}

PrintController::PngExportData
PrintController::query_png_export_data(const DynamicPrintConfig& conf)
{
    PngExportData ret;

    auto zippath = query_destination_path("Output zip file", "*.zip", "out");

    ret.zippath = zippath;

    ret.width_mm = conf.opt_float("display_width");
    ret.height_mm = conf.opt_float("display_height");

    ret.width_px = conf.opt_int("display_pixels_x");
    ret.height_px = conf.opt_int("display_pixels_y");

    auto opt_corr = conf.opt<ConfigOptionFloats>("printer_correction");

    if(opt_corr) {
        ret.corr_x = opt_corr->values[0];
        ret.corr_y = opt_corr->values[1];
        ret.corr_z = opt_corr->values[2];
    }

    ret.exp_time_first_s = conf.opt_float("initial_exposure_time");
    ret.exp_time_s = conf.opt_float("exposure_time");

    return ret;
}

void PrintController::slice(AppControllerBoilerplate::ProgresIndicatorPtr pri)
{
    print_->set_status_callback([pri](int st, const std::string& msg){
        pri->update(unsigned(st), msg);
    });

    print_->process();
}

void PrintController::slice()
{
    auto pri = global_progress_indicator();
    if(!pri) pri = create_progress_indicator(100, L("Slicing"));
    slice(pri);
}

template<> class LayerWriter<Zipper> {
    Zipper m_zip;
public:

    inline LayerWriter(const std::string& zipfile_path): m_zip(zipfile_path) {}

    inline void next_entry(const std::string& fname) { m_zip.next_entry(fname); }

    inline std::string get_name() const { return m_zip.get_name(); }

    template<class T> inline LayerWriter& operator<<(const T& arg) {
        m_zip.stream() << arg; return *this;
    }

    inline void close() { m_zip.close(); }
};

void PrintController::slice_to_png()
{
    using Pointf3 = Vec3d;

    auto presetbundle = GUI::get_preset_bundle();

    assert(presetbundle);

    auto pt = presetbundle->printers.get_selected_preset().printer_technology();
    if(pt != ptSLA) {
        report_issue(IssueType::ERR, L("Printer technology is not SLA!"),
                     L("Error"));
        return;
    }

    auto conf = presetbundle->full_config();
    conf.validate();

    auto exd = query_png_export_data(conf);
    if(exd.zippath.empty()) return;

    Print *print = print_;

    try {
        print->apply_config(conf);
        print->validate();
    } catch(std::exception& e) {
        report_issue(IssueType::ERR, e.what(), "Error");
        return;
    }

    // TODO: copy the model and work with the copy only
    bool correction = false;
    if(exd.corr_x != 1.0 || exd.corr_y != 1.0 || exd.corr_z != 1.0) {
        correction = true;
//        print->invalidate_all_steps();

//        for(auto po : print->objects) {
//            po->model_object()->scale(
//                        Pointf3(exd.corr_x, exd.corr_y, exd.corr_z)
//                        );
//            po->model_object()->invalidate_bounding_box();
//            po->reload_model_instances();
//            po->invalidate_all_steps();
//        }
    }

    // Turn back the correction scaling on the model.
    auto scale_back = [this, print, correction, exd]() {
        if(correction) { // scale the model back
//            print->invalidate_all_steps();
//            for(auto po : print->objects) {
//                po->model_object()->scale(
//                    Pointf3(1.0/exd.corr_x, 1.0/exd.corr_y, 1.0/exd.corr_z)
//                );
//                po->model_object()->invalidate_bounding_box();
//                po->reload_model_instances();
//                po->invalidate_all_steps();
//            }
        }
    };

    auto print_bb = print->bounding_box();
    Vec2d punsc = unscale(print_bb.size());

    // If the print does not fit into the print area we should cry about it.
    if(px(punsc) > exd.width_mm || py(punsc) > exd.height_mm) {
        std::stringstream ss;

        ss << L("Print will not fit and will be truncated!") << "\n"
           << L("Width needed: ") << px(punsc) << " mm\n"
           << L("Height needed: ") << py(punsc) << " mm\n";

       if(!report_issue(IssueType::WARN_Q, ss.str(), L("Warning")))  {
            scale_back();
            return;
       }
    }

    auto pri = create_progress_indicator(
                200, L("Slicing to zipped png files..."));

    pri->on_cancel([&print](){ print->cancel(); });

    try {
        pri->update(0, L("Slicing..."));
        slice(pri);
    } catch (std::exception& e) {
        report_issue(IssueType::ERR, e.what(), L("Exception occurred"));
        scale_back();
        if(print->canceled()) print->restart();
        return;
    }

    auto initstate = unsigned(pri->state());
    print->set_status_callback([pri, initstate](int st, const std::string& msg)
    {
        pri->update(initstate + unsigned(st), msg);
    });

    try {
        print_to<FilePrinterFormat::PNG, Zipper>( *print, exd.zippath,
                    exd.width_mm, exd.height_mm,
                    exd.width_px, exd.height_px,
                    exd.exp_time_s, exd.exp_time_first_s);

    } catch (std::exception& e) {
        report_issue(IssueType::ERR, e.what(), L("Exception occurred"));
    }

    scale_back();
    if(print->canceled()) print->restart();
    print->set_status_default();
}

const PrintConfig &PrintController::config() const
{
    return print_->config();
}

void ProgressIndicator::message_fmt(
        const std::string &fmtstr, ...) {
    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());
}

void AppController::arrange_model()
{
    using Coord = libnest2d::TCoord<libnest2d::PointImpl>;

    if(arranging_.load()) return;

    // to prevent UI reentrancies
    arranging_.store(true);

    unsigned count = 0;
    for(auto obj : model_->objects) count += obj->instances.size();

    auto pind = global_progress_indicator();

    float pmax = 1.0;

    if(pind) {
        pmax = pind->max();

        // Set the range of the progress to the object count
        pind->max(count);

        pind->on_cancel([this](){
            arranging_.store(false);
        });
    }

    auto dist = print_ctl()->config().min_object_distance();

    // Create the arranger config
    auto min_obj_distance = static_cast<Coord>(dist/SCALING_FACTOR);

    auto& bedpoints = print_ctl()->config().bed_shape.values;
    Polyline bed; bed.points.reserve(bedpoints.size());
    for(auto& v : bedpoints)
        bed.append(Point::new_scale(v(0), v(1)));

    if(pind) pind->update(0, L("Arranging objects..."));

    try {
        arr::BedShapeHint hint;
        // TODO: from Sasha from GUI
        hint.type = arr::BedShapeType::WHO_KNOWS;

        arr::arrange(*model_,
                      min_obj_distance,
                      bed,
                      hint,
                      false, // create many piles not just one pile
                      [this, pind, count](unsigned rem) {
            if(pind)
                pind->update(count - rem, L("Arranging objects..."));

            process_events();
        }, [this] () { return !arranging_.load(); });
    } catch(std::exception& e) {
        std::cerr << e.what() << std::endl;
        report_issue(IssueType::ERR,
                        L("Could not arrange model objects! "
                        "Some geometries may be invalid."),
                        L("Exception occurred"));
    }

    // Restore previous max value
    if(pind) {
        pind->max(pmax);
        pind->update(0, arranging_.load() ? L("Arranging done.") :
                                            L("Arranging canceled."));

        pind->on_cancel(/*remove cancel function*/);
    }

    arranging_.store(false);
}

}