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>
22 #include <HYDROGUI_AISShape.h>
24 #include <HYDROData_Channel.h>
25 #include <HYDROData_Document.h>
26 #include <HYDROData_DummyObject3D.h>
27 #include <HYDROData_ImmersibleZone.h>
28 #include <HYDROData_Obstacle.h>
29 #include <HYDROData_PolylineXY.h>
30 #include <HYDROData_Polyline3D.h>
31 #include <HYDROData_Profile.h>
32 #include <HYDROData_ShapesGroup.h>
33 #include <HYDROData_Stream.h>
34 #include <HYDROData_Zone.h>
35 #include <HYDROData_LandCoverMap.h>
37 #include <BRep_Builder.hxx>
38 #include <BRepBuilderAPI_MakeFace.hxx>
39 #include <Prs3d_IsoAspect.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 ),
58 mySelectionMode( AIS_Shape::SelectionMode( TopAbs_SHAPE ) )
62 HYDROGUI_Shape::~HYDROGUI_Shape()
67 Handle(AIS_InteractiveContext) HYDROGUI_Shape::getContext() const
72 Handle(HYDROData_Entity) HYDROGUI_Shape::getObject() const
77 TopoDS_Shape HYDROGUI_Shape::getTopoShape() const
82 bool HYDROGUI_Shape::getIsToUpdate() const
87 void HYDROGUI_Shape::setIsToUpdate( bool theState )
89 myIsToUpdate = theState;
92 bool HYDROGUI_Shape::isVisible() const
97 QList<Handle(AIS_InteractiveObject)> HYDROGUI_Shape::getAISObjects() const
102 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
104 if ( myContext.IsNull() || myShapes.empty() || !isVisible() )
107 displayShape( theIsUpdateViewer );
110 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
112 if ( myContext.IsNull() || myShapes.empty() )
115 eraseShape( theIsUpdateViewer );
118 void HYDROGUI_Shape::update( bool isUpdateViewer,
119 bool isDeactivateSelection )
122 setIsToUpdate( false );
124 if ( myContext.IsNull() )
127 // Try to retrieve information from object
128 if ( !myObject.IsNull() )
130 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
132 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
134 Handle(HYDROData_ImmersibleZone) aZoneObj =
135 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
137 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
138 if ( !aZoneShape.IsNull() ) {
139 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
140 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
141 setFace( aZoneFace, false, false, "" );
143 myTopoShape = aZoneShape;
144 //TODO: myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
145 myDisplayMode = AIS_Shaded;
148 updateShape( false, false );
152 QColor aFillingColor = aZoneObj->GetFillingColor();
153 QColor aBorderColor = aZoneObj->GetBorderColor();
155 setFillingColor( aFillingColor, false, false );
156 setBorderColor( aBorderColor, false, false );
158 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
160 Handle(HYDROData_PolylineXY) aPolyline =
161 Handle(HYDROData_PolylineXY)::DownCast( myObject );
163 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
165 if ( !aPolylineShape.IsNull() )
167 if ( aPolylineShape.ShapeType() == TopAbs_WIRE )
169 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
170 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 ( myShapes.empty() || !isVisible() )
308 displayShape( isUpdateViewer );
310 if (isDeactivateSelection)
311 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
312 myContext->Deactivate( aShape );
315 void HYDROGUI_Shape::setVisible( const bool theState,
316 const bool theIsUpdateViewer )
318 myIsVisible = theState;
320 if ( myShapes.empty() )
323 if ( ( myIsVisible && myContext->IsDisplayed( myShapes[0] ) ) ||
324 ( !myIsVisible && !myContext->IsDisplayed( myShapes[0] ) ) )
328 displayShape( theIsUpdateViewer );
331 eraseShape( theIsUpdateViewer );
334 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
336 if ( myIsHighlight == theIsHighlight )
339 myIsHighlight = theIsHighlight;
341 if ( myContext.IsNull() || myShapes.empty() )
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,
426 const int theDisplayMode )
428 myTopoShape = theShape;
429 myDisplayMode = theDisplayMode;
432 updateShape( theToDisplay, theIsUpdateViewer );
435 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
436 const bool theToDisplay,
437 const bool theIsUpdateViewer )
439 myFillingColor = theColor;
440 updateShape( theToDisplay, theIsUpdateViewer );
443 QColor HYDROGUI_Shape::getFillingColor() const
445 return myFillingColor;
448 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
449 const bool theToDisplay,
450 const bool theIsUpdateViewer )
452 myBorderColor = theColor;
453 updateShape( theToDisplay, theIsUpdateViewer );
456 QColor HYDROGUI_Shape::getBorderColor() const
458 return myBorderColor;
461 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
463 myHighlightColor = theColor;
466 QColor HYDROGUI_Shape::getHighlightColor() const
468 return myHighlightColor;
471 void HYDROGUI_Shape::setZLayer( const int theZLayer )
473 if ( myZLayer == theZLayer )
476 myZLayer = theZLayer;
477 if( isVisible() && !myContext.IsNull() && myZLayer >= 0 )
478 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
479 myContext->SetZLayer( aShape, myZLayer );
482 QList<Handle(AIS_InteractiveObject)> HYDROGUI_Shape::createShape() const
484 QList<Handle(AIS_InteractiveObject)> shapes;
485 if( myTopoShape.IsNull() ) {
489 TopAbs_ShapeEnum aShapeType = myTopoShape.ShapeType();
490 bool IsWireEdgeCompound = aShapeType==TopAbs_COMPOUND;
491 if (IsWireEdgeCompound) {
492 TopoDS_Iterator itr(myTopoShape);
493 while (itr.More() && IsWireEdgeCompound) {
494 if (itr.Value().ShapeType() != TopAbs_WIRE && itr.Value().ShapeType() != TopAbs_EDGE)
495 IsWireEdgeCompound = false;
500 if ( aShapeType==TopAbs_EDGE || aShapeType==TopAbs_WIRE || IsWireEdgeCompound)
502 shapes.append( HYDROGUI_Polyline::createPresentations( myTopoShape ) );
506 shapes.append( new HYDROGUI_AISShape( myTopoShape ) );
511 void HYDROGUI_Shape::buildShape()
513 // Erase previously created shape
516 myShapes = createShape();
517 if( myShapes.empty() )
520 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
522 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( aShape );
523 if( !anAISShape.IsNull() )
524 anAISShape ->SetHLRAngleAndDeviation( 0.001 );
526 if ( !myObject.IsNull() )
527 aShape->SetOwner( myObject );
529 if ( !myObject.IsNull() && !myObject->IsKind( STANDARD_TYPE(HYDROData_LandCoverMap) ) )
530 aShape->SetTransparency( 0 );
531 aShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
533 // Init default params for shape
534 const Handle(Prs3d_Drawer)& anAttributes = aShape->Attributes();
535 if ( !anAttributes.IsNull() )
537 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
538 if ( !anIsoAspect.IsNull() ) {
539 anIsoAspect->SetNumber( 0 );
540 anAttributes->SetUIsoAspect( anIsoAspect );
543 anIsoAspect = anAttributes->VIsoAspect();
544 if ( !anIsoAspect.IsNull() )
546 anIsoAspect->SetNumber( 0 );
547 anAttributes->SetVIsoAspect( anIsoAspect );
550 if ( myDisplayMode == AIS_Shaded )
552 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
554 else if ( myDisplayMode == AIS_WireFrame )
556 anAttributes->SetWireDraw( true );
562 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
563 const bool theIsUpdateViewer )
565 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
567 const Handle(Prs3d_Drawer)& anAttributes = aShape->Attributes();
568 if ( !anAttributes.IsNull() )
570 if ( myDisplayMode == AIS_Shaded && myFillingColor.isValid() )
572 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
573 getQuantityColorVal( myFillingColor.green() ),
574 getQuantityColorVal( myFillingColor.blue() ),
577 aShape->SetColor( aFillingColor );
578 aShape->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
580 else if ( myDisplayMode == AIS_WireFrame )
588 colorShapeBorder( myBorderColor );
589 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
592 displayShape( theIsUpdateViewer );
595 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
597 if ( myContext->HasOpenedContext() && mySelectionMode > 0 )
598 myContext->CloseLocalContext();
600 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
602 if ( mySelectionMode > 0 )
603 // Display object in local context with selection
604 myContext->Display( aShape, myDisplayMode, mySelectionMode, Standard_False, Standard_False );
607 if ( !myContext->HasOpenedContext() )
608 // Ordinary display of object published in the Object Browser
609 myContext->Display( aShape, Standard_False );
611 // Display object in local context without selection
612 myContext->Display( aShape, myDisplayMode, -1, Standard_False, Standard_False );
616 if ( mySelectionMode > 0 )
618 myContext->OpenLocalContext();
619 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
620 myContext->Activate( aShape, mySelectionMode, Standard_True );
625 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
626 myContext->SetZLayer( aShape, myZLayer );
629 if ( theIsUpdateViewer ) {
630 myContext->UpdateCurrentViewer();
634 void HYDROGUI_Shape::eraseShape( const bool theIsUpdateViewer )
636 if ( myContext->HasOpenedContext() && mySelectionMode > 0 )
637 myContext->CloseLocalContext();
639 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
640 myContext->Erase( aShape, Standard_False );
642 if( theIsUpdateViewer )
643 myContext->UpdateCurrentViewer();
646 QColor HYDROGUI_Shape::getActiveColor() const
648 return isHighlighted() ? myHighlightColor : myBorderColor;
651 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
653 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
656 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
658 foreach( Handle(AIS_InteractiveObject) aShape1, myShapes )
660 const Handle(Prs3d_Drawer)& anAttributes = aShape1->Attributes();
661 if ( anAttributes.IsNull() )
664 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
665 getQuantityColorVal( theColor.green() ),
666 getQuantityColorVal( theColor.blue() ),
669 if( !myTopoShape.IsNull() )
671 if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode
673 aShape1->SetColor( aBorderColor );
675 else if ( myDisplayMode == AIS_Shaded )
677 Handle(HYDROGUI_AISShape) aShape = Handle(HYDROGUI_AISShape)::DownCast( aShape1 );
678 if( !aShape.IsNull() )
679 aShape->SetBorderColor( aBorderColor );
681 else if ( myDisplayMode == AIS_WireFrame )
683 aShape1->SetColor( aBorderColor );
689 void HYDROGUI_Shape::setDisplayMode( int theDisplayMode )
691 myDisplayMode = theDisplayMode;
694 void HYDROGUI_Shape::setSelectionMode( int theSelectionMode )
696 mySelectionMode = theSelectionMode;