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 <TopoDS_Wire.hxx>
37 #include <TopoDS_Face.hxx>
39 #include <Prs3d_ShadingAspect.hxx>
40 #include <Prs3d_LineAspect.hxx>
41 #include <Prs3d_IsoAspect.hxx>
45 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext )
46 : myContext( theContext ),
47 myIsHighlight( false ),
49 myFillingColor( Qt::green ),
50 myBorderColor( Qt::black ),
51 myHighlightColor( Qt::white )
55 HYDROGUI_Shape::~HYDROGUI_Shape()
59 if ( !myShape.IsNull() )
63 void HYDROGUI_Shape::display()
65 if ( !myContext || myShape.IsNull() )
68 myContext->Display( myShape );
71 void HYDROGUI_Shape::erase()
73 if ( !myContext || myShape.IsNull() )
76 myContext->Erase( myShape );
79 void HYDROGUI_Shape::highlight( bool theIsHighlight )
81 if ( myIsHighlight == theIsHighlight )
84 myIsHighlight = theIsHighlight;
86 if ( !myContext || myShape.IsNull() )
89 colorShapeBorder( getActiveColor() );
90 myContext->Display( myShape );
93 bool HYDROGUI_Shape::isHighlighted() const
98 void HYDROGUI_Shape::setPath( const QPainterPath& thePath,
99 const bool theToDisplay )
103 updateShape( theToDisplay );
106 QPainterPath HYDROGUI_Shape::getPath() const
111 void HYDROGUI_Shape::setZIndex( const double theZIndex,
112 const bool theToDisplay )
114 myZIndex = theZIndex;
116 updateShape( theToDisplay );
119 double HYDROGUI_Shape::getZIndex() const
124 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
125 const bool theToDisplay )
127 myFillingColor = theColor;
128 updateShape( theToDisplay );
131 QColor HYDROGUI_Shape::getFillingColor() const
133 return myFillingColor;
136 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
137 const bool theToDisplay )
139 myBorderColor = theColor;
140 updateShape( theToDisplay );
143 QColor HYDROGUI_Shape::getBorderColor() const
145 return myBorderColor;
148 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
150 myHighlightColor = theColor;
153 QColor HYDROGUI_Shape::getHighlightColor() const
155 return myHighlightColor;
158 void HYDROGUI_Shape::buildShape()
160 // Erase previously created shape
163 if ( myPath.isEmpty() )
166 BRepBuilderAPI_MakeWire aMakeWire;
168 // Build new shape from path
169 gp_Pnt aPrevPoint( 0, 0, myZIndex );
170 for ( int i = 0; i < myPath.elementCount(); ++i )
172 const QPainterPath::Element& aPathElem = myPath.elementAt( i );
174 gp_Pnt aCurPoint( aPathElem.x, aPathElem.y, myZIndex );
175 switch ( aPathElem.type )
177 case QPainterPath::LineToElement:
179 BRepBuilderAPI_MakeEdge aMakeEdge( aPrevPoint, aCurPoint );
181 if ( aMakeEdge.IsDone() )
183 TopoDS_Edge anEdge = aMakeEdge;
184 aMakeWire.Add( anEdge );
188 case QPainterPath::CurveToElement:
193 case QPainterPath::MoveToElement:
198 aPrevPoint = aCurPoint;
201 TopoDS_Face aResShape;
204 if ( aMakeWire.IsDone() )
206 TopoDS_Wire aCommonWire = aMakeWire;
208 BRepBuilderAPI_MakeFace aMakeFace( aCommonWire, true );
210 if ( aMakeFace.IsDone() )
211 aResShape = aMakeFace;
214 myShape = new AIS_Shape( aResShape );
216 myShape->SetTransparency( 0 );
217 myShape->SetDisplayMode( AIS_Shaded );
219 // Init default params for shape
220 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
221 if ( !anAttributes.IsNull() )
223 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
224 if ( !anIsoAspect.IsNull() )
225 anIsoAspect->SetNumber( 0 );
227 anIsoAspect = anAttributes->VIsoAspect();
228 if ( !anIsoAspect.IsNull() )
229 anIsoAspect->SetNumber( 0 );
231 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
232 if ( !aShadingAspect.IsNull() )
234 Graphic3d_MaterialAspect aMatAspect;
235 aMatAspect.SetAmbient( 1 );
236 aMatAspect.SetDiffuse( 0 );
238 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
239 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
244 void HYDROGUI_Shape::updateShape( const bool theIsForce )
246 if ( myShape.IsNull() )
249 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
250 if ( !anAttributes.IsNull() )
252 // Coloring face filling
253 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
254 if ( !aShadingAspect.IsNull() )
256 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
257 getQuantityColorVal( myFillingColor.green() ),
258 getQuantityColorVal( myFillingColor.blue() ),
261 aShadingAspect->SetColor( aFillingColor );
262 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
266 colorShapeBorder( getActiveColor() );
269 if ( !theIsForce || !myContext )
272 myContext->Display( myShape );
275 QColor HYDROGUI_Shape::getActiveColor() const
277 return isHighlighted() ? myHighlightColor : myBorderColor;
280 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
282 return theColorVal == 0 ? 0 : (double)( theColorVal / 255 );
285 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
287 if ( myShape.IsNull() )
290 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
291 if ( anAttributes.IsNull() )
294 if ( theColor.alpha() == 0 )
296 anAttributes->SetFaceBoundaryDraw( false );
300 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
301 getQuantityColorVal( theColor.green() ),
302 getQuantityColorVal( theColor.blue() ),
305 anAttributes->SetFaceBoundaryDraw( true );
307 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
308 if ( !aBoundaryAspect.IsNull() )
310 aBoundaryAspect->SetColor( aBorderColor );