Skip to content

Commit 11fec1f

Browse files
committed
Fem: Improve post-processing object style - fixes FreeCAD#13017
1 parent 41de357 commit 11fec1f

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

src/Mod/Fem/Gui/ViewProviderFemPostObject.cpp

+75-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#ifndef _PreComp_
2626
#include <Inventor/nodes/SoCoordinate3.h>
27+
#include <Inventor/nodes/SoDepthBuffer.h>
2728
#include <Inventor/nodes/SoDrawStyle.h>
2829
#include <Inventor/nodes/SoIndexedFaceSet.h>
2930
#include <Inventor/nodes/SoIndexedLineSet.h>
@@ -33,7 +34,7 @@
3334
#include <Inventor/nodes/SoNormal.h>
3435
#include <Inventor/nodes/SoSeparator.h>
3536
#include <Inventor/nodes/SoShapeHints.h>
36-
#include <Inventor/nodes/SoDepthBuffer.h>
37+
#include <Inventor/nodes/SoSwitch.h>
3738
#include <Inventor/nodes/SoTransparencyType.h>
3839
#include <functional>
3940

@@ -141,6 +142,8 @@ class FemPostObjectSelectionObserver
141142

142143
// ----------------------------------------------------------------------------
143144

145+
App::PropertyFloatConstraint::Constraints ViewProviderFemPostObject::sizeRange = {1.0, 64.0, 1.0};
146+
144147
PROPERTY_SOURCE(FemGui::ViewProviderFemPostObject, Gui::ViewProviderDocumentObject)
145148

146149
ViewProviderFemPostObject::ViewProviderFemPostObject()
@@ -156,7 +159,27 @@ ViewProviderFemPostObject::ViewProviderFemPostObject()
156159
"Coloring",
157160
App::Prop_None,
158161
"Select what to show for a vector field");
159-
ADD_PROPERTY(Transparency, (0));
162+
ADD_PROPERTY_TYPE(Transparency,
163+
(0),
164+
"Object Style",
165+
App::Prop_None,
166+
"Set object transparency.");
167+
ADD_PROPERTY_TYPE(EdgeColor,
168+
(0.0f, 0.0f, 0.0f),
169+
"Object Style",
170+
App::Prop_None,
171+
"Set wireframe line color.");
172+
ADD_PROPERTY_TYPE(PlainColorEdgeOnSurface,
173+
(false),
174+
"Object Style",
175+
App::Prop_None,
176+
"Use plain color for edges on surface.");
177+
ADD_PROPERTY_TYPE(LineWidth, (2), "Object Style", App::Prop_None, "Set wireframe line width.");
178+
ADD_PROPERTY_TYPE(PointSize, (3), "Object Style", App::Prop_None, "Set node point size.");
179+
180+
181+
LineWidth.setConstraints(&sizeRange);
182+
PointSize.setConstraints(&sizeRange);
160183

161184
sPixmap = "fem-femmesh-from-shape";
162185

@@ -174,8 +197,12 @@ ViewProviderFemPostObject::ViewProviderFemPostObject()
174197
m_coordinates->ref();
175198
m_materialBinding = new SoMaterialBinding();
176199
m_materialBinding->ref();
200+
m_switchMatEdges = new SoSwitch();
201+
m_switchMatEdges->ref();
177202
m_material = new SoMaterial();
178203
m_material->ref();
204+
m_matPlainEdges = new SoMaterial();
205+
m_matPlainEdges->ref();
179206
m_normalBinding = new SoNormalBinding();
180207
m_normalBinding->ref();
181208
m_normals = new SoNormal();
@@ -190,8 +217,8 @@ ViewProviderFemPostObject::ViewProviderFemPostObject()
190217
m_lines->ref();
191218
m_drawStyle = new SoDrawStyle();
192219
m_drawStyle->ref();
193-
m_drawStyle->lineWidth.setValue(2);
194-
m_drawStyle->pointSize.setValue(3);
220+
m_drawStyle->lineWidth.setValue(LineWidth.getValue());
221+
m_drawStyle->pointSize.setValue(PointSize.getValue());
195222
m_sepMarkerLine = new SoSeparator();
196223
m_sepMarkerLine->ref();
197224
m_separator = new SoSeparator();
@@ -245,6 +272,8 @@ ViewProviderFemPostObject::~ViewProviderFemPostObject()
245272
m_sepMarkerLine->unref();
246273
m_separator->unref();
247274
m_material->unref();
275+
m_matPlainEdges->unref();
276+
m_switchMatEdges->unref();
248277
m_colorBar->Detach(this);
249278
m_colorBar->unref();
250279
m_colorStyle->unref();
@@ -255,12 +284,14 @@ void ViewProviderFemPostObject::attach(App::DocumentObject* pcObj)
255284
{
256285
ViewProviderDocumentObject::attach(pcObj);
257286

287+
m_switchMatEdges->addChild(m_material);
288+
m_switchMatEdges->addChild(m_matPlainEdges);
258289
// marker and line nodes
259290
m_sepMarkerLine->addChild(m_transpType);
260291
m_sepMarkerLine->addChild(m_depthBuffer);
261292
m_sepMarkerLine->addChild(m_drawStyle);
262293
m_sepMarkerLine->addChild(m_materialBinding);
263-
m_sepMarkerLine->addChild(m_material);
294+
m_sepMarkerLine->addChild(m_switchMatEdges);
264295
m_sepMarkerLine->addChild(m_coordinates);
265296
m_sepMarkerLine->addChild(m_markers);
266297
m_sepMarkerLine->addChild(m_lines);
@@ -638,14 +669,21 @@ void ViewProviderFemPostObject::WriteColorData(bool ResetColorBarRange)
638669
setRangeOfColorBar(range[0], range[1]);
639670
}
640671

641-
m_material->diffuseColor.setNum(pd->GetNumberOfPoints());
672+
vtkIdType numPts = pd->GetNumberOfPoints();
673+
m_material->diffuseColor.setNum(numPts);
674+
m_matPlainEdges->diffuseColor.setNum(numPts);
642675
SbColor* diffcol = m_material->diffuseColor.startEditing();
676+
SbColor* edgeDiffcol = m_matPlainEdges->diffuseColor.startEditing();
643677

644678
float overallTransp = Transparency.getValue() / 100.0f;
645-
m_material->transparency.setNum(pd->GetNumberOfPoints());
679+
m_material->transparency.setNum(numPts);
680+
m_matPlainEdges->transparency.setNum(numPts);
646681
float* transp = m_material->transparency.startEditing();
682+
float* edgeTransp = m_matPlainEdges->transparency.startEditing();
647683

648-
for (int i = 0; i < pd->GetNumberOfPoints(); i++) {
684+
App::Color c;
685+
App::Color cEdge = EdgeColor.getValue();
686+
for (int i = 0; i < numPts; i++) {
649687

650688
double value = 0;
651689
if (component >= 0) {
@@ -659,13 +697,17 @@ void ViewProviderFemPostObject::WriteColorData(bool ResetColorBarRange)
659697
value = std::sqrt(value);
660698
}
661699

662-
App::Color c = m_colorBar->getColor(value);
700+
c = m_colorBar->getColor(value);
663701
diffcol[i].setValue(c.r, c.g, c.b);
664702
transp[i] = std::max(c.a, overallTransp);
703+
edgeDiffcol[i].setValue(cEdge.r, cEdge.g, cEdge.b);
704+
edgeTransp[i] = std::max(cEdge.a, overallTransp);
665705
}
666706

667707
m_material->diffuseColor.finishEditing();
668708
m_material->transparency.finishEditing();
709+
m_matPlainEdges->diffuseColor.finishEditing();
710+
m_matPlainEdges->transparency.finishEditing();
669711
m_materialBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED;
670712

671713
// In order to apply the transparency changes the shape nodes must be touched
@@ -677,10 +719,14 @@ void ViewProviderFemPostObject::WriteTransparency()
677719
{
678720
float trans = static_cast<float>(Transparency.getValue()) / 100.0;
679721
float* value = m_material->transparency.startEditing();
722+
float* edgeValue = m_matPlainEdges->transparency.startEditing();
723+
// m_material and m_matPlainEdges field containers have same size
680724
for (int i = 0; i < m_material->transparency.getNum(); ++i) {
681725
value[i] = trans;
726+
edgeValue[i] = trans;
682727
}
683728
m_material->transparency.finishEditing();
729+
m_matPlainEdges->transparency.finishEditing();
684730

685731
if (Transparency.getValue() > 99) {
686732
m_depthBuffer->test.setValue(false);
@@ -853,6 +899,26 @@ void ViewProviderFemPostObject::onChanged(const App::Property* prop)
853899
else if (prop == &Transparency) {
854900
WriteTransparency();
855901
}
902+
else if (prop == &LineWidth) {
903+
m_drawStyle->lineWidth.setValue(LineWidth.getValue());
904+
}
905+
else if (prop == &PointSize) {
906+
m_drawStyle->pointSize.setValue(PointSize.getValue());
907+
}
908+
else if (prop == &EdgeColor && setupPipeline()) {
909+
App::Color c = EdgeColor.getValue();
910+
SbColor* edgeColor = m_matPlainEdges->diffuseColor.startEditing();
911+
for (int i = 0; i < m_matPlainEdges->diffuseColor.getNum(); ++i) {
912+
edgeColor[i].setValue(c.r, c.g, c.b);
913+
}
914+
m_matPlainEdges->diffuseColor.finishEditing();
915+
}
916+
else if (prop == &PlainColorEdgeOnSurface || prop == &DisplayMode) {
917+
bool plainColor = PlainColorEdgeOnSurface.getValue()
918+
&& (strcmp("Surface with Edges", DisplayMode.getValueAsString()) == 0);
919+
int child = plainColor ? 1 : 0;
920+
m_switchMatEdges->whichChild.setValue(child);
921+
}
856922

857923
ViewProviderDocumentObject::onChanged(prop);
858924
}

src/Mod/Fem/Gui/ViewProviderFemPostObject.h

+9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class SoIndexedLineSet;
5555
class SoIndexedTriangleStripSet;
5656
class SoTransparencyType;
5757
class SoDepthBuffer;
58+
class SoSwitch;
5859

5960
namespace Gui
6061
{
@@ -82,6 +83,10 @@ class FemGuiExport ViewProviderFemPostObject: public Gui::ViewProviderDocumentOb
8283
App::PropertyEnumeration Field;
8384
App::PropertyEnumeration VectorMode;
8485
App::PropertyPercent Transparency;
86+
App::PropertyBool PlainColorEdgeOnSurface;
87+
App::PropertyColor EdgeColor;
88+
App::PropertyFloatConstraint LineWidth;
89+
App::PropertyFloatConstraint PointSize;
8590

8691
void attach(App::DocumentObject* pcObject) override;
8792
void setDisplayMode(const char* ModeName) override;
@@ -135,7 +140,9 @@ class FemGuiExport ViewProviderFemPostObject: public Gui::ViewProviderDocumentOb
135140
SoIndexedLineSet* m_lines;
136141
SoIndexedFaceSet* m_faces;
137142
SoIndexedTriangleStripSet* m_triangleStrips;
143+
SoSwitch* m_switchMatEdges;
138144
SoMaterial* m_material;
145+
SoMaterial* m_matPlainEdges;
139146
SoMaterialBinding* m_materialBinding;
140147
SoShapeHints* m_shapeHints;
141148
SoNormalBinding* m_normalBinding;
@@ -167,6 +174,8 @@ class FemGuiExport ViewProviderFemPostObject: public Gui::ViewProviderDocumentOb
167174

168175
App::Enumeration m_coloringEnum, m_vectorEnum;
169176
bool m_blockPropertyChanges {false};
177+
178+
static App::PropertyFloatConstraint::Constraints sizeRange;
170179
};
171180

172181
} // namespace FemGui

0 commit comments

Comments
 (0)