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 ),
50 myFillingColor( Qt::green ),
51 myBorderColor( Qt::black ),
52 myHighlightColor( Qt::white )
56 HYDROGUI_Shape::~HYDROGUI_Shape()
60 if ( !myShape.IsNull() )
64 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
66 if ( !myContext || myShape.IsNull() )
69 myContext->Display( myShape, theIsUpdateViewer );
72 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
74 if ( !myContext || myShape.IsNull() )
77 myContext->Erase( myShape, theIsUpdateViewer );
80 void HYDROGUI_Shape::highlight( bool theIsHighlight )
82 if ( myIsHighlight == theIsHighlight )
85 myIsHighlight = theIsHighlight;
87 if ( !myContext || myShape.IsNull() )
90 colorShapeBorder( getActiveColor() );
91 myContext->Display( myShape );
94 bool HYDROGUI_Shape::isHighlighted() const
99 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
100 const bool theToDisplay )
102 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
103 aFaceBuilder.Build();
104 if( aFaceBuilder.IsDone() )
106 TopoDS_Face aFace = aFaceBuilder.Face();
107 setFace( aFace, theToDisplay );
111 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
112 const bool theToDisplay )
116 updateShape( theToDisplay );
119 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
120 const bool theToDisplay )
122 myFillingColor = theColor;
123 updateShape( theToDisplay );
126 QColor HYDROGUI_Shape::getFillingColor() const
128 return myFillingColor;
131 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
132 const bool theToDisplay )
134 myBorderColor = theColor;
135 updateShape( theToDisplay );
138 QColor HYDROGUI_Shape::getBorderColor() const
140 return myBorderColor;
143 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
145 myHighlightColor = theColor;
148 QColor HYDROGUI_Shape::getHighlightColor() const
150 return myHighlightColor;
153 void HYDROGUI_Shape::buildShape()
155 // Erase previously created shape
158 if( myFace.IsNull() )
161 myShape = new AIS_Shape( myFace );
163 myShape->SetTransparency( 0 );
164 myShape->SetDisplayMode( AIS_Shaded );
166 // Init default params for shape
167 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
168 if ( !anAttributes.IsNull() )
170 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
171 if ( !anIsoAspect.IsNull() )
172 anIsoAspect->SetNumber( 0 );
174 anIsoAspect = anAttributes->VIsoAspect();
175 if ( !anIsoAspect.IsNull() )
176 anIsoAspect->SetNumber( 0 );
178 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
179 if ( !aShadingAspect.IsNull() )
181 Graphic3d_MaterialAspect aMatAspect;
182 aMatAspect.SetAmbient( 1 );
183 aMatAspect.SetDiffuse( 0 );
185 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
186 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
191 void HYDROGUI_Shape::updateShape( const bool theIsForce )
193 if ( myShape.IsNull() )
196 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
197 if ( !anAttributes.IsNull() )
199 // Coloring face filling
200 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
201 if ( !aShadingAspect.IsNull() )
203 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
204 getQuantityColorVal( myFillingColor.green() ),
205 getQuantityColorVal( myFillingColor.blue() ),
208 aShadingAspect->SetColor( aFillingColor );
209 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
213 colorShapeBorder( getActiveColor() );
216 if ( !theIsForce || !myContext )
219 myContext->Display( myShape, theIsForce );
222 QColor HYDROGUI_Shape::getActiveColor() const
224 return isHighlighted() ? myHighlightColor : myBorderColor;
227 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
229 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
232 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
234 if ( myShape.IsNull() )
237 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
238 if ( anAttributes.IsNull() )
241 if ( theColor.alpha() == 0 )
243 anAttributes->SetFaceBoundaryDraw( false );
247 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
248 getQuantityColorVal( theColor.green() ),
249 getQuantityColorVal( theColor.blue() ),
252 anAttributes->SetFaceBoundaryDraw( true );
254 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
255 if ( !aBoundaryAspect.IsNull() )
257 aBoundaryAspect->SetColor( aBorderColor );