1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROGUI_Shape.h>
20 #include <HYDROGUI_Tool.h>
21 #include <HYDROData_Channel.h>
22 #include <HYDROData_Document.h>
23 #include <HYDROData_DummyObject3D.h>
24 #include <HYDROData_ImmersibleZone.h>
25 #include <HYDROData_Obstacle.h>
26 #include <HYDROData_PolylineXY.h>
27 #include <HYDROData_Polyline3D.h>
28 #include <HYDROData_Profile.h>
29 #include <HYDROData_ShapesGroup.h>
30 #include <HYDROData_Stream.h>
31 #include <HYDROData_Zone.h>
32 #include <HYDROGUI_Polyline.h>
34 #include <AIS_Shape.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRepBuilderAPI_MakeFace.hxx>
37 #include <Graphic3d_AspectFillArea3d.hxx>
38 #include <Prs3d_IsoAspect.hxx>
39 #include <Prs3d_ShadingAspect.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Wire.hxx>
43 #include <TopExp_Explorer.hxx>
45 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
46 const Handle(HYDROData_Entity)& theObject,
48 : myContext( theContext ),
49 myObject( theObject ),
50 myZLayer( theZLayer ),
51 myIsHighlight( false ),
52 myFillingColor( Qt::transparent ),
53 myBorderColor( Qt::black ),
54 myHighlightColor( Qt::white ),
55 myIsToUpdate( false ),
57 myDisplayMode( AIS_Shaded )
61 HYDROGUI_Shape::~HYDROGUI_Shape()
65 if ( !myShape.IsNull() )
69 Handle(AIS_InteractiveContext) HYDROGUI_Shape::getContext() const
74 Handle(HYDROData_Entity) HYDROGUI_Shape::getObject() const
79 TopoDS_Shape HYDROGUI_Shape::getTopoShape() const
84 bool HYDROGUI_Shape::getIsToUpdate() const
89 void HYDROGUI_Shape::setIsToUpdate( bool theState )
91 myIsToUpdate = theState;
94 bool HYDROGUI_Shape::isVisible() const
99 Handle(AIS_InteractiveObject) HYDROGUI_Shape::getAISObject() const
104 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
106 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
109 displayShape( theIsUpdateViewer );
112 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
114 if ( myContext.IsNull() || myShape.IsNull() )
117 myContext->Erase( myShape, theIsUpdateViewer );
120 void HYDROGUI_Shape::update( bool isUpdateViewer,
121 bool isDeactivateSelection )
124 setIsToUpdate( false );
126 if ( myContext.IsNull() )
129 // Try to retrieve information from object
130 if ( !myObject.IsNull() )
132 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
134 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
136 Handle(HYDROData_ImmersibleZone) aZoneObj =
137 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
139 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
140 if ( !aZoneShape.IsNull() ) {
141 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
142 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
143 setFace( aZoneFace, false, false, "" );
145 myTopoShape = aZoneShape;
146 //TODO: myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
147 myDisplayMode = AIS_Shaded;
150 updateShape( false, false );
154 QColor aFillingColor = aZoneObj->GetFillingColor();
155 QColor aBorderColor = aZoneObj->GetBorderColor();
157 setFillingColor( aFillingColor, false, false );
158 setBorderColor( aBorderColor, false, false );
160 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
162 Handle(HYDROData_PolylineXY) aPolyline =
163 Handle(HYDROData_PolylineXY)::DownCast( myObject );
165 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
167 if ( !aPolylineShape.IsNull() ) {
168 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
169 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
170 setWire( aPolylineWire, false, false );
172 myTopoShape = aPolylineShape;
173 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
174 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
175 myDisplayMode = AIS_Shaded;
178 updateShape( false, false );
182 QColor aWireColor = aPolyline->GetWireColor();
183 setBorderColor( aWireColor, false, false );
185 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
187 Handle(HYDROData_Polyline3D) aPolyline =
188 Handle(HYDROData_Polyline3D)::DownCast( myObject );
190 TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
192 if ( !aPolylineShape.IsNull() ) {
193 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
194 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
195 setWire( aPolylineWire, false, false );
197 myTopoShape = aPolylineShape;
198 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
199 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
200 myDisplayMode = AIS_Shaded;
203 updateShape( false, false );
207 QColor aWireColor = aPolyline->GetBorderColor();
208 setBorderColor( aWireColor, false, false );
210 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
212 Handle(HYDROData_Zone) aZone =
213 Handle(HYDROData_Zone)::DownCast( myObject );
215 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
217 setFace( aZoneFace, false, false, "" );
218 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
220 // Red color for a zone with bathymetry conflict
221 setFillingColor( Qt::red );
225 // Set the filling color for zone
226 setFillingColor( aZone->GetColor(HYDROData_ImmersibleZone::DefaultFillingColor()) );
229 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
231 Handle(HYDROData_Profile) aProfile =
232 Handle(HYDROData_Profile)::DownCast( myObject );
234 TopoDS_Wire aProfileWire;
236 if ( aProfile->IsValid() ) {
237 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
239 if ( !aProfileShape.IsNull() &&
240 aProfileShape.ShapeType() == TopAbs_WIRE ) {
241 aProfileWire = TopoDS::Wire( aProfileShape );
245 setWire( aProfileWire, false, false );
247 QColor aWireColor = aProfile->GetBorderColor();
248 setBorderColor( aWireColor, false, false );
250 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
251 myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
252 myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
254 Handle(HYDROData_Object) aGeomObject =
255 Handle(HYDROData_Object)::DownCast( myObject );
257 TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
259 setShape( anObjShape, false, false );
261 QColor aFillingColor = aGeomObject->GetFillingColor();
262 QColor aBorderColor = aGeomObject->GetBorderColor();
264 setFillingColor( aFillingColor, false, false );
265 setBorderColor( aBorderColor, false, false );
267 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
269 Handle(HYDROData_DummyObject3D) anObject3D =
270 Handle(HYDROData_DummyObject3D)::DownCast( myObject );
271 TopoDS_Shape aShape3D = anObject3D->GetShape();
273 setShape( aShape3D, false, false );
275 QColor aFillingColor = anObject3D->GetFillingColor();
276 QColor aBorderColor = anObject3D->GetBorderColor();
278 setFillingColor( aFillingColor, false, false );
279 setBorderColor( aBorderColor, false, false );
281 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
283 Handle(HYDROData_ShapesGroup) aShapesGroup =
284 Handle(HYDROData_ShapesGroup)::DownCast( myObject );
286 TopTools_SequenceOfShape aShapes;
287 aShapesGroup->GetShapes( aShapes );
289 TopoDS_Compound aCompound;
290 BRep_Builder aCompoundBuilder;
291 aCompoundBuilder.MakeCompound( aCompound );
293 for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
295 const TopoDS_Shape& aShape = aShapes.Value( i );
296 aCompoundBuilder.Add( aCompound, aShape );
299 setShape( aCompound, false, false );
303 if ( myShape.IsNull() || !isVisible() )
306 displayShape( isUpdateViewer );
308 if (isDeactivateSelection)
309 myContext->Deactivate(myShape);
312 void HYDROGUI_Shape::setVisible( const bool theState,
313 const bool theIsUpdateViewer )
315 myIsVisible = theState;
317 if ( myShape.IsNull() )
320 if ( ( myIsVisible && myContext->IsDisplayed( myShape ) ) ||
321 ( !myIsVisible && !myContext->IsDisplayed( myShape ) ) )
325 displayShape( theIsUpdateViewer );
328 myContext->Erase( myShape, theIsUpdateViewer );
331 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
333 if ( myIsHighlight == theIsHighlight )
336 myIsHighlight = theIsHighlight;
338 if ( myContext.IsNull() || myShape.IsNull() )
341 colorShapeBorder( getActiveColor() );
342 displayShape( isUpdateViewer );
345 bool HYDROGUI_Shape::isHighlighted() const
347 return myIsHighlight;
350 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
351 const bool theToDisplay,
352 const bool theIsUpdateViewer )
354 myTopoShape = theWire;
355 // To avoid that hilight presentation is equal to "normal" object presentation.
356 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
357 myDisplayMode = AIS_Shaded;
360 updateShape( theToDisplay, theIsUpdateViewer );
363 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
364 const bool theToDisplay,
365 const bool theIsUpdateViewer )
367 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
368 TopoDS_Compound aCompound;
369 BRep_Builder aBuilder;
370 aBuilder.MakeCompound( aCompound );
372 for ( ; anExp.More(); anExp.Next() ) {
373 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
374 if ( aWire.IsNull() ) {
378 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
380 if( aMakeFace.IsDone() ) {
381 aBuilder.Add( aCompound, aMakeFace.Face() );
385 myTopoShape = aCompound;
386 myDisplayMode = AIS_Shaded;
389 updateShape( theToDisplay, theIsUpdateViewer );
392 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
393 const bool theToDisplay,
394 const bool theIsUpdateViewer,
395 const QString& theTextureFileName )
397 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
398 aFaceBuilder.Build();
399 if( aFaceBuilder.IsDone() )
401 TopoDS_Face aFace = aFaceBuilder.Face();
402 setFace( aFace, theToDisplay, theIsUpdateViewer, theTextureFileName );
406 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
407 const bool theToDisplay,
408 const bool theIsUpdateViewer,
409 const QString& theTextureFileName )
411 myTopoShape = theFace;
412 myDisplayMode = theTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
413 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
414 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
417 updateShape( theToDisplay, theIsUpdateViewer );
420 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
421 const bool theToDisplay,
422 const bool theIsUpdateViewer )
424 myTopoShape = theShape;
425 myDisplayMode = AIS_Shaded;
428 updateShape( theToDisplay, theIsUpdateViewer );
431 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
432 const bool theToDisplay,
433 const bool theIsUpdateViewer )
435 myFillingColor = theColor;
436 updateShape( theToDisplay, theIsUpdateViewer );
439 QColor HYDROGUI_Shape::getFillingColor() const
441 return myFillingColor;
444 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
445 const bool theToDisplay,
446 const bool theIsUpdateViewer )
448 myBorderColor = theColor;
449 updateShape( theToDisplay, theIsUpdateViewer );
452 QColor HYDROGUI_Shape::getBorderColor() const
454 return myBorderColor;
457 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
459 myHighlightColor = theColor;
462 QColor HYDROGUI_Shape::getHighlightColor() const
464 return myHighlightColor;
467 void HYDROGUI_Shape::setZLayer( const int theZLayer )
469 if ( myZLayer == theZLayer )
472 myZLayer = theZLayer;
473 if ( !myShape.IsNull() && isVisible() && !myContext.IsNull() && myZLayer >= 0 )
474 myContext->SetZLayer( myShape, myZLayer );
477 Handle_AIS_InteractiveObject HYDROGUI_Shape::createShape() const
479 if( myTopoShape.IsNull() )
480 return Handle_AIS_InteractiveObject();
482 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) ||
483 myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D)) ||
484 myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ))
485 return new HYDROGUI_Polyline(myTopoShape);
487 return new AIS_Shape( myTopoShape );
490 void HYDROGUI_Shape::buildShape()
492 // Erase previously created shape
495 myShape = createShape();
496 if( myShape.IsNull() )
499 Handle_AIS_Shape anAISShape = Handle_AIS_Shape::DownCast( myShape );
500 if( !anAISShape.IsNull() )
501 anAISShape ->SetHLRAngleAndDeviation( 0.001 );
503 if ( !myObject.IsNull() )
504 myShape->SetOwner( myObject );
506 myShape->SetTransparency( 0 );
507 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
509 // Init default params for shape
510 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
511 if ( !anAttributes.IsNull() )
513 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
514 if ( !anIsoAspect.IsNull() ) {
515 anIsoAspect->SetNumber( 0 );
516 anAttributes->SetUIsoAspect( anIsoAspect );
519 anIsoAspect = anAttributes->VIsoAspect();
520 if ( !anIsoAspect.IsNull() ) {
521 anIsoAspect->SetNumber( 0 );
522 anAttributes->SetVIsoAspect( anIsoAspect );
525 if ( myDisplayMode == AIS_Shaded )
527 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
528 if ( !aShadingAspect.IsNull() )
530 Graphic3d_MaterialAspect aMatAspect( Graphic3d_NOM_PLASTIC );
531 //aMatAspect.SetAmbient( 1 );
532 //aMatAspect.SetDiffuse( 0 );
534 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
535 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
538 else if ( myDisplayMode == AIS_WireFrame )
540 anAttributes->SetWireDraw( true );
545 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
546 const bool theIsUpdateViewer )
548 if ( myShape.IsNull() )
551 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
552 if ( !anAttributes.IsNull() )
554 if ( myDisplayMode == AIS_Shaded )
556 // Coloring face filling
557 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
558 if ( !aShadingAspect.IsNull() )
560 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
561 getQuantityColorVal( myFillingColor.green() ),
562 getQuantityColorVal( myFillingColor.blue() ),
565 aShadingAspect->SetColor( aFillingColor );
566 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
569 else if ( myDisplayMode == AIS_WireFrame )
574 colorShapeBorder( getActiveColor() );
577 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
580 displayShape( theIsUpdateViewer );
583 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
585 myContext->Display( myShape, Standard_False );
588 myContext->SetZLayer( myShape, myZLayer );
590 if ( theIsUpdateViewer ) {
591 myContext->UpdateCurrentViewer();
595 QColor HYDROGUI_Shape::getActiveColor() const
597 return isHighlighted() ? myHighlightColor : myBorderColor;
600 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
602 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
605 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
607 if ( myShape.IsNull() )
610 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
611 if ( anAttributes.IsNull() )
614 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
615 getQuantityColorVal( theColor.green() ),
616 getQuantityColorVal( theColor.blue() ),
619 if( !myTopoShape.IsNull() )
621 if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode
623 myShape->SetColor( aBorderColor );
625 else if ( myDisplayMode == AIS_Shaded )
627 if ( theColor.alpha() == 0 )
629 anAttributes->SetFaceBoundaryDraw( false );
633 anAttributes->SetFaceBoundaryDraw( true );
635 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
636 if ( !aBoundaryAspect.IsNull() )
638 aBoundaryAspect->SetColor( aBorderColor );
639 anAttributes->SetFaceBoundaryAspect( aBoundaryAspect );
641 Handle(Prs3d_LineAspect) aWireAspect = anAttributes->WireAspect();
642 if ( !aWireAspect.IsNull() )
644 aWireAspect->SetColor( aBorderColor );
645 anAttributes->SetWireAspect( aWireAspect );
649 else if ( myDisplayMode == AIS_WireFrame )
651 myShape->SetColor( aBorderColor );
656 void HYDROGUI_Shape::setDisplayMode( int theDisplayMode )
658 myDisplayMode = theDisplayMode;