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 <HYDROGUI_Polyline.h>
23 #include <HYDROData_Channel.h>
24 #include <HYDROData_Document.h>
25 #include <HYDROData_DummyObject3D.h>
26 #include <HYDROData_ImmersibleZone.h>
27 #include <HYDROData_Obstacle.h>
28 #include <HYDROData_PolylineXY.h>
29 #include <HYDROData_Polyline3D.h>
30 #include <HYDROData_Profile.h>
31 #include <HYDROData_ShapesGroup.h>
32 #include <HYDROData_Stream.h>
33 #include <HYDROData_Zone.h>
34 #include <HYDROData_LandCover.h>
36 #include <AIS_Shape.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRepBuilderAPI_MakeFace.hxx>
39 #include <Graphic3d_AspectFillArea3d.hxx>
40 #include <Prs3d_IsoAspect.hxx>
41 #include <Prs3d_ShadingAspect.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Wire.hxx>
45 #include <TopExp_Explorer.hxx>
47 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
48 const Handle(HYDROData_Entity)& theObject,
50 : myContext( theContext ),
51 myObject( theObject ),
52 myZLayer( theZLayer ),
53 myIsHighlight( false ),
54 myFillingColor( Qt::transparent ),
55 myBorderColor( Qt::black ),
56 myHighlightColor( Qt::white ),
57 myIsToUpdate( false ),
59 myDisplayMode( AIS_Shaded )
63 HYDROGUI_Shape::~HYDROGUI_Shape()
67 if ( !myShape.IsNull() )
71 Handle(AIS_InteractiveContext) HYDROGUI_Shape::getContext() const
76 Handle(HYDROData_Entity) HYDROGUI_Shape::getObject() const
81 TopoDS_Shape HYDROGUI_Shape::getTopoShape() const
86 bool HYDROGUI_Shape::getIsToUpdate() const
91 void HYDROGUI_Shape::setIsToUpdate( bool theState )
93 myIsToUpdate = theState;
96 bool HYDROGUI_Shape::isVisible() const
101 Handle(AIS_InteractiveObject) HYDROGUI_Shape::getAISObject() const
106 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
108 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
111 displayShape( theIsUpdateViewer );
114 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
116 if ( myContext.IsNull() || myShape.IsNull() )
119 myContext->Erase( myShape, theIsUpdateViewer );
122 void HYDROGUI_Shape::update( bool isUpdateViewer,
123 bool isDeactivateSelection )
126 setIsToUpdate( false );
128 if ( myContext.IsNull() )
131 // Try to retrieve information from object
132 if ( !myObject.IsNull() )
134 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
136 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
138 Handle(HYDROData_ImmersibleZone) aZoneObj =
139 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
141 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
142 if ( !aZoneShape.IsNull() ) {
143 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
144 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
145 setFace( aZoneFace, false, false, "" );
147 myTopoShape = aZoneShape;
148 //TODO: myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
149 myDisplayMode = AIS_Shaded;
152 updateShape( false, false );
156 QColor aFillingColor = aZoneObj->GetFillingColor();
157 QColor aBorderColor = aZoneObj->GetBorderColor();
159 setFillingColor( aFillingColor, false, false );
160 setBorderColor( aBorderColor, false, false );
162 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
164 Handle(HYDROData_PolylineXY) aPolyline =
165 Handle(HYDROData_PolylineXY)::DownCast( myObject );
167 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
169 if ( !aPolylineShape.IsNull() ) {
170 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
171 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
172 setWire( aPolylineWire, false, false );
174 myTopoShape = aPolylineShape;
175 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
176 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
177 myDisplayMode = AIS_Shaded;
180 updateShape( false, false );
184 QColor aWireColor = aPolyline->GetWireColor();
185 setBorderColor( aWireColor, false, false );
187 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
189 Handle(HYDROData_Polyline3D) aPolyline =
190 Handle(HYDROData_Polyline3D)::DownCast( myObject );
192 TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
194 if ( !aPolylineShape.IsNull() ) {
195 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
196 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
197 setWire( aPolylineWire, false, false );
199 myTopoShape = aPolylineShape;
200 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
201 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
202 myDisplayMode = AIS_Shaded;
205 updateShape( false, false );
209 QColor aWireColor = aPolyline->GetBorderColor();
210 setBorderColor( aWireColor, false, false );
212 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
214 Handle(HYDROData_Zone) aZone =
215 Handle(HYDROData_Zone)::DownCast( myObject );
217 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
219 setFace( aZoneFace, false, false, "" );
220 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
222 // Red color for a zone with bathymetry conflict
223 setFillingColor( Qt::red );
227 // Set the filling color for zone
228 setFillingColor( aZone->GetColor(HYDROData_ImmersibleZone::DefaultFillingColor()) );
231 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
233 Handle(HYDROData_Profile) aProfile =
234 Handle(HYDROData_Profile)::DownCast( myObject );
236 TopoDS_Wire aProfileWire;
238 if ( aProfile->IsValid() ) {
239 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
241 if ( !aProfileShape.IsNull() &&
242 aProfileShape.ShapeType() == TopAbs_WIRE ) {
243 aProfileWire = TopoDS::Wire( aProfileShape );
247 setWire( aProfileWire, false, false );
249 QColor aWireColor = aProfile->GetBorderColor();
250 setBorderColor( aWireColor, false, false );
252 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
253 myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
254 myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
256 Handle(HYDROData_Object) aGeomObject =
257 Handle(HYDROData_Object)::DownCast( myObject );
259 TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
261 setShape( anObjShape, false, false );
263 QColor aFillingColor = aGeomObject->GetFillingColor();
264 QColor aBorderColor = aGeomObject->GetBorderColor();
266 setFillingColor( aFillingColor, false, false );
267 setBorderColor( aBorderColor, false, false );
269 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
271 Handle(HYDROData_DummyObject3D) anObject3D =
272 Handle(HYDROData_DummyObject3D)::DownCast( myObject );
273 TopoDS_Shape aShape3D = anObject3D->GetShape();
275 setShape( aShape3D, false, false );
277 QColor aFillingColor = anObject3D->GetFillingColor();
278 QColor aBorderColor = anObject3D->GetBorderColor();
280 setFillingColor( aFillingColor, false, false );
281 setBorderColor( aBorderColor, false, false );
283 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
285 Handle(HYDROData_ShapesGroup) aShapesGroup =
286 Handle(HYDROData_ShapesGroup)::DownCast( myObject );
288 TopTools_SequenceOfShape aShapes;
289 aShapesGroup->GetShapes( aShapes );
291 TopoDS_Compound aCompound;
292 BRep_Builder aCompoundBuilder;
293 aCompoundBuilder.MakeCompound( aCompound );
295 for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
297 const TopoDS_Shape& aShape = aShapes.Value( i );
298 aCompoundBuilder.Add( aCompound, aShape );
301 setShape( aCompound, false, false );
303 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_LandCover) ) )
305 Handle(HYDROData_LandCover) aLandCoverObj =
306 Handle(HYDROData_LandCover)::DownCast( myObject );
308 TopoDS_Shape aLandCoverShape = aLandCoverObj->GetShape();
309 if ( !aLandCoverShape.IsNull() ) {
310 setShape( aLandCoverShape, false, false );
313 QColor aFillingColor = aLandCoverObj->GetFillingColor();
314 QColor aBorderColor = aLandCoverObj->GetBorderColor();
316 setFillingColor( aFillingColor, false, false );
317 setBorderColor( aBorderColor, false, false );
321 if ( myShape.IsNull() || !isVisible() )
324 displayShape( isUpdateViewer );
326 if (isDeactivateSelection)
327 myContext->Deactivate(myShape);
330 void HYDROGUI_Shape::setVisible( const bool theState,
331 const bool theIsUpdateViewer )
333 myIsVisible = theState;
335 if ( myShape.IsNull() )
338 if ( ( myIsVisible && myContext->IsDisplayed( myShape ) ) ||
339 ( !myIsVisible && !myContext->IsDisplayed( myShape ) ) )
343 displayShape( theIsUpdateViewer );
346 myContext->Erase( myShape, theIsUpdateViewer );
349 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
351 if ( myIsHighlight == theIsHighlight )
354 myIsHighlight = theIsHighlight;
356 if ( myContext.IsNull() || myShape.IsNull() )
359 colorShapeBorder( getActiveColor() );
360 displayShape( isUpdateViewer );
363 bool HYDROGUI_Shape::isHighlighted() const
365 return myIsHighlight;
368 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
369 const bool theToDisplay,
370 const bool theIsUpdateViewer )
372 myTopoShape = theWire;
373 // To avoid that hilight presentation is equal to "normal" object presentation.
374 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
375 myDisplayMode = AIS_Shaded;
378 updateShape( theToDisplay, theIsUpdateViewer );
381 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
382 const bool theToDisplay,
383 const bool theIsUpdateViewer )
385 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
386 TopoDS_Compound aCompound;
387 BRep_Builder aBuilder;
388 aBuilder.MakeCompound( aCompound );
390 for ( ; anExp.More(); anExp.Next() ) {
391 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
392 if ( aWire.IsNull() ) {
396 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
398 if( aMakeFace.IsDone() ) {
399 aBuilder.Add( aCompound, aMakeFace.Face() );
403 myTopoShape = aCompound;
404 myDisplayMode = AIS_Shaded;
407 updateShape( theToDisplay, theIsUpdateViewer );
410 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
411 const bool theToDisplay,
412 const bool theIsUpdateViewer,
413 const QString& theTextureFileName )
415 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
416 aFaceBuilder.Build();
417 if( aFaceBuilder.IsDone() )
419 TopoDS_Face aFace = aFaceBuilder.Face();
420 setFace( aFace, theToDisplay, theIsUpdateViewer, theTextureFileName );
424 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
425 const bool theToDisplay,
426 const bool theIsUpdateViewer,
427 const QString& theTextureFileName )
429 myTopoShape = theFace;
430 myDisplayMode = theTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
431 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
432 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
435 updateShape( theToDisplay, theIsUpdateViewer );
438 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
439 const bool theToDisplay,
440 const bool theIsUpdateViewer )
442 myTopoShape = theShape;
443 myDisplayMode = AIS_Shaded;
446 updateShape( theToDisplay, theIsUpdateViewer );
449 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
450 const bool theToDisplay,
451 const bool theIsUpdateViewer )
453 myFillingColor = theColor;
454 updateShape( theToDisplay, theIsUpdateViewer );
457 QColor HYDROGUI_Shape::getFillingColor() const
459 return myFillingColor;
462 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
463 const bool theToDisplay,
464 const bool theIsUpdateViewer )
466 myBorderColor = theColor;
467 updateShape( theToDisplay, theIsUpdateViewer );
470 QColor HYDROGUI_Shape::getBorderColor() const
472 return myBorderColor;
475 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
477 myHighlightColor = theColor;
480 QColor HYDROGUI_Shape::getHighlightColor() const
482 return myHighlightColor;
485 void HYDROGUI_Shape::setZLayer( const int theZLayer )
487 if ( myZLayer == theZLayer )
490 myZLayer = theZLayer;
491 if ( !myShape.IsNull() && isVisible() && !myContext.IsNull() && myZLayer >= 0 )
492 myContext->SetZLayer( myShape, myZLayer );
495 Handle_AIS_InteractiveObject HYDROGUI_Shape::createShape() const
497 if( myTopoShape.IsNull() ) {
498 return Handle_AIS_InteractiveObject();
501 TopAbs_ShapeEnum aShapeType = myTopoShape.ShapeType();
502 bool IsWireEdgeCompound = aShapeType==TopAbs_COMPOUND;
503 if (IsWireEdgeCompound) {
504 TopoDS_Iterator itr(myTopoShape);
505 while (itr.More() && IsWireEdgeCompound) {
506 if (itr.Value().ShapeType() != TopAbs_WIRE && itr.Value().ShapeType() != TopAbs_EDGE)
507 IsWireEdgeCompound = false;
512 if ( aShapeType==TopAbs_EDGE || aShapeType==TopAbs_WIRE || IsWireEdgeCompound) {
513 return new HYDROGUI_Polyline( myTopoShape );
515 return new AIS_Shape( myTopoShape );
519 void HYDROGUI_Shape::buildShape()
521 // Erase previously created shape
524 myShape = createShape();
525 if( myShape.IsNull() )
528 Handle_AIS_Shape anAISShape = Handle_AIS_Shape::DownCast( myShape );
529 if( !anAISShape.IsNull() )
530 anAISShape ->SetHLRAngleAndDeviation( 0.001 );
532 if ( !myObject.IsNull() )
533 myShape->SetOwner( myObject );
535 myShape->SetTransparency( 0 );
536 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
538 // Init default params for shape
539 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
540 if ( !anAttributes.IsNull() )
542 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
543 if ( !anIsoAspect.IsNull() ) {
544 anIsoAspect->SetNumber( 0 );
545 anAttributes->SetUIsoAspect( anIsoAspect );
548 anIsoAspect = anAttributes->VIsoAspect();
549 if ( !anIsoAspect.IsNull() ) {
550 anIsoAspect->SetNumber( 0 );
551 anAttributes->SetVIsoAspect( anIsoAspect );
554 if ( myDisplayMode == AIS_Shaded )
556 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
557 if ( !aShadingAspect.IsNull() )
559 Graphic3d_MaterialAspect aMatAspect( Graphic3d_NOM_PLASTIC );
560 //aMatAspect.SetAmbient( 1 );
561 //aMatAspect.SetDiffuse( 0 );
563 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
564 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
567 else if ( myDisplayMode == AIS_WireFrame )
569 anAttributes->SetWireDraw( true );
574 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
575 const bool theIsUpdateViewer )
577 if ( myShape.IsNull() )
580 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
581 if ( !anAttributes.IsNull() )
583 if ( myDisplayMode == AIS_Shaded )
585 // Coloring face filling
586 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
587 if ( !aShadingAspect.IsNull() )
589 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
590 getQuantityColorVal( myFillingColor.green() ),
591 getQuantityColorVal( myFillingColor.blue() ),
594 aShadingAspect->SetColor( aFillingColor );
595 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
598 else if ( myDisplayMode == AIS_WireFrame )
603 colorShapeBorder( getActiveColor() );
606 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
609 displayShape( theIsUpdateViewer );
612 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
614 myContext->Display( myShape, Standard_False );
617 myContext->SetZLayer( myShape, myZLayer );
619 if ( theIsUpdateViewer ) {
620 myContext->UpdateCurrentViewer();
624 QColor HYDROGUI_Shape::getActiveColor() const
626 return isHighlighted() ? myHighlightColor : myBorderColor;
629 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
631 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
634 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
636 if ( myShape.IsNull() )
639 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
640 if ( anAttributes.IsNull() )
643 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
644 getQuantityColorVal( theColor.green() ),
645 getQuantityColorVal( theColor.blue() ),
648 if( !myTopoShape.IsNull() )
650 if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode
652 myShape->SetColor( aBorderColor );
654 else if ( myDisplayMode == AIS_Shaded )
656 if ( theColor.alpha() == 0 )
658 anAttributes->SetFaceBoundaryDraw( false );
662 anAttributes->SetFaceBoundaryDraw( true );
664 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
665 if ( !aBoundaryAspect.IsNull() )
667 aBoundaryAspect->SetColor( aBorderColor );
668 anAttributes->SetFaceBoundaryAspect( aBoundaryAspect );
670 Handle(Prs3d_LineAspect) aWireAspect = anAttributes->WireAspect();
671 if ( !aWireAspect.IsNull() )
673 aWireAspect->SetColor( aBorderColor );
674 anAttributes->SetWireAspect( aWireAspect );
678 else if ( myDisplayMode == AIS_WireFrame )
680 myShape->SetColor( aBorderColor );
685 void HYDROGUI_Shape::setDisplayMode( int theDisplayMode )
687 myDisplayMode = theDisplayMode;