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()
66 if ( !myShape.IsNull() )
70 Handle(AIS_InteractiveContext) HYDROGUI_Shape::getContext() const
75 Handle(HYDROData_Entity) HYDROGUI_Shape::getObject() const
80 TopoDS_Shape HYDROGUI_Shape::getTopoShape() const
85 bool HYDROGUI_Shape::getIsToUpdate() const
90 void HYDROGUI_Shape::setIsToUpdate( bool theState )
92 myIsToUpdate = theState;
95 bool HYDROGUI_Shape::isVisible() const
100 Handle(AIS_InteractiveObject) HYDROGUI_Shape::getAISObject() const
105 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
107 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
110 displayShape( theIsUpdateViewer );
113 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
115 if ( myContext.IsNull() || myShape.IsNull() )
118 eraseShape( theIsUpdateViewer );
121 void HYDROGUI_Shape::update( bool isUpdateViewer,
122 bool isDeactivateSelection )
125 setIsToUpdate( false );
127 if ( myContext.IsNull() )
130 // Try to retrieve information from object
131 if ( !myObject.IsNull() )
133 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
135 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
137 Handle(HYDROData_ImmersibleZone) aZoneObj =
138 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
140 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
141 if ( !aZoneShape.IsNull() ) {
142 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
143 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
144 setFace( aZoneFace, false, false, "" );
146 myTopoShape = aZoneShape;
147 //TODO: myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
148 myDisplayMode = AIS_Shaded;
151 updateShape( false, false );
155 QColor aFillingColor = aZoneObj->GetFillingColor();
156 QColor aBorderColor = aZoneObj->GetBorderColor();
158 setFillingColor( aFillingColor, false, false );
159 setBorderColor( aBorderColor, false, false );
161 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
163 Handle(HYDROData_PolylineXY) aPolyline =
164 Handle(HYDROData_PolylineXY)::DownCast( myObject );
166 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
168 if ( !aPolylineShape.IsNull() )
170 if ( aPolylineShape.ShapeType() == TopAbs_WIRE )
172 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
173 setWire( aPolylineWire, false, false );
177 myTopoShape = aPolylineShape;
178 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
179 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
180 myDisplayMode = AIS_Shaded;
183 updateShape( false, false );
187 QColor aWireColor = aPolyline->GetWireColor();
188 setBorderColor( aWireColor, false, false );
190 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
192 Handle(HYDROData_Polyline3D) aPolyline =
193 Handle(HYDROData_Polyline3D)::DownCast( myObject );
195 TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
197 if ( !aPolylineShape.IsNull() ) {
198 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
199 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
200 setWire( aPolylineWire, false, false );
202 myTopoShape = aPolylineShape;
203 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
204 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
205 myDisplayMode = AIS_Shaded;
208 updateShape( false, false );
212 QColor aWireColor = aPolyline->GetBorderColor();
213 setBorderColor( aWireColor, false, false );
215 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
217 Handle(HYDROData_Zone) aZone =
218 Handle(HYDROData_Zone)::DownCast( myObject );
220 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
222 setFace( aZoneFace, false, false, "" );
223 if( aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
225 // Red color for a zone with bathymetry conflict
226 setFillingColor( Qt::red );
230 // Set the filling color for zone
231 setFillingColor( aZone->GetColor( Qt::darkBlue ) );
234 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
236 Handle(HYDROData_Profile) aProfile =
237 Handle(HYDROData_Profile)::DownCast( myObject );
239 TopoDS_Wire aProfileWire;
241 if ( aProfile->IsValid() ) {
242 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
244 if ( !aProfileShape.IsNull() &&
245 aProfileShape.ShapeType() == TopAbs_WIRE ) {
246 aProfileWire = TopoDS::Wire( aProfileShape );
250 setWire( aProfileWire, false, false );
252 QColor aWireColor = aProfile->GetBorderColor();
253 setBorderColor( aWireColor, false, false );
255 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
256 myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
257 myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
259 Handle(HYDROData_Object) aGeomObject =
260 Handle(HYDROData_Object)::DownCast( myObject );
262 TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
264 setShape( anObjShape, false, false );
266 QColor aFillingColor = aGeomObject->GetFillingColor();
267 QColor aBorderColor = aGeomObject->GetBorderColor();
269 setFillingColor( aFillingColor, false, false );
270 setBorderColor( aBorderColor, false, false );
272 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
274 Handle(HYDROData_DummyObject3D) anObject3D =
275 Handle(HYDROData_DummyObject3D)::DownCast( myObject );
276 TopoDS_Shape aShape3D = anObject3D->GetShape();
278 setShape( aShape3D, false, false );
280 QColor aFillingColor = anObject3D->GetFillingColor();
281 QColor aBorderColor = anObject3D->GetBorderColor();
283 setFillingColor( aFillingColor, false, false );
284 setBorderColor( aBorderColor, false, false );
286 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
288 Handle(HYDROData_ShapesGroup) aShapesGroup =
289 Handle(HYDROData_ShapesGroup)::DownCast( myObject );
291 TopTools_SequenceOfShape aShapes;
292 aShapesGroup->GetShapes( aShapes );
294 TopoDS_Compound aCompound;
295 BRep_Builder aCompoundBuilder;
296 aCompoundBuilder.MakeCompound( aCompound );
298 for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
300 const TopoDS_Shape& aShape = aShapes.Value( i );
301 aCompoundBuilder.Add( aCompound, aShape );
304 setShape( aCompound, false, false );
308 if ( myShape.IsNull() || !isVisible() )
311 displayShape( isUpdateViewer );
313 if (isDeactivateSelection)
314 myContext->Deactivate(myShape);
317 void HYDROGUI_Shape::setVisible( const bool theState,
318 const bool theIsUpdateViewer )
320 myIsVisible = theState;
322 if ( myShape.IsNull() )
325 if ( ( myIsVisible && myContext->IsDisplayed( myShape ) ) ||
326 ( !myIsVisible && !myContext->IsDisplayed( myShape ) ) )
330 displayShape( theIsUpdateViewer );
333 eraseShape( theIsUpdateViewer );
336 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
338 if ( myIsHighlight == theIsHighlight )
341 myIsHighlight = theIsHighlight;
343 if ( myContext.IsNull() || myShape.IsNull() )
346 colorShapeBorder( getActiveColor() );
347 displayShape( isUpdateViewer );
350 bool HYDROGUI_Shape::isHighlighted() const
352 return myIsHighlight;
355 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
356 const bool theToDisplay,
357 const bool theIsUpdateViewer )
359 myTopoShape = theWire;
360 // To avoid that hilight presentation is equal to "normal" object presentation.
361 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
362 myDisplayMode = AIS_Shaded;
365 updateShape( theToDisplay, theIsUpdateViewer );
368 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
369 const bool theToDisplay,
370 const bool theIsUpdateViewer )
372 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
373 TopoDS_Compound aCompound;
374 BRep_Builder aBuilder;
375 aBuilder.MakeCompound( aCompound );
377 for ( ; anExp.More(); anExp.Next() ) {
378 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
379 if ( aWire.IsNull() ) {
383 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
385 if( aMakeFace.IsDone() ) {
386 aBuilder.Add( aCompound, aMakeFace.Face() );
390 myTopoShape = aCompound;
391 myDisplayMode = AIS_Shaded;
394 updateShape( theToDisplay, theIsUpdateViewer );
397 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
398 const bool theToDisplay,
399 const bool theIsUpdateViewer,
400 const QString& theTextureFileName )
402 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
403 aFaceBuilder.Build();
404 if( aFaceBuilder.IsDone() )
406 TopoDS_Face aFace = aFaceBuilder.Face();
407 setFace( aFace, theToDisplay, theIsUpdateViewer, theTextureFileName );
411 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
412 const bool theToDisplay,
413 const bool theIsUpdateViewer,
414 const QString& theTextureFileName )
416 myTopoShape = theFace;
417 myDisplayMode = theTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
418 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
419 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
422 updateShape( theToDisplay, theIsUpdateViewer );
425 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
426 const bool theToDisplay,
427 const bool theIsUpdateViewer,
428 const int theDisplayMode )
430 myTopoShape = theShape;
431 myDisplayMode = theDisplayMode;
434 updateShape( theToDisplay, theIsUpdateViewer );
437 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
438 const bool theToDisplay,
439 const bool theIsUpdateViewer )
441 myFillingColor = theColor;
442 updateShape( theToDisplay, theIsUpdateViewer );
445 QColor HYDROGUI_Shape::getFillingColor() const
447 return myFillingColor;
450 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
451 const bool theToDisplay,
452 const bool theIsUpdateViewer )
454 myBorderColor = theColor;
455 updateShape( theToDisplay, theIsUpdateViewer );
458 QColor HYDROGUI_Shape::getBorderColor() const
460 return myBorderColor;
463 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
465 myHighlightColor = theColor;
468 QColor HYDROGUI_Shape::getHighlightColor() const
470 return myHighlightColor;
473 void HYDROGUI_Shape::setZLayer( const int theZLayer )
475 if ( myZLayer == theZLayer )
478 myZLayer = theZLayer;
479 if ( !myShape.IsNull() && isVisible() && !myContext.IsNull() && myZLayer >= 0 )
480 myContext->SetZLayer( myShape, myZLayer );
483 Handle_AIS_InteractiveObject HYDROGUI_Shape::createShape() const
485 if( myTopoShape.IsNull() ) {
486 return Handle_AIS_InteractiveObject();
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) {
501 return new HYDROGUI_Polyline( myTopoShape );
503 return new HYDROGUI_AISShape( myTopoShape );
507 void HYDROGUI_Shape::buildShape()
509 // Erase previously created shape
512 myShape = createShape();
513 if( myShape.IsNull() )
516 Handle_AIS_Shape anAISShape = Handle_AIS_Shape::DownCast( myShape );
517 if( !anAISShape.IsNull() )
518 anAISShape ->SetHLRAngleAndDeviation( 0.001 );
520 if ( !myObject.IsNull() )
521 myShape->SetOwner( myObject );
523 if ( !myObject.IsNull() && !myObject->IsKind( STANDARD_TYPE(HYDROData_LandCoverMap) ) )
524 myShape->SetTransparency( 0 );
525 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
526 myShape->SetSelectionMode( (Standard_Integer)mySelectionMode );
528 // Init default params for shape
529 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
530 if ( !anAttributes.IsNull() )
532 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
533 if ( !anIsoAspect.IsNull() ) {
534 anIsoAspect->SetNumber( 0 );
535 anAttributes->SetUIsoAspect( anIsoAspect );
538 anIsoAspect = anAttributes->VIsoAspect();
539 if ( !anIsoAspect.IsNull() )
541 anIsoAspect->SetNumber( 0 );
542 anAttributes->SetVIsoAspect( anIsoAspect );
545 if ( myDisplayMode == AIS_Shaded )
547 myShape->SetMaterial( Graphic3d_NOM_PLASTIC );
549 else if ( myDisplayMode == AIS_WireFrame )
551 anAttributes->SetWireDraw( true );
556 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
557 const bool theIsUpdateViewer )
559 if ( myShape.IsNull() )
562 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
563 if ( !anAttributes.IsNull() )
565 if ( myDisplayMode == AIS_Shaded && myFillingColor.isValid() )
567 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
568 getQuantityColorVal( myFillingColor.green() ),
569 getQuantityColorVal( myFillingColor.blue() ),
572 myShape->SetColor( aFillingColor );
573 myShape->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
575 else if ( myDisplayMode == AIS_WireFrame )
580 colorShapeBorder( myBorderColor );
583 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
586 displayShape( theIsUpdateViewer );
589 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
591 if ( myContext->HasOpenedContext() && mySelectionMode > 0 )
592 myContext->CloseLocalContext();
594 if ( mySelectionMode > 0 )
595 // Display object in local context with selection
596 myContext->Display( myShape, myDisplayMode, mySelectionMode, Standard_False, Standard_False );
599 if ( !myContext->HasOpenedContext() )
600 // Ordinary display of object published in the Object Browser
601 myContext->Display( myShape, Standard_False );
603 // Display object in local context without selection
604 myContext->Display( myShape, myDisplayMode, -1, Standard_False, Standard_False );
607 if ( mySelectionMode > 0 )
609 myContext->OpenLocalContext();
610 myContext->Activate( myShape, mySelectionMode, Standard_True );
614 myContext->SetZLayer( myShape, myZLayer );
616 if ( theIsUpdateViewer ) {
617 myContext->UpdateCurrentViewer();
621 void HYDROGUI_Shape::eraseShape( const bool theIsUpdateViewer )
623 if ( myContext->HasOpenedContext() && mySelectionMode > 0 )
624 myContext->CloseLocalContext();
626 myContext->Erase( myShape, theIsUpdateViewer );
629 QColor HYDROGUI_Shape::getActiveColor() const
631 return isHighlighted() ? myHighlightColor : myBorderColor;
634 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
636 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
639 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
641 if ( myShape.IsNull() )
644 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
645 if ( anAttributes.IsNull() )
648 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
649 getQuantityColorVal( theColor.green() ),
650 getQuantityColorVal( theColor.blue() ),
653 if( !myTopoShape.IsNull() )
655 if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode
657 myShape->SetColor( aBorderColor );
659 else if ( myDisplayMode == AIS_Shaded )
661 Handle(HYDROGUI_AISShape) aShape = Handle(HYDROGUI_AISShape)::DownCast( myShape );
662 if( !aShape.IsNull() )
663 aShape->SetBorderColor( aBorderColor );
665 else if ( myDisplayMode == AIS_WireFrame )
667 myShape->SetColor( aBorderColor );
672 void HYDROGUI_Shape::setDisplayMode( int theDisplayMode )
674 myDisplayMode = theDisplayMode;
677 void HYDROGUI_Shape::setSelectionMode( int theSelectionMode )
679 mySelectionMode = theSelectionMode;