FreeCAD

Форк
0
/
Importer.cpp 
127 строк · 4.8 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2021 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

25
#include <App/Document.h>
26

27
#include "Importer.h"
28
#include "MeshFeature.h"
29

30

31
using namespace Mesh;
32

33

34
Importer::Importer(App::Document* doc)
35
    : document(doc)
36
{}
37

38
void Importer::load(const std::string& fileName)
39
{
40
    MeshObject mesh;
41
    MeshCore::Material mat;
42

43
    if (mesh.load(fileName.c_str(), &mat)) {
44
        Base::FileInfo file(fileName.c_str());
45
        unsigned long segmct = mesh.countSegments();
46
        if (segmct > 1) {
47
            createMeshFromSegments(file.fileNamePure(), mat, mesh);
48
        }
49
        else if (mat.binding == MeshCore::MeshIO::PER_VERTEX
50
                 && mat.diffuseColor.size() == mesh.countPoints()) {
51
            Feature* feature = createMesh(file.fileNamePure(), mesh);
52
            addVertexColors(feature, mat.diffuseColor);
53
            feature->purgeTouched();
54
        }
55
        else if (mat.binding == MeshCore::MeshIO::PER_FACE
56
                 && mat.diffuseColor.size() == mesh.countFacets()) {
57
            Feature* feature = createMesh(file.fileNamePure(), mesh);
58
            addFaceColors(feature, mat.diffuseColor);
59
            feature->purgeTouched();
60
        }
61
        else {
62
            Feature* feature = createMesh(file.fileNamePure(), mesh);
63
            feature->purgeTouched();
64
        }
65
    }
66
}
67

68
void Importer::addVertexColors(Feature* feature, const std::vector<App::Color>& colors)
69
{
70
    addColors(feature, "VertexColors", colors);
71
}
72

73
void Importer::addFaceColors(Feature* feature, const std::vector<App::Color>& colors)
74
{
75
    addColors(feature, "FaceColors", colors);
76
}
77

78
void Importer::addColors(Feature* feature,
79
                         const std::string& property,
80
                         const std::vector<App::Color>& colors)
81
{
82
    App::PropertyColorList* prop = static_cast<App::PropertyColorList*>(
83
        feature->addDynamicProperty("App::PropertyColorList", property.c_str()));
84
    if (prop) {
85
        prop->setValues(colors);
86
    }
87
}
88

89
void Importer::createMeshFromSegments(const std::string& name,
90
                                      MeshCore::Material& mat,
91
                                      MeshObject& mesh)
92
{
93
    unsigned long segmct = mesh.countSegments();
94
    for (unsigned long i = 0; i < segmct; i++) {
95
        const Segment& group = mesh.getSegment(i);
96
        std::string groupName = group.getName();
97
        if (groupName.empty()) {
98
            groupName = name;
99
        }
100

101
        std::unique_ptr<MeshObject> segm(mesh.meshFromSegment(group.getIndices()));
102
        Feature* feature = createMesh(groupName, *segm);
103

104
        // if colors are set per face
105
        if (mat.binding == MeshCore::MeshIO::PER_FACE
106
            && mat.diffuseColor.size() == mesh.countFacets()) {
107

108
            std::vector<App::Color> diffuseColor;
109
            diffuseColor.reserve(group.getIndices().size());
110
            for (const auto& it : group.getIndices()) {
111
                diffuseColor.push_back(mat.diffuseColor[it]);
112
            }
113

114
            addFaceColors(feature, diffuseColor);
115
        }
116
        feature->purgeTouched();
117
    }
118
}
119

120
Feature* Importer::createMesh(const std::string& name, MeshObject& mesh)
121
{
122
    Mesh::Feature* pcFeature =
123
        static_cast<Mesh::Feature*>(document->addObject("Mesh::Feature", name.c_str()));
124
    pcFeature->Label.setValue(name);
125
    pcFeature->Mesh.swapMesh(mesh);
126
    return pcFeature;
127
}
128

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

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

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

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