Salome HOME
refs #327 - Polyline is not shown during creation
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Shape.cxx
1 // Copyright (C) 2007-2013  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.
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 #include "HYDROGUI_Shape.h"
24
25 #include "HYDROGUI_DataObject.h"
26 #include "HYDROGUI_Tool.h"
27
28 #include <AIS_Drawer.hxx>
29 #include <AIS_TexturedShape.hxx>
30
31 #include <V3d_Viewer.hxx>
32
33 #include <BRepBuilderAPI_MakeEdge.hxx>
34 #include <BRepBuilderAPI_MakeWire.hxx>
35 #include <BRepBuilderAPI_MakeFace.hxx>
36
37 #include <gp_Pnt.hxx>
38
39 #include <Graphic3d_AspectFillArea3d.hxx>
40 #include <Graphic3d_MaterialAspect.hxx>
41
42 #include <HYDROData_Channel.h>
43 #include <HYDROData_Document.h>
44 #include <HYDROData_DummyObject3D.h>
45 #include <HYDROData_Image.h>
46 #include <HYDROData_ImmersibleZone.h>
47 #include <HYDROData_Obstacle.h>
48 #include <HYDROData_PolylineXY.h>
49 #include <HYDROData_Polyline3D.h>
50 #include <HYDROData_Profile.h>
51 #include <HYDROData_Region.h>
52 #include <HYDROData_ShapesGroup.h>
53 #include <HYDROData_Stream.h>
54 #include <HYDROData_Zone.h>
55
56 #include <TopoDS.hxx>
57 #include <TopoDS_Wire.hxx>
58 #include <TopoDS_Face.hxx>
59
60 #include <TopExp_Explorer.hxx>
61
62 #include <BRep_Builder.hxx>
63
64 #include <Precision.hxx>
65
66 #include <Prs3d_ShadingAspect.hxx>
67 #include <Prs3d_LineAspect.hxx>
68 #include <Prs3d_IsoAspect.hxx>
69
70 #include <SUIT_MessageBox.h>
71
72 #include <QColor>
73 #include <QFile>
74
75 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
76                                 const Handle(HYDROData_Entity)&       theObject,
77                                 const int                             theZLayer )
78 : myContext( theContext ),
79   myObject( theObject ),
80   myZLayer( theZLayer ),
81   myIsHighlight( false ),
82   myFillingColor( Qt::transparent ),
83   myBorderColor( Qt::black ),
84   myHighlightColor( Qt::white ),
85   myIsToUpdate( false ),
86   myIsVisible( true ),
87   myDisplayMode( AIS_WireFrame )
88 {
89 }
90
91 HYDROGUI_Shape::~HYDROGUI_Shape()
92 {
93   erase( false );
94
95   if ( !myShape.IsNull() )
96     myShape.Nullify();
97
98   removeTextureFile();
99 }
100
101 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
102 {
103   if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
104     return;
105
106   displayShape( theIsUpdateViewer );
107 }
108
109 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
110 {
111   if ( myContext.IsNull() || myShape.IsNull() )
112     return;
113
114   myContext->Erase( myShape, theIsUpdateViewer );
115 }
116
117 void HYDROGUI_Shape::update( const bool theIsUpdateViewer,
118                              const bool theIsDisplayOnTop )
119 {
120   setIsToUpdate( false );
121
122   if ( myContext.IsNull() )
123     return;
124
125   bool isDeactivateSelection = false;
126   // Try to retrieve information from object
127   if ( !myObject.IsNull() )
128   {
129     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
130   
131     if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
132     {
133       Handle(HYDROData_ImmersibleZone) aZoneObj =
134         Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
135
136       TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
137       if ( !aZoneShape.IsNull() ) {
138         if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
139           TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
140           setFace( aZoneFace, false, false );
141         } else {
142           myTopoShape = aZoneShape;
143           myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
144
145           buildShape();
146           updateShape( false, false );
147         }
148       }
149
150       QColor aFillingColor = aZoneObj->GetFillingColor();
151       QColor aBorderColor = aZoneObj->GetBorderColor();
152
153       setFillingColor( aFillingColor, false, false );
154       setBorderColor( aBorderColor, false, false );
155     }
156     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
157     {
158       Handle(HYDROData_PolylineXY) aPolyline =
159         Handle(HYDROData_PolylineXY)::DownCast( myObject );
160
161       TopoDS_Shape aPolylineShape = aPolyline->GetShape();
162
163       if ( !aPolylineShape.IsNull() ) {
164         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
165           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
166           setWire( aPolylineWire, false, false );  
167         } else {
168           myTopoShape = aPolylineShape;
169           myDisplayMode = AIS_WireFrame;
170
171           buildShape();
172           updateShape( false, false );
173         }
174       }
175
176       QColor aWireColor = aPolyline->GetWireColor();
177       setBorderColor( aWireColor, false, false );
178     }
179     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
180     {
181       Handle(HYDROData_Polyline3D) aPolyline =
182         Handle(HYDROData_Polyline3D)::DownCast( myObject );
183
184       TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
185
186       if ( !aPolylineShape.IsNull() ) {
187         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
188           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
189           setWire( aPolylineWire, false, false );  
190         } else {
191           myTopoShape = aPolylineShape;
192           myDisplayMode = AIS_WireFrame;
193
194           buildShape();
195           updateShape( false, false );
196         }
197       }
198
199       QColor aWireColor = aPolyline->GetBorderColor();
200       setBorderColor( aWireColor, false, false );
201     }
202     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
203     {
204       Handle(HYDROData_Zone) aZone =
205         Handle(HYDROData_Zone)::DownCast( myObject );
206
207       TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
208
209       setFace( aZoneFace, false, false );
210       if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
211       {
212         // Red color for a zone with bathymetry conflict
213         setFillingColor( Qt::red );
214       }
215       else
216       {
217         // Generate the filling color for zone
218         QStringList aGeomObjectsNames;
219
220         HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
221         HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
222         for ( ; anIter.More(); anIter.Next() )
223         {
224           Handle(HYDROData_Object) aRefbject = 
225             Handle(HYDROData_Object)::DownCast( anIter.Value() );
226           if ( aRefbject.IsNull() )
227             continue;
228
229           QString aRefObjectName = aRefbject->GetName();
230           if ( aRefObjectName.isEmpty() )
231             continue;
232
233           aGeomObjectsNames.append( aRefObjectName );
234         }
235
236         setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
237       }
238     }
239     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
240     {
241       Handle(HYDROData_Image) anImageObj =
242         Handle(HYDROData_Image)::DownCast( myObject );
243
244       removeTextureFile();
245
246       QString aTextureFileName = generateTextureFileName( anImageObj );
247
248       QImage anImage = anImageObj->Image();
249       QString aFilePath = anImageObj->GetFilePath();
250       QTransform aTrsf = anImageObj->Trsf();
251
252       int aWidth = anImage.width();
253       int aHeight = anImage.height();
254
255       QString anImageError = "";
256
257       QTransform anInversion = QTransform::fromScale( -1, -1 );
258       anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
259
260       if ( anImage.isNull() )
261         anImageError = QObject::tr( "IMAGE_TRANSFORMATION_CAN_NOT_BE_APPLYED" );
262       else
263       {
264         // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
265         int aTrsfWidth = anImage.width();
266         int aDelta = aTrsfWidth % 4;
267         if ( aDelta > 0 )
268         {
269           aTrsfWidth += ( 4 - aDelta );
270         }
271         anImage = anImage.scaledToWidth( aTrsfWidth );
272
273         // temporary optimization, to reduce the saved image size (and the texture quality)
274         QImage anImageToSave = anImage; //RKV:reduceTexture( anImage, 500 );
275
276         bool isSaved = anImageToSave.save( aTextureFileName );
277         if ( !isSaved )
278           anImageError = QObject::tr( "FILE_CAN_NOT_BE_CREATED" ).arg( aTextureFileName );
279       }
280
281       if ( !anImageError.isEmpty() )
282       {
283         SUIT_MessageBox::warning( 0, QObject::tr( "SHAPE_IMAGE_ERROR" ),
284                                   QObject::tr( "IMAGE_CAN_NOT_BE_CREATED" ) + anImageError );
285       }
286
287       QPointF aPoint1( 0, 0 );            // 1: top left
288       QPointF aPoint2( aWidth, 0 );       // 2: top right
289       QPointF aPoint3( aWidth, aHeight ); // 3: bottom right
290       QPointF aPoint4( 0, aHeight );      // 4: bottom left
291
292       aPoint1 = aTrsf.map( aPoint1 );
293       aPoint2 = aTrsf.map( aPoint2 );
294       aPoint3 = aTrsf.map( aPoint3 );
295       aPoint4 = aTrsf.map( aPoint4 );
296
297       QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
298       QRectF aRect = aPolygon.boundingRect();
299
300       gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
301       gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
302       gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
303       gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
304
305       TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
306       TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
307       TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
308       TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
309
310       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
311       aWire.Closed( true );
312
313       setTextureFileName( aTextureFileName, false, false );
314       setFace( aWire, false, false );
315       isDeactivateSelection = true;
316     }
317     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
318     {
319       Handle(HYDROData_Profile) aProfile =
320         Handle(HYDROData_Profile)::DownCast( myObject );
321
322       TopoDS_Wire aProfileWire;
323
324       if ( aProfile->IsValid() ) {
325         TopoDS_Shape aProfileShape = aProfile->GetShape3D();
326
327         if ( !aProfileShape.IsNull() && 
328              aProfileShape.ShapeType() == TopAbs_WIRE ) {
329           aProfileWire = TopoDS::Wire( aProfileShape );
330         }
331       }
332
333       setWire( aProfileWire, false, false );  
334
335       QColor aWireColor = aProfile->GetBorderColor();
336       setBorderColor( aWireColor, false, false );
337     }
338     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
339               myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
340               myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
341     {
342       Handle(HYDROData_Object) aGeomObject =
343         Handle(HYDROData_Object)::DownCast( myObject );
344
345       TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
346
347       setShape( anObjShape, false, false );
348
349       QColor aFillingColor = aGeomObject->GetFillingColor();
350       QColor aBorderColor = aGeomObject->GetBorderColor();
351
352       setFillingColor( aFillingColor, false, false );
353       setBorderColor( aBorderColor, false, false );
354     }
355     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
356     {
357       Handle(HYDROData_DummyObject3D) anObject3D =
358         Handle(HYDROData_DummyObject3D)::DownCast( myObject );
359       TopoDS_Shape aShape3D = anObject3D->GetShape();
360
361       setShape( aShape3D, false, false );
362
363       QColor aFillingColor = anObject3D->GetFillingColor();
364       QColor aBorderColor = anObject3D->GetBorderColor();
365
366       setFillingColor( aFillingColor, false, false );
367       setBorderColor( aBorderColor, false, false );
368     }
369     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
370     {
371       Handle(HYDROData_ShapesGroup) aShapesGroup =
372         Handle(HYDROData_ShapesGroup)::DownCast( myObject );
373
374       TopTools_SequenceOfShape aShapes;
375       aShapesGroup->GetShapes( aShapes );
376
377       TopoDS_Compound aCompound;
378       BRep_Builder aCompoundBuilder;
379       aCompoundBuilder.MakeCompound( aCompound );
380
381       for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
382       {
383         const TopoDS_Shape& aShape = aShapes.Value( i );
384         aCompoundBuilder.Add( aCompound, aShape );
385       }
386
387       setShape( aCompound, false, false );  
388     }
389   }
390  
391   if ( myShape.IsNull() || !isVisible() )
392     return;
393
394   displayShape( theIsUpdateViewer );
395   // the following code is not necessary if the Z layer is set for the shape
396   if ( myZLayer && theIsDisplayOnTop )
397   {
398     // Display the shape on the top Z layer
399     Standard_Integer aNewLayerId = -1;
400     if ( myContext->CurrentViewer() && 
401          myContext->CurrentViewer()->AddZLayer( aNewLayerId ) &&
402          aNewLayerId > 0 ) {
403       myContext->SetZLayer( myShape, aNewLayerId );
404     }
405   }
406
407   if (isDeactivateSelection)
408     myContext->Deactivate(myShape);
409 }
410
411 void HYDROGUI_Shape::setVisible( const bool theState,
412                                  const bool theIsUpdateViewer )
413 {
414   myIsVisible = theState;
415
416   if ( myShape.IsNull() )
417     return;
418
419   if ( ( myIsVisible && myContext->IsDisplayed( myShape ) ) ||
420        ( !myIsVisible && !myContext->IsDisplayed( myShape ) ) )
421     return;
422
423   if ( myIsVisible ) {
424     displayShape( theIsUpdateViewer );
425   }
426   else
427     myContext->Erase( myShape, theIsUpdateViewer );
428 }
429
430 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
431 {
432   if ( myIsHighlight == theIsHighlight )
433     return;
434
435   myIsHighlight = theIsHighlight;
436
437   if ( myContext.IsNull() || myShape.IsNull() )
438     return;
439
440   colorShapeBorder( getActiveColor() );
441   displayShape( isUpdateViewer );
442 }
443
444 bool HYDROGUI_Shape::isHighlighted() const
445 {
446   return myIsHighlight;
447 }
448
449 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
450                               const bool         theToDisplay,
451                               const bool         theIsUpdateViewer )
452 {
453   myTopoShape = theWire;
454   myDisplayMode = AIS_WireFrame;
455
456   buildShape();
457   updateShape( theToDisplay, theIsUpdateViewer );
458 }
459
460 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
461                                const bool             theToDisplay,
462                                const bool             theIsUpdateViewer )
463 {
464   TopExp_Explorer anExp( theWires, TopAbs_WIRE );
465   TopoDS_Compound aCompound;
466   BRep_Builder aBuilder;
467     aBuilder.MakeCompound( aCompound );
468
469   for ( ; anExp.More(); anExp.Next() ) {
470     TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
471     if ( aWire.IsNull() ) {
472       continue;
473     }
474
475     BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
476     aMakeFace.Build();
477     if( aMakeFace.IsDone() ) {
478       aBuilder.Add( aCompound, aMakeFace.Face() );
479     }
480   }
481
482   myTopoShape = aCompound;
483   myDisplayMode = AIS_Shaded;
484
485   buildShape();
486   updateShape( theToDisplay, theIsUpdateViewer );
487 }
488
489 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
490                               const bool         theToDisplay,
491                               const bool         theIsUpdateViewer )
492 {
493   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
494   aFaceBuilder.Build();
495   if( aFaceBuilder.IsDone() )
496   {
497     TopoDS_Face aFace = aFaceBuilder.Face();
498     setFace( aFace, theToDisplay, theIsUpdateViewer );
499   }
500 }
501
502 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
503                               const bool         theToDisplay,
504                               const bool         theIsUpdateViewer )
505 {
506   myTopoShape = theFace;
507   myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
508   //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
509   //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
510
511   buildShape();
512   updateShape( theToDisplay, theIsUpdateViewer );
513 }
514
515 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
516                                const bool          theToDisplay,
517                                const bool          theIsUpdateViewer )
518 {
519   myTopoShape = theShape;
520   myDisplayMode = AIS_Shaded;
521
522   buildShape();
523   updateShape( theToDisplay, theIsUpdateViewer );
524 }
525
526 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
527                                       const bool    theToDisplay,
528                                       const bool    theIsUpdateViewer )
529 {
530   myFillingColor = theColor;
531   updateShape( theToDisplay, theIsUpdateViewer );
532 }
533
534 QColor HYDROGUI_Shape::getFillingColor() const
535 {
536   return myFillingColor;
537 }
538
539 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
540                                      const bool    theToDisplay,
541                                      const bool    theIsUpdateViewer )
542 {
543   myBorderColor = theColor;
544   updateShape( theToDisplay, theIsUpdateViewer );
545 }
546
547 QColor HYDROGUI_Shape::getBorderColor() const
548 {
549   return myBorderColor;
550 }
551
552 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
553 {
554   myHighlightColor = theColor;
555 }
556
557 QColor HYDROGUI_Shape::getHighlightColor() const
558 {
559   return myHighlightColor;
560 }
561
562 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
563                                          const bool     theToDisplay,
564                                          const bool     theIsUpdateViewer )
565 {
566   myTextureFileName = theFileName;
567   updateShape( theToDisplay, theIsUpdateViewer );
568 }
569
570 QString HYDROGUI_Shape::getTextureFileName() const
571 {
572   return myTextureFileName;
573 }
574
575 void HYDROGUI_Shape::setZLayer( const int theZLayer )
576 {
577   if ( myZLayer == theZLayer )
578     return;
579
580   myZLayer = theZLayer;
581   if ( !myShape.IsNull() && isVisible() && !myContext.IsNull() && myZLayer >= 0 )
582     myContext->SetZLayer( myShape, myZLayer );
583 }
584
585 void HYDROGUI_Shape::buildShape()
586 {
587   // Erase previously created shape
588   erase();
589
590   if ( myTopoShape.IsNull() ) {
591     myShape.Nullify();
592     return;
593   }
594
595   QString aTextureFileName = getTextureFileName();
596   bool anIsTexture = !aTextureFileName.isEmpty();
597
598   if ( anIsTexture )
599   {
600     myShape = new AIS_TexturedShape( myTopoShape );
601   }
602   else
603   {
604     myShape = new AIS_Shape( myTopoShape );
605   }
606
607   myShape->SetHLRAngleAndDeviation( 0.001 );
608
609   if ( !myObject.IsNull() )
610     myShape->SetOwner( myObject );
611
612   myShape->SetTransparency( 0 );
613   myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
614
615   if( anIsTexture )
616   {
617     Handle(AIS_TexturedShape) aTexturedShape = 
618       Handle(AIS_TexturedShape)::DownCast( myShape );
619
620     aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
621     aTexturedShape->SetTextureMapOn();
622     // Just use the texture image as is
623     aTexturedShape->DisableTextureModulate();
624     aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
625   }
626
627     // Init default params for shape
628   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
629   if ( !anAttributes.IsNull() )
630   {
631     Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
632     if ( !anIsoAspect.IsNull() ) {
633       anIsoAspect->SetNumber( 0 );
634       anAttributes->SetUIsoAspect( anIsoAspect );
635     }
636       
637     anIsoAspect = anAttributes->VIsoAspect();
638     if ( !anIsoAspect.IsNull() ) {
639       anIsoAspect->SetNumber( 0 );
640       anAttributes->SetVIsoAspect( anIsoAspect );
641     }
642
643     if ( myDisplayMode == AIS_Shaded )
644     {
645       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
646       if ( !aShadingAspect.IsNull() )
647       {
648         Graphic3d_MaterialAspect aMatAspect( Graphic3d_NOM_PLASTIC );
649         //aMatAspect.SetAmbient( 1 );
650         //aMatAspect.SetDiffuse( 0 );
651
652         aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
653         aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
654       }
655     }
656     else if ( myDisplayMode == AIS_WireFrame )
657     {
658       anAttributes->SetWireDraw( true );
659     }
660   }
661 }
662
663 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
664                                   const bool theIsUpdateViewer )
665 {
666   if ( myShape.IsNull() )
667     return;
668
669   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
670   if ( !anAttributes.IsNull() )
671   {
672     if ( myDisplayMode == AIS_Shaded )
673     {
674       // Coloring face filling
675       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
676       if ( !aShadingAspect.IsNull() )
677       {
678         Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
679                                       getQuantityColorVal( myFillingColor.green() ),
680                                       getQuantityColorVal( myFillingColor.blue() ),
681                                       Quantity_TOC_RGB );
682
683         aShadingAspect->SetColor( aFillingColor );
684         aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
685       }
686     }
687     else if ( myDisplayMode == AIS_WireFrame )
688     {
689     }
690
691     // Coloring borders
692     colorShapeBorder( getActiveColor() );
693   }
694
695   if ( !theToDisplay || !isVisible() || myContext.IsNull() )
696     return;
697   
698   displayShape( theIsUpdateViewer );
699 }
700
701 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
702 {
703   myContext->Display( myShape, Standard_False );
704
705   if ( myZLayer >= 0 )
706     myContext->SetZLayer( myShape, myZLayer );
707
708   myContext->UpdateCurrentViewer();
709 }
710
711 QColor HYDROGUI_Shape::getActiveColor() const
712 {
713   return isHighlighted() ? myHighlightColor : myBorderColor;
714 }
715
716 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
717 {
718   return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
719 }
720
721 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
722 {
723   if ( myShape.IsNull() )
724     return;
725
726   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
727   if ( anAttributes.IsNull() )
728     return;
729
730   Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
731                                getQuantityColorVal( theColor.green() ),
732                                getQuantityColorVal( theColor.blue() ),
733                                Quantity_TOC_RGB );
734   if ( myDisplayMode == AIS_Shaded )
735   {
736     if ( theColor.alpha() == 0 )
737     {
738       anAttributes->SetFaceBoundaryDraw( false );
739     }
740     else
741     {
742       anAttributes->SetFaceBoundaryDraw( true );
743
744       Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
745       if ( !aBoundaryAspect.IsNull() )
746       {
747         aBoundaryAspect->SetColor( aBorderColor );
748         anAttributes->SetFaceBoundaryAspect( aBoundaryAspect );
749       }
750     }
751   }
752   else if ( myDisplayMode == AIS_WireFrame )
753   {
754     myShape->SetColor( aBorderColor );
755   }
756 }
757
758 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
759 {
760   QString aResult;
761   if( !theImageObj.IsNull() )
762   {
763     QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
764
765     int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
766     QString aPrefix = QString( "image_%1" ).arg( aStudyId );
767
768     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
769     anEntry.replace( ':', '_' );
770
771     QString anExtension = "bmp";
772
773     aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
774   }
775   return aResult;
776 }
777
778 void HYDROGUI_Shape::removeTextureFile() const
779 {
780   QFile aFile( getTextureFileName() );
781   if( aFile.exists() )
782     aFile.remove();
783 }
784
785 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
786 {
787   double aSizeLimit = (double)theSizeLimit;
788   double aWidth = (double)theImage.width();
789   double aHeight = (double)theImage.height();
790   if( aWidth > aSizeLimit || aHeight > aSizeLimit )
791   {
792     if( aWidth > aHeight )
793     {
794       aHeight /= ( aWidth / aSizeLimit );
795       aWidth = aSizeLimit;
796     }
797     else
798     {
799       aWidth /= ( aHeight / aSizeLimit );
800       aHeight = aSizeLimit;
801     }
802   }
803   return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );
804 }