Salome HOME
Merge branch 'master' into V9_3_BR
[modules/geom.git] / src / OBJECT / GEOM_AISShape.cxx
1 // Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 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 GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 /*!
24   \class GEOM_AISShape GEOM_AISShape.hxx
25   \brief ....
26 */
27
28 #include "GEOM_AISShape.hxx"
29 #include "GEOM_AISVector.hxx"
30
31 #include <GEOMUtils.hxx>
32
33 // Open CASCADE Includes
34 #include <AIS_InteractiveContext.hxx>
35 #include <BRepAdaptor_Surface.hxx>
36 #include <BRep_Tool.hxx>
37 #include <GCPnts_AbscissaPoint.hxx>
38 #include <GeomAdaptor_Curve.hxx>
39 #include <Graphic3d_ArrayOfPoints.hxx>
40 #include <Graphic3d_AspectFillArea3d.hxx>
41 #include <Graphic3d_AspectLine3d.hxx>
42 #include <Graphic3d_AspectMarker3d.hxx>
43 #include <Graphic3d_AspectText3d.hxx>
44 #include <Prs3d.hxx>
45 #include <Prs3d_Arrow.hxx>
46 #include <Prs3d_IsoAspect.hxx>
47 #include <Prs3d_ShadingAspect.hxx>
48 #include <SelectBasics_SensitiveEntity.hxx>
49 #include <SelectMgr_EntityOwner.hxx>
50 #include <SelectMgr_IndexedMapOfOwner.hxx>
51 #include <SelectMgr_Selection.hxx>
52 #include <StdPrs_ShadedShape.hxx>
53 #include <StdPrs_ToolTriangulatedShape.hxx>
54 #include <StdSelect_BRepOwner.hxx>
55 #include <StdSelect_DisplayMode.hxx>
56 #include <TColStd_IndexedMapOfInteger.hxx>
57 #include <TColStd_ListIteratorOfListOfInteger.hxx>
58 #include <TColStd_ListOfInteger.hxx>
59 #include <TopExp.hxx>
60 #include <TopExp_Explorer.hxx>
61 #include <TopTools_IndexedMapOfShape.hxx>
62 #include <TopoDS.hxx>
63 #include <TopoDS_Edge.hxx>
64 #include <TopoDS_Shape.hxx>
65 #include <TopoDS_Vertex.hxx>
66 #include <V3d_View.hxx>
67 #include <gp_Dir.hxx>
68 #include <gp_Pnt.hxx>
69 #include <gp_Vec.hxx>
70 #include <Prs3d_VertexDrawMode.hxx>
71 #include <StdPrs_WFShape.hxx>
72
73 #include <Basics_OCCTVersion.hxx>
74
75 #include <SalomeApp_Tools.h>
76 #include <SUIT_Session.h>
77 #include <SUIT_ResourceMgr.h>
78
79 IMPLEMENT_STANDARD_RTTIEXT(GEOM_AISShape, SALOME_AISShape)
80
81 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::myTopLevelDm = GEOM_AISShape::TopKeepCurrent;
82 Quantity_Color GEOM_AISShape::myTopLevelColor;
83
84
85 static void getEntityOwners( const Handle(AIS_InteractiveObject)&  theObj,
86                              const Handle(AIS_InteractiveContext)& theIC,
87                              SelectMgr_IndexedMapOfOwner&          theMap )
88 {
89   if ( theObj.IsNull() || theIC.IsNull() )
90     return;
91
92   TColStd_ListOfInteger modes;
93   theIC->ActivatedModes( theObj, modes );
94
95   TColStd_ListIteratorOfListOfInteger itr( modes );
96   for (; itr.More(); itr.Next() ) {
97     int m = itr.Value();
98     if ( !theObj->HasSelection( m ) )
99       continue;
100
101     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
102
103     for ( sel->Init(); sel->More(); sel->Next() ) {
104       const Handle(SelectMgr_SensitiveEntity) aHSenEntity = sel->Sensitive();
105       if( aHSenEntity.IsNull() )
106         continue;
107
108       Handle(SelectBasics_SensitiveEntity) entity = aHSenEntity->BaseSensitive();
109       if ( entity.IsNull() )
110         continue;
111       Handle(SelectMgr_EntityOwner) owner =
112         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
113       if ( !owner.IsNull() )
114         theMap.Add( owner );
115     }
116   }
117 }
118
119 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
120                              const TopoDS_Shape&                aMainShape,
121                              const SelectMgr_IndexedMapOfOwner& anAllMap,
122                              SelectMgr_IndexedMapOfOwner&       aToHiliteMap )
123 {
124   TopTools_IndexedMapOfShape aMapOfShapes;
125   TopExp::MapShapes(aMainShape, aMapOfShapes);
126
127   for  ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
128     Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
129     if ( anOwner.IsNull() || !anOwner->HasShape() )
130       continue;
131
132     const TopoDS_Shape& aSubShape = anOwner->Shape();
133     Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
134     if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
135       continue;
136
137     if ( !aToHiliteMap.Contains( anOwner ) )
138       aToHiliteMap.Add( anOwner );
139   }
140 }
141
142 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape&    shape,
143                              const Standard_CString aName)
144   : SALOME_AISShape(shape),
145     myName(aName),
146     myDisplayVectors(false),
147     myDisplayVertices(false),
148     myFieldDataType(GEOM::FDT_Double),
149     myFieldDimension(0),
150     myFieldStepRangeMin(0),
151     myFieldStepRangeMax(0)
152 {
153   //rnv: Commented to avoid bug with local selection
154   //SetHilightMode( CustomHighlight ); // override setting the mode to 0 inside AIS_Shape constructor
155
156   myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
157   myPrevDisplayMode = 0;
158
159   myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
160
161   myTopLevel = Standard_False;
162   Graphic3d_MaterialAspect aMatAspect;
163   if ( !HasMaterial() ) {
164     aMatAspect.SetAmbient( 0.5 );
165     aMatAspect.SetDiffuse( 0.5 );
166     aMatAspect.SetEmissive( 0.5 );
167     aMatAspect.SetShininess(0.5 );
168     aMatAspect.SetSpecular( 0.5 );
169
170     myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
171     myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(aMatAspect);
172   }
173 }
174
175 GEOM_AISShape::~GEOM_AISShape()
176 {
177 }
178
179 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io)
180 {
181   SetOwner( io );
182 }
183
184 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO()
185 {
186   Handle(SALOME_InteractiveObject) IO;
187   if ( !GetOwner().IsNull() )
188     IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
189   return IO;
190 }
191
192 Standard_Boolean GEOM_AISShape::hasIO()
193 {
194   return !getIO().IsNull();
195 }
196
197 void GEOM_AISShape::setName(const Standard_CString aName)
198 {
199   myName = aName;
200
201   Handle(SALOME_InteractiveObject) IO = getIO();
202   if ( !IO.IsNull() )
203     IO->setName(aName);
204 }
205
206 Standard_CString GEOM_AISShape::getName()
207 {
208   return myName.ToCString();
209 }
210
211 Standard_Boolean GEOM_AISShape::AcceptDisplayMode(const Standard_Integer theMode) const
212 {
213   return theMode >= Wireframe && theMode <= CustomHighlight;
214 }
215
216 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
217                             const Handle(Prs3d_Presentation)&           aPrs,
218                             const Standard_Integer                      aMode)
219
220   if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
221
222   Handle(AIS_InteractiveContext) anIC = GetContext();
223   // AKL: use old behavior to avoid keeping object's wireframe
224   //      if to change shape properties (for example: 'Clear Top Level State','Color', 'Isos') 
225   //      calling popup menu over(!) the shape in OCC viewer.
226   anIC->SetToHilightSelected( false );
227
228   bool anIsField = !myFieldStepData.isEmpty();
229   bool anIsColorField = anIsField && myFieldDataType != GEOM::FDT_String;
230   bool anIsTextField = anIsField && myFieldDataType == GEOM::FDT_String;
231
232   if (isShowVertices())
233     myDrawer->SetVertexDrawMode(Prs3d_VDM_All);
234
235   //   StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
236   bool isTopLev = isTopLevel() && switchTopLevel();
237   switch (aMode) {
238     case Wireframe:
239     // Begin 0023271: [CEA 1823] Deflection Coefficient not taken into account in wireframe
240     {
241       StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
242
243       // After this call if type of deflection is relative
244       // computed deflection coefficient is stored as absolute.
245       Prs3d::GetDeflection (myshape, myDrawer);
246     }
247     // End 0023271
248     case CustomHighlight:
249     {
250       if(isTopLev) {
251               SetColor(topLevelColor());
252               Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
253               anAspect->SetColor( topLevelColor() );
254               Attributes()->SetWireAspect( anAspect );
255       }
256       if( !isTopLev && anIsColorField && myFieldDimension == 1 )
257         drawField( aPrs, false, aMode == CustomHighlight );
258       else
259         StdPrs_WFShape::Add(aPrs,myshape,myDrawer);
260       break;
261     }
262     case Shading:
263     {
264       shadingMode(aPresentationManager, aPrs, aMode);
265       break;
266     }
267     case ShadingWithEdges:
268     {
269       myDrawer->SetFaceBoundaryDraw( Standard_True );
270       shadingMode(aPresentationManager, aPrs, Shading);
271       if( anIsColorField && myFieldDimension == 1 ) {
272         myDrawer->SetFaceBoundaryDraw( Standard_False );
273         drawField( aPrs );
274       }
275       break;
276     }
277     case TexturedShape:
278     {
279       shadingMode(aPresentationManager, aPrs, Shading);
280       break;
281     }
282   }
283   if (isShowVectors())
284   {
285     const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
286     TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
287     for ( ; Exp.More(); Exp.Next() ) {
288       TopoDS_Vertex aV1, aV2;
289       TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
290       if ( !isVector )
291         // draw curve direction (issue 0021087)
292         anEdgeE.Orientation( TopAbs_FORWARD );
293
294       if ( anEdgeE.IsNull() ) continue;
295
296       TopExp::Vertices(anEdgeE, aV1, aV2);
297       gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
298       gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
299
300       double fp,lp;
301       gp_Vec aDirVec;
302       Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
303
304       if ( C.IsNull() ) continue;
305
306       if ( anEdgeE.Orientation() == TopAbs_FORWARD )
307         C->D1(lp, aP2, aDirVec);
308       else {
309         C->D1(fp, aP1, aDirVec);
310         aP2 = aP1;
311       }
312
313       GeomAdaptor_Curve aAdC;
314       aAdC.Load(C, fp, lp);
315       Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp); 
316      
317       if (aDist > gp::Resolution()) {
318         gp_Dir aDir;
319         if ( anEdgeE.Orientation() == TopAbs_FORWARD )
320           aDir = aDirVec;
321         else
322           aDir = -aDirVec;
323
324         Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
325       }
326     }
327   }
328
329   // draw color field on vertices
330   if( anIsColorField && myFieldDimension == 0 && aMode != CustomHighlight )
331     drawField( aPrs );
332
333   // draw text field
334   if( anIsTextField )
335     drawField( aPrs, true );
336
337   if( isShowName() )
338     drawName( aPrs );
339
340   //  aPrs->ReCompute(); // for hidden line recomputation if necessary...
341 }
342
343 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
344 {
345   myShadingColor = aCol;
346 }
347
348 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
349 {
350   myEdgesInShadingColor = aCol;
351 }
352
353 void GEOM_AISShape::SetLabelColor(const Quantity_Color &aCol)
354 {
355   myLabelColor = aCol;
356 }
357
358 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap, 
359                                        const Standard_Boolean             aHighlight )
360 {
361   Handle(AIS_InteractiveObject) anObj = this;
362   Handle(AIS_InteractiveContext) anIC = GetContext();
363   if ( anIC.IsNull() 
364 #if OCC_VERSION_LARGE <= 0x07030000
365        || !anIC->HasOpenedContext() 
366 #endif
367      ) 
368     return;
369
370   Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
371   anIC->SetAutomaticHilight( false );
372
373   anIC->ClearSelected( false );
374
375   if ( aHighlight ) {
376     SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
377
378     // Get entity owners for all activated selection modes
379     getEntityOwners( anObj, anIC, anAllMap );
380
381     // Convert <aIndexMap> into the map of owners to highlight/unhighlight
382     indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
383
384
385     for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
386       anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
387   }
388
389   anIC->SetAutomaticHilight( isAutoHilight );
390   anIC->HilightSelected( false );
391 }
392
393 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
394 {
395   myDisplayVectors = isDisplayed;
396 }
397
398 void GEOM_AISShape::SetDisplayVertices(bool isDisplayed)
399 {
400   myDisplayVertices = isDisplayed;
401 }
402
403 void GEOM_AISShape::SetDisplayName(bool isDisplayed)
404 {
405   myDisplayName = isDisplayed;
406 }
407
408 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
409                                 const Handle(Prs3d_Presentation)&           aPrs,
410                                 const Standard_Integer                      aMode)
411 {
412   myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
413
414   Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
415   aMatAspect.SetTransparency(Transparency());
416   Graphic3d_MaterialAspect currentFrontMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
417   Graphic3d_MaterialAspect currentBackMaterial  = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
418   myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : currentFrontMaterial );
419   myDrawer->ShadingAspect()->Aspect()->SetBackMaterial ( isTopLevel() ? aMatAspect : currentBackMaterial  );
420
421   if( isTopLevel() && switchTopLevel() )
422     myDrawer->ShadingAspect()->SetColor( topLevelColor() );
423   else {
424     if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
425       myDrawer->ShadingAspect()->SetColor(myShadingColor);
426     else
427       myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
428   }
429
430   bool anIsColorField = !myFieldStepData.isEmpty() && myFieldDataType != GEOM::FDT_String;
431   if( anIsColorField && ( myFieldDimension == 2 || myFieldDimension == 3 || myFieldDimension == -1 ) )
432   {
433     drawField( aPrs );
434   }
435   else
436   {
437     // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
438     //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
439     SALOME_AISShape::Compute(aPresentationManager, aPrs, aMode);
440   }
441 }
442
443 Standard_Boolean GEOM_AISShape::isTopLevel()
444 {
445   return myTopLevel;
446 }
447
448 void GEOM_AISShape::setTopLevel(Standard_Boolean f)
449 {
450   if(f) {
451     if(f != myTopLevel)
452       myPrevDisplayMode = DisplayMode();
453     Standard_Integer dm;
454     switch(topLevelDisplayMode()) {
455       case TopWireFrame :        dm = Wireframe;         break;
456       case TopShading :          dm = Shading;           break;
457       case TopShadingWithEdges : dm = ShadingWithEdges;  break;
458       default :                  dm = myPrevDisplayMode; break;
459     }
460     SetDisplayMode(dm);
461   } else {
462     if(f != myTopLevel)
463       SetDisplayMode(myPrevDisplayMode);
464   }
465   myTopLevel = f;
466 }
467
468 void GEOM_AISShape::setPrevDisplayMode(const Standard_Integer mode)
469 {
470   myPrevDisplayMode = mode;
471 }
472
473 Quantity_Color GEOM_AISShape::topLevelColor()
474 {
475   return myTopLevelColor;
476 }
477
478 void GEOM_AISShape::setTopLevelColor(const Quantity_Color c)
479 {
480   myTopLevelColor = c;
481 }
482
483 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::topLevelDisplayMode()
484 {
485   return myTopLevelDm;
486 }
487
488 void GEOM_AISShape::setTopLevelDisplayMode(const GEOM_AISShape::TopLevelDispMode dm)
489 {
490   myTopLevelDm = dm;
491 }
492
493 Standard_Boolean GEOM_AISShape::switchTopLevel()
494 {
495   return myTopLevelDm != TopShowAdditionalWActor;
496 }
497
498 Standard_Boolean GEOM_AISShape::toActivate()
499 {
500   return ( myTopLevel && myTopLevelDm == TopShowAdditionalWActor ) ? false : true;
501 }
502
503 void GEOM_AISShape::setFieldStepInfo( const GEOM::field_data_type    theFieldDataType,
504                                       const int                      theFieldDimension,
505                                       const QList<QVariant>&         theFieldStepData,
506                                       const TCollection_AsciiString& theFieldStepName,
507                                       const double                   theFieldStepRangeMin,
508                                       const double                   theFieldStepRangeMax )
509 {
510   myFieldDataType     = theFieldDataType;
511   myFieldDimension    = theFieldDimension;
512   myFieldStepData     = theFieldStepData;
513   myFieldStepName     = theFieldStepName;
514   myFieldStepRangeMin = theFieldStepRangeMin;
515   myFieldStepRangeMax = theFieldStepRangeMax;
516 }
517
518 void GEOM_AISShape::getFieldStepInfo( GEOM::field_data_type&   theFieldDataType,
519                                       int&                     theFieldDimension,
520                                       QList<QVariant>&         theFieldStepData,
521                                       TCollection_AsciiString& theFieldStepName,
522                                       double&                  theFieldStepRangeMin,
523                                       double&                  theFieldStepRangeMax ) const
524 {
525   theFieldDataType     = myFieldDataType;
526   theFieldDimension    = myFieldDimension;
527   theFieldStepData     = myFieldStepData;
528   theFieldStepName     = myFieldStepName;
529   theFieldStepRangeMin = myFieldStepRangeMin;
530   theFieldStepRangeMax = myFieldStepRangeMax;
531 }
532
533 void GEOM_AISShape::drawField( const Handle(Prs3d_Presentation)& thePrs,
534                                const bool                        theIsString,
535                                const bool                        theIsHighlight )
536 {
537   if( myFieldStepData.isEmpty() )
538     return;
539
540   QListIterator<QVariant> aFieldStepDataIter( myFieldStepData );
541
542   TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
543   switch( myFieldDimension )
544   {
545     case 0:  aShapeType = TopAbs_VERTEX; break;
546     case 1:  aShapeType = TopAbs_EDGE; break;
547     case 2:  aShapeType = TopAbs_FACE; break;
548     case 3:  aShapeType = TopAbs_SOLID; break;
549     case -1: aShapeType = TopAbs_VERTEX; break;
550   }
551
552   TopTools_IndexedMapOfShape aShapeMap;
553   TopExp::MapShapes( myshape, aShapeMap );
554
555   TColStd_IndexedMapOfInteger anIndexMap;
556
557   TopExp_Explorer anExp;
558   for( anExp.Init( myshape, aShapeType ); anExp.More(); anExp.Next() )
559   {
560     TopoDS_Shape aSubShape = anExp.Current();
561     if( !aSubShape.IsNull() )
562     {
563       Standard_Integer aSubShapeIndex = aShapeMap.FindIndex( aSubShape );
564       if( anIndexMap.Contains( aSubShapeIndex ) )
565         continue;
566
567       anIndexMap.Add( aSubShapeIndex );
568
569       Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup( thePrs );
570
571       QColor aQColor;
572       QString aString;
573       if( aFieldStepDataIter.hasNext() )
574       {
575         const QVariant& aVariant = aFieldStepDataIter.next();
576         if( theIsString )
577           aString = aVariant.toString();
578         else
579           aQColor = aVariant.value<QColor>();
580       }
581       else
582         break;
583
584       if( theIsString )
585       {
586         gp_Pnt aCenter;
587         if( computeMassCenter( aSubShape, aCenter ) )
588         {
589           Graphic3d_Vertex aVertex( aCenter.X(), aCenter.Y(), aCenter.Z() );
590
591           Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
592           anAspectText3d->SetStyle( Aspect_TOST_ANNOTATION );
593           anAspectText3d->SetColor( myLabelColor );
594           aGroup->SetPrimitivesAspect( anAspectText3d );
595
596           aGroup->Text( aString.toUtf8().constData(), aVertex, 14 );
597         }
598       }
599       else
600       {
601         Quantity_Color aColor( aQColor.redF(), aQColor.greenF(), aQColor.blueF(), Quantity_TOC_RGB );
602         SetCustomColor( aSubShape, aColor );
603         if( myFieldDimension == 0 )
604         {
605           TopoDS_Vertex aVertexShape = TopoDS::Vertex( aSubShape );
606           if( !aVertexShape.IsNull() )
607           {
608             gp_Pnt aPnt = BRep_Tool::Pnt( aVertexShape );
609
610             Handle(Graphic3d_AspectMarker3d) anAspectMarker3d = new Graphic3d_AspectMarker3d();
611             anAspectMarker3d->SetColor( aColor );
612             anAspectMarker3d->SetType( Aspect_TOM_POINT );
613             anAspectMarker3d->SetScale( 10.0 );
614             aGroup->SetPrimitivesAspect( anAspectMarker3d );
615
616             Handle(Graphic3d_ArrayOfPoints) anArray = new Graphic3d_ArrayOfPoints( 1 );
617             anArray->AddVertex( aPnt.X(), aPnt.Y(), aPnt.Z() );
618
619             aGroup->AddPrimitiveArray( anArray );
620           }
621         }
622         else if( myFieldDimension == 1 )
623         {
624           myDrawer->WireAspect()->SetColor( aColor );
625           if( theIsHighlight )
626             myDrawer->WireAspect()->SetWidth( myOwnWidth );
627           else
628             myDrawer->WireAspect()->SetWidth( myOwnWidth + 4 );
629           /*
630           StdPrs_WFShape::Add( thePrs, aSubShape, myDrawer );
631           */
632         }
633         else if( myFieldDimension == 2 ||
634                  myFieldDimension == 3 ||
635                  myFieldDimension == -1 )
636         {
637           //Handle(Prs3d_ShadingAspect) anAspect = new Prs3d_ShadingAspect();
638           //anAspect->SetColor( aColor );
639           //myDrawer->SetShadingAspect( anAspect );
640           //StdPrs_ShadedShape::Add( thePrs, aSubShape, myDrawer );
641         }
642       }
643     }
644   }
645
646   SALOME_AISShape::Compute( Handle(PrsMgr_PresentationManager3d)(),
647                             thePrs, AIS_Shaded );
648 }
649
650 void GEOM_AISShape::drawName( const Handle(Prs3d_Presentation)& thePrs )
651 {
652   Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup( thePrs );
653
654   gp_Ax3 anAx3 = GEOMUtils::GetPosition(myshape);
655   gp_Pnt aCenter = anAx3.Location();
656
657   Graphic3d_Vertex aVertex( aCenter.X(), aCenter.Y(), aCenter.Z() );
658
659   Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
660   anAspectText3d->SetStyle( Aspect_TOST_ANNOTATION );
661   anAspectText3d->SetColor( myLabelColor );
662   aGroup->SetPrimitivesAspect( anAspectText3d );
663
664   const char* aName = getIO()->getName();
665   aGroup->Text( TCollection_ExtendedString( aName ), aVertex, 16 );
666 }
667
668 Standard_Boolean GEOM_AISShape::computeMassCenter( const TopoDS_Shape& theShape,
669                                                    gp_Pnt&             theCenter )
670 {
671   theCenter.SetCoord( 0,0,0 );
672   Standard_Integer aNbPoints = 0;
673
674   if ( theShape.ShapeType() == TopAbs_EDGE )
675   {
676     double f,l;
677     Handle(Geom_Curve) curve = BRep_Tool::Curve( TopoDS::Edge( theShape ), f, l );
678     if ( !curve.IsNull() )
679     {
680       theCenter = curve->Value( 0.5 * ( f + l ));
681       aNbPoints = 1;
682     }
683   }
684   else if ( theShape.ShapeType() == TopAbs_FACE )
685   {
686     const TopoDS_Face& F = TopoDS::Face( theShape );
687     BRepAdaptor_Surface surface( F );
688
689     TopLoc_Location L;
690     Handle(Poly_Triangulation) triangulation = BRep_Tool::Triangulation( F, L );
691     if ( !triangulation.IsNull() && triangulation->HasUVNodes() )
692     {
693       gp_XY C( 0, 0 );
694       double A = 0;
695       const TColgp_Array1OfPnt2d& uvArray = triangulation->UVNodes();
696       const Poly_Array1OfTriangle&  trias = triangulation->Triangles();
697       Standard_Integer n1,n2,n3;
698       for ( int iT = trias.Lower(); iT <= trias.Upper(); ++iT )
699       {
700         trias( iT ).Get( n1,n2,n3 );
701         const gp_Pnt2d& uv1 = uvArray( n1 );
702         const gp_Pnt2d& uv2 = uvArray( n2 );
703         const gp_Pnt2d& uv3 = uvArray( n3 );
704         double a = 0.5 * sqrt(( uv1.X() - uv3.X() ) * ( uv2.Y() - uv1.Y() ) -
705                               ( uv1.X() - uv2.X() ) * ( uv3.Y() - uv1.Y() ));
706         C += ( uv1.XY() + uv2.XY() + uv3.XY() ) / 3. * a;
707         A += a;
708       }
709       if ( A > std::numeric_limits<double>::min() )
710       {
711         C /= A;
712         theCenter = surface.Value( C.X(), C.Y() );
713         aNbPoints = 1;
714       }
715     }
716     if ( aNbPoints == 0 )
717     {
718       theCenter = surface.Value( 0.5 * ( surface.FirstUParameter() + surface.LastUParameter() ),
719                                  0.5 * ( surface.FirstVParameter() + surface.LastVParameter() ));
720     }
721     aNbPoints = 1;
722   }
723
724   if ( aNbPoints == 0 )
725   {
726     TopExp_Explorer anExp;
727     for( anExp.Init( theShape, TopAbs_VERTEX ); anExp.More(); anExp.Next() )
728     {
729       TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
730       if( !aVertex.IsNull() )
731       {
732         gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
733         theCenter.ChangeCoord() += aPnt.XYZ();
734         aNbPoints++;
735       }
736     }
737   }
738
739   if ( aNbPoints > 0 )
740     theCenter.ChangeCoord() /= (Standard_Real) aNbPoints;
741
742   return aNbPoints;
743 }