FreeCAD

Форк
0
/
ReferenceHighlighter.cpp 
260 строк · 9.0 Кб
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
#ifndef _PreComp_
25
# include <TopExp.hxx>
26
# include <TopExp_Explorer.hxx>
27

28
# include <boost/algorithm/string/predicate.hpp>
29
#endif
30

31
#include "ReferenceHighlighter.h"
32

33

34
using namespace PartGui;
35

36
ReferenceHighlighter::ReferenceHighlighter(const TopoDS_Shape& shape, const App::Color& color)
37
    : defaultColor(color)
38
    , elementColor(1.0f,0.0f,1.0f) // magenta
39
    , objectColor(0.6f,0.0f,1.0f) // purple
40
{
41
    TopExp::MapShapes(shape, TopAbs_VERTEX, vMap);
42
    TopExp::MapShapes(shape, TopAbs_EDGE, eMap);
43
    TopExp::MapShapes(shape, TopAbs_WIRE, wMap);
44
    TopExp::MapShapes(shape, TopAbs_FACE, fMap);
45
}
46

47
void ReferenceHighlighter::getVertexColor(const std::string& element, std::vector<App::Color>& colors) const
48
{
49
    int idx = std::stoi(element.substr(6)) - 1;
50
    assert ( idx >= 0 );
51
    std::size_t pos = std::size_t(idx);
52
    if (pos < colors.size())
53
        colors[pos] = elementColor;
54
}
55

56
void ReferenceHighlighter::getVertexColorsOfEdge(const std::string& element, std::vector<App::Color>& colors) const
57
{
58
    int idx = std::stoi(element.substr(4));
59
    assert ( idx > 0 );
60
    // get the vertexes of the edge
61
    TopoDS_Shape edge = eMap.FindKey(idx);
62
    for (TopExp_Explorer xp(edge, TopAbs_VERTEX); xp.More(); xp.Next()) {
63
        int vertexIndex = vMap.FindIndex(xp.Current());
64

65
        // Vertex found?
66
        if (vertexIndex > 0) {
67
            std::size_t pos = std::size_t(vertexIndex - 1);
68
            if (pos < colors.size())
69
                colors[pos] = elementColor;
70
        }
71
    }
72
}
73

74
void ReferenceHighlighter::getVertexColorsOfWire(const std::string& element, std::vector<App::Color>& colors) const
75
{
76
    int idx = std::stoi(element.substr(4));
77
    assert ( idx > 0 );
78
    // get the vertexes of the wire
79
    TopoDS_Shape wire = wMap.FindKey(idx);
80
    for (TopExp_Explorer xp(wire, TopAbs_VERTEX); xp.More(); xp.Next()) {
81
        int vertexIndex = vMap.FindIndex(xp.Current());
82

83
        // Vertex found?
84
        if (vertexIndex > 0) {
85
            std::size_t pos = std::size_t(vertexIndex - 1);
86
            if (pos < colors.size())
87
                colors[pos] = elementColor;
88
        }
89
    }
90
}
91

92
void ReferenceHighlighter::getVertexColorsOfFace(const std::string& element, std::vector<App::Color>& colors) const
93
{
94
    int idx = std::stoi(element.substr(4));
95
    assert ( idx > 0 );
96
    // get the vertexes of the face
97
    TopoDS_Shape face = fMap.FindKey(idx);
98
    for (TopExp_Explorer xp(face, TopAbs_VERTEX); xp.More(); xp.Next()) {
99
        int vertexIndex = vMap.FindIndex(xp.Current());
100

101
        // Vertex found?
102
        if (vertexIndex > 0) {
103
            std::size_t pos = std::size_t(vertexIndex - 1);
104
            if (pos < colors.size())
105
                colors[pos] = elementColor;
106
        }
107
    }
108
}
109

110
void ReferenceHighlighter::getVertexColors(const std::vector<std::string>& elements,
111
                                           std::vector<App::Color>& colors) const
112
{
113
    colors.resize(vMap.Extent(), defaultColor);
114

115
    if (!elements.empty()) {
116
        for (const std::string& e : elements) {
117
            if (boost::starts_with(e, "Vertex")) {
118
                getVertexColor(e, colors);
119
            }
120
            else if (boost::starts_with(e, "Edge")) {
121
                getVertexColorsOfEdge(e, colors);
122
            }
123
            else if (boost::starts_with(e, "Wire")) {
124
                getVertexColorsOfWire(e, colors);
125
            }
126
            else if (boost::starts_with(e, "Face")) {
127
                getVertexColorsOfFace(e, colors);
128
            }
129
        }
130
    }
131
    else {
132
        std::fill(colors.begin(), colors.end(), objectColor);
133
    }
134
}
135

136
void ReferenceHighlighter::getEdgeColor(const std::string& element, std::vector<App::Color>& colors) const
137
{
138
    int idx = std::stoi(element.substr(4)) - 1;
139
    assert ( idx >= 0 );
140
    std::size_t pos = std::size_t(idx);
141
    if (pos < colors.size())
142
        colors[pos] = elementColor;
143
}
144

145
void ReferenceHighlighter::getEdgeColorsOfWire(const std::string& element, std::vector<App::Color>& colors) const
146
{
147
    int idx = std::stoi(element.substr(4));
148
    assert ( idx > 0 );
149
    // get the edges of the wire
150
    TopoDS_Shape wire = wMap.FindKey(idx);
151
    for (TopExp_Explorer xp(wire, TopAbs_EDGE); xp.More(); xp.Next()) {
152
        int edgeIndex = eMap.FindIndex(xp.Current());
153

154
        // Edge found?
155
        if (edgeIndex > 0) {
156
            std::size_t pos = std::size_t(edgeIndex - 1);
157
            if (pos < colors.size())
158
                colors[pos] = elementColor;
159
        }
160
    }
161
}
162

163
void ReferenceHighlighter::getEdgeColorsOfFace(const std::string& element, std::vector<App::Color>& colors) const
164
{
165
    int idx = std::stoi(element.substr(4));
166
    assert ( idx > 0 );
167
    // get the edges of the faces
168
    TopoDS_Shape face = fMap.FindKey(idx);
169
    for (TopExp_Explorer xp(face, TopAbs_EDGE); xp.More(); xp.Next()) {
170
        int edgeIndex = eMap.FindIndex(xp.Current());
171

172
        // Edge found?
173
        if (edgeIndex > 0) {
174
            std::size_t pos = std::size_t(edgeIndex - 1);
175
            if (pos < colors.size())
176
                colors[pos] = elementColor;
177
        }
178
    }
179
}
180

181
void ReferenceHighlighter::getEdgeColors(const std::vector<std::string>& elements,
182
                                         std::vector<App::Color>& colors) const
183
{
184
    colors.resize(eMap.Extent(), defaultColor);
185

186
    if (!elements.empty()) {
187
        for (const std::string& e : elements) {
188
            if (boost::starts_with(e, "Edge")) {
189
                getEdgeColor(e, colors);
190
            }
191
            else if (boost::starts_with(e, "Wire")) {
192
                getEdgeColorsOfWire(e, colors);
193
            }
194
            else if (boost::starts_with(e, "Face")) {
195
                getEdgeColorsOfFace(e, colors);
196
            }
197
        }
198
    }
199
    else {
200
        std::fill(colors.begin(), colors.end(), objectColor);
201
    }
202
}
203

204
void ReferenceHighlighter::getFaceColor(const std::string& element, std::vector<App::Color>& colors) const
205
{
206
    int idx = std::stoi(element.substr(4)) - 1;
207
    assert ( idx >= 0 );
208
    std::size_t pos = std::size_t(idx);
209
    if (pos < colors.size())
210
        colors[pos] = elementColor;
211
}
212

213
void ReferenceHighlighter::getFaceColor(const std::string& element,
214
                                        std::vector<App::Material>& materials) const
215
{
216
    int idx = std::stoi(element.substr(4)) - 1;
217
    assert(idx >= 0);
218
    std::size_t pos = std::size_t(idx);
219
    if (pos < materials.size()) {
220
        materials[pos].diffuseColor = elementColor;
221
    }
222
}
223

224
void ReferenceHighlighter::getFaceColors(const std::vector<std::string>& elements,
225
                                         std::vector<App::Color>& colors) const
226
{
227
    colors.resize(fMap.Extent(), defaultColor);
228

229
    if (!elements.empty()) {
230
        for (const std::string& e : elements) {
231
            if (boost::starts_with(e, "Face")) {
232
                getFaceColor(e, colors);
233
            }
234
        }
235
    }
236
    else {
237
        std::fill(colors.begin(), colors.end(), objectColor);
238
    }
239
}
240

241
void ReferenceHighlighter::getFaceMaterials(const std::vector<std::string>& elements,
242
                                         std::vector<App::Material>& materials) const
243
{
244
    App::Material defaultMaterial;
245
    materials.resize(fMap.Extent(), defaultMaterial);
246

247
    if (!elements.empty()) {
248
        for (const std::string& e : elements) {
249
            if (boost::starts_with(e, "Face")) {
250
                getFaceColor(e, materials);
251
            }
252
        }
253
    }
254
    else {
255
        for (auto& material : materials) {
256
            material.diffuseColor = objectColor;
257
        }
258
        // std::fill(materials.begin(), materials.end(), objectColor);
259
    }
260
}
261

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

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

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

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