Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Shape.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROGUI_Shape.h>
20 #include <HYDROGUI_Tool.h>
21 #include <HYDROGUI_Polyline.h>
22 #include <HYDROGUI_AISShape.h>
23
24 #include <HYDROData_Channel.h>
25 #include <HYDROData_Document.h>
26 #include <HYDROData_DummyObject3D.h>
27 #include <HYDROData_ImmersibleZone.h>
28 #include <HYDROData_BCPolygon.h>
29 #include <HYDROData_Obstacle.h>
30 #include <HYDROData_PolylineXY.h>
31 #include <HYDROData_Polyline3D.h>
32 #include <HYDROData_Profile.h>
33 #include <HYDROData_ShapesGroup.h>
34 #include <HYDROData_Stream.h>
35 #include <HYDROData_Zone.h>
36 #include <HYDROData_LandCoverMap.h>
37
38 #include <BRep_Builder.hxx>
39 #include <BRepBuilderAPI_MakeFace.hxx>
40 #include <Prs3d_IsoAspect.hxx>
41 #include <TopoDS.hxx>
42 #include <TopoDS_Face.hxx>
43 #include <TopoDS_Wire.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <TopExp.hxx>
46
47 #include <SUIT_ResourceMgr.h>
48 #include <SUIT_Session.h>
49
50 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
51                                 const Handle(HYDROData_Entity)&       theObject,
52                                 const int                             theZLayer )
53 : myContext( theContext ),
54   myObject( theObject ),
55   myZLayer( theZLayer ),
56   myIsHighlight( false ),
57   myFillingColor( Qt::transparent ),
58   myBorderColor( Qt::black ),
59   myHighlightColor( Qt::white ),
60   myIsToUpdate( false ),
61   myIsVisible( true ),
62   myDisplayMode( AIS_Shaded ),
63   mySelectionMode( AIS_Shape::SelectionMode( TopAbs_SHAPE ) )
64 {
65 }
66
67 HYDROGUI_Shape::~HYDROGUI_Shape()
68 {
69   erase( false );
70 }
71
72 Handle(AIS_InteractiveContext) HYDROGUI_Shape::getContext() const
73 {
74   return myContext;
75 }
76
77 Handle(HYDROData_Entity) HYDROGUI_Shape::getObject() const
78 {
79   return myObject;
80 }
81
82 TopoDS_Shape HYDROGUI_Shape::getTopoShape() const
83 {
84   return myTopoShape;
85 }
86
87 bool HYDROGUI_Shape::getIsToUpdate() const
88 {
89   return myIsToUpdate;
90 }
91
92 void HYDROGUI_Shape::setIsToUpdate( bool theState )
93 {
94   myIsToUpdate = theState;
95 }
96
97 bool HYDROGUI_Shape::isVisible() const
98 {
99   return myIsVisible;
100 }
101
102 QList<Handle(AIS_InteractiveObject)> HYDROGUI_Shape::getAISObjects() const
103 {
104   return myShapes;
105 }
106
107 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
108 {
109   if ( myContext.IsNull() || myShapes.empty() || !isVisible() )
110     return;
111
112   displayShape( theIsUpdateViewer );
113 }
114
115 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
116 {
117   if ( myContext.IsNull() || myShapes.empty() )
118     return;
119
120   eraseShape( theIsUpdateViewer );
121 }
122
123 void HYDROGUI_Shape::update( bool isUpdateViewer,
124                              bool isDeactivateSelection )
125
126 {
127   setIsToUpdate( false );
128
129   if ( myContext.IsNull() )
130     return;
131
132   // Try to retrieve information from object
133   if ( !myObject.IsNull() )
134   {
135     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
136   
137     if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
138     {
139       Handle(HYDROData_ImmersibleZone) aZoneObj =
140         Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
141
142       TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
143       if ( !aZoneShape.IsNull() ) {
144         if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
145           TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
146           setFace( aZoneFace, false, false, "" );
147         } else {
148           myTopoShape = aZoneShape;
149           //TODO: myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
150           myDisplayMode = AIS_Shaded;
151
152           buildShape();
153           updateShape( false, false );
154         }
155       }
156
157       QColor aFillingColor = aZoneObj->GetFillingColor();
158       QColor aBorderColor = aZoneObj->GetBorderColor();
159
160       setFillingColor( aFillingColor, false, false );
161       setBorderColor( aBorderColor, false, false );
162     }
163     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
164     {
165       Handle(HYDROData_PolylineXY) aPolyline =
166         Handle(HYDROData_PolylineXY)::DownCast( myObject );
167
168       TopoDS_Shape aPolylineShape = aPolyline->GetShape();
169
170       if ( !aPolylineShape.IsNull() )
171       {
172         if ( aPolylineShape.ShapeType() == TopAbs_WIRE )
173         {
174           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
175           setWire( aPolylineWire, false, false );  
176         }
177         else
178         {
179           myTopoShape = aPolylineShape;
180           // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
181           // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
182           myDisplayMode = AIS_Shaded;
183
184           buildShape();
185           updateShape( false, false );
186         }
187       }
188
189       //QColor aWireColor = aPolyline->GetWireColor();
190       std::vector<QColor> aSectColors;
191       //TODO backward comp. with old aWireColor??
192       int nbSec = aPolyline->NbSections();
193       for (int i = 0; i < nbSec; i++)
194       {
195         QColor aColor;
196         aPolyline->GetSectionColor(i, aColor);
197         aSectColors.push_back(aColor);
198       }         
199
200       //
201       //setBorderColor( aWireColor, false, false );
202
203       foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
204       {
205         if( !myTopoShape.IsNull() )
206         {
207           Handle(HYDROGUI_Polyline) aPShape = Handle(HYDROGUI_Polyline)::DownCast( aShape );
208           bool WireOrCmp = myTopoShape.ShapeType() == TopAbs_WIRE || myTopoShape.ShapeType() == TopAbs_COMPOUND;
209           if ( !aPShape.IsNull() && WireOrCmp)
210           {
211             TopTools_IndexedMapOfShape MW;
212             TopExp::MapShapes(myTopoShape, TopAbs_WIRE, MW);
213             if (MW.Extent() == nbSec)
214             {
215               for (int i=0;i<nbSec;i++)
216               {
217                 const TopoDS_Shape& CW = MW(i+1);
218                 Quantity_Color aFColor( getQuantityColorVal( aSectColors[i].red() ), 
219                   getQuantityColorVal( aSectColors[i].green() ),
220                   getQuantityColorVal( aSectColors[i].blue() ),
221                   Quantity_TOC_RGB );
222                 aPShape->myShapeToColor.Add(CW, aFColor);
223               }
224             }
225           }
226         }
227       }    
228
229       //if ( theToDisplay && isVisible() && !myContext.IsNull() )  
230       //  displayShape( false );
231     }
232     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
233     {
234       Handle(HYDROData_Polyline3D) aPolyline =
235         Handle(HYDROData_Polyline3D)::DownCast( myObject );
236
237       TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
238
239       if ( !aPolylineShape.IsNull() ) {
240         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
241           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
242           setWire( aPolylineWire, false, false );  
243         } else {
244           myTopoShape = aPolylineShape;
245           // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
246           // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
247           myDisplayMode = AIS_Shaded;
248
249           buildShape();
250           updateShape( false, false );
251         }
252       }
253
254       QColor aWireColor = aPolyline->GetBorderColor();
255       setBorderColor( aWireColor, false, false );
256     }
257     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
258     {
259       Handle(HYDROData_Zone) aZone =
260         Handle(HYDROData_Zone)::DownCast( myObject );
261
262       TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
263
264       setFace( aZoneFace, false, false, "" );
265       if( aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
266       {
267         // Red color for a zone with bathymetry conflict
268         setFillingColor( Qt::red );
269       }
270       else
271       {
272                     // Set the filling color for zone
273         setFillingColor( aZone->GetColor( Qt::darkBlue ) );
274       }
275     }
276     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
277     {
278       Handle(HYDROData_Profile) aProfile =
279         Handle(HYDROData_Profile)::DownCast( myObject );
280
281       TopoDS_Wire aProfileWire;
282
283       if ( aProfile->IsValid() ) {
284         TopoDS_Shape aProfileShape = aProfile->GetShape3D();
285
286         if ( !aProfileShape.IsNull() && 
287              aProfileShape.ShapeType() == TopAbs_WIRE ) {
288           aProfileWire = TopoDS::Wire( aProfileShape );
289         }
290       }
291
292       setWire( aProfileWire, false, false );  
293
294       QColor aWireColor = aProfile->GetBorderColor();
295       setBorderColor( aWireColor, false, false );
296     }
297     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
298               myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
299               myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
300     {
301       Handle(HYDROData_Object) aGeomObject =
302         Handle(HYDROData_Object)::DownCast( myObject );
303
304       TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
305
306       setShape( anObjShape, false, false );
307
308       QColor aFillingColor = aGeomObject->GetFillingColor();
309       QColor aBorderColor = aGeomObject->GetBorderColor();
310
311       setFillingColor( aFillingColor, false, false );
312       setBorderColor( aBorderColor, false, false );
313     }
314     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
315     {
316       Handle(HYDROData_DummyObject3D) anObject3D =
317         Handle(HYDROData_DummyObject3D)::DownCast( myObject );
318       TopoDS_Shape aShape3D = anObject3D->GetShape();
319
320       setShape( aShape3D, false, false );
321
322       QColor aFillingColor = anObject3D->GetFillingColor();
323       QColor aBorderColor = anObject3D->GetBorderColor();
324
325       setFillingColor( aFillingColor, false, false );
326       setBorderColor( aBorderColor, false, false );
327     }
328     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
329     {
330       Handle(HYDROData_ShapesGroup) aShapesGroup =
331         Handle(HYDROData_ShapesGroup)::DownCast( myObject );
332
333       TopTools_SequenceOfShape aShapes;
334       aShapesGroup->GetShapes( aShapes );
335
336       TopoDS_Compound aCompound;
337       BRep_Builder aCompoundBuilder;
338       aCompoundBuilder.MakeCompound( aCompound );
339
340       for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
341       {
342         const TopoDS_Shape& aShape = aShapes.Value( i );
343         aCompoundBuilder.Add( aCompound, aShape );
344       }
345
346       setShape( aCompound, false, false );  
347     }
348     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_BCPolygon) ) )
349     {
350       Handle(HYDROData_BCPolygon) aBCObj = Handle(HYDROData_BCPolygon)::DownCast( myObject );
351
352       TopoDS_Shape aBCShape = aBCObj->GetTopShape();
353       if ( !aBCShape.IsNull() ) 
354       {
355         if ( aBCShape.ShapeType() == TopAbs_FACE ) 
356         {
357           TopoDS_Face aBCFace = TopoDS::Face( aBCShape );
358           setFace( aBCFace, false, false, "" );
359         } 
360         else 
361         {
362           myTopoShape = aBCShape;
363           myDisplayMode = AIS_Shaded;
364           buildShape();
365           updateShape( false, false );
366         }
367       }
368
369       QColor aFillingColor = aBCObj->GetFillingColor();
370       QColor aBorderColor = aBCObj->GetBorderColor();
371       aFillingColor.setAlpha(175);
372
373       setFillingColor( aFillingColor, false, false );
374       setBorderColor( aBorderColor, false, false );
375     }
376
377   }
378  
379   if ( myShapes.empty() || !isVisible() )
380     return;
381
382   displayShape( isUpdateViewer );
383
384   if (isDeactivateSelection)
385     foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
386       myContext->Deactivate( aShape );
387 }
388
389 void HYDROGUI_Shape::setVisible( const bool theState,
390                                  const bool theIsUpdateViewer )
391 {
392   myIsVisible = theState;
393
394   if ( myShapes.empty() )
395     return;
396
397   if ( ( myIsVisible && myContext->IsDisplayed( myShapes[0] ) ) ||
398        ( !myIsVisible && !myContext->IsDisplayed( myShapes[0] ) ) )
399     return;
400
401   if ( myIsVisible ) {
402     displayShape( theIsUpdateViewer );
403   }
404   else
405     eraseShape( theIsUpdateViewer );
406 }
407
408 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
409 {
410   if ( myIsHighlight == theIsHighlight )
411     return;
412
413   myIsHighlight = theIsHighlight;
414
415   if ( myContext.IsNull() || myShapes.empty() )
416     return;
417
418   colorShapeBorder( getActiveColor() );
419   displayShape( isUpdateViewer );
420 }
421
422 bool HYDROGUI_Shape::isHighlighted() const
423 {
424   return myIsHighlight;
425 }
426
427 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
428                               const bool         theToDisplay,
429                               const bool         theIsUpdateViewer )
430 {
431   myTopoShape = theWire;
432   // To avoid that hilight presentation is equal to "normal" object presentation.
433   // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
434   myDisplayMode = AIS_Shaded;
435
436   buildShape();
437   updateShape( theToDisplay, theIsUpdateViewer );
438 }
439
440 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
441                                const bool             theToDisplay,
442                                const bool             theIsUpdateViewer )
443 {
444   TopExp_Explorer anExp( theWires, TopAbs_WIRE );
445   TopoDS_Compound aCompound;
446   BRep_Builder aBuilder;
447     aBuilder.MakeCompound( aCompound );
448
449   for ( ; anExp.More(); anExp.Next() ) {
450     TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
451     if ( aWire.IsNull() ) {
452       continue;
453     }
454
455     BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
456     aMakeFace.Build();
457     if( aMakeFace.IsDone() ) {
458       aBuilder.Add( aCompound, aMakeFace.Face() );
459     }
460   }
461
462   myTopoShape = aCompound;
463   myDisplayMode = AIS_Shaded;
464
465   buildShape();
466   updateShape( theToDisplay, theIsUpdateViewer );
467 }
468
469 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
470                               const bool         theToDisplay,
471                               const bool         theIsUpdateViewer,
472                               const QString&     theTextureFileName )
473 {
474   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
475   aFaceBuilder.Build();
476   if( aFaceBuilder.IsDone() )
477   {
478     TopoDS_Face aFace = aFaceBuilder.Face();
479     setFace( aFace, theToDisplay, theIsUpdateViewer, theTextureFileName );
480   }
481 }
482
483 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
484                               const bool         theToDisplay,
485                               const bool         theIsUpdateViewer,
486                               const QString&     theTextureFileName )
487 {
488   myTopoShape = theFace;
489   myDisplayMode = theTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
490   //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
491   //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
492
493   buildShape();
494   updateShape( theToDisplay, theIsUpdateViewer );
495 }
496
497 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
498                                const bool          theToDisplay,
499                                const bool          theIsUpdateViewer,
500                                const int           theDisplayMode )
501 {
502   myTopoShape = theShape;
503   myDisplayMode = theDisplayMode;
504
505   buildShape();
506   updateShape( theToDisplay, theIsUpdateViewer );
507 }
508
509 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
510                                       const bool    theToDisplay,
511                                       const bool    theIsUpdateViewer )
512 {
513   myFillingColor = theColor;
514   updateShape( theToDisplay, theIsUpdateViewer );
515 }
516
517 QColor HYDROGUI_Shape::getFillingColor() const
518 {
519   return myFillingColor;
520 }
521
522 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
523                                      const bool    theToDisplay,
524                                      const bool    theIsUpdateViewer )
525
526   myBorderColor = theColor;
527   updateShape( theToDisplay, theIsUpdateViewer );
528 }
529
530 QColor HYDROGUI_Shape::getBorderColor() const
531 {
532   return myBorderColor;
533 }
534
535 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
536 {
537   myHighlightColor = theColor;
538 }
539
540 QColor HYDROGUI_Shape::getHighlightColor() const
541 {
542   return myHighlightColor;
543 }
544
545 void HYDROGUI_Shape::setZLayer( const int theZLayer )
546 {
547   if ( myZLayer == theZLayer )
548     return;
549
550   myZLayer = theZLayer;
551   if( isVisible() && !myContext.IsNull() && myZLayer >= 0 )
552     foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
553       myContext->SetZLayer( aShape, myZLayer );
554 }
555
556 QList<Handle(AIS_InteractiveObject)> HYDROGUI_Shape::createShape() const
557 {
558   QList<Handle(AIS_InteractiveObject)> shapes;
559   if( myTopoShape.IsNull() ) {
560     return shapes;
561   }
562
563   TopAbs_ShapeEnum aShapeType = myTopoShape.ShapeType();
564   bool IsWireEdgeCompound = aShapeType==TopAbs_COMPOUND;
565   if (IsWireEdgeCompound) {
566     TopoDS_Iterator itr(myTopoShape);
567     while (itr.More() && IsWireEdgeCompound) {
568       if (itr.Value().ShapeType() != TopAbs_WIRE && itr.Value().ShapeType() != TopAbs_EDGE)
569         IsWireEdgeCompound = false;
570       itr.Next();
571     }
572   }
573
574   if ( aShapeType==TopAbs_EDGE || aShapeType==TopAbs_WIRE || IsWireEdgeCompound)
575   {
576     SUIT_ResourceMgr* aResMgr = 0;
577     SUIT_Session* aSession = SUIT_Session::session();
578     if ( aSession )
579       aResMgr = SUIT_Session::session()->resourceMgr();
580
581     int aType = -1;
582     int aSize = -1;
583
584     if( myShapes.length()>1 )
585     {
586       Handle(HYDROGUI_Arrow) arrow = Handle(HYDROGUI_Arrow)::DownCast( myShapes[1] );
587       if( !arrow.IsNull() )
588       {
589         aType = arrow->GetType();
590         aSize = arrow->GetSize();
591       }
592     }
593     if( aResMgr && ( aType<0 || aSize < 0 ) )
594     {
595       aResMgr->value( "polyline", "arrow_type", aType );
596       aResMgr->value( "polyline", "arrow_size", aSize );
597     }
598
599     shapes.append( HYDROGUI_Polyline::createPresentations( myTopoShape, aType, aSize ) );
600   }
601   else
602   {
603     shapes.append( new HYDROGUI_AISShape( myTopoShape ) );
604   }
605   return shapes;
606 }
607
608 void HYDROGUI_Shape::buildShape()
609 {
610   // Erase previously created shape
611   erase(false);
612
613   myShapes = createShape();
614   if( myShapes.empty() )
615     return;
616
617   foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
618   {
619     Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( aShape );
620     if( !anAISShape.IsNull() )
621       anAISShape ->SetHLRAngleAndDeviation( 0.001 );
622
623     if ( !myObject.IsNull() )
624       aShape->SetOwner( myObject );
625
626     if ( !myObject.IsNull() && !myObject->IsKind( STANDARD_TYPE(HYDROData_LandCoverMap) ) )
627       aShape->SetTransparency( 0 );
628     aShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
629
630     // Init default params for shape
631     const Handle(Prs3d_Drawer)& anAttributes = aShape->Attributes();
632     if ( !anAttributes.IsNull() )
633     {
634       Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
635       if ( !anIsoAspect.IsNull() ) {
636         anIsoAspect->SetNumber( 0 );
637         anAttributes->SetUIsoAspect( anIsoAspect );
638       }
639       
640       anIsoAspect = anAttributes->VIsoAspect();
641       if ( !anIsoAspect.IsNull() )
642       {
643         anIsoAspect->SetNumber( 0 );
644         anAttributes->SetVIsoAspect( anIsoAspect );
645       }
646
647       if ( myDisplayMode == AIS_Shaded )
648       {
649         aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
650       }
651       else if ( myDisplayMode == AIS_WireFrame )
652       {
653         anAttributes->SetWireDraw( true );
654       }
655     }
656   }
657 }
658
659 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
660                                   const bool theIsUpdateViewer )
661 {
662   foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
663   {
664     const Handle(Prs3d_Drawer)& anAttributes = aShape->Attributes();
665     if ( !anAttributes.IsNull() )
666     {
667       if ( myDisplayMode == AIS_Shaded && myFillingColor.isValid() )
668       {
669         Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
670                                       getQuantityColorVal( myFillingColor.green() ),
671                                       getQuantityColorVal( myFillingColor.blue() ),
672                                       Quantity_TOC_RGB );
673   
674         aShape->SetColor( aFillingColor );
675         aShape->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
676       }
677       else if ( myDisplayMode == AIS_WireFrame )
678       {
679       }
680   
681       // Coloring borders
682     }
683   }
684   
685   colorShapeBorder( myBorderColor );
686   if ( !theToDisplay || !isVisible() || myContext.IsNull() )
687     return;
688   
689   displayShape( theIsUpdateViewer );
690 }
691
692 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
693 {
694   if ( myContext->HasOpenedContext() && mySelectionMode > 0 )
695     myContext->CloseLocalContext();
696
697   foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
698   {
699     if ( mySelectionMode > 0 )
700       // Display object in local context with selection
701       myContext->Display( aShape, myDisplayMode, mySelectionMode, Standard_False, Standard_False );
702     else
703     {
704       if ( !myContext->HasOpenedContext() )
705         // Ordinary display of object published in the Object Browser
706         myContext->Display( aShape, Standard_False );
707       else
708         // Display object in local context without selection
709         myContext->Display( aShape, myDisplayMode, -1, Standard_False, Standard_False );
710     }
711   }
712   
713   if ( mySelectionMode > 0 )
714   {
715     myContext->OpenLocalContext();
716     foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
717       myContext->Activate( aShape, mySelectionMode, Standard_True );
718   }
719
720   if ( myZLayer >= 0 )
721   {
722     foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
723       myContext->SetZLayer( aShape, myZLayer );
724   }
725
726   if ( theIsUpdateViewer ) {
727     myContext->UpdateCurrentViewer();
728   }
729 }
730
731 void HYDROGUI_Shape::eraseShape( const bool theIsUpdateViewer )
732 {
733   if ( myContext->HasOpenedContext() && mySelectionMode > 0 )
734     myContext->CloseLocalContext();
735
736   foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
737     myContext->Erase( aShape, Standard_False );
738
739   if( theIsUpdateViewer )
740     myContext->UpdateCurrentViewer();
741 }
742
743 QColor HYDROGUI_Shape::getActiveColor() const
744 {
745   return isHighlighted() ? myHighlightColor : myBorderColor;
746 }
747
748 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
749 {
750   return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
751 }
752
753 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
754 {
755   foreach( Handle(AIS_InteractiveObject) aShape1, myShapes )
756   {
757     const Handle(Prs3d_Drawer)& anAttributes = aShape1->Attributes();
758     if ( anAttributes.IsNull() )
759       continue;
760
761     Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
762                                  getQuantityColorVal( theColor.green() ),
763                                  getQuantityColorVal( theColor.blue() ),
764                                  Quantity_TOC_RGB );
765   
766     if( !myTopoShape.IsNull() )
767     {
768       if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode
769       {
770         aShape1->SetColor( aBorderColor );
771       }
772       else if ( myDisplayMode == AIS_Shaded )
773       {
774         Handle(HYDROGUI_AISShape) aShape = Handle(HYDROGUI_AISShape)::DownCast( aShape1 );
775         if( !aShape.IsNull() )
776           aShape->SetBorderColor( aBorderColor );
777       }
778       else if ( myDisplayMode == AIS_WireFrame )
779       {
780         aShape1->SetColor( aBorderColor );
781       }
782     }
783   }
784 }
785
786 void HYDROGUI_Shape::setDisplayMode( int theDisplayMode )
787 {
788   myDisplayMode = theDisplayMode;
789 }
790
791 void HYDROGUI_Shape::setSelectionMode( int theSelectionMode )
792 {
793   mySelectionMode = theSelectionMode;
794 }