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