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_BCPolygon.h>
29 #include <HYDROData_Obstacle.h>
30 #include <HYDROData_PolylineXY.h>
31 #include <HYDROData_Polyline3D.h>
32 #include <HYDROData_Profile.h>
33 #include <HYDROData_ShapesGroup.h>
34 #include <HYDROData_Stream.h>
35 #include <HYDROData_Zone.h>
36 #include <HYDROData_LandCoverMap.h>
38 #include <BRep_Builder.hxx>
39 #include <BRepBuilderAPI_MakeFace.hxx>
40 #include <Prs3d_IsoAspect.hxx>
42 #include <TopoDS_Face.hxx>
43 #include <TopoDS_Wire.hxx>
44 #include <TopExp_Explorer.hxx>
46 #include <SUIT_ResourceMgr.h>
47 #include <SUIT_Session.h>
49 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
50 const Handle(HYDROData_Entity)& theObject,
52 : myContext( theContext ),
53 myObject( theObject ),
54 myZLayer( theZLayer ),
55 myIsHighlight( false ),
56 myFillingColor( Qt::transparent ),
57 myBorderColor( Qt::black ),
58 myHighlightColor( Qt::white ),
59 myIsToUpdate( false ),
61 myDisplayMode( AIS_Shaded ),
62 mySelectionMode( AIS_Shape::SelectionMode( TopAbs_SHAPE ) )
66 HYDROGUI_Shape::~HYDROGUI_Shape()
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 QList<Handle(AIS_InteractiveObject)> HYDROGUI_Shape::getAISObjects() const
106 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
108 if ( myContext.IsNull() || myShapes.empty() || !isVisible() )
111 displayShape( theIsUpdateViewer );
114 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
116 if ( myContext.IsNull() || myShapes.empty() )
119 eraseShape( 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() )
171 if ( aPolylineShape.ShapeType() == TopAbs_WIRE )
173 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
174 setWire( aPolylineWire, false, false );
178 myTopoShape = aPolylineShape;
179 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
180 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
181 myDisplayMode = AIS_Shaded;
184 updateShape( false, false );
188 QColor aWireColor = aPolyline->GetWireColor();
189 setBorderColor( aWireColor, false, false );
191 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
193 Handle(HYDROData_Polyline3D) aPolyline =
194 Handle(HYDROData_Polyline3D)::DownCast( myObject );
196 TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
198 if ( !aPolylineShape.IsNull() ) {
199 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
200 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
201 setWire( aPolylineWire, false, false );
203 myTopoShape = aPolylineShape;
204 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
205 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
206 myDisplayMode = AIS_Shaded;
209 updateShape( false, false );
213 QColor aWireColor = aPolyline->GetBorderColor();
214 setBorderColor( aWireColor, false, false );
216 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
218 Handle(HYDROData_Zone) aZone =
219 Handle(HYDROData_Zone)::DownCast( myObject );
221 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
223 setFace( aZoneFace, false, false, "" );
224 if( aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
226 // Red color for a zone with bathymetry conflict
227 setFillingColor( Qt::red );
231 // Set the filling color for zone
232 setFillingColor( aZone->GetColor( Qt::darkBlue ) );
235 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
237 Handle(HYDROData_Profile) aProfile =
238 Handle(HYDROData_Profile)::DownCast( myObject );
240 TopoDS_Wire aProfileWire;
242 if ( aProfile->IsValid() ) {
243 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
245 if ( !aProfileShape.IsNull() &&
246 aProfileShape.ShapeType() == TopAbs_WIRE ) {
247 aProfileWire = TopoDS::Wire( aProfileShape );
251 setWire( aProfileWire, false, false );
253 QColor aWireColor = aProfile->GetBorderColor();
254 setBorderColor( aWireColor, false, false );
256 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
257 myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
258 myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
260 Handle(HYDROData_Object) aGeomObject =
261 Handle(HYDROData_Object)::DownCast( myObject );
263 TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
265 setShape( anObjShape, false, false );
267 QColor aFillingColor = aGeomObject->GetFillingColor();
268 QColor aBorderColor = aGeomObject->GetBorderColor();
270 setFillingColor( aFillingColor, false, false );
271 setBorderColor( aBorderColor, false, false );
273 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
275 Handle(HYDROData_DummyObject3D) anObject3D =
276 Handle(HYDROData_DummyObject3D)::DownCast( myObject );
277 TopoDS_Shape aShape3D = anObject3D->GetShape();
279 setShape( aShape3D, false, false );
281 QColor aFillingColor = anObject3D->GetFillingColor();
282 QColor aBorderColor = anObject3D->GetBorderColor();
284 setFillingColor( aFillingColor, false, false );
285 setBorderColor( aBorderColor, false, false );
287 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
289 Handle(HYDROData_ShapesGroup) aShapesGroup =
290 Handle(HYDROData_ShapesGroup)::DownCast( myObject );
292 TopTools_SequenceOfShape aShapes;
293 aShapesGroup->GetShapes( aShapes );
295 TopoDS_Compound aCompound;
296 BRep_Builder aCompoundBuilder;
297 aCompoundBuilder.MakeCompound( aCompound );
299 for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
301 const TopoDS_Shape& aShape = aShapes.Value( i );
302 aCompoundBuilder.Add( aCompound, aShape );
305 setShape( aCompound, false, false );
307 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_BCPolygon) ) )
309 Handle(HYDROData_BCPolygon) aBCObj = Handle(HYDROData_BCPolygon)::DownCast( myObject );
311 TopoDS_Shape aBCShape = aBCObj->GetTopShape();
312 if ( !aBCShape.IsNull() )
314 if ( aBCShape.ShapeType() == TopAbs_FACE )
316 TopoDS_Face aBCFace = TopoDS::Face( aBCShape );
317 setFace( aBCFace, false, false, "" );
321 myTopoShape = aBCShape;
322 myDisplayMode = AIS_Shaded;
324 updateShape( false, false );
328 QColor aFillingColor = aBCObj->GetFillingColor();
329 QColor aBorderColor = aBCObj->GetBorderColor();
330 aFillingColor.setAlpha(175);
332 setFillingColor( aFillingColor, false, false );
333 setBorderColor( aBorderColor, false, false );
338 if ( myShapes.empty() || !isVisible() )
341 displayShape( isUpdateViewer );
343 if (isDeactivateSelection)
344 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
345 myContext->Deactivate( aShape );
348 void HYDROGUI_Shape::setVisible( const bool theState,
349 const bool theIsUpdateViewer )
351 myIsVisible = theState;
353 if ( myShapes.empty() )
356 if ( ( myIsVisible && myContext->IsDisplayed( myShapes[0] ) ) ||
357 ( !myIsVisible && !myContext->IsDisplayed( myShapes[0] ) ) )
361 displayShape( theIsUpdateViewer );
364 eraseShape( theIsUpdateViewer );
367 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
369 if ( myIsHighlight == theIsHighlight )
372 myIsHighlight = theIsHighlight;
374 if ( myContext.IsNull() || myShapes.empty() )
377 colorShapeBorder( getActiveColor() );
378 displayShape( isUpdateViewer );
381 bool HYDROGUI_Shape::isHighlighted() const
383 return myIsHighlight;
386 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
387 const bool theToDisplay,
388 const bool theIsUpdateViewer )
390 myTopoShape = theWire;
391 // To avoid that hilight presentation is equal to "normal" object presentation.
392 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
393 myDisplayMode = AIS_Shaded;
396 updateShape( theToDisplay, theIsUpdateViewer );
399 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
400 const bool theToDisplay,
401 const bool theIsUpdateViewer )
403 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
404 TopoDS_Compound aCompound;
405 BRep_Builder aBuilder;
406 aBuilder.MakeCompound( aCompound );
408 for ( ; anExp.More(); anExp.Next() ) {
409 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
410 if ( aWire.IsNull() ) {
414 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
416 if( aMakeFace.IsDone() ) {
417 aBuilder.Add( aCompound, aMakeFace.Face() );
421 myTopoShape = aCompound;
422 myDisplayMode = AIS_Shaded;
425 updateShape( theToDisplay, theIsUpdateViewer );
428 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
429 const bool theToDisplay,
430 const bool theIsUpdateViewer,
431 const QString& theTextureFileName )
433 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
434 aFaceBuilder.Build();
435 if( aFaceBuilder.IsDone() )
437 TopoDS_Face aFace = aFaceBuilder.Face();
438 setFace( aFace, theToDisplay, theIsUpdateViewer, theTextureFileName );
442 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
443 const bool theToDisplay,
444 const bool theIsUpdateViewer,
445 const QString& theTextureFileName )
447 myTopoShape = theFace;
448 myDisplayMode = theTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
449 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
450 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
453 updateShape( theToDisplay, theIsUpdateViewer );
456 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
457 const bool theToDisplay,
458 const bool theIsUpdateViewer,
459 const int theDisplayMode )
461 myTopoShape = theShape;
462 myDisplayMode = theDisplayMode;
465 updateShape( theToDisplay, theIsUpdateViewer );
468 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
469 const bool theToDisplay,
470 const bool theIsUpdateViewer )
472 myFillingColor = theColor;
473 updateShape( theToDisplay, theIsUpdateViewer );
476 QColor HYDROGUI_Shape::getFillingColor() const
478 return myFillingColor;
481 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
482 const bool theToDisplay,
483 const bool theIsUpdateViewer )
485 myBorderColor = theColor;
486 updateShape( theToDisplay, theIsUpdateViewer );
489 QColor HYDROGUI_Shape::getBorderColor() const
491 return myBorderColor;
494 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
496 myHighlightColor = theColor;
499 QColor HYDROGUI_Shape::getHighlightColor() const
501 return myHighlightColor;
504 void HYDROGUI_Shape::setZLayer( const int theZLayer )
506 if ( myZLayer == theZLayer )
509 myZLayer = theZLayer;
510 if( isVisible() && !myContext.IsNull() && myZLayer >= 0 )
511 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
512 myContext->SetZLayer( aShape, myZLayer );
515 QList<Handle(AIS_InteractiveObject)> HYDROGUI_Shape::createShape() const
517 QList<Handle(AIS_InteractiveObject)> shapes;
518 if( myTopoShape.IsNull() ) {
522 TopAbs_ShapeEnum aShapeType = myTopoShape.ShapeType();
523 bool IsWireEdgeCompound = aShapeType==TopAbs_COMPOUND;
524 if (IsWireEdgeCompound) {
525 TopoDS_Iterator itr(myTopoShape);
526 while (itr.More() && IsWireEdgeCompound) {
527 if (itr.Value().ShapeType() != TopAbs_WIRE && itr.Value().ShapeType() != TopAbs_EDGE)
528 IsWireEdgeCompound = false;
533 if ( aShapeType==TopAbs_EDGE || aShapeType==TopAbs_WIRE || IsWireEdgeCompound)
535 SUIT_ResourceMgr* aResMgr = 0;
536 SUIT_Session* aSession = SUIT_Session::session();
538 aResMgr = SUIT_Session::session()->resourceMgr();
543 if( myShapes.length()>1 )
545 Handle(HYDROGUI_Arrow) arrow = Handle(HYDROGUI_Arrow)::DownCast( myShapes[1] );
546 if( !arrow.IsNull() )
548 aType = arrow->GetType();
549 aSize = arrow->GetSize();
552 if( aResMgr && ( aType<0 || aSize < 0 ) )
554 aResMgr->value( "polyline", "arrow_type", aType );
555 aResMgr->value( "polyline", "arrow_size", aSize );
558 shapes.append( HYDROGUI_Polyline::createPresentations( myTopoShape, aType, aSize ) );
562 shapes.append( new HYDROGUI_AISShape( myTopoShape ) );
567 void HYDROGUI_Shape::buildShape()
569 // Erase previously created shape
572 myShapes = createShape();
573 if( myShapes.empty() )
576 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
578 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast( aShape );
579 if( !anAISShape.IsNull() )
580 anAISShape ->SetHLRAngleAndDeviation( 0.001 );
582 if ( !myObject.IsNull() )
583 aShape->SetOwner( myObject );
585 if ( !myObject.IsNull() && !myObject->IsKind( STANDARD_TYPE(HYDROData_LandCoverMap) ) )
586 aShape->SetTransparency( 0 );
587 aShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
589 // Init default params for shape
590 const Handle(Prs3d_Drawer)& anAttributes = aShape->Attributes();
591 if ( !anAttributes.IsNull() )
593 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
594 if ( !anIsoAspect.IsNull() ) {
595 anIsoAspect->SetNumber( 0 );
596 anAttributes->SetUIsoAspect( anIsoAspect );
599 anIsoAspect = anAttributes->VIsoAspect();
600 if ( !anIsoAspect.IsNull() )
602 anIsoAspect->SetNumber( 0 );
603 anAttributes->SetVIsoAspect( anIsoAspect );
606 if ( myDisplayMode == AIS_Shaded )
608 aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
610 else if ( myDisplayMode == AIS_WireFrame )
612 anAttributes->SetWireDraw( true );
618 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
619 const bool theIsUpdateViewer )
621 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
623 const Handle(Prs3d_Drawer)& anAttributes = aShape->Attributes();
624 if ( !anAttributes.IsNull() )
626 if ( myDisplayMode == AIS_Shaded && myFillingColor.isValid() )
628 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
629 getQuantityColorVal( myFillingColor.green() ),
630 getQuantityColorVal( myFillingColor.blue() ),
633 aShape->SetColor( aFillingColor );
634 aShape->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
636 else if ( myDisplayMode == AIS_WireFrame )
644 colorShapeBorder( myBorderColor );
645 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
648 displayShape( theIsUpdateViewer );
651 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
653 if ( myContext->HasOpenedContext() && mySelectionMode > 0 )
654 myContext->CloseLocalContext();
656 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
658 if ( mySelectionMode > 0 )
659 // Display object in local context with selection
660 myContext->Display( aShape, myDisplayMode, mySelectionMode, Standard_False, Standard_False );
663 if ( !myContext->HasOpenedContext() )
664 // Ordinary display of object published in the Object Browser
665 myContext->Display( aShape, Standard_False );
667 // Display object in local context without selection
668 myContext->Display( aShape, myDisplayMode, -1, Standard_False, Standard_False );
672 if ( mySelectionMode > 0 )
674 myContext->OpenLocalContext();
675 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
676 myContext->Activate( aShape, mySelectionMode, Standard_True );
681 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
682 myContext->SetZLayer( aShape, myZLayer );
685 if ( theIsUpdateViewer ) {
686 myContext->UpdateCurrentViewer();
690 void HYDROGUI_Shape::eraseShape( const bool theIsUpdateViewer )
692 if ( myContext->HasOpenedContext() && mySelectionMode > 0 )
693 myContext->CloseLocalContext();
695 foreach( Handle(AIS_InteractiveObject) aShape, myShapes )
696 myContext->Erase( aShape, Standard_False );
698 if( theIsUpdateViewer )
699 myContext->UpdateCurrentViewer();
702 QColor HYDROGUI_Shape::getActiveColor() const
704 return isHighlighted() ? myHighlightColor : myBorderColor;
707 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
709 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
712 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
714 foreach( Handle(AIS_InteractiveObject) aShape1, myShapes )
716 const Handle(Prs3d_Drawer)& anAttributes = aShape1->Attributes();
717 if ( anAttributes.IsNull() )
720 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
721 getQuantityColorVal( theColor.green() ),
722 getQuantityColorVal( theColor.blue() ),
725 if( !myTopoShape.IsNull() )
727 if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode
729 aShape1->SetColor( aBorderColor );
731 else if ( myDisplayMode == AIS_Shaded )
733 Handle(HYDROGUI_AISShape) aShape = Handle(HYDROGUI_AISShape)::DownCast( aShape1 );
734 if( !aShape.IsNull() )
735 aShape->SetBorderColor( aBorderColor );
737 else if ( myDisplayMode == AIS_WireFrame )
739 aShape1->SetColor( aBorderColor );
745 void HYDROGUI_Shape::setDisplayMode( int theDisplayMode )
747 myDisplayMode = theDisplayMode;
750 void HYDROGUI_Shape::setSelectionMode( int theSelectionMode )
752 mySelectionMode = theSelectionMode;