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"
26
# include <TopExp_Explorer.hxx>
28
# include <boost/algorithm/string/predicate.hpp>
31
#include "ReferenceHighlighter.h"
34
using namespace PartGui;
36
ReferenceHighlighter::ReferenceHighlighter(const TopoDS_Shape& shape, const App::Color& color)
38
, elementColor(1.0f,0.0f,1.0f) // magenta
39
, objectColor(0.6f,0.0f,1.0f) // purple
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);
47
void ReferenceHighlighter::getVertexColor(const std::string& element, std::vector<App::Color>& colors) const
49
int idx = std::stoi(element.substr(6)) - 1;
51
std::size_t pos = std::size_t(idx);
52
if (pos < colors.size())
53
colors[pos] = elementColor;
56
void ReferenceHighlighter::getVertexColorsOfEdge(const std::string& element, std::vector<App::Color>& colors) const
58
int idx = std::stoi(element.substr(4));
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());
66
if (vertexIndex > 0) {
67
std::size_t pos = std::size_t(vertexIndex - 1);
68
if (pos < colors.size())
69
colors[pos] = elementColor;
74
void ReferenceHighlighter::getVertexColorsOfWire(const std::string& element, std::vector<App::Color>& colors) const
76
int idx = std::stoi(element.substr(4));
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());
84
if (vertexIndex > 0) {
85
std::size_t pos = std::size_t(vertexIndex - 1);
86
if (pos < colors.size())
87
colors[pos] = elementColor;
92
void ReferenceHighlighter::getVertexColorsOfFace(const std::string& element, std::vector<App::Color>& colors) const
94
int idx = std::stoi(element.substr(4));
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());
102
if (vertexIndex > 0) {
103
std::size_t pos = std::size_t(vertexIndex - 1);
104
if (pos < colors.size())
105
colors[pos] = elementColor;
110
void ReferenceHighlighter::getVertexColors(const std::vector<std::string>& elements,
111
std::vector<App::Color>& colors) const
113
colors.resize(vMap.Extent(), defaultColor);
115
if (!elements.empty()) {
116
for (const std::string& e : elements) {
117
if (boost::starts_with(e, "Vertex")) {
118
getVertexColor(e, colors);
120
else if (boost::starts_with(e, "Edge")) {
121
getVertexColorsOfEdge(e, colors);
123
else if (boost::starts_with(e, "Wire")) {
124
getVertexColorsOfWire(e, colors);
126
else if (boost::starts_with(e, "Face")) {
127
getVertexColorsOfFace(e, colors);
132
std::fill(colors.begin(), colors.end(), objectColor);
136
void ReferenceHighlighter::getEdgeColor(const std::string& element, std::vector<App::Color>& colors) const
138
int idx = std::stoi(element.substr(4)) - 1;
140
std::size_t pos = std::size_t(idx);
141
if (pos < colors.size())
142
colors[pos] = elementColor;
145
void ReferenceHighlighter::getEdgeColorsOfWire(const std::string& element, std::vector<App::Color>& colors) const
147
int idx = std::stoi(element.substr(4));
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());
156
std::size_t pos = std::size_t(edgeIndex - 1);
157
if (pos < colors.size())
158
colors[pos] = elementColor;
163
void ReferenceHighlighter::getEdgeColorsOfFace(const std::string& element, std::vector<App::Color>& colors) const
165
int idx = std::stoi(element.substr(4));
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());
174
std::size_t pos = std::size_t(edgeIndex - 1);
175
if (pos < colors.size())
176
colors[pos] = elementColor;
181
void ReferenceHighlighter::getEdgeColors(const std::vector<std::string>& elements,
182
std::vector<App::Color>& colors) const
184
colors.resize(eMap.Extent(), defaultColor);
186
if (!elements.empty()) {
187
for (const std::string& e : elements) {
188
if (boost::starts_with(e, "Edge")) {
189
getEdgeColor(e, colors);
191
else if (boost::starts_with(e, "Wire")) {
192
getEdgeColorsOfWire(e, colors);
194
else if (boost::starts_with(e, "Face")) {
195
getEdgeColorsOfFace(e, colors);
200
std::fill(colors.begin(), colors.end(), objectColor);
204
void ReferenceHighlighter::getFaceColor(const std::string& element, std::vector<App::Color>& colors) const
206
int idx = std::stoi(element.substr(4)) - 1;
208
std::size_t pos = std::size_t(idx);
209
if (pos < colors.size())
210
colors[pos] = elementColor;
213
void ReferenceHighlighter::getFaceColor(const std::string& element,
214
std::vector<App::Material>& materials) const
216
int idx = std::stoi(element.substr(4)) - 1;
218
std::size_t pos = std::size_t(idx);
219
if (pos < materials.size()) {
220
materials[pos].diffuseColor = elementColor;
224
void ReferenceHighlighter::getFaceColors(const std::vector<std::string>& elements,
225
std::vector<App::Color>& colors) const
227
colors.resize(fMap.Extent(), defaultColor);
229
if (!elements.empty()) {
230
for (const std::string& e : elements) {
231
if (boost::starts_with(e, "Face")) {
232
getFaceColor(e, colors);
237
std::fill(colors.begin(), colors.end(), objectColor);
241
void ReferenceHighlighter::getFaceMaterials(const std::vector<std::string>& elements,
242
std::vector<App::Material>& materials) const
244
App::Material defaultMaterial;
245
materials.resize(fMap.Extent(), defaultMaterial);
247
if (!elements.empty()) {
248
for (const std::string& e : elements) {
249
if (boost::starts_with(e, "Face")) {
250
getFaceColor(e, materials);
255
for (auto& material : materials) {
256
material.diffuseColor = objectColor;
258
// std::fill(materials.begin(), materials.end(), objectColor);