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