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>
35 #include <AIS_Shape.hxx>
36 #include <BRep_Builder.hxx>
37 #include <BRepBuilderAPI_MakeFace.hxx>
38 #include <Graphic3d_AspectFillArea3d.hxx>
39 #include <Prs3d_IsoAspect.hxx>
40 #include <Prs3d_ShadingAspect.hxx>
42 #include <TopoDS_Face.hxx>
43 #include <TopoDS_Wire.hxx>
44 #include <TopExp_Explorer.hxx>
46 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
47 const Handle(HYDROData_Entity)& theObject,
49 : myContext( theContext ),
50 myObject( theObject ),
51 myZLayer( theZLayer ),
52 myIsHighlight( false ),
53 myFillingColor( Qt::transparent ),
54 myBorderColor( Qt::black ),
55 myHighlightColor( Qt::white ),
56 myIsToUpdate( false ),
58 myDisplayMode( AIS_Shaded )
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 myContext->Erase( myShape, 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() ) {
169 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
170 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
171 setWire( aPolylineWire, false, false );
173 myTopoShape = aPolylineShape;
174 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
175 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
176 myDisplayMode = AIS_Shaded;
179 updateShape( false, false );
183 QColor aWireColor = aPolyline->GetWireColor();
184 setBorderColor( aWireColor, false, false );
186 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
188 Handle(HYDROData_Polyline3D) aPolyline =
189 Handle(HYDROData_Polyline3D)::DownCast( myObject );
191 TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
193 if ( !aPolylineShape.IsNull() ) {
194 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
195 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
196 setWire( aPolylineWire, false, false );
198 myTopoShape = aPolylineShape;
199 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
200 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
201 myDisplayMode = AIS_Shaded;
204 updateShape( false, false );
208 QColor aWireColor = aPolyline->GetBorderColor();
209 setBorderColor( aWireColor, false, false );
211 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
213 Handle(HYDROData_Zone) aZone =
214 Handle(HYDROData_Zone)::DownCast( myObject );
216 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
218 setFace( aZoneFace, false, false, "" );
219 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
221 // Red color for a zone with bathymetry conflict
222 setFillingColor( Qt::red );
226 // Set the filling color for zone
227 setFillingColor( aZone->GetColor( Qt::darkBlue ) );
230 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
232 Handle(HYDROData_Profile) aProfile =
233 Handle(HYDROData_Profile)::DownCast( myObject );
235 TopoDS_Wire aProfileWire;
237 if ( aProfile->IsValid() ) {
238 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
240 if ( !aProfileShape.IsNull() &&
241 aProfileShape.ShapeType() == TopAbs_WIRE ) {
242 aProfileWire = TopoDS::Wire( aProfileShape );
246 setWire( aProfileWire, false, false );
248 QColor aWireColor = aProfile->GetBorderColor();
249 setBorderColor( aWireColor, false, false );
251 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
252 myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
253 myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
255 Handle(HYDROData_Object) aGeomObject =
256 Handle(HYDROData_Object)::DownCast( myObject );
258 TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
260 setShape( anObjShape, false, false );
262 QColor aFillingColor = aGeomObject->GetFillingColor();
263 QColor aBorderColor = aGeomObject->GetBorderColor();
265 setFillingColor( aFillingColor, false, false );
266 setBorderColor( aBorderColor, false, false );
268 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
270 Handle(HYDROData_DummyObject3D) anObject3D =
271 Handle(HYDROData_DummyObject3D)::DownCast( myObject );
272 TopoDS_Shape aShape3D = anObject3D->GetShape();
274 setShape( aShape3D, false, false );
276 QColor aFillingColor = anObject3D->GetFillingColor();
277 QColor aBorderColor = anObject3D->GetBorderColor();
279 setFillingColor( aFillingColor, false, false );
280 setBorderColor( aBorderColor, false, false );
282 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
284 Handle(HYDROData_ShapesGroup) aShapesGroup =
285 Handle(HYDROData_ShapesGroup)::DownCast( myObject );
287 TopTools_SequenceOfShape aShapes;
288 aShapesGroup->GetShapes( aShapes );
290 TopoDS_Compound aCompound;
291 BRep_Builder aCompoundBuilder;
292 aCompoundBuilder.MakeCompound( aCompound );
294 for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
296 const TopoDS_Shape& aShape = aShapes.Value( i );
297 aCompoundBuilder.Add( aCompound, aShape );
300 setShape( aCompound, false, false );
304 if ( myShape.IsNull() || !isVisible() )
307 displayShape( isUpdateViewer );
309 if (isDeactivateSelection)
310 myContext->Deactivate(myShape);
313 void HYDROGUI_Shape::setVisible( const bool theState,
314 const bool theIsUpdateViewer )
316 myIsVisible = theState;
318 if ( myShape.IsNull() )
321 if ( ( myIsVisible && myContext->IsDisplayed( myShape ) ) ||
322 ( !myIsVisible && !myContext->IsDisplayed( myShape ) ) )
326 displayShape( theIsUpdateViewer );
329 myContext->Erase( myShape, theIsUpdateViewer );
332 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
334 if ( myIsHighlight == theIsHighlight )
337 myIsHighlight = theIsHighlight;
339 if ( myContext.IsNull() || myShape.IsNull() )
342 colorShapeBorder( getActiveColor() );
343 displayShape( isUpdateViewer );
346 bool HYDROGUI_Shape::isHighlighted() const
348 return myIsHighlight;
351 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
352 const bool theToDisplay,
353 const bool theIsUpdateViewer )
355 myTopoShape = theWire;
356 // To avoid that hilight presentation is equal to "normal" object presentation.
357 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
358 myDisplayMode = AIS_Shaded;
361 updateShape( theToDisplay, theIsUpdateViewer );
364 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
365 const bool theToDisplay,
366 const bool theIsUpdateViewer )
368 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
369 TopoDS_Compound aCompound;
370 BRep_Builder aBuilder;
371 aBuilder.MakeCompound( aCompound );
373 for ( ; anExp.More(); anExp.Next() ) {
374 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
375 if ( aWire.IsNull() ) {
379 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
381 if( aMakeFace.IsDone() ) {
382 aBuilder.Add( aCompound, aMakeFace.Face() );
386 myTopoShape = aCompound;
387 myDisplayMode = AIS_Shaded;
390 updateShape( theToDisplay, theIsUpdateViewer );
393 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
394 const bool theToDisplay,
395 const bool theIsUpdateViewer,
396 const QString& theTextureFileName )
398 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
399 aFaceBuilder.Build();
400 if( aFaceBuilder.IsDone() )
402 TopoDS_Face aFace = aFaceBuilder.Face();
403 setFace( aFace, theToDisplay, theIsUpdateViewer, theTextureFileName );
407 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
408 const bool theToDisplay,
409 const bool theIsUpdateViewer,
410 const QString& theTextureFileName )
412 myTopoShape = theFace;
413 myDisplayMode = theTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
414 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
415 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
418 updateShape( theToDisplay, theIsUpdateViewer );
421 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
422 const bool theToDisplay,
423 const bool theIsUpdateViewer )
425 myTopoShape = theShape;
426 myDisplayMode = AIS_Shaded;
429 updateShape( theToDisplay, theIsUpdateViewer );
432 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
433 const bool theToDisplay,
434 const bool theIsUpdateViewer )
436 myFillingColor = theColor;
437 updateShape( theToDisplay, theIsUpdateViewer );
440 QColor HYDROGUI_Shape::getFillingColor() const
442 return myFillingColor;
445 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
446 const bool theToDisplay,
447 const bool theIsUpdateViewer )
449 myBorderColor = theColor;
450 updateShape( theToDisplay, theIsUpdateViewer );
453 QColor HYDROGUI_Shape::getBorderColor() const
455 return myBorderColor;
458 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
460 myHighlightColor = theColor;
463 QColor HYDROGUI_Shape::getHighlightColor() const
465 return myHighlightColor;
468 void HYDROGUI_Shape::setZLayer( const int theZLayer )
470 if ( myZLayer == theZLayer )
473 myZLayer = theZLayer;
474 if ( !myShape.IsNull() && isVisible() && !myContext.IsNull() && myZLayer >= 0 )
475 myContext->SetZLayer( myShape, myZLayer );
478 Handle_AIS_InteractiveObject HYDROGUI_Shape::createShape() const
480 if( myTopoShape.IsNull() ) {
481 return Handle_AIS_InteractiveObject();
484 TopAbs_ShapeEnum aShapeType = myTopoShape.ShapeType();
485 bool IsWireEdgeCompound = aShapeType==TopAbs_COMPOUND;
486 if (IsWireEdgeCompound) {
487 TopoDS_Iterator itr(myTopoShape);
488 while (itr.More() && IsWireEdgeCompound) {
489 if (itr.Value().ShapeType() != TopAbs_WIRE && itr.Value().ShapeType() != TopAbs_EDGE)
490 IsWireEdgeCompound = false;
495 if ( aShapeType==TopAbs_EDGE || aShapeType==TopAbs_WIRE || IsWireEdgeCompound) {
496 return new HYDROGUI_Polyline( myTopoShape );
498 return new AIS_Shape( myTopoShape );
502 void HYDROGUI_Shape::buildShape()
504 // Erase previously created shape
507 myShape = createShape();
508 if( myShape.IsNull() )
511 Handle_AIS_Shape anAISShape = Handle_AIS_Shape::DownCast( myShape );
512 if( !anAISShape.IsNull() )
513 anAISShape ->SetHLRAngleAndDeviation( 0.001 );
515 if ( !myObject.IsNull() )
516 myShape->SetOwner( myObject );
518 myShape->SetTransparency( 0 );
519 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
521 // Init default params for shape
522 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
523 if ( !anAttributes.IsNull() )
525 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
526 if ( !anIsoAspect.IsNull() ) {
527 anIsoAspect->SetNumber( 0 );
528 anAttributes->SetUIsoAspect( anIsoAspect );
531 anIsoAspect = anAttributes->VIsoAspect();
532 if ( !anIsoAspect.IsNull() ) {
533 anIsoAspect->SetNumber( 0 );
534 anAttributes->SetVIsoAspect( anIsoAspect );
537 if ( myDisplayMode == AIS_Shaded )
539 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
540 if ( !aShadingAspect.IsNull() )
542 Graphic3d_MaterialAspect aMatAspect( Graphic3d_NOM_PLASTIC );
543 //aMatAspect.SetAmbient( 1 );
544 //aMatAspect.SetDiffuse( 0 );
546 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
547 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
550 else if ( myDisplayMode == AIS_WireFrame )
552 anAttributes->SetWireDraw( true );
557 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
558 const bool theIsUpdateViewer )
560 if ( myShape.IsNull() )
563 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
564 if ( !anAttributes.IsNull() )
566 if ( myDisplayMode == AIS_Shaded )
568 // Coloring face filling
569 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
570 if ( !aShadingAspect.IsNull() )
572 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
573 getQuantityColorVal( myFillingColor.green() ),
574 getQuantityColorVal( myFillingColor.blue() ),
577 aShadingAspect->SetColor( aFillingColor );
578 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
581 else if ( myDisplayMode == AIS_WireFrame )
586 colorShapeBorder( getActiveColor() );
589 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
592 displayShape( theIsUpdateViewer );
595 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
597 myContext->Display( myShape, Standard_False );
600 myContext->SetZLayer( myShape, myZLayer );
602 if ( theIsUpdateViewer ) {
603 myContext->UpdateCurrentViewer();
607 QColor HYDROGUI_Shape::getActiveColor() const
609 return isHighlighted() ? myHighlightColor : myBorderColor;
612 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
614 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
617 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
619 if ( myShape.IsNull() )
622 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
623 if ( anAttributes.IsNull() )
626 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
627 getQuantityColorVal( theColor.green() ),
628 getQuantityColorVal( theColor.blue() ),
631 if( !myTopoShape.IsNull() )
633 if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode
635 myShape->SetColor( aBorderColor );
637 else if ( myDisplayMode == AIS_Shaded )
639 if ( theColor.alpha() == 0 )
641 anAttributes->SetFaceBoundaryDraw( false );
645 anAttributes->SetFaceBoundaryDraw( true );
647 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
648 if ( !aBoundaryAspect.IsNull() )
650 aBoundaryAspect->SetColor( aBorderColor );
651 anAttributes->SetFaceBoundaryAspect( aBoundaryAspect );
653 Handle(Prs3d_LineAspect) aWireAspect = anAttributes->WireAspect();
654 if ( !aWireAspect.IsNull() )
656 aWireAspect->SetColor( aBorderColor );
657 anAttributes->SetWireAspect( aWireAspect );
661 else if ( myDisplayMode == AIS_WireFrame )
663 myShape->SetColor( aBorderColor );
668 void HYDROGUI_Shape::setDisplayMode( int theDisplayMode )
670 myDisplayMode = theDisplayMode;