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 <Precision.hxx>
41 #include <Prs3d_ShadingAspect.hxx>
42 #include <Prs3d_LineAspect.hxx>
43 #include <Prs3d_IsoAspect.hxx>
47 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext )
48 : myContext( theContext ),
49 myIsHighlight( false ),
51 myFillingColor( Qt::green ),
52 myBorderColor( Qt::black ),
53 myHighlightColor( Qt::white )
57 HYDROGUI_Shape::~HYDROGUI_Shape()
61 if ( !myShape.IsNull() )
65 void HYDROGUI_Shape::display()
67 if ( !myContext || myShape.IsNull() )
70 myContext->Display( myShape );
73 void HYDROGUI_Shape::erase()
75 if ( !myContext || myShape.IsNull() )
78 myContext->Erase( myShape );
81 void HYDROGUI_Shape::highlight( bool theIsHighlight )
83 if ( myIsHighlight == theIsHighlight )
86 myIsHighlight = theIsHighlight;
88 if ( !myContext || myShape.IsNull() )
91 colorShapeBorder( getActiveColor() );
92 myContext->Display( myShape );
95 bool HYDROGUI_Shape::isHighlighted() const
100 void HYDROGUI_Shape::setPath( const QPainterPath& thePath,
101 const bool theToDisplay )
105 updateShape( theToDisplay );
108 QPainterPath HYDROGUI_Shape::getPath() const
113 void HYDROGUI_Shape::setZIndex( const double theZIndex,
114 const bool theToDisplay )
116 myZIndex = theZIndex;
118 updateShape( theToDisplay );
121 double HYDROGUI_Shape::getZIndex() const
126 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
127 const bool theToDisplay )
129 myFillingColor = theColor;
130 updateShape( theToDisplay );
133 QColor HYDROGUI_Shape::getFillingColor() const
135 return myFillingColor;
138 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
139 const bool theToDisplay )
141 myBorderColor = theColor;
142 updateShape( theToDisplay );
145 QColor HYDROGUI_Shape::getBorderColor() const
147 return myBorderColor;
150 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
152 myHighlightColor = theColor;
155 QColor HYDROGUI_Shape::getHighlightColor() const
157 return myHighlightColor;
160 void HYDROGUI_Shape::makeEdge( BRepBuilderAPI_MakeWire& theWireBuilder,
161 gp_Pnt& theFirstPoint,
162 gp_Pnt& theLastPoint ) const
164 BRepBuilderAPI_MakeEdge anEdgeBuilder( theFirstPoint, theLastPoint );
165 anEdgeBuilder.Build();
166 if ( anEdgeBuilder.IsDone() )
168 TopoDS_Edge anEdge = anEdgeBuilder;
169 theWireBuilder.Add( anEdge );
173 void HYDROGUI_Shape::makeWire( BRepBuilderAPI_MakeFace& theFaceBuilder,
174 BRepBuilderAPI_MakeWire& theWireBuilder,
175 bool& theIsComposed ) const
177 theWireBuilder.Build();
178 if ( theWireBuilder.IsDone() )
180 TopoDS_Wire aBuildedWire = theWireBuilder;
183 theFaceBuilder.Add( aBuildedWire );
187 theFaceBuilder = BRepBuilderAPI_MakeFace( aBuildedWire, true );
189 theFaceBuilder.Build();
190 theIsComposed = theFaceBuilder.IsDone();
194 theWireBuilder = BRepBuilderAPI_MakeWire();
197 void HYDROGUI_Shape::buildShape()
199 // Erase previously created shape
202 if ( myPath.isEmpty() )
205 BRepBuilderAPI_MakeFace aFaceBuilder;
206 BRepBuilderAPI_MakeWire aWireBuilder;
208 // Build new shape from path
209 bool anIsComposed = false;
210 bool anIsStarted = false;
211 gp_Pnt aStartPoint( 0, 0, myZIndex );
212 gp_Pnt aPrevPoint( 0, 0, myZIndex );
214 for ( int i = 0; i < myPath.elementCount(); ++i )
216 const QPainterPath::Element& aPathElem = myPath.elementAt( i );
218 gp_Pnt aCurPoint( aPathElem.x, aPathElem.y, myZIndex );
219 switch ( aPathElem.type )
221 case QPainterPath::LineToElement:
223 // Just build the edge
224 makeEdge( aWireBuilder, aPrevPoint, aCurPoint );
227 case QPainterPath::CurveToElement:
232 case QPainterPath::MoveToElement:
237 if ( !aStartPoint.IsEqual( aPrevPoint, Precision::Confusion() ) )
240 makeEdge( aWireBuilder, aPrevPoint, aStartPoint );
243 // Make face wiyth edge
244 makeWire( aFaceBuilder, aWireBuilder, anIsComposed );
248 aStartPoint = aCurPoint;
255 aPrevPoint = aCurPoint;
258 makeWire( aFaceBuilder, aWireBuilder, anIsComposed );
260 TopoDS_Face aResShape;
262 aFaceBuilder.Build();
263 if ( aFaceBuilder.IsDone() )
264 aResShape = aFaceBuilder;
266 myShape = new AIS_Shape( aResShape );
268 myShape->SetTransparency( 0 );
269 myShape->SetDisplayMode( AIS_Shaded );
271 // Init default params for shape
272 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
273 if ( !anAttributes.IsNull() )
275 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
276 if ( !anIsoAspect.IsNull() )
277 anIsoAspect->SetNumber( 0 );
279 anIsoAspect = anAttributes->VIsoAspect();
280 if ( !anIsoAspect.IsNull() )
281 anIsoAspect->SetNumber( 0 );
283 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
284 if ( !aShadingAspect.IsNull() )
286 Graphic3d_MaterialAspect aMatAspect;
287 aMatAspect.SetAmbient( 1 );
288 aMatAspect.SetDiffuse( 0 );
290 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
291 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
296 void HYDROGUI_Shape::updateShape( const bool theIsForce )
298 if ( myShape.IsNull() )
301 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
302 if ( !anAttributes.IsNull() )
304 // Coloring face filling
305 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
306 if ( !aShadingAspect.IsNull() )
308 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
309 getQuantityColorVal( myFillingColor.green() ),
310 getQuantityColorVal( myFillingColor.blue() ),
313 aShadingAspect->SetColor( aFillingColor );
314 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
318 colorShapeBorder( getActiveColor() );
321 if ( !theIsForce || !myContext )
324 myContext->Display( myShape );
327 QColor HYDROGUI_Shape::getActiveColor() const
329 return isHighlighted() ? myHighlightColor : myBorderColor;
332 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
334 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
337 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
339 if ( myShape.IsNull() )
342 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
343 if ( anAttributes.IsNull() )
346 if ( theColor.alpha() == 0 )
348 anAttributes->SetFaceBoundaryDraw( false );
352 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
353 getQuantityColorVal( theColor.green() ),
354 getQuantityColorVal( theColor.blue() ),
357 anAttributes->SetFaceBoundaryDraw( true );
359 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
360 if ( !aBoundaryAspect.IsNull() )
362 aBoundaryAspect->SetColor( aBorderColor );