]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_Shape.cxx
Salome HOME
107fb7436f3a48553a6b1ca92dec05dc3a79a26e
[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 <AIS_Drawer.hxx>
26
27 #include <BRepBuilderAPI_MakeEdge.hxx>
28 #include <BRepBuilderAPI_MakeWire.hxx>
29 #include <BRepBuilderAPI_MakeFace.hxx>
30
31 #include <gp_Pnt.hxx>
32
33 #include <Graphic3d_AspectFillArea3d.hxx>
34 #include <Graphic3d_MaterialAspect.hxx>
35
36 #include <TopoDS_Wire.hxx>
37 #include <TopoDS_Face.hxx>
38
39 #include <Prs3d_ShadingAspect.hxx>
40 #include <Prs3d_LineAspect.hxx>
41 #include <Prs3d_IsoAspect.hxx>
42
43 #include <QColor>
44
45 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext )
46 : myContext( theContext ),
47   myIsHighlight( false ),
48   myZIndex( 0.0 ),
49   myFillingColor( Qt::green ),
50   myBorderColor( Qt::black ),
51   myHighlightColor( Qt::white )
52 {
53 }
54
55 HYDROGUI_Shape::~HYDROGUI_Shape()
56 {
57   erase();
58
59   if ( !myShape.IsNull() )
60     myShape.Nullify();
61 }
62
63 void HYDROGUI_Shape::display()
64 {
65   if ( !myContext || myShape.IsNull() )
66     return;
67
68   myContext->Display( myShape );
69 }
70
71 void HYDROGUI_Shape::erase()
72 {
73   if ( !myContext || myShape.IsNull() )
74     return;
75
76   myContext->Erase( myShape );
77 }
78
79 void HYDROGUI_Shape::highlight( bool theIsHighlight )
80 {
81   if ( myIsHighlight == theIsHighlight )
82     return;
83
84   myIsHighlight = theIsHighlight;
85
86   if ( !myContext || myShape.IsNull() )
87     return;
88
89   colorShapeBorder( getActiveColor() );
90   myContext->Display( myShape );
91 }
92
93 bool HYDROGUI_Shape::isHighlighted() const
94 {
95   return myIsHighlight;
96 }
97
98 void HYDROGUI_Shape::setPath( const QPainterPath& thePath,
99                               const bool          theToDisplay )
100 {
101   myPath = thePath;
102   buildShape();
103   updateShape( theToDisplay );
104 }
105
106 QPainterPath HYDROGUI_Shape::getPath() const
107 {
108   return myPath;
109 }
110
111 void HYDROGUI_Shape::setZIndex( const double theZIndex,
112                                 const bool   theToDisplay )
113 {
114   myZIndex = theZIndex;
115   buildShape();
116   updateShape( theToDisplay );
117 }
118
119 double HYDROGUI_Shape::getZIndex() const
120 {
121   return myZIndex;
122 }
123
124 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
125                                       const bool    theToDisplay )
126 {
127   myFillingColor = theColor;
128   updateShape( theToDisplay );
129 }
130
131 QColor HYDROGUI_Shape::getFillingColor() const
132 {
133   return myFillingColor;
134 }
135
136 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
137                                      const bool    theToDisplay )
138 {
139   myBorderColor = theColor;
140   updateShape( theToDisplay );
141 }
142
143 QColor HYDROGUI_Shape::getBorderColor() const
144 {
145   return myBorderColor;
146 }
147
148 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
149 {
150   myHighlightColor = theColor;
151 }
152
153 QColor HYDROGUI_Shape::getHighlightColor() const
154 {
155   return myHighlightColor;
156 }
157
158 void HYDROGUI_Shape::buildShape()
159 {
160   // Erase previously created shape
161   erase();
162
163   if ( myPath.isEmpty() )
164     return;
165
166   BRepBuilderAPI_MakeWire aMakeWire;
167
168   // Build new shape from path
169   gp_Pnt aPrevPoint( 0, 0, myZIndex );
170   for ( int i = 0; i < myPath.elementCount(); ++i )
171   {
172     const QPainterPath::Element& aPathElem = myPath.elementAt( i );
173
174     gp_Pnt aCurPoint( aPathElem.x, aPathElem.y, myZIndex );
175     switch ( aPathElem.type )
176     {
177       case QPainterPath::LineToElement:
178       {
179         BRepBuilderAPI_MakeEdge aMakeEdge( aPrevPoint, aCurPoint );
180         aMakeEdge.Build();
181         if ( aMakeEdge.IsDone() )
182         {
183           TopoDS_Edge anEdge = aMakeEdge;
184           aMakeWire.Add( anEdge );
185         }
186         break;
187       }
188       case QPainterPath::CurveToElement:
189       {
190         // TODO
191         break;
192       }
193       case QPainterPath::MoveToElement:
194       default:
195         break;
196     }
197
198     aPrevPoint = aCurPoint;
199   }
200
201   TopoDS_Face aResShape;
202
203   aMakeWire.Build();
204   if ( aMakeWire.IsDone() )
205   {
206     TopoDS_Wire aCommonWire = aMakeWire;
207
208     BRepBuilderAPI_MakeFace aMakeFace( aCommonWire, true );
209     aMakeFace.Build();
210     if ( aMakeFace.IsDone() )
211        aResShape = aMakeFace;
212   }
213
214   myShape = new AIS_Shape( aResShape );
215
216   myShape->SetTransparency( 0 );
217   myShape->SetDisplayMode( AIS_Shaded );
218
219   // Init default params for shape
220   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
221   if ( !anAttributes.IsNull() )
222   {
223     Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
224     if ( !anIsoAspect.IsNull() )
225       anIsoAspect->SetNumber( 0 );
226     
227     anIsoAspect = anAttributes->VIsoAspect();
228     if ( !anIsoAspect.IsNull() )
229       anIsoAspect->SetNumber( 0 );
230
231     Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
232     if ( !aShadingAspect.IsNull() )
233     {
234       Graphic3d_MaterialAspect aMatAspect;
235           aMatAspect.SetAmbient( 1 );
236           aMatAspect.SetDiffuse( 0 );
237         
238           aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
239           aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
240     }
241   }
242 }
243
244 void HYDROGUI_Shape::updateShape( const bool theIsForce )
245 {
246   if ( myShape.IsNull() )
247     return;
248
249   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
250   if ( !anAttributes.IsNull() )
251   {
252     // Coloring face filling
253     Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
254     if ( !aShadingAspect.IsNull() )
255     {
256       Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
257                                     getQuantityColorVal( myFillingColor.green() ),
258                                     getQuantityColorVal( myFillingColor.blue() ),
259                                     Quantity_TOC_RGB );
260
261       aShadingAspect->SetColor( aFillingColor );
262       aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
263     }
264
265     // Coloring borders
266     colorShapeBorder( getActiveColor() );
267   }
268
269   if ( !theIsForce || !myContext )
270     return;
271   
272   myContext->Display( myShape );
273 }
274
275 QColor HYDROGUI_Shape::getActiveColor() const
276 {
277   return isHighlighted() ? myHighlightColor : myBorderColor;
278 }
279
280 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
281 {
282   return theColorVal == 0 ? 0 : (double)( theColorVal / 255 );
283 }
284
285 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
286 {
287   if ( myShape.IsNull() )
288     return;
289
290   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
291   if ( anAttributes.IsNull() )
292     return;
293
294   if ( theColor.alpha() == 0 )
295   {
296     anAttributes->SetFaceBoundaryDraw( false );
297   }
298   else
299   {
300     Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
301                                  getQuantityColorVal( theColor.green() ),
302                                  getQuantityColorVal( theColor.blue() ),
303                                  Quantity_TOC_RGB );
304
305     anAttributes->SetFaceBoundaryDraw( true );
306
307     Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
308     if ( !aBoundaryAspect.IsNull() )
309     {
310       aBoundaryAspect->SetColor( aBorderColor );
311     }
312   }
313 }
314
315