FreeCAD

Форк
0
/
ThumbnailExtension.cpp 
101 строка · 3.8 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
#include <QBuffer>
26
#include <QByteArray>
27

28
#include <Inventor/SbRotation.h>
29
#include <Inventor/SbViewportRegion.h>
30
#include <Inventor/nodes/SoCoordinate3.h>
31
#include <Inventor/nodes/SoDirectionalLight.h>
32
#include <Inventor/nodes/SoIndexedFaceSet.h>
33
#include <Inventor/nodes/SoOrthographicCamera.h>
34
#include <Inventor/nodes/SoSeparator.h>
35
#endif
36

37
#include <Gui/SoFCOffscreenRenderer.h>
38

39
#include "ThumbnailExtension.h"
40
#include "ViewProvider.h"
41

42

43
using namespace MeshGui;
44

45
Mesh::Extension3MF::Resource ThumbnailExtension3MF::addMesh(const Mesh::MeshObject& mesh)
46
{
47
    SoCoordinate3* coord = new SoCoordinate3();
48
    SoIndexedFaceSet* faces = new SoIndexedFaceSet();
49

50
    SoOrthographicCamera* cam = new SoOrthographicCamera();
51
    SoSeparator* root = new SoSeparator();
52
    root->ref();
53
    root->addChild(cam);
54
    root->addChild(new SoDirectionalLight);
55
    root->addChild(coord);
56
    root->addChild(faces);
57

58
    ViewProviderMeshBuilder().createMesh(mesh.getKernel(), coord, faces);
59

60
    SbRotation rot(-0.35355F, -0.14644F, -0.35355F, -0.85355F);
61
    cam->orientation.setValue(rot);
62
    SbViewportRegion vpr(256, 256);
63
    cam->viewAll(root, vpr);
64

65
    Gui::SoQtOffscreenRenderer renderer(vpr);
66
    renderer.setBackgroundColor(SbColor4f(1.0F, 1.0F, 1.0F, 0.0F));
67
    QImage img;
68
    renderer.render(root);
69
    renderer.writeToImage(img);
70
    root->unref();
71

72
    QByteArray data;
73
    QBuffer buffer(&data);
74
    buffer.open(QIODevice::WriteOnly);
75
    img.save(&buffer, "PNG");
76
    buffer.close();
77

78
    Mesh::Extension3MF::Resource res;
79
    res.extension = "png";
80
    res.contentType = "image/png";
81
    res.relationshipType =
82
        "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail";
83
    res.fileContent = std::string(data.data(), data.size());
84
    setContentName(res);
85

86
    index++;
87
    return res;
88
}
89

90
void ThumbnailExtension3MF::setContentName(Mesh::Extension3MF::Resource& res)
91
{
92
    if (index == 0) {
93
        res.relationshipTarget = "/Metadata/thumbnail.png";
94
        res.fileNameInZip = "Metadata/thumbnail.png";
95
    }
96
    else {
97
        std::string suf = std::to_string(index);
98
        res.relationshipTarget = "/Metadata/thumbnail" + suf + ".png";
99
        res.fileNameInZip = "Metadata/thumbnail" + suf + ".png";
100
    }
101
}
102

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.