1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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>
24 #include <HYDROGUI_Tool.h>
25 #include <HYDROData_Channel.h>
26 #include <HYDROData_Document.h>
27 #include <HYDROData_DummyObject3D.h>
28 #include <HYDROData_ImmersibleZone.h>
29 #include <HYDROData_Obstacle.h>
30 #include <HYDROData_PolylineXY.h>
31 #include <HYDROData_Polyline3D.h>
32 #include <HYDROData_Profile.h>
33 #include <HYDROData_ShapesGroup.h>
34 #include <HYDROData_Stream.h>
35 #include <HYDROData_Zone.h>
37 #include <AIS_Shape.hxx>
38 #include <BRep_Builder.hxx>
39 #include <BRepBuilderAPI_MakeFace.hxx>
40 #include <Graphic3d_AspectFillArea3d.hxx>
41 #include <Prs3d_IsoAspect.hxx>
42 #include <Prs3d_ShadingAspect.hxx>
44 #include <TopoDS_Face.hxx>
45 #include <TopoDS_Wire.hxx>
46 #include <TopExp_Explorer.hxx>
48 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
49 const Handle(HYDROData_Entity)& theObject,
51 : myContext( theContext ),
52 myObject( theObject ),
53 myZLayer( theZLayer ),
54 myIsHighlight( false ),
55 myFillingColor( Qt::transparent ),
56 myBorderColor( Qt::black ),
57 myHighlightColor( Qt::white ),
58 myIsToUpdate( false ),
60 myDisplayMode( AIS_Shaded )
64 HYDROGUI_Shape::~HYDROGUI_Shape()
68 if ( !myShape.IsNull() )
72 Handle(AIS_InteractiveContext) HYDROGUI_Shape::getContext() const
77 Handle(HYDROData_Entity) HYDROGUI_Shape::getObject() const
82 TopoDS_Shape HYDROGUI_Shape::getTopoShape() const
87 bool HYDROGUI_Shape::getIsToUpdate() const
92 void HYDROGUI_Shape::setIsToUpdate( bool theState )
94 myIsToUpdate = theState;
97 bool HYDROGUI_Shape::isVisible() const
102 Handle(AIS_InteractiveObject) HYDROGUI_Shape::getAISObject() const
107 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
109 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
112 displayShape( theIsUpdateViewer );
115 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
117 if ( myContext.IsNull() || myShape.IsNull() )
120 myContext->Erase( myShape, theIsUpdateViewer );
123 void HYDROGUI_Shape::update( bool isUpdateViewer,
124 bool isDeactivateSelection )
127 setIsToUpdate( false );
129 if ( myContext.IsNull() )
132 // Try to retrieve information from object
133 if ( !myObject.IsNull() )
135 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
137 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
139 Handle(HYDROData_ImmersibleZone) aZoneObj =
140 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
142 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
143 if ( !aZoneShape.IsNull() ) {
144 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
145 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
146 setFace( aZoneFace, false, false, "" );
148 myTopoShape = aZoneShape;
149 //TODO: myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
150 myDisplayMode = AIS_Shaded;
153 updateShape( false, false );
157 QColor aFillingColor = aZoneObj->GetFillingColor();
158 QColor aBorderColor = aZoneObj->GetBorderColor();
160 setFillingColor( aFillingColor, false, false );
161 setBorderColor( aBorderColor, false, false );
163 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
165 Handle(HYDROData_PolylineXY) aPolyline =
166 Handle(HYDROData_PolylineXY)::DownCast( myObject );
168 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
170 if ( !aPolylineShape.IsNull() ) {
171 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
172 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
173 setWire( aPolylineWire, false, false );
175 myTopoShape = aPolylineShape;
176 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
177 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
178 myDisplayMode = AIS_Shaded;
181 updateShape( false, false );
185 QColor aWireColor = aPolyline->GetWireColor();
186 setBorderColor( aWireColor, false, false );
188 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
190 Handle(HYDROData_Polyline3D) aPolyline =
191 Handle(HYDROData_Polyline3D)::DownCast( myObject );
193 TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
195 if ( !aPolylineShape.IsNull() ) {
196 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
197 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
198 setWire( aPolylineWire, false, false );
200 myTopoShape = aPolylineShape;
201 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
202 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
203 myDisplayMode = AIS_Shaded;
206 updateShape( false, false );
210 QColor aWireColor = aPolyline->GetBorderColor();
211 setBorderColor( aWireColor, false, false );
213 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
215 Handle(HYDROData_Zone) aZone =
216 Handle(HYDROData_Zone)::DownCast( myObject );
218 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
220 setFace( aZoneFace, false, false, "" );
221 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
223 // Red color for a zone with bathymetry conflict
224 setFillingColor( Qt::red );
228 // Set the filling color for zone
229 setFillingColor( aZone->GetColor(HYDROData_ImmersibleZone::DefaultFillingColor()) );
232 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
234 Handle(HYDROData_Profile) aProfile =
235 Handle(HYDROData_Profile)::DownCast( myObject );
237 TopoDS_Wire aProfileWire;
239 if ( aProfile->IsValid() ) {
240 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
242 if ( !aProfileShape.IsNull() &&
243 aProfileShape.ShapeType() == TopAbs_WIRE ) {
244 aProfileWire = TopoDS::Wire( aProfileShape );
248 setWire( aProfileWire, false, false );
250 QColor aWireColor = aProfile->GetBorderColor();
251 setBorderColor( aWireColor, false, false );
253 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
254 myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
255 myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
257 Handle(HYDROData_Object) aGeomObject =
258 Handle(HYDROData_Object)::DownCast( myObject );
260 TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
262 setShape( anObjShape, false, false );
264 QColor aFillingColor = aGeomObject->GetFillingColor();
265 QColor aBorderColor = aGeomObject->GetBorderColor();
267 setFillingColor( aFillingColor, false, false );
268 setBorderColor( aBorderColor, false, false );
270 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
272 Handle(HYDROData_DummyObject3D) anObject3D =
273 Handle(HYDROData_DummyObject3D)::DownCast( myObject );
274 TopoDS_Shape aShape3D = anObject3D->GetShape();
276 setShape( aShape3D, false, false );
278 QColor aFillingColor = anObject3D->GetFillingColor();
279 QColor aBorderColor = anObject3D->GetBorderColor();
281 setFillingColor( aFillingColor, false, false );
282 setBorderColor( aBorderColor, false, false );
284 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
286 Handle(HYDROData_ShapesGroup) aShapesGroup =
287 Handle(HYDROData_ShapesGroup)::DownCast( myObject );
289 TopTools_SequenceOfShape aShapes;
290 aShapesGroup->GetShapes( aShapes );
292 TopoDS_Compound aCompound;
293 BRep_Builder aCompoundBuilder;
294 aCompoundBuilder.MakeCompound( aCompound );
296 for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
298 const TopoDS_Shape& aShape = aShapes.Value( i );
299 aCompoundBuilder.Add( aCompound, aShape );
302 setShape( aCompound, false, false );
306 if ( myShape.IsNull() || !isVisible() )
309 displayShape( isUpdateViewer );
311 if (isDeactivateSelection)
312 myContext->Deactivate(myShape);
315 void HYDROGUI_Shape::setVisible( const bool theState,
316 const bool theIsUpdateViewer )
318 myIsVisible = theState;
320 if ( myShape.IsNull() )
323 if ( ( myIsVisible && myContext->IsDisplayed( myShape ) ) ||
324 ( !myIsVisible && !myContext->IsDisplayed( myShape ) ) )
328 displayShape( theIsUpdateViewer );
331 myContext->Erase( myShape, theIsUpdateViewer );
334 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
336 if ( myIsHighlight == theIsHighlight )
339 myIsHighlight = theIsHighlight;
341 if ( myContext.IsNull() || myShape.IsNull() )
344 colorShapeBorder( getActiveColor() );
345 displayShape( isUpdateViewer );
348 bool HYDROGUI_Shape::isHighlighted() const
350 return myIsHighlight;
353 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
354 const bool theToDisplay,
355 const bool theIsUpdateViewer )
357 myTopoShape = theWire;
358 // To avoid that hilight presentation is equal to "normal" object presentation.
359 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
360 myDisplayMode = AIS_Shaded;
363 updateShape( theToDisplay, theIsUpdateViewer );
366 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
367 const bool theToDisplay,
368 const bool theIsUpdateViewer )
370 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
371 TopoDS_Compound aCompound;
372 BRep_Builder aBuilder;
373 aBuilder.MakeCompound( aCompound );
375 for ( ; anExp.More(); anExp.Next() ) {
376 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
377 if ( aWire.IsNull() ) {
381 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
383 if( aMakeFace.IsDone() ) {
384 aBuilder.Add( aCompound, aMakeFace.Face() );
388 myTopoShape = aCompound;
389 myDisplayMode = AIS_Shaded;
392 updateShape( theToDisplay, theIsUpdateViewer );
395 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
396 const bool theToDisplay,
397 const bool theIsUpdateViewer,
398 const QString& theTextureFileName )
400 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
401 aFaceBuilder.Build();
402 if( aFaceBuilder.IsDone() )
404 TopoDS_Face aFace = aFaceBuilder.Face();
405 setFace( aFace, theToDisplay, theIsUpdateViewer, theTextureFileName );
409 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
410 const bool theToDisplay,
411 const bool theIsUpdateViewer,
412 const QString& theTextureFileName )
414 myTopoShape = theFace;
415 myDisplayMode = theTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
416 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
417 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
420 updateShape( theToDisplay, theIsUpdateViewer );
423 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
424 const bool theToDisplay,
425 const bool theIsUpdateViewer )
427 myTopoShape = theShape;
428 myDisplayMode = AIS_Shaded;
431 updateShape( theToDisplay, theIsUpdateViewer );
434 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
435 const bool theToDisplay,
436 const bool theIsUpdateViewer )
438 myFillingColor = theColor;
439 updateShape( theToDisplay, theIsUpdateViewer );
442 QColor HYDROGUI_Shape::getFillingColor() const
444 return myFillingColor;
447 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
448 const bool theToDisplay,
449 const bool theIsUpdateViewer )
451 myBorderColor = theColor;
452 updateShape( theToDisplay, theIsUpdateViewer );
455 QColor HYDROGUI_Shape::getBorderColor() const
457 return myBorderColor;
460 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
462 myHighlightColor = theColor;
465 QColor HYDROGUI_Shape::getHighlightColor() const
467 return myHighlightColor;
470 void HYDROGUI_Shape::setZLayer( const int theZLayer )
472 if ( myZLayer == theZLayer )
475 myZLayer = theZLayer;
476 if ( !myShape.IsNull() && isVisible() && !myContext.IsNull() && myZLayer >= 0 )
477 myContext->SetZLayer( myShape, myZLayer );
480 Handle_AIS_InteractiveObject HYDROGUI_Shape::createShape() const
482 if( myTopoShape.IsNull() )
483 return Handle_AIS_InteractiveObject();
485 return new AIS_Shape( myTopoShape );
488 void HYDROGUI_Shape::buildShape()
490 // Erase previously created shape
493 myShape = createShape();
494 if( myShape.IsNull() )
497 Handle_AIS_Shape anAISShape = Handle_AIS_Shape::DownCast( myShape );
498 if( !anAISShape.IsNull() )
499 anAISShape ->SetHLRAngleAndDeviation( 0.001 );
501 if ( !myObject.IsNull() )
502 myShape->SetOwner( myObject );
504 myShape->SetTransparency( 0 );
505 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
507 // Init default params for shape
508 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
509 if ( !anAttributes.IsNull() )
511 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
512 if ( !anIsoAspect.IsNull() ) {
513 anIsoAspect->SetNumber( 0 );
514 anAttributes->SetUIsoAspect( anIsoAspect );
517 anIsoAspect = anAttributes->VIsoAspect();
518 if ( !anIsoAspect.IsNull() ) {
519 anIsoAspect->SetNumber( 0 );
520 anAttributes->SetVIsoAspect( anIsoAspect );
523 if ( myDisplayMode == AIS_Shaded )
525 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
526 if ( !aShadingAspect.IsNull() )
528 Graphic3d_MaterialAspect aMatAspect( Graphic3d_NOM_PLASTIC );
529 //aMatAspect.SetAmbient( 1 );
530 //aMatAspect.SetDiffuse( 0 );
532 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
533 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
536 else if ( myDisplayMode == AIS_WireFrame )
538 anAttributes->SetWireDraw( true );
543 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
544 const bool theIsUpdateViewer )
546 if ( myShape.IsNull() )
549 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
550 if ( !anAttributes.IsNull() )
552 if ( myDisplayMode == AIS_Shaded )
554 // Coloring face filling
555 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
556 if ( !aShadingAspect.IsNull() )
558 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
559 getQuantityColorVal( myFillingColor.green() ),
560 getQuantityColorVal( myFillingColor.blue() ),
563 aShadingAspect->SetColor( aFillingColor );
564 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
567 else if ( myDisplayMode == AIS_WireFrame )
572 colorShapeBorder( getActiveColor() );
575 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
578 displayShape( theIsUpdateViewer );
581 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
583 myContext->Display( myShape, Standard_False );
586 myContext->SetZLayer( myShape, myZLayer );
588 myContext->UpdateCurrentViewer();
591 QColor HYDROGUI_Shape::getActiveColor() const
593 return isHighlighted() ? myHighlightColor : myBorderColor;
596 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
598 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
601 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
603 if ( myShape.IsNull() )
606 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
607 if ( anAttributes.IsNull() )
610 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
611 getQuantityColorVal( theColor.green() ),
612 getQuantityColorVal( theColor.blue() ),
615 if( !myTopoShape.IsNull() )
617 if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode
619 myShape->SetColor( aBorderColor );
621 else if ( myDisplayMode == AIS_Shaded )
623 if ( theColor.alpha() == 0 )
625 anAttributes->SetFaceBoundaryDraw( false );
629 anAttributes->SetFaceBoundaryDraw( true );
631 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
632 if ( !aBoundaryAspect.IsNull() )
634 aBoundaryAspect->SetColor( aBorderColor );
635 anAttributes->SetFaceBoundaryAspect( aBoundaryAspect );
637 Handle(Prs3d_LineAspect) aWireAspect = anAttributes->WireAspect();
638 if ( !aWireAspect.IsNull() )
640 aWireAspect->SetColor( aBorderColor );
641 anAttributes->SetWireAspect( aWireAspect );
645 else if ( myDisplayMode == AIS_WireFrame )
647 myShape->SetColor( aBorderColor );
652 void HYDROGUI_Shape::setDisplayMode( int theDisplayMode )
654 myDisplayMode = theDisplayMode;