1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_Shape.h"
25 #include <AIS_Drawer.hxx>
27 #include <BRepBuilderAPI_MakeEdge.hxx>
28 #include <BRepBuilderAPI_MakeWire.hxx>
29 #include <BRepBuilderAPI_MakeFace.hxx>
33 #include <Graphic3d_AspectFillArea3d.hxx>
34 #include <Graphic3d_MaterialAspect.hxx>
36 #include <HYDROData_Domain.h>
37 #include <HYDROData_Polyline.h>
39 #include <TopoDS_Wire.hxx>
40 #include <TopoDS_Face.hxx>
42 #include <Precision.hxx>
44 #include <Prs3d_ShadingAspect.hxx>
45 #include <Prs3d_LineAspect.hxx>
46 #include <Prs3d_IsoAspect.hxx>
50 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
51 const Handle(HYDROData_Object)& theObject )
52 : myContext( theContext ),
53 myObject( theObject ),
54 myIsHighlight( false ),
55 myFillingColor( Qt::transparent ),
56 myBorderColor( Qt::black ),
57 myHighlightColor( Qt::white ),
58 myIsToUpdate( false ),
60 myDisplayMode( AIS_WireFrame )
64 HYDROGUI_Shape::~HYDROGUI_Shape()
68 if ( !myShape.IsNull() )
72 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
74 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
77 myContext->Display( myShape, theIsUpdateViewer );
80 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
82 if ( myContext.IsNull() || myShape.IsNull() )
85 myContext->Erase( myShape, theIsUpdateViewer );
88 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
90 setIsToUpdate( false );
92 if ( myContext.IsNull() )
95 // Try to retrieve information from object
96 if ( !myObject.IsNull() )
98 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Domain) ) )
100 Handle(HYDROData_Domain) aDomainObj =
101 Handle(HYDROData_Domain)::DownCast( myObject );
103 QColor aFillingColor = aDomainObj->GetFillingColor();
104 QColor aBorderColor = aDomainObj->GetBorderColor();
105 TopoDS_Face aDomainFace = aDomainObj->Face();
107 setFillingColor( aFillingColor, false, false );
108 setBorderColor( aBorderColor, false, false );
109 setFace( aDomainFace, false, false );
111 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline) ) )
113 Handle(HYDROData_Polyline) aPolyline =
114 Handle(HYDROData_Polyline)::DownCast( myObject );
116 TopoDS_Wire aPolylineWire = aPolyline->Wire();
118 setWire( aPolylineWire, false, false );
122 if ( myShape.IsNull() || !isVisible() )
125 myContext->Display( myShape, theIsUpdateViewer );
128 void HYDROGUI_Shape::setVisible( const bool theState,
129 const bool theIsUpdateViewer )
131 if ( myIsVisible == theState )
134 myIsVisible = theState;
136 if ( myShape.IsNull() )
140 myContext->Display( myShape, theIsUpdateViewer );
142 myContext->Erase( myShape, theIsUpdateViewer );
145 void HYDROGUI_Shape::highlight( bool theIsHighlight )
147 if ( myIsHighlight == theIsHighlight )
150 myIsHighlight = theIsHighlight;
152 if ( myContext.IsNull() || myShape.IsNull() )
155 colorShapeBorder( getActiveColor() );
156 myContext->Display( myShape );
159 bool HYDROGUI_Shape::isHighlighted() const
161 return myIsHighlight;
164 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
165 const bool theToDisplay,
166 const bool theIsUpdateViewer )
168 myTopoShape = theWire;
169 myDisplayMode = AIS_WireFrame;
172 updateShape( theToDisplay, theIsUpdateViewer );
175 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
176 const bool theToDisplay,
177 const bool theIsUpdateViewer )
179 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
180 aFaceBuilder.Build();
181 if( aFaceBuilder.IsDone() )
183 TopoDS_Face aFace = aFaceBuilder.Face();
184 setFace( aFace, theToDisplay, theIsUpdateViewer );
188 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
189 const bool theToDisplay,
190 const bool theIsUpdateViewer )
192 myTopoShape = theFace;
193 myDisplayMode = AIS_Shaded;
196 updateShape( theToDisplay, theIsUpdateViewer );
199 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
200 const bool theToDisplay,
201 const bool theIsUpdateViewer )
203 myFillingColor = theColor;
204 updateShape( theToDisplay, theIsUpdateViewer );
207 QColor HYDROGUI_Shape::getFillingColor() const
209 return myFillingColor;
212 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
213 const bool theToDisplay,
214 const bool theIsUpdateViewer )
216 myBorderColor = theColor;
217 updateShape( theToDisplay, theIsUpdateViewer );
220 QColor HYDROGUI_Shape::getBorderColor() const
222 return myBorderColor;
225 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
227 myHighlightColor = theColor;
230 QColor HYDROGUI_Shape::getHighlightColor() const
232 return myHighlightColor;
235 void HYDROGUI_Shape::buildShape()
237 // Erase previously created shape
240 if ( myTopoShape.IsNull() )
243 myShape = new AIS_Shape( myTopoShape );
245 if ( !myObject.IsNull() )
246 myShape->SetOwner( myObject );
248 myShape->SetTransparency( 0 );
249 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
251 // Init default params for shape
252 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
253 if ( !anAttributes.IsNull() )
255 if ( myDisplayMode == AIS_Shaded )
257 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
258 if ( !anIsoAspect.IsNull() )
259 anIsoAspect->SetNumber( 0 );
261 anIsoAspect = anAttributes->VIsoAspect();
262 if ( !anIsoAspect.IsNull() )
263 anIsoAspect->SetNumber( 0 );
265 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
266 if ( !aShadingAspect.IsNull() )
268 Graphic3d_MaterialAspect aMatAspect;
269 aMatAspect.SetAmbient( 1 );
270 aMatAspect.SetDiffuse( 0 );
272 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
273 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
276 else if ( myDisplayMode == AIS_WireFrame )
278 anAttributes->SetWireDraw( true );
283 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
284 const bool theIsUpdateViewer )
286 if ( myShape.IsNull() )
289 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
290 if ( !anAttributes.IsNull() )
292 if ( myDisplayMode == AIS_Shaded )
294 // Coloring face filling
295 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
296 if ( !aShadingAspect.IsNull() )
298 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
299 getQuantityColorVal( myFillingColor.green() ),
300 getQuantityColorVal( myFillingColor.blue() ),
303 aShadingAspect->SetColor( aFillingColor );
304 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
307 else if ( myDisplayMode == AIS_WireFrame )
312 colorShapeBorder( getActiveColor() );
315 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
318 myContext->Display( myShape, theIsUpdateViewer );
321 QColor HYDROGUI_Shape::getActiveColor() const
323 return isHighlighted() ? myHighlightColor : myBorderColor;
326 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
328 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
331 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
333 if ( myShape.IsNull() )
336 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
337 if ( anAttributes.IsNull() )
340 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
341 getQuantityColorVal( theColor.green() ),
342 getQuantityColorVal( theColor.blue() ),
344 if ( myDisplayMode == AIS_Shaded )
346 if ( theColor.alpha() == 0 )
348 anAttributes->SetFaceBoundaryDraw( false );
352 anAttributes->SetFaceBoundaryDraw( true );
354 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
355 if ( !aBoundaryAspect.IsNull() )
356 aBoundaryAspect->SetColor( aBorderColor );
359 else if ( myDisplayMode == AIS_WireFrame )
361 myShape->SetColor( aBorderColor );