/
ViewProviderExt.cpp
1183 lines (1040 loc) · 46.9 KB
/
ViewProviderExt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2011 Juergen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <sstream>
# include <Bnd_Box.hxx>
# include <Poly_Polygon3D.hxx>
# include <BRepBndLib.hxx>
# include <BRepBuilderAPI_MakeVertex.hxx>
# include <BRepExtrema_DistShapeShape.hxx>
# include <BRepMesh_IncrementalMesh.hxx>
# include <BRep_Tool.hxx>
# include <BRepTools.hxx>
# include <BRepAdaptor_Curve.hxx>
# include <BRepAdaptor_Surface.hxx>
# include <GeomLib.hxx>
# include <GeomAbs_CurveType.hxx>
# include <GeomAbs_SurfaceType.hxx>
# include <Geom_BezierCurve.hxx>
# include <Geom_BSplineCurve.hxx>
# include <Geom_BezierSurface.hxx>
# include <Geom_BSplineSurface.hxx>
# include <GeomAPI_ProjectPointOnSurf.hxx>
# include <GeomLProp_SLProps.hxx>
# include <gp_Trsf.hxx>
# include <Poly_Array1OfTriangle.hxx>
# include <Poly_Triangulation.hxx>
# include <Poly_Connect.hxx>
# include <Standard_Version.hxx>
# include <TColgp_Array1OfPnt.hxx>
# include <TopoDS.hxx>
# include <TopoDS_Edge.hxx>
# include <TopoDS_Wire.hxx>
# include <TopoDS_Face.hxx>
# include <TopoDS_Shape.hxx>
# include <TopoDS_Vertex.hxx>
# include <TopoDS_Iterator.hxx>
# include <TopExp_Explorer.hxx>
# include <TopExp.hxx>
# include <TopTools_IndexedMapOfShape.hxx>
# include <Poly_PolygonOnTriangulation.hxx>
# include <TColStd_Array1OfInteger.hxx>
# include <TColgp_Array1OfDir.hxx>
# include <TColgp_Array1OfPnt2d.hxx>
# include <TopTools_ListOfShape.hxx>
# include <TShort_Array1OfShortReal.hxx>
# include <TShort_HArray1OfShortReal.hxx>
# include <Precision.hxx>
# include <Inventor/SoPickedPoint.h>
# include <Inventor/details/SoFaceDetail.h>
# include <Inventor/details/SoLineDetail.h>
# include <Inventor/details/SoPointDetail.h>
# include <Inventor/events/SoMouseButtonEvent.h>
# include <Inventor/nodes/SoBaseColor.h>
# include <Inventor/nodes/SoCoordinate3.h>
# include <Inventor/nodes/SoDrawStyle.h>
# include <Inventor/nodes/SoIndexedFaceSet.h>
# include <Inventor/nodes/SoIndexedLineSet.h>
# include <Inventor/nodes/SoLocateHighlight.h>
# include <Inventor/nodes/SoMaterial.h>
# include <Inventor/nodes/SoMaterialBinding.h>
# include <Inventor/nodes/SoNormal.h>
# include <Inventor/nodes/SoNormalBinding.h>
# include <Inventor/nodes/SoPointSet.h>
# include <Inventor/nodes/SoPolygonOffset.h>
# include <Inventor/nodes/SoShapeHints.h>
# include <Inventor/nodes/SoSwitch.h>
# include <Inventor/nodes/SoGroup.h>
# include <Inventor/nodes/SoSphere.h>
# include <Inventor/nodes/SoScale.h>
# include <Inventor/nodes/SoLightModel.h>
# include <QAction>
# include <QMenu>
#endif
/// Here the FreeCAD includes sorted by Base,App,Gui......
#include <Base/Console.h>
#include <Base/Parameter.h>
#include <Base/Exception.h>
#include <Base/TimeInfo.h>
#include <App/Application.h>
#include <App/Document.h>
#include <Gui/SoFCUnifiedSelection.h>
#include <Gui/Selection.h>
#include <Gui/View3DInventorViewer.h>
#include <Gui/Utilities.h>
#include <Gui/Control.h>
#include "ViewProviderExt.h"
#include "SoBrepPointSet.h"
#include "SoBrepEdgeSet.h"
#include "SoBrepFaceSet.h"
#include "TaskFaceColors.h"
#include <Mod/Part/App/PartFeature.h>
#include <Mod/Part/App/PrimitiveFeature.h>
using namespace PartGui;
PROPERTY_SOURCE(PartGui::ViewProviderPartExt, Gui::ViewProviderGeometryObject)
void ViewProviderPartExt::GetNormals(const TopoDS_Face& theFace,
const Handle(Poly_Triangulation)& aPolyTri,
TColgp_Array1OfDir& theNormals)
{
Poly_Connect thePolyConnect(aPolyTri);
const TColgp_Array1OfPnt& aNodes = aPolyTri->Nodes();
if(aPolyTri->HasNormals())
{
// normals pre-computed in triangulation structure
const TShort_Array1OfShortReal& aNormals = aPolyTri->Normals();
const Standard_ShortReal* aNormArr = &(aNormals.Value(aNormals.Lower()));
for(Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
{
const Standard_Integer anId = 3 * (aNodeIter - aNodes.Lower());
const gp_Dir aNorm(aNormArr[anId + 0],
aNormArr[anId + 1],
aNormArr[anId + 2]);
theNormals(aNodeIter) = aNorm;
}
if(theFace.Orientation() == TopAbs_REVERSED)
{
for(Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
{
theNormals.ChangeValue(aNodeIter).Reverse();
}
}
return;
}
// take in face the surface location
const TopoDS_Face aZeroFace = TopoDS::Face(theFace.Located(TopLoc_Location()));
Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aZeroFace);
const Standard_Real aTol = Precision::Confusion();
Handle(TShort_HArray1OfShortReal) aNormals = new TShort_HArray1OfShortReal(1, aPolyTri->NbNodes() * 3);
const Poly_Array1OfTriangle& aTriangles = aPolyTri->Triangles();
const TColgp_Array1OfPnt2d* aNodesUV = aPolyTri->HasUVNodes() && !aSurf.IsNull()
? &aPolyTri->UVNodes()
: NULL;
Standard_Integer aTri[3];
for(Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
{
// try to retrieve normal from real surface first, when UV coordinates are available
if(aNodesUV == NULL
|| GeomLib::NormEstim(aSurf, aNodesUV->Value(aNodeIter), aTol, theNormals(aNodeIter)) > 1)
{
// compute flat normals
gp_XYZ eqPlan(0.0, 0.0, 0.0);
for(thePolyConnect.Initialize(aNodeIter); thePolyConnect.More(); thePolyConnect.Next())
{
aTriangles(thePolyConnect.Value()).Get(aTri[0], aTri[1], aTri[2]);
const gp_XYZ v1(aNodes(aTri[1]).Coord() - aNodes(aTri[0]).Coord());
const gp_XYZ v2(aNodes(aTri[2]).Coord() - aNodes(aTri[1]).Coord());
const gp_XYZ vv = v1 ^ v2;
const Standard_Real aMod = vv.Modulus();
if(aMod >= aTol)
{
eqPlan += vv / aMod;
}
}
const Standard_Real aModMax = eqPlan.Modulus();
theNormals(aNodeIter) = (aModMax > aTol) ? gp_Dir(eqPlan) : gp::DZ();
}
const Standard_Integer anId = (aNodeIter - aNodes.Lower()) * 3;
aNormals->SetValue(anId + 1, (Standard_ShortReal)theNormals(aNodeIter).X());
aNormals->SetValue(anId + 2, (Standard_ShortReal)theNormals(aNodeIter).Y());
aNormals->SetValue(anId + 3, (Standard_ShortReal)theNormals(aNodeIter).Z());
}
aPolyTri->SetNormals(aNormals);
if(theFace.Orientation() == TopAbs_REVERSED)
{
for(Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
{
theNormals.ChangeValue(aNodeIter).Reverse();
}
}
}
//**************************************************************************
// Construction/Destruction
App::PropertyFloatConstraint::Constraints ViewProviderPartExt::sizeRange = {1.0,64.0,1.0};
App::PropertyFloatConstraint::Constraints ViewProviderPartExt::tessRange = {0.0001,100.0,0.01};
App::PropertyQuantityConstraint::Constraints ViewProviderPartExt::angDeflectionRange = {0.0,180.0,0.05};
const char* ViewProviderPartExt::LightingEnums[]= {"One side","Two side",NULL};
const char* ViewProviderPartExt::DrawStyleEnums[]= {"Solid","Dashed","Dotted","Dashdot",NULL};
ViewProviderPartExt::ViewProviderPartExt()
{
VisualTouched = true;
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
unsigned long lcol = hGrp->GetUnsigned("DefaultShapeLineColor",421075455UL); // dark grey (25,25,25)
float r,g,b;
r = ((lcol >> 24) & 0xff) / 255.0; g = ((lcol >> 16) & 0xff) / 255.0; b = ((lcol >> 8) & 0xff) / 255.0;
int lwidth = hGrp->GetInt("DefaultShapeLineWidth",2);
App::Material mat;
mat.ambientColor.set(0.2f,0.2f,0.2f);
mat.diffuseColor.set(r,g,b);
mat.specularColor.set(0.0f,0.0f,0.0f);
mat.emissiveColor.set(0.0f,0.0f,0.0f);
mat.shininess = 1.0f;
mat.transparency = 0.0f;
ADD_PROPERTY(LineMaterial,(mat));
ADD_PROPERTY(PointMaterial,(mat));
ADD_PROPERTY(LineColor,(mat.diffuseColor));
ADD_PROPERTY(PointColor,(mat.diffuseColor));
ADD_PROPERTY(DiffuseColor,(ShapeColor.getValue()));
ADD_PROPERTY(LineColorArray,(LineColor.getValue()));
ADD_PROPERTY(LineWidth,(lwidth));
LineWidth.setConstraints(&sizeRange);
PointSize.setConstraints(&sizeRange);
ADD_PROPERTY(PointSize,(lwidth));
ADD_PROPERTY(Deviation,(0.5f));
Deviation.setConstraints(&tessRange);
ADD_PROPERTY(AngularDeflection,(28.65));
AngularDeflection.setConstraints(&angDeflectionRange);
ADD_PROPERTY(Lighting,(1));
Lighting.setEnums(LightingEnums);
ADD_PROPERTY(DrawStyle,((long int)0));
DrawStyle.setEnums(DrawStyleEnums);
coords = new SoCoordinate3();
coords->ref();
faceset = new SoBrepFaceSet();
faceset->ref();
norm = new SoNormal;
norm->ref();
normb = new SoNormalBinding;
normb->value = SoNormalBinding::PER_VERTEX_INDEXED;
normb->ref();
lineset = new SoBrepEdgeSet();
lineset->ref();
nodeset = new SoBrepPointSet();
nodeset->ref();
pcShapeBind = new SoMaterialBinding();
pcShapeBind->ref();
pcLineBind = new SoMaterialBinding();
pcLineBind->ref();
pcLineMaterial = new SoMaterial;
pcLineMaterial->ref();
LineMaterial.touch();
pcPointMaterial = new SoMaterial;
pcPointMaterial->ref();
PointMaterial.touch();
pcLineStyle = new SoDrawStyle();
pcLineStyle->ref();
pcLineStyle->style = SoDrawStyle::LINES;
pcLineStyle->lineWidth = LineWidth.getValue();
pcPointStyle = new SoDrawStyle();
pcPointStyle->ref();
pcPointStyle->style = SoDrawStyle::POINTS;
pcPointStyle->pointSize = PointSize.getValue();
pShapeHints = new SoShapeHints;
pShapeHints->shapeType = SoShapeHints::UNKNOWN_SHAPE_TYPE;
pShapeHints->ref();
Lighting.touch();
DrawStyle.touch();
sPixmap = "Tree_Part";
loadParameter();
}
ViewProviderPartExt::~ViewProviderPartExt()
{
pcShapeBind->unref();
pcLineBind->unref();
pcLineMaterial->unref();
pcPointMaterial->unref();
pcLineStyle->unref();
pcPointStyle->unref();
pShapeHints->unref();
coords->unref();
faceset->unref();
norm->unref();
normb->unref();
lineset->unref();
nodeset->unref();
}
void ViewProviderPartExt::onChanged(const App::Property* prop)
{
Part::Feature* feature = dynamic_cast<Part::Feature*>(pcObject);
if (prop == &Deviation) {
if(Visibility.getValue() && feature && !feature->Shape.getValue().IsNull())
updateVisual(feature->Shape.getValue());
else
VisualTouched = true;
}
if (prop == &AngularDeflection) {
if(Visibility.getValue() && feature && !feature->Shape.getValue().IsNull())
updateVisual(feature->Shape.getValue());
else
VisualTouched = true;
}
if (prop == &LineWidth) {
pcLineStyle->lineWidth = LineWidth.getValue();
}
else if (prop == &PointSize) {
pcPointStyle->pointSize = PointSize.getValue();
}
else if (prop == &LineColor) {
const App::Color& c = LineColor.getValue();
pcLineMaterial->diffuseColor.setValue(c.r,c.g,c.b);
if (c != LineMaterial.getValue().diffuseColor)
LineMaterial.setDiffuseColor(c);
LineColorArray.setValue(LineColor.getValue());
}
else if (prop == &PointColor) {
const App::Color& c = PointColor.getValue();
pcPointMaterial->diffuseColor.setValue(c.r,c.g,c.b);
if (c != PointMaterial.getValue().diffuseColor)
PointMaterial.setDiffuseColor(c);
PointColorArray.setValue(PointColor.getValue());
}
else if (prop == &LineMaterial) {
const App::Material& Mat = LineMaterial.getValue();
if (LineColor.getValue() != Mat.diffuseColor)
LineColor.setValue(Mat.diffuseColor);
pcLineMaterial->ambientColor.setValue(Mat.ambientColor.r,Mat.ambientColor.g,Mat.ambientColor.b);
pcLineMaterial->diffuseColor.setValue(Mat.diffuseColor.r,Mat.diffuseColor.g,Mat.diffuseColor.b);
pcLineMaterial->specularColor.setValue(Mat.specularColor.r,Mat.specularColor.g,Mat.specularColor.b);
pcLineMaterial->emissiveColor.setValue(Mat.emissiveColor.r,Mat.emissiveColor.g,Mat.emissiveColor.b);
pcLineMaterial->shininess.setValue(Mat.shininess);
pcLineMaterial->transparency.setValue(Mat.transparency);
}
else if (prop == &PointMaterial) {
const App::Material& Mat = PointMaterial.getValue();
if (PointColor.getValue() != Mat.diffuseColor)
PointColor.setValue(Mat.diffuseColor);
pcPointMaterial->ambientColor.setValue(Mat.ambientColor.r,Mat.ambientColor.g,Mat.ambientColor.b);
pcPointMaterial->diffuseColor.setValue(Mat.diffuseColor.r,Mat.diffuseColor.g,Mat.diffuseColor.b);
pcPointMaterial->specularColor.setValue(Mat.specularColor.r,Mat.specularColor.g,Mat.specularColor.b);
pcPointMaterial->emissiveColor.setValue(Mat.emissiveColor.r,Mat.emissiveColor.g,Mat.emissiveColor.b);
pcPointMaterial->shininess.setValue(Mat.shininess);
pcPointMaterial->transparency.setValue(Mat.transparency);
}
else if (prop == &PointColorArray) {
setHighlightedPoints(PointColorArray.getValues());
}
else if (prop == &LineColorArray) {
setHighlightedEdges(LineColorArray.getValues());
}
else if (prop == &DiffuseColor) {
setHighlightedFaces(DiffuseColor.getValues());
}
else if (prop == &ShapeMaterial || prop == &ShapeColor) {
pcShapeBind->value = SoMaterialBinding::OVERALL;
ViewProviderGeometryObject::onChanged(prop);
DiffuseColor.setValue(ShapeColor.getValue());
}
else if (prop == &Transparency) {
const App::Material& Mat = ShapeMaterial.getValue();
long value = (long)(100*Mat.transparency);
if (value != Transparency.getValue()) {
float trans = Transparency.getValue()/100.0f;
if (pcShapeBind->value.getValue() == SoMaterialBinding::PER_PART) {
int cnt = pcShapeMaterial->diffuseColor.getNum();
pcShapeMaterial->transparency.setNum(cnt);
float *t = pcShapeMaterial->transparency.startEditing();
for (int i=0; i<cnt; i++)
t[i] = trans;
pcShapeMaterial->transparency.finishEditing();
}
else {
pcShapeMaterial->transparency = trans;
}
App::PropertyContainer* parent = ShapeMaterial.getContainer();
ShapeMaterial.setContainer(0);
ShapeMaterial.setTransparency(trans);
ShapeMaterial.setContainer(parent);
}
}
else if (prop == &Lighting) {
if (Lighting.getValue() == 0)
pShapeHints->vertexOrdering = SoShapeHints::UNKNOWN_ORDERING;
else
pShapeHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
}
else if (prop == &DrawStyle) {
if (DrawStyle.getValue() == 0)
pcLineStyle->linePattern = 0xffff;
else if (DrawStyle.getValue() == 1)
pcLineStyle->linePattern = 0xf00f;
else if (DrawStyle.getValue() == 2)
pcLineStyle->linePattern = 0x0f0f;
else
pcLineStyle->linePattern = 0xff88;
}
else {
// if the object was invisible and has been changed, recreate the visual
if (prop == &Visibility && Visibility.getValue() && VisualTouched) {
updateVisual(feature->Shape.getValue());
// The material has to be checked again (#0001736)
onChanged(&DiffuseColor);
}
}
ViewProviderGeometryObject::onChanged(prop);
}
void ViewProviderPartExt::attach(App::DocumentObject *pcFeat)
{
// call parent attach method
ViewProviderGeometryObject::attach(pcFeat);
// Workaround for #0000433, i.e. use SoSeparator instead of SoGroup
SoGroup* pcNormalRoot = new SoSeparator();
SoGroup* pcFlatRoot = new SoSeparator();
SoGroup* pcWireframeRoot = new SoSeparator();
SoGroup* pcPointsRoot = new SoSeparator();
// enable two-side rendering
pShapeHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
pShapeHints->shapeType = SoShapeHints::UNKNOWN_SHAPE_TYPE;
// Avoid any Z-buffer artefacts, so that the lines always appear on top of the faces
// The correct order is Edges, Polygon offset, Faces.
SoPolygonOffset* offset = new SoPolygonOffset();
// wireframe node
SoSeparator* wireframe = new SoSeparator();
wireframe->addChild(pcLineBind);
wireframe->addChild(pcLineMaterial);
wireframe->addChild(pcLineStyle);
wireframe->addChild(lineset);
// normal viewing with edges and points
pcNormalRoot->addChild(wireframe);
pcNormalRoot->addChild(offset);
pcNormalRoot->addChild(pcFlatRoot);
pcNormalRoot->addChild(pcPointsRoot);
// just faces with no edges or points
pcFlatRoot->addChild(pShapeHints);
pcFlatRoot->addChild(pcShapeBind);
pcFlatRoot->addChild(pcShapeMaterial);
SoDrawStyle* pcFaceStyle = new SoDrawStyle();
pcFaceStyle->style = SoDrawStyle::FILLED;
pcFlatRoot->addChild(pcFaceStyle);
pcFlatRoot->addChild(norm);
pcFlatRoot->addChild(normb);
pcFlatRoot->addChild(faceset);
// edges and points
pcWireframeRoot->addChild(wireframe);
pcWireframeRoot->addChild(pcPointsRoot);
// normal viewing with edges and points
pcPointsRoot->addChild(pcPointMaterial);
pcPointsRoot->addChild(pcPointStyle);
pcPointsRoot->addChild(nodeset);
// Move 'coords' before the switch
pcRoot->insertChild(coords,pcRoot->findChild(pcModeSwitch));
// putting all together with the switch
addDisplayMaskMode(pcNormalRoot, "Flat Lines");
addDisplayMaskMode(pcFlatRoot, "Shaded");
addDisplayMaskMode(pcWireframeRoot, "Wireframe");
addDisplayMaskMode(pcPointsRoot, "Point");
}
void ViewProviderPartExt::setDisplayMode(const char* ModeName)
{
if ( strcmp("Flat Lines",ModeName)==0 )
setDisplayMaskMode("Flat Lines");
else if ( strcmp("Shaded",ModeName)==0 )
setDisplayMaskMode("Shaded");
else if ( strcmp("Wireframe",ModeName)==0 )
setDisplayMaskMode("Wireframe");
else if ( strcmp("Points",ModeName)==0 )
setDisplayMaskMode("Point");
ViewProviderGeometryObject::setDisplayMode( ModeName );
}
std::vector<std::string> ViewProviderPartExt::getDisplayModes(void) const
{
// get the modes of the father
std::vector<std::string> StrList = ViewProviderGeometryObject::getDisplayModes();
// add your own modes
StrList.push_back("Flat Lines");
StrList.push_back("Shaded");
StrList.push_back("Wireframe");
StrList.push_back("Points");
return StrList;
}
std::string ViewProviderPartExt::getElement(const SoDetail* detail) const
{
std::stringstream str;
if (detail) {
if (detail->getTypeId() == SoFaceDetail::getClassTypeId()) {
const SoFaceDetail* face_detail = static_cast<const SoFaceDetail*>(detail);
int face = face_detail->getPartIndex() + 1;
str << "Face" << face;
}
else if (detail->getTypeId() == SoLineDetail::getClassTypeId()) {
const SoLineDetail* line_detail = static_cast<const SoLineDetail*>(detail);
int edge = line_detail->getLineIndex() + 1;
str << "Edge" << edge;
}
else if (detail->getTypeId() == SoPointDetail::getClassTypeId()) {
const SoPointDetail* point_detail = static_cast<const SoPointDetail*>(detail);
int vertex = point_detail->getCoordinateIndex() - nodeset->startIndex.getValue() + 1;
str << "Vertex" << vertex;
}
}
return str.str();
}
SoDetail* ViewProviderPartExt::getDetail(const char* subelement) const
{
std::string element = subelement;
std::string::size_type pos = element.find_first_of("0123456789");
int index = -1;
if (pos != std::string::npos) {
index = std::atoi(element.substr(pos).c_str());
element = element.substr(0,pos);
}
SoDetail* detail = 0;
if (index < 0)
return detail;
if (element == "Face") {
detail = new SoFaceDetail();
static_cast<SoFaceDetail*>(detail)->setPartIndex(index - 1);
}
else if (element == "Edge") {
detail = new SoLineDetail();
static_cast<SoLineDetail*>(detail)->setLineIndex(index - 1);
}
else if (element == "Vertex") {
detail = new SoPointDetail();
static_cast<SoPointDetail*>(detail)->setCoordinateIndex(index + nodeset->startIndex.getValue() - 1);
}
return detail;
}
std::vector<Base::Vector3d> ViewProviderPartExt::getModelPoints(const SoPickedPoint* pp) const
{
try {
std::vector<Base::Vector3d> pts;
std::string element = this->getElement(pp->getDetail());
const Part::TopoShape& shape = static_cast<Part::Feature*>(getObject())->Shape.getShape();
TopoDS_Shape subShape = shape.getSubShape(element.c_str());
// get the point of the vertex directly
if (subShape.ShapeType() == TopAbs_VERTEX) {
const TopoDS_Vertex& v = TopoDS::Vertex(subShape);
gp_Pnt p = BRep_Tool::Pnt(v);
pts.push_back(Base::Vector3d(p.X(),p.Y(),p.Z()));
}
// get the nearest point on the edge
else if (subShape.ShapeType() == TopAbs_EDGE) {
const SbVec3f& vec = pp->getPoint();
BRepBuilderAPI_MakeVertex mkVert(gp_Pnt(vec[0],vec[1],vec[2]));
BRepExtrema_DistShapeShape distSS(subShape, mkVert.Vertex(), 0.1);
if (distSS.NbSolution() > 0) {
gp_Pnt p = distSS.PointOnShape1(1);
pts.push_back(Base::Vector3d(p.X(),p.Y(),p.Z()));
}
}
// get the nearest point on the face
else if (subShape.ShapeType() == TopAbs_FACE) {
const SbVec3f& vec = pp->getPoint();
BRepBuilderAPI_MakeVertex mkVert(gp_Pnt(vec[0],vec[1],vec[2]));
BRepExtrema_DistShapeShape distSS(subShape, mkVert.Vertex(), 0.1);
if (distSS.NbSolution() > 0) {
gp_Pnt p = distSS.PointOnShape1(1);
pts.push_back(Base::Vector3d(p.X(),p.Y(),p.Z()));
}
}
return pts;
}
catch (...) {
}
// if something went wrong returns an empty array
return std::vector<Base::Vector3d>();
}
std::vector<Base::Vector3d> ViewProviderPartExt::getSelectionShape(const char* Element) const
{
return std::vector<Base::Vector3d>();
}
void ViewProviderPartExt::setHighlightedFaces(const std::vector<App::Color>& colors)
{
int size = static_cast<int>(colors.size());
if (size > 1 && size == this->faceset->partIndex.getNum()) {
pcShapeBind->value = SoMaterialBinding::PER_PART;
pcShapeMaterial->diffuseColor.setNum(size);
pcShapeMaterial->transparency.setNum(size);
SbColor* ca = pcShapeMaterial->diffuseColor.startEditing();
float *t = pcShapeMaterial->transparency.startEditing();
for (int i = 0; i < size; i++) {
ca[i].setValue(colors[i].r, colors[i].g, colors[i].b);
t[i] = colors[i].a;
}
pcShapeMaterial->diffuseColor.finishEditing();
pcShapeMaterial->transparency.finishEditing();
}
else if (colors.size() == 1) {
pcShapeBind->value = SoMaterialBinding::OVERALL;
pcShapeMaterial->diffuseColor.setValue(colors[0].r, colors[0].g, colors[0].b);
pcShapeMaterial->transparency = colors[0].a;
}
}
void ViewProviderPartExt::setHighlightedFaces(const std::vector<App::Material>& colors)
{
int size = static_cast<int>(colors.size());
if (size > 1 && size == this->faceset->partIndex.getNum()) {
pcShapeBind->value = SoMaterialBinding::PER_PART;
pcShapeMaterial->diffuseColor.setNum(size);
pcShapeMaterial->ambientColor.setNum(size);
pcShapeMaterial->specularColor.setNum(size);
pcShapeMaterial->emissiveColor.setNum(size);
SbColor* dc = pcShapeMaterial->diffuseColor.startEditing();
SbColor* ac = pcShapeMaterial->ambientColor.startEditing();
SbColor* sc = pcShapeMaterial->specularColor.startEditing();
SbColor* ec = pcShapeMaterial->emissiveColor.startEditing();
for (int i = 0; i < size; i++) {
dc[i].setValue(colors[i].diffuseColor.r, colors[i].diffuseColor.g, colors[i].diffuseColor.b);
ac[i].setValue(colors[i].ambientColor.r, colors[i].ambientColor.g, colors[i].ambientColor.b);
sc[i].setValue(colors[i].specularColor.r, colors[i].specularColor.g, colors[i].specularColor.b);
ec[i].setValue(colors[i].emissiveColor.r, colors[i].emissiveColor.g, colors[i].emissiveColor.b);
}
pcShapeMaterial->diffuseColor.finishEditing();
pcShapeMaterial->ambientColor.finishEditing();
pcShapeMaterial->specularColor.finishEditing();
pcShapeMaterial->emissiveColor.finishEditing();
}
else if (colors.size() == 1) {
pcShapeBind->value = SoMaterialBinding::OVERALL;
pcShapeMaterial->diffuseColor.setValue(colors[0].diffuseColor.r, colors[0].diffuseColor.g, colors[0].diffuseColor.b);
pcShapeMaterial->ambientColor.setValue(colors[0].ambientColor.r, colors[0].ambientColor.g, colors[0].ambientColor.b);
pcShapeMaterial->specularColor.setValue(colors[0].specularColor.r, colors[0].specularColor.g, colors[0].specularColor.b);
pcShapeMaterial->emissiveColor.setValue(colors[0].emissiveColor.r, colors[0].emissiveColor.g, colors[0].emissiveColor.b);
}
}
void ViewProviderPartExt::unsetHighlightedFaces()
{
ShapeMaterial.touch();
Transparency.touch();
}
void ViewProviderPartExt::setHighlightedEdges(const std::vector<App::Color>& colors)
{
int size = static_cast<int>(colors.size());
if (size > 1) {
pcLineBind->value = SoMaterialBinding::PER_PART;
const int32_t* cindices = this->lineset->coordIndex.getValues(0);
int numindices = this->lineset->coordIndex.getNum();
pcLineMaterial->diffuseColor.setNum(size);
SbColor* ca = pcLineMaterial->diffuseColor.startEditing();
int linecount = 0;
for (int i = 0; i < numindices; ++i) {
if (cindices[i] < 0) {
ca[linecount].setValue(colors[linecount].r, colors[linecount].g, colors[linecount].b);
linecount++;
if (linecount >= size)
break;
}
}
pcLineMaterial->diffuseColor.finishEditing();
}
else if (size == 1) {
pcLineBind->value = SoMaterialBinding::OVERALL;
pcLineMaterial->diffuseColor.setValue(colors[0].r, colors[0].g, colors[0].b);
}
}
void ViewProviderPartExt::unsetHighlightedEdges()
{
LineMaterial.touch();
}
void ViewProviderPartExt::setHighlightedPoints(const std::vector<App::Color>& colors)
{
int size = static_cast<int>(colors.size());
if (size > 1) {
// FIXME: Check for size mismatch between number of points and number of colors
pcShapeBind->value = SoMaterialBinding::PER_VERTEX;
pcPointMaterial->diffuseColor.setNum(size);
SbColor* ca = pcPointMaterial->diffuseColor.startEditing();
for (int i = 0; i < size; ++i)
ca[i].setValue(colors[i].r, colors[i].g, colors[i].b);
pcPointMaterial->diffuseColor.finishEditing();
}
else if (size == 1) {
pcShapeBind->value = SoMaterialBinding::OVERALL;
pcPointMaterial->diffuseColor.setValue(colors[0].r, colors[0].g, colors[0].b);
}
}
void ViewProviderPartExt::unsetHighlightedPoints()
{
PointMaterial.touch();
}
bool ViewProviderPartExt::loadParameter()
{
bool changed = false;
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath
("User parameter:BaseApp/Preferences/Mod/Part");
float deviation = hGrp->GetFloat("MeshDeviation",0.2);
float angularDeflection = hGrp->GetFloat("MeshAngularDeflection",28.65);
bool novertexnormals = hGrp->GetBool("NoPerVertexNormals",false);
bool qualitynormals = hGrp->GetBool("QualityNormals",false);
if (Deviation.getValue() != deviation) {
Deviation.setValue(deviation);
changed = true;
}
if (AngularDeflection.getValue() != angularDeflection ) {
AngularDeflection.setValue(angularDeflection);
}
if (this->noPerVertexNormals != novertexnormals) {
this->noPerVertexNormals = novertexnormals;
changed = true;
}
if (this->qualityNormals != qualitynormals) {
this->qualityNormals = qualitynormals;
changed = true;
}
return changed;
}
void ViewProviderPartExt::reload()
{
if (loadParameter()) {
App::Property* shape = pcObject->getPropertyByName("Shape");
if (shape) update(shape);
}
}
void ViewProviderPartExt::updateData(const App::Property* prop)
{
if (prop->getTypeId() == Part::PropertyPartShape::getClassTypeId()) {
// get the shape to show
const TopoDS_Shape &cShape = static_cast<const Part::PropertyPartShape*>(prop)->getValue();
// calculate the visual only if visible
if (Visibility.getValue())
updateVisual(cShape);
else
VisualTouched = true;
if (!VisualTouched) {
if (this->faceset->partIndex.getNum() >
this->pcShapeMaterial->diffuseColor.getNum()) {
this->pcShapeBind->value = SoMaterialBinding::OVERALL;
}
}
}
Gui::ViewProviderGeometryObject::updateData(prop);
}
void ViewProviderPartExt::setupContextMenu(QMenu* menu, QObject* receiver, const char* member)
{
Gui::ViewProviderGeometryObject::setupContextMenu(menu, receiver, member);
QAction* act = menu->addAction(QObject::tr("Set colors..."), receiver, member);
act->setData(QVariant((int)ViewProvider::Color));
}
bool ViewProviderPartExt::setEdit(int ModNum)
{
if (ModNum == ViewProvider::Color) {
// When double-clicking on the item for this pad the
// object unsets and sets its edit mode without closing
// the task panel
Gui::TaskView::TaskDialog *dlg = Gui::Control().activeDialog();
if (dlg) {
Gui::Control().showDialog(dlg);
return false;
}
Gui::Selection().clearSelection();
Gui::Control().showDialog(new TaskFaceColors(this));
return true;
}
else {
return Gui::ViewProviderGeometryObject::setEdit(ModNum);
}
}
void ViewProviderPartExt::unsetEdit(int ModNum)
{
if (ModNum == ViewProvider::Color) {
}
else {
Gui::ViewProviderGeometryObject::unsetEdit(ModNum);
}
}
void ViewProviderPartExt::updateVisual(const TopoDS_Shape& inputShape)
{
// Clear selection
Gui::SoSelectionElementAction saction(Gui::SoSelectionElementAction::None);
saction.apply(this->faceset);
saction.apply(this->lineset);
saction.apply(this->nodeset);
// Clear highlighting
Gui::SoHighlightElementAction haction;
haction.apply(this->faceset);
haction.apply(this->lineset);
haction.apply(this->nodeset);
TopoDS_Shape cShape(inputShape);
if (cShape.IsNull()) {
coords ->point .setNum(0);
norm ->vector .setNum(0);
faceset ->coordIndex .setNum(0);
faceset ->partIndex .setNum(0);
lineset ->coordIndex .setNum(0);
nodeset ->startIndex .setValue(0);
VisualTouched = false;
return;
}
// time measurement and book keeping
Base::TimeInfo start_time;
int numTriangles=0,numNodes=0,numNorms=0,numFaces=0,numEdges=0,numLines=0;
std::set<int> faceEdges;
try {
// calculating the deflection value
Bnd_Box bounds;
BRepBndLib::Add(cShape, bounds);
bounds.SetGap(0.0);
Standard_Real xMin, yMin, zMin, xMax, yMax, zMax;
bounds.Get(xMin, yMin, zMin, xMax, yMax, zMax);
Standard_Real deflection = ((xMax-xMin)+(yMax-yMin)+(zMax-zMin))/300.0 *
Deviation.getValue();
// create or use the mesh on the data structure
#if OCC_VERSION_HEX >= 0x060600
Standard_Real AngDeflectionRads = AngularDeflection.getValue() / 180.0 * M_PI;
BRepMesh_IncrementalMesh(cShape,deflection,Standard_False,
AngDeflectionRads,Standard_True);
#else
BRepMesh_IncrementalMesh(cShape,deflection);
#endif
// We must reset the location here because the transformation data
// are set in the placement property
TopLoc_Location aLoc;
cShape.Location(aLoc);
// count triangles and nodes in the mesh
TopExp_Explorer Ex;
for (Ex.Init(cShape,TopAbs_FACE);Ex.More();Ex.Next()) {
Handle (Poly_Triangulation) mesh = BRep_Tool::Triangulation(TopoDS::Face(Ex.Current()), aLoc);
// Note: we must also count empty faces
if (!mesh.IsNull()) {
numTriangles += mesh->NbTriangles();
numNodes += mesh->NbNodes();
numNorms += mesh->NbNodes();
}
TopExp_Explorer xp;
for (xp.Init(Ex.Current(),TopAbs_EDGE);xp.More();xp.Next())
faceEdges.insert(xp.Current().HashCode(INT_MAX));
numFaces++;
}
// get an indexed map of edges
TopTools_IndexedMapOfShape edgeMap;
TopExp::MapShapes(cShape, TopAbs_EDGE, edgeMap);
// key is the edge number, value the coord indexes. This is needed to keep the same order as the edges.
std::map<int, std::vector<int32_t> > lineSetMap;
std::set<int> edgeIdxSet;
std::vector<int32_t> edgeVector;
// count and index the edges
for (int i=1; i <= edgeMap.Extent(); i++) {
edgeIdxSet.insert(i);
numEdges++;
const TopoDS_Edge& aEdge = TopoDS::Edge(edgeMap(i));
TopLoc_Location aLoc;
// handling of the free edge that are not associated to a face
// Note: The assumption that if for an edge BRep_Tool::Polygon3D
// returns a valid object is wrong. This e.g. happens for ruled
// surfaces which gets created by two edges or wires.
// So, we have to store the hashes of the edges associated to a face.
// If the hash of a given edge is not in this list we know it's really
// a free edge.
int hash = aEdge.HashCode(INT_MAX);
if (faceEdges.find(hash) == faceEdges.end()) {
Handle(Poly_Polygon3D) aPoly = BRep_Tool::Polygon3D(aEdge, aLoc);
if (!aPoly.IsNull()) {
int nbNodesInEdge = aPoly->NbNodes();
numNodes += nbNodesInEdge;
}
}
}
// handling of the vertices
TopTools_IndexedMapOfShape vertexMap;
TopExp::MapShapes(cShape, TopAbs_VERTEX, vertexMap);
numNodes += vertexMap.Extent();
// create memory for the nodes and indexes
coords ->point .setNum(numNodes);
norm ->vector .setNum(numNorms);
faceset ->coordIndex .setNum(numTriangles*4);
faceset ->partIndex .setNum(numFaces);
// get the raw memory for fast fill up
SbVec3f* verts = coords ->point .startEditing();
SbVec3f* norms = norm ->vector .startEditing();
int32_t* index = faceset ->coordIndex .startEditing();
int32_t* parts = faceset ->partIndex .startEditing();
// preset the normal vector with null vector
for (int i=0;i < numNorms;i++)
norms[i]= SbVec3f(0.0,0.0,0.0);
int ii = 0,faceNodeOffset=0,faceTriaOffset=0;
for (Ex.Init(cShape, TopAbs_FACE); Ex.More(); Ex.Next(),ii++) {
TopLoc_Location aLoc;
const TopoDS_Face &actFace = TopoDS::Face(Ex.Current());
// get the mesh of the shape
Handle (Poly_Triangulation) mesh = BRep_Tool::Triangulation(actFace,aLoc);
if (mesh.IsNull()) continue;
// getting the transformation of the shape/face
gp_Trsf myTransf;
Standard_Boolean identity = true;
if (!aLoc.IsIdentity()) {
identity = false;
myTransf = aLoc.Transformation();
}
// getting size of node and triangle array of this face