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