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( Qt::darkBlue ) );
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 );
305 if ( myShape.IsNull() || !isVisible() )
308 displayShape( isUpdateViewer );
310 if (isDeactivateSelection)
311 myContext->Deactivate(myShape);
314 void HYDROGUI_Shape::setVisible( const bool theState,
315 const bool theIsUpdateViewer )
317 myIsVisible = theState;
319 if ( myShape.IsNull() )
322 if ( ( myIsVisible && myContext->IsDisplayed( myShape ) ) ||
323 ( !myIsVisible && !myContext->IsDisplayed( myShape ) ) )
327 displayShape( theIsUpdateViewer );
330 myContext->Erase( myShape, theIsUpdateViewer );
333 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
335 if ( myIsHighlight == theIsHighlight )
338 myIsHighlight = theIsHighlight;
340 if ( myContext.IsNull() || myShape.IsNull() )
343 colorShapeBorder( getActiveColor() );
344 displayShape( isUpdateViewer );
347 bool HYDROGUI_Shape::isHighlighted() const
349 return myIsHighlight;
352 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
353 const bool theToDisplay,
354 const bool theIsUpdateViewer )
356 myTopoShape = theWire;
357 // To avoid that hilight presentation is equal to "normal" object presentation.
358 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
359 myDisplayMode = AIS_Shaded;
362 updateShape( theToDisplay, theIsUpdateViewer );
365 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
366 const bool theToDisplay,
367 const bool theIsUpdateViewer )
369 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
370 TopoDS_Compound aCompound;
371 BRep_Builder aBuilder;
372 aBuilder.MakeCompound( aCompound );
374 for ( ; anExp.More(); anExp.Next() ) {
375 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
376 if ( aWire.IsNull() ) {
380 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
382 if( aMakeFace.IsDone() ) {
383 aBuilder.Add( aCompound, aMakeFace.Face() );
387 myTopoShape = aCompound;
388 myDisplayMode = AIS_Shaded;
391 updateShape( theToDisplay, theIsUpdateViewer );
394 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
395 const bool theToDisplay,
396 const bool theIsUpdateViewer,
397 const QString& theTextureFileName )
399 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
400 aFaceBuilder.Build();
401 if( aFaceBuilder.IsDone() )
403 TopoDS_Face aFace = aFaceBuilder.Face();
404 setFace( aFace, theToDisplay, theIsUpdateViewer, theTextureFileName );
408 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
409 const bool theToDisplay,
410 const bool theIsUpdateViewer,
411 const QString& theTextureFileName )
413 myTopoShape = theFace;
414 myDisplayMode = theTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
415 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
416 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
419 updateShape( theToDisplay, theIsUpdateViewer );
422 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
423 const bool theToDisplay,
424 const bool theIsUpdateViewer )
426 myTopoShape = theShape;
427 myDisplayMode = AIS_Shaded;
430 updateShape( theToDisplay, theIsUpdateViewer );
433 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
434 const bool theToDisplay,
435 const bool theIsUpdateViewer )
437 myFillingColor = theColor;
438 updateShape( theToDisplay, theIsUpdateViewer );
441 QColor HYDROGUI_Shape::getFillingColor() const
443 return myFillingColor;
446 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
447 const bool theToDisplay,
448 const bool theIsUpdateViewer )
450 myBorderColor = theColor;
451 updateShape( theToDisplay, theIsUpdateViewer );
454 QColor HYDROGUI_Shape::getBorderColor() const
456 return myBorderColor;
459 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
461 myHighlightColor = theColor;
464 QColor HYDROGUI_Shape::getHighlightColor() const
466 return myHighlightColor;
469 void HYDROGUI_Shape::setZLayer( const int theZLayer )
471 if ( myZLayer == theZLayer )
474 myZLayer = theZLayer;
475 if ( !myShape.IsNull() && isVisible() && !myContext.IsNull() && myZLayer >= 0 )
476 myContext->SetZLayer( myShape, myZLayer );
479 Handle_AIS_InteractiveObject HYDROGUI_Shape::createShape() const
481 if( myTopoShape.IsNull() ) {
482 return Handle_AIS_InteractiveObject();
485 TopAbs_ShapeEnum aShapeType = myTopoShape.ShapeType();
486 bool IsWireEdgeCompound = aShapeType==TopAbs_COMPOUND;
487 if (IsWireEdgeCompound) {
488 TopoDS_Iterator itr(myTopoShape);
489 while (itr.More() && IsWireEdgeCompound) {
490 if (itr.Value().ShapeType() != TopAbs_WIRE && itr.Value().ShapeType() != TopAbs_EDGE)
491 IsWireEdgeCompound = false;
496 if ( aShapeType==TopAbs_EDGE || aShapeType==TopAbs_WIRE || IsWireEdgeCompound) {
497 return new HYDROGUI_Polyline( myTopoShape );
499 return new AIS_Shape( myTopoShape );
503 void HYDROGUI_Shape::buildShape()
505 // Erase previously created shape
508 myShape = createShape();
509 if( myShape.IsNull() )
512 Handle_AIS_Shape anAISShape = Handle_AIS_Shape::DownCast( myShape );
513 if( !anAISShape.IsNull() )
514 anAISShape ->SetHLRAngleAndDeviation( 0.001 );
516 if ( !myObject.IsNull() )
517 myShape->SetOwner( myObject );
519 myShape->SetTransparency( 0 );
520 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
522 // Init default params for shape
523 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
524 if ( !anAttributes.IsNull() )
526 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
527 if ( !anIsoAspect.IsNull() ) {
528 anIsoAspect->SetNumber( 0 );
529 anAttributes->SetUIsoAspect( anIsoAspect );
532 anIsoAspect = anAttributes->VIsoAspect();
533 if ( !anIsoAspect.IsNull() ) {
534 anIsoAspect->SetNumber( 0 );
535 anAttributes->SetVIsoAspect( anIsoAspect );
538 if ( myDisplayMode == AIS_Shaded )
540 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
541 if ( !aShadingAspect.IsNull() )
543 Graphic3d_MaterialAspect aMatAspect( Graphic3d_NOM_PLASTIC );
544 //aMatAspect.SetAmbient( 1 );
545 //aMatAspect.SetDiffuse( 0 );
547 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
548 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
551 else if ( myDisplayMode == AIS_WireFrame )
553 anAttributes->SetWireDraw( true );
558 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
559 const bool theIsUpdateViewer )
561 if ( myShape.IsNull() )
564 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
565 if ( !anAttributes.IsNull() )
567 if ( myDisplayMode == AIS_Shaded )
569 // Coloring face filling
570 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
571 if ( !aShadingAspect.IsNull() )
573 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
574 getQuantityColorVal( myFillingColor.green() ),
575 getQuantityColorVal( myFillingColor.blue() ),
578 aShadingAspect->SetColor( aFillingColor );
579 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
582 else if ( myDisplayMode == AIS_WireFrame )
587 colorShapeBorder( getActiveColor() );
590 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
593 displayShape( theIsUpdateViewer );
596 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
598 myContext->Display( myShape, Standard_False );
601 myContext->SetZLayer( myShape, myZLayer );
603 if ( theIsUpdateViewer ) {
604 myContext->UpdateCurrentViewer();
608 QColor HYDROGUI_Shape::getActiveColor() const
610 return isHighlighted() ? myHighlightColor : myBorderColor;
613 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
615 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
618 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
620 if ( myShape.IsNull() )
623 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
624 if ( anAttributes.IsNull() )
627 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
628 getQuantityColorVal( theColor.green() ),
629 getQuantityColorVal( theColor.blue() ),
632 if( !myTopoShape.IsNull() )
634 if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode
636 myShape->SetColor( aBorderColor );
638 else if ( myDisplayMode == AIS_Shaded )
640 if ( theColor.alpha() == 0 )
642 anAttributes->SetFaceBoundaryDraw( false );
646 anAttributes->SetFaceBoundaryDraw( true );
648 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
649 if ( !aBoundaryAspect.IsNull() )
651 aBoundaryAspect->SetColor( aBorderColor );
652 anAttributes->SetFaceBoundaryAspect( aBoundaryAspect );
654 Handle(Prs3d_LineAspect) aWireAspect = anAttributes->WireAspect();
655 if ( !aWireAspect.IsNull() )
657 aWireAspect->SetColor( aBorderColor );
658 anAttributes->SetWireAspect( aWireAspect );
662 else if ( myDisplayMode == AIS_WireFrame )
664 myShape->SetColor( aBorderColor );
669 void HYDROGUI_Shape::setDisplayMode( int theDisplayMode )
671 myDisplayMode = theDisplayMode;