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