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

PrintExport.hpp « libslic3r « src « xs - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7c3871251fddb58df9982326fb74809cfef4c1d4 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#ifndef PRINTEXPORT_HPP
#define PRINTEXPORT_HPP

#include "Print.hpp"

// For png export of the sliced model
#include <fstream>
#include <sstream>

#include <wx/stdstream.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>

#include <boost/log/trivial.hpp>

#include "Rasterizer/Rasterizer.hpp"
#include <tbb/parallel_for.h>
#include <tbb/spin_mutex.h>//#include "tbb/mutex.h"

namespace Slic3r {

enum class FilePrinterFormat {
    PNG,
    SVG
};

/*
 * Interface for a file printer of the slices. Implementation can be an SVG
 * or PNG printer or any other format.
 *
 * The format argument specifies the output format of the printer and it enables
 * different implementations of this class template for each supported format.
 *
 */
template<FilePrinterFormat format>
class FilePrinter {
public:

    void printConfig(const Print&);

    // Draw an ExPolygon which is a polygon inside a slice on the specified layer.
    void drawPolygon(const ExPolygon& p, unsigned lyr);

    // Tell the printer how many layers should it consider.
    void layers(unsigned layernum);

    // Get the number of layers in the print.
    unsigned layers() const;

    /* Switch to a particular layer. If there where less layers then the
     * specified layer number than an appropriate number of layers will be
     * allocated in the printer.
     */
    void beginLayer(unsigned layer);

    // Allocate a new layer on top of the last and switch to it.
    void beginLayer();

    /*
     * Finish the selected layer. It means that no drawing is allowed on that
     * layer anymore. This fact can be used to prepare the file system output
     * data like png comprimation and so on.
     */
    void finishLayer(unsigned layer);

    // Finish the top layer.
    void finishLayer();

    // Save all the layers into the file (or dir) specified in the path argument
    void save(const std::string& path);

    // Save only the selected layer to the file specified in path argument.
    void saveLayer(unsigned lyr, const std::string& path);
};

// Implementation for PNG raster output
// Be aware that if a large number of layers are allocated, it can very well
// exhaust the available memory especially on 32 bit platform.
template<> class FilePrinter<FilePrinterFormat::PNG> {

    struct Layer {
        Raster first;
        std::stringstream second;

        Layer() {}

        Layer(const Layer&) = delete;
        Layer(Layer&& m):
            first(std::move(m.first))/*, second(std::move(m.second))*/ {}
    };

    // We will save the compressed PNG data into stringstreams which can be done
    // in parallel. Later we can write every layer to the disk sequentially.
    std::vector<Layer> layers_rst_;
    Raster::Resolution res_;
    Raster::PixelDim pxdim_;
    const Print *print_ = nullptr;
    double exp_time_s_ = .0, exp_time_first_s_ = .0;

    std::string createIniContent(const std::string& projectname) {
        double layer_height = print_?
                    print_->default_object_config().layer_height.getFloat() :
                    0.05;

        using std::string;
        using std::to_string;

        auto expt_str = to_string(exp_time_s_);
        auto expt_first_str = to_string(exp_time_first_s_);
        auto stepnum_str = to_string(static_cast<unsigned>(800*layer_height));
        auto layerh_str = to_string(layer_height);

        return string(
        "action = print\n"
        "jobDir = ") + projectname + "\n" +
        "expTime = " + expt_str + "\n"
        "expTimeFirst = " + expt_first_str + "\n"
        "stepNum = " + stepnum_str + "\n"
        "wifiOn = 1\n"
        "tiltSlow = 60\n"
        "tiltFast = 15\n"
        "numFade = 10\n"
        "startdelay = 0\n"
        "layerHeight = " + layerh_str + "\n"
        "noteInfo = "
        "expTime="+expt_str+"+resinType=generic+layerHeight="
                  +layerh_str+"+printer=DWARF3\n";
    }

    // Change this to TOP_LEFT if you want correct PNG orientation
    static const Raster::Origin ORIGIN = Raster::Origin::BOTTOM_LEFT;

public:
    inline FilePrinter(double width_mm, double height_mm,
                       unsigned width_px, unsigned height_px,
                       double exp_time, double exp_time_first):
        res_(width_px, height_px),
        pxdim_(width_mm/width_px, height_mm/height_px),
        exp_time_s_(exp_time),
        exp_time_first_s_(exp_time_first)
    {
    }

    FilePrinter(const FilePrinter& ) = delete;
    FilePrinter(FilePrinter&& m):
        layers_rst_(std::move(m.layers_rst_)),
        res_(m.res_),
        pxdim_(m.pxdim_) {}

    inline void layers(unsigned cnt) { if(cnt > 0) layers_rst_.resize(cnt); }
    inline unsigned layers() const { return layers_rst_.size(); }

    void printConfig(const Print& printconf) { print_ = &printconf; }

    inline void drawPolygon(const ExPolygon& p, unsigned lyr) {
        assert(lyr < layers_rst_.size());
        layers_rst_[lyr].first.draw(p);
    }

    inline void beginLayer(unsigned lyr) {
        if(layers_rst_.size() <= lyr) layers_rst_.resize(lyr+1);
        layers_rst_[lyr].first.reset(res_, pxdim_, ORIGIN);
    }

    inline void beginLayer() {
        layers_rst_.emplace_back();
        layers_rst_.front().first.reset(res_, pxdim_, ORIGIN);
    }

    inline void finishLayer(unsigned lyr_id) {
        assert(lyr_id < layers_rst_.size());
        layers_rst_[lyr_id].first.save(layers_rst_[lyr_id].second,
                                       Raster::Compression::PNG);
        layers_rst_[lyr_id].first.reset();
    }

    inline void finishLayer() {
        if(!layers_rst_.empty()) {
            layers_rst_.back().first.save(layers_rst_.back().second,
                                          Raster::Compression::PNG);
            layers_rst_.back().first.reset();
        }
    }

    inline void save(const std::string& path) {

        wxFileName filepath(path);

        wxFFileOutputStream zipfile(path);

        std::string project = filepath.GetName().ToStdString();

        if(!zipfile.IsOk()) {
            BOOST_LOG_TRIVIAL(error) << "Can't create zip file for layers! "
                                     << path;
            return;
        }

        wxZipOutputStream zipstream(zipfile);
        wxStdOutputStream pngstream(zipstream);

        zipstream.PutNextEntry("config.ini");
        pngstream << createIniContent(project);

        for(unsigned i = 0; i < layers_rst_.size(); i++) {
            if(layers_rst_[i].second.rdbuf()->in_avail() > 0) {
                char lyrnum[6];
                std::sprintf(lyrnum, "%.5d", i);
                auto zfilename = project + lyrnum + ".png";
                zipstream.PutNextEntry(zfilename);
                pngstream << layers_rst_[i].second.rdbuf();
                layers_rst_[i].second.str("");
            }
        }

        zipstream.Close();
        zipfile.Close();
    }

    void saveLayer(unsigned lyr, const std::string& path) {
        unsigned i = lyr;
        assert(i < layers_rst_.size());

        char lyrnum[6];
        std::sprintf(lyrnum, "%.5d", lyr);
        std::string loc = path + "layer" + lyrnum + ".png";

        std::fstream out(loc, std::fstream::out | std::fstream::binary);
        if(out.good()) {
            layers_rst_[i].first.save(out, Raster::Compression::PNG);
        } else {
            BOOST_LOG_TRIVIAL(error) << "Can't create file for layer";
        }

        out.close();
        layers_rst_[i].first.reset();
    }
};

// Let's shadow this eigen interface
inline coord_t px(const Point& p) { return p(0); }
inline coord_t py(const Point& p) { return p(1); }
inline coordf_t px(const Vec2d& p) { return p(0); }
inline coordf_t py(const Vec2d& p) { return p(1); }

template<FilePrinterFormat format, class...Args>
void print_to(Print& print,
              std::string dirpath,
              double width_mm,
              double height_mm,
              Args&&...args)
{

    std::string& dir = dirpath;

    // This map will hold the layers sorted by z coordinate. Layers on the
    // same height (from different objects) will be mapped to the same key and
    // rasterized to the same image.
    std::map<long long, LayerPtrs> layers;

    auto& objects = print.objects();

    // Merge the sliced layers with the support layers
    std::for_each(objects.cbegin(), objects.cend(), [&layers](const PrintObject *o) {
        for(const auto l : o->layers()) {
            auto& lyrs = layers[static_cast<long long>(scale_(l->print_z))];
            lyrs.push_back(l);
        }

        for(const auto l : o->support_layers()) {
            auto& lyrs = layers[static_cast<long long>(scale_(l->print_z))];
            lyrs.push_back(l);
        }
    });

    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) > width_mm || py(punsc) > height_mm) {
        BOOST_LOG_TRIVIAL(warning) << "Warning: Print will not fit!" << "\n"
            << "Width needed: " << px(punsc) << "\n"
            << "Height needed: " << py(punsc) << "\n";
    }

    // Offset for centering the print onto the print area
    auto cx = scale_(width_mm)/2 - (px(print_bb.center()) - px(print_bb.min));
    auto cy = scale_(height_mm)/2 - (py(print_bb.center()) - py(print_bb.min));

    // Create the actual printer, forward any additional arguments to it.
    FilePrinter<format> printer(width_mm, height_mm,
                                std::forward<Args>(args)...);

    printer.printConfig(print);

    printer.layers(layers.size());  // Allocate space for all the layers

    int st_prev = 0;
    const std::string jobdesc = "Rasterizing and compressing sliced layers";
    tbb::spin_mutex m;

    std::vector<long long> keys;
    keys.reserve(layers.size());
    for(auto& e : layers) keys.push_back(e.first);

    //FIXME
    int initstatus = //print.progressindicator? print.progressindicator->state() : 
        0;
    print.set_status(initstatus, jobdesc);

    // Method that prints one layer
    auto process_layer = [&layers, &keys, &printer, &st_prev, &m,
            &jobdesc, print_bb, dir, cx, cy, &print, initstatus]
            (unsigned layer_id)
    {
        LayerPtrs lrange = layers[keys[layer_id]];

        printer.beginLayer(layer_id);   // Switch to the appropriate layer

        for(Layer *lp : lrange) {
            Layer& l = *lp;

            ExPolygonCollection slices = l.slices;  // Copy the layer slices

            // Sort the polygons in the layer
            std::stable_sort(slices.expolygons.begin(), slices.expolygons.end(),
                             [](const ExPolygon& a, const ExPolygon& b) {
                return a.contour.contains(b.contour.first_point()) ? false :
                                                                     true;
            });

            // Draw all the polygons in the slice to the actual layer.
            for (const Point &d : l.object()->copies())
                for (ExPolygon slice : slices.expolygons) {
                    slice.translate(px(d), py(d));
                    slice.translate(-px(print_bb.min) + cx,
                                    -py(print_bb.min) + cy);

                    printer.drawPolygon(slice, layer_id);
                }

            /*if(print.has_support_material() && layer_id > 0) {
                BOOST_LOG_TRIVIAL(warning) << "support material for layer "
                                           << layer_id
                                           << " defined but export is "
                                              "not yet implemented.";

            }*/

        }

        printer.finishLayer(layer_id);  // Finish the layer for later saving it.

        auto st = static_cast<int>(layer_id*80.0/layers.size());
        m.lock();
        if( st - st_prev > 10) {
            print.set_status(initstatus + st, jobdesc);
            st_prev = st;
        }
        m.unlock();

        // printer.saveLayer(layer_id, dir); We could save the layer immediately
    };

    // Print all the layers in parallel
    tbb::parallel_for<size_t, decltype(process_layer)>(0,
                                                       layers.size(),
                                                       process_layer);

    // Sequential version (for testing)
    // for(unsigned l = 0; l < layers.size(); ++l) process_layer(l);

//    print.set_status(100, jobdesc);

    // Save the print into the file system.
    print.set_status(initstatus + 90, "Writing layers to disk");
    printer.save(dir);
    print.set_status(initstatus + 100, "Writing layers completed");
}

}

#endif // PRINTEXPORT_HPP