Skip to content

Commit

Permalink
Mesh: issue FreeCAD#6131: Colors set prior to saving aren't retained …
Browse files Browse the repository at this point in the history
…when re-opening file

Note: A mesh feature must have a PropertyColorList property with the name FaceColors or VertexColors. The colors won't be set when loading a project but in the context-menu there is the function 'Display colors'
  • Loading branch information
wwmayer authored and coreyoconnor committed Aug 5, 2022
1 parent eda5157 commit 2a8e818
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
64 changes: 58 additions & 6 deletions src/Mod/Mesh/Gui/ViewProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,14 +741,20 @@ void ViewProviderMesh::setupContextMenu(QMenu* menu, QObject* receiver, const ch
QAction* act = menu->addAction(QObject::tr("Display components"));
act->setCheckable(true);
act->setChecked(pcMatBinding->value.getValue() == SoMaterialBinding::PER_FACE &&
highlightMode == "Component");
highlightMode == HighlighMode::Component);
func->toggle(act, boost::bind(&ViewProviderMesh::setHighlightedComponents, this, bp::_1));

QAction* seg = menu->addAction(QObject::tr("Display segments"));
seg->setCheckable(true);
seg->setChecked(pcMatBinding->value.getValue() == SoMaterialBinding::PER_FACE &&
highlightMode == "Segment");
highlightMode == HighlighMode::Segment);
func->toggle(seg, boost::bind(&ViewProviderMesh::setHighlightedSegments, this, bp::_1));

QAction* col = menu->addAction(QObject::tr("Display colors"));
col->setVisible(canHighlightColors());
col->setCheckable(true);
col->setChecked(highlightMode == HighlighMode::Color);
func->toggle(col, boost::bind(&ViewProviderMesh::setHighlightedColors, this, bp::_1));
}

bool ViewProviderMesh::setEdit(int ModNum)
Expand Down Expand Up @@ -2136,11 +2142,11 @@ void ViewProviderMesh::unhighlightSelection()
void ViewProviderMesh::setHighlightedComponents(bool on)
{
if (on) {
highlightMode = "Component";
highlightMode = HighlighMode::Component;
highlightComponents();
}
else {
highlightMode.clear();
highlightMode = HighlighMode::None;
unhighlightSelection();
}
}
Expand Down Expand Up @@ -2171,11 +2177,11 @@ void ViewProviderMesh::highlightComponents()
void ViewProviderMesh::setHighlightedSegments(bool on)
{
if (on) {
highlightMode = "Segment";
highlightMode = HighlighMode::Segment;
highlightSegments();
}
else {
highlightMode.clear();
highlightMode = HighlighMode::None;
unhighlightSelection();
}
}
Expand Down Expand Up @@ -2227,6 +2233,52 @@ void ViewProviderMesh::highlightSegments(const std::vector<App::Color>& colors)
}
}

void ViewProviderMesh::setHighlightedColors(bool on)
{
if (on) {
highlightMode = HighlighMode::Color;
highlightColors();
}
else {
highlightMode = HighlighMode::None;
unhighlightSelection();
}
}

void ViewProviderMesh::highlightColors()
{
const Mesh::MeshObject& rMesh = static_cast<Mesh::Feature*>(pcObject)->Mesh.getValue();
{
App::PropertyColorList* prop = Base::freecad_dynamic_cast<App::PropertyColorList>(pcObject->getPropertyByName("FaceColors"));
if (prop && prop->getSize() == int(rMesh.countFacets())) {
setColorPerFace(prop);
}
}
{
App::PropertyColorList* prop = Base::freecad_dynamic_cast<App::PropertyColorList>(pcObject->getPropertyByName("VertexColors"));
if (prop && prop->getSize() == int(rMesh.countPoints())) {
setColorPerVertex(prop);
}
}
}

bool ViewProviderMesh::canHighlightColors() const
{
const Mesh::MeshObject& rMesh = static_cast<Mesh::Feature*>(pcObject)->Mesh.getValue();
{
App::PropertyColorList* prop = Base::freecad_dynamic_cast<App::PropertyColorList>(pcObject->getPropertyByName("FaceColors"));
if (prop && prop->getSize() == int(rMesh.countFacets()))
return true;
}
{
App::PropertyColorList* prop = Base::freecad_dynamic_cast<App::PropertyColorList>(pcObject->getPropertyByName("VertexColors"));
if (prop && prop->getSize() == int(rMesh.countPoints()))
return true;
}

return false;
}

PyObject* ViewProviderMesh::getPyObject()
{
if (!pyViewObject)
Expand Down
11 changes: 10 additions & 1 deletion src/Mod/Mesh/Gui/ViewProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ class MeshGuiExport ViewProviderMesh : public Gui::ViewProviderGeometryObject
void setHighlightedComponents(bool);
void highlightSegments();
void setHighlightedSegments(bool);
void setHighlightedColors(bool);
void highlightColors();
bool canHighlightColors() const;
App::PropertyColorList* getColorProperty() const;
void tryColorPerVertexOrFace(bool);
void setColorPerVertex(const App::PropertyColorList*);
Expand Down Expand Up @@ -218,7 +221,13 @@ class MeshGuiExport ViewProviderMesh : public Gui::ViewProviderGeometryObject
static void panCamera(SoCamera*, float, const SbPlane&, const SbVec2f&, const SbVec2f&);

protected:
std::string highlightMode;
enum class HighlighMode {
None,
Component,
Segment,
Color
};
HighlighMode highlightMode;
Gui::SoFCSelection * pcHighlight;
SoGroup * pcShapeGroup;
SoDrawStyle * pcLineStyle;
Expand Down

0 comments on commit 2a8e818

Please sign in to comment.