1
/***************************************************************************
2
* Copyright (c) 2021 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
25
#include <App/Document.h>
28
#include "MeshFeature.h"
34
Importer::Importer(App::Document* doc)
38
void Importer::load(const std::string& fileName)
41
MeshCore::Material mat;
43
if (mesh.load(fileName.c_str(), &mat)) {
44
Base::FileInfo file(fileName.c_str());
45
unsigned long segmct = mesh.countSegments();
47
createMeshFromSegments(file.fileNamePure(), mat, mesh);
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();
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();
62
Feature* feature = createMesh(file.fileNamePure(), mesh);
63
feature->purgeTouched();
68
void Importer::addVertexColors(Feature* feature, const std::vector<App::Color>& colors)
70
addColors(feature, "VertexColors", colors);
73
void Importer::addFaceColors(Feature* feature, const std::vector<App::Color>& colors)
75
addColors(feature, "FaceColors", colors);
78
void Importer::addColors(Feature* feature,
79
const std::string& property,
80
const std::vector<App::Color>& colors)
82
App::PropertyColorList* prop = static_cast<App::PropertyColorList*>(
83
feature->addDynamicProperty("App::PropertyColorList", property.c_str()));
85
prop->setValues(colors);
89
void Importer::createMeshFromSegments(const std::string& name,
90
MeshCore::Material& mat,
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()) {
101
std::unique_ptr<MeshObject> segm(mesh.meshFromSegment(group.getIndices()));
102
Feature* feature = createMesh(groupName, *segm);
104
// if colors are set per face
105
if (mat.binding == MeshCore::MeshIO::PER_FACE
106
&& mat.diffuseColor.size() == mesh.countFacets()) {
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]);
114
addFaceColors(feature, diffuseColor);
116
feature->purgeTouched();
120
Feature* Importer::createMesh(const std::string& name, MeshObject& mesh)
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);