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