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 <HYDROData_Channel.h>
22 #include <HYDROData_Document.h>
23 #include <HYDROData_DummyObject3D.h>
24 #include <HYDROData_ImmersibleZone.h>
25 #include <HYDROData_Obstacle.h>
26 #include <HYDROData_PolylineXY.h>
27 #include <HYDROData_Polyline3D.h>
28 #include <HYDROData_Profile.h>
29 #include <HYDROData_ShapesGroup.h>
30 #include <HYDROData_Stream.h>
31 #include <HYDROData_Zone.h>
33 #include <AIS_Shape.hxx>
34 #include <BRep_Builder.hxx>
35 #include <BRepBuilderAPI_MakeFace.hxx>
36 #include <Graphic3d_AspectFillArea3d.hxx>
37 #include <Prs3d_IsoAspect.hxx>
38 #include <Prs3d_ShadingAspect.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <TopoDS_Wire.hxx>
42 #include <TopExp_Explorer.hxx>
44 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
45 const Handle(HYDROData_Entity)& theObject,
47 : myContext( theContext ),
48 myObject( theObject ),
49 myZLayer( theZLayer ),
50 myIsHighlight( false ),
51 myFillingColor( Qt::transparent ),
52 myBorderColor( Qt::black ),
53 myHighlightColor( Qt::white ),
54 myIsToUpdate( false ),
56 myDisplayMode( AIS_Shaded )
60 HYDROGUI_Shape::~HYDROGUI_Shape()
64 if ( !myShape.IsNull() )
68 Handle(AIS_InteractiveContext) HYDROGUI_Shape::getContext() const
73 Handle(HYDROData_Entity) HYDROGUI_Shape::getObject() const
78 TopoDS_Shape HYDROGUI_Shape::getTopoShape() const
83 bool HYDROGUI_Shape::getIsToUpdate() const
88 void HYDROGUI_Shape::setIsToUpdate( bool theState )
90 myIsToUpdate = theState;
93 bool HYDROGUI_Shape::isVisible() const
98 Handle(AIS_InteractiveObject) HYDROGUI_Shape::getAISObject() const
103 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
105 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
108 displayShape( theIsUpdateViewer );
111 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
113 if ( myContext.IsNull() || myShape.IsNull() )
116 myContext->Erase( myShape, theIsUpdateViewer );
119 void HYDROGUI_Shape::update( bool isUpdateViewer,
120 bool isDeactivateSelection )
123 setIsToUpdate( false );
125 if ( myContext.IsNull() )
128 // Try to retrieve information from object
129 if ( !myObject.IsNull() )
131 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
133 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
135 Handle(HYDROData_ImmersibleZone) aZoneObj =
136 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
138 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
139 if ( !aZoneShape.IsNull() ) {
140 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
141 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
142 setFace( aZoneFace, false, false, "" );
144 myTopoShape = aZoneShape;
145 //TODO: myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
146 myDisplayMode = AIS_Shaded;
149 updateShape( false, false );
153 QColor aFillingColor = aZoneObj->GetFillingColor();
154 QColor aBorderColor = aZoneObj->GetBorderColor();
156 setFillingColor( aFillingColor, false, false );
157 setBorderColor( aBorderColor, false, false );
159 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
161 Handle(HYDROData_PolylineXY) aPolyline =
162 Handle(HYDROData_PolylineXY)::DownCast( myObject );
164 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
166 if ( !aPolylineShape.IsNull() ) {
167 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
168 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
169 setWire( aPolylineWire, false, false );
171 myTopoShape = aPolylineShape;
172 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
173 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
174 myDisplayMode = AIS_Shaded;
177 updateShape( false, false );
181 QColor aWireColor = aPolyline->GetWireColor();
182 setBorderColor( aWireColor, false, false );
184 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
186 Handle(HYDROData_Polyline3D) aPolyline =
187 Handle(HYDROData_Polyline3D)::DownCast( myObject );
189 TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
191 if ( !aPolylineShape.IsNull() ) {
192 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
193 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
194 setWire( aPolylineWire, false, false );
196 myTopoShape = aPolylineShape;
197 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
198 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
199 myDisplayMode = AIS_Shaded;
202 updateShape( false, false );
206 QColor aWireColor = aPolyline->GetBorderColor();
207 setBorderColor( aWireColor, false, false );
209 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
211 Handle(HYDROData_Zone) aZone =
212 Handle(HYDROData_Zone)::DownCast( myObject );
214 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
216 setFace( aZoneFace, false, false, "" );
217 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
219 // Red color for a zone with bathymetry conflict
220 setFillingColor( Qt::red );
224 // Generate the filling color for zone
225 QStringList aGeomObjectsNames;
227 HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
228 HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
229 for ( ; anIter.More(); anIter.Next() )
231 Handle(HYDROData_Object) aRefbject =
232 Handle(HYDROData_Object)::DownCast( anIter.Value() );
233 if ( aRefbject.IsNull() )
236 QString aRefObjectName = aRefbject->GetName();
237 if ( aRefObjectName.isEmpty() )
240 aGeomObjectsNames.append( aRefObjectName );
243 setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
246 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
248 Handle(HYDROData_Profile) aProfile =
249 Handle(HYDROData_Profile)::DownCast( myObject );
251 TopoDS_Wire aProfileWire;
253 if ( aProfile->IsValid() ) {
254 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
256 if ( !aProfileShape.IsNull() &&
257 aProfileShape.ShapeType() == TopAbs_WIRE ) {
258 aProfileWire = TopoDS::Wire( aProfileShape );
262 setWire( aProfileWire, false, false );
264 QColor aWireColor = aProfile->GetBorderColor();
265 setBorderColor( aWireColor, false, false );
267 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
268 myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
269 myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
271 Handle(HYDROData_Object) aGeomObject =
272 Handle(HYDROData_Object)::DownCast( myObject );
274 TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
276 setShape( anObjShape, false, false );
278 QColor aFillingColor = aGeomObject->GetFillingColor();
279 QColor aBorderColor = aGeomObject->GetBorderColor();
281 setFillingColor( aFillingColor, false, false );
282 setBorderColor( aBorderColor, false, false );
284 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
286 Handle(HYDROData_DummyObject3D) anObject3D =
287 Handle(HYDROData_DummyObject3D)::DownCast( myObject );
288 TopoDS_Shape aShape3D = anObject3D->GetShape();
290 setShape( aShape3D, false, false );
292 QColor aFillingColor = anObject3D->GetFillingColor();
293 QColor aBorderColor = anObject3D->GetBorderColor();
295 setFillingColor( aFillingColor, false, false );
296 setBorderColor( aBorderColor, false, false );
298 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
300 Handle(HYDROData_ShapesGroup) aShapesGroup =
301 Handle(HYDROData_ShapesGroup)::DownCast( myObject );
303 TopTools_SequenceOfShape aShapes;
304 aShapesGroup->GetShapes( aShapes );
306 TopoDS_Compound aCompound;
307 BRep_Builder aCompoundBuilder;
308 aCompoundBuilder.MakeCompound( aCompound );
310 for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
312 const TopoDS_Shape& aShape = aShapes.Value( i );
313 aCompoundBuilder.Add( aCompound, aShape );
316 setShape( aCompound, false, false );
320 if ( myShape.IsNull() || !isVisible() )
323 displayShape( isUpdateViewer );
325 if (isDeactivateSelection)
326 myContext->Deactivate(myShape);
329 void HYDROGUI_Shape::setVisible( const bool theState,
330 const bool theIsUpdateViewer )
332 myIsVisible = theState;
334 if ( myShape.IsNull() )
337 if ( ( myIsVisible && myContext->IsDisplayed( myShape ) ) ||
338 ( !myIsVisible && !myContext->IsDisplayed( myShape ) ) )
342 displayShape( theIsUpdateViewer );
345 myContext->Erase( myShape, theIsUpdateViewer );
348 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
350 if ( myIsHighlight == theIsHighlight )
353 myIsHighlight = theIsHighlight;
355 if ( myContext.IsNull() || myShape.IsNull() )
358 colorShapeBorder( getActiveColor() );
359 displayShape( isUpdateViewer );
362 bool HYDROGUI_Shape::isHighlighted() const
364 return myIsHighlight;
367 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
368 const bool theToDisplay,
369 const bool theIsUpdateViewer )
371 myTopoShape = theWire;
372 // To avoid that hilight presentation is equal to "normal" object presentation.
373 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
374 myDisplayMode = AIS_Shaded;
377 updateShape( theToDisplay, theIsUpdateViewer );
380 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
381 const bool theToDisplay,
382 const bool theIsUpdateViewer )
384 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
385 TopoDS_Compound aCompound;
386 BRep_Builder aBuilder;
387 aBuilder.MakeCompound( aCompound );
389 for ( ; anExp.More(); anExp.Next() ) {
390 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
391 if ( aWire.IsNull() ) {
395 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
397 if( aMakeFace.IsDone() ) {
398 aBuilder.Add( aCompound, aMakeFace.Face() );
402 myTopoShape = aCompound;
403 myDisplayMode = AIS_Shaded;
406 updateShape( theToDisplay, theIsUpdateViewer );
409 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
410 const bool theToDisplay,
411 const bool theIsUpdateViewer,
412 const QString& theTextureFileName )
414 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
415 aFaceBuilder.Build();
416 if( aFaceBuilder.IsDone() )
418 TopoDS_Face aFace = aFaceBuilder.Face();
419 setFace( aFace, theToDisplay, theIsUpdateViewer, theTextureFileName );
423 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
424 const bool theToDisplay,
425 const bool theIsUpdateViewer,
426 const QString& theTextureFileName )
428 myTopoShape = theFace;
429 myDisplayMode = theTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
430 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
431 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
434 updateShape( theToDisplay, theIsUpdateViewer );
437 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
438 const bool theToDisplay,
439 const bool theIsUpdateViewer )
441 myTopoShape = theShape;
442 myDisplayMode = AIS_Shaded;
445 updateShape( theToDisplay, theIsUpdateViewer );
448 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
449 const bool theToDisplay,
450 const bool theIsUpdateViewer )
452 myFillingColor = theColor;
453 updateShape( theToDisplay, theIsUpdateViewer );
456 QColor HYDROGUI_Shape::getFillingColor() const
458 return myFillingColor;
461 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
462 const bool theToDisplay,
463 const bool theIsUpdateViewer )
465 myBorderColor = theColor;
466 updateShape( theToDisplay, theIsUpdateViewer );
469 QColor HYDROGUI_Shape::getBorderColor() const
471 return myBorderColor;
474 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
476 myHighlightColor = theColor;
479 QColor HYDROGUI_Shape::getHighlightColor() const
481 return myHighlightColor;
484 void HYDROGUI_Shape::setZLayer( const int theZLayer )
486 if ( myZLayer == theZLayer )
489 myZLayer = theZLayer;
490 if ( !myShape.IsNull() && isVisible() && !myContext.IsNull() && myZLayer >= 0 )
491 myContext->SetZLayer( myShape, myZLayer );
494 Handle_AIS_InteractiveObject HYDROGUI_Shape::createShape() const
496 if( myTopoShape.IsNull() )
497 return Handle_AIS_InteractiveObject();
499 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 myContext->UpdateCurrentViewer();
605 QColor HYDROGUI_Shape::getActiveColor() const
607 return isHighlighted() ? myHighlightColor : myBorderColor;
610 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
612 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
615 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
617 if ( myShape.IsNull() )
620 const Handle(Prs3d_Drawer)& anAttributes = myShape->Attributes();
621 if ( anAttributes.IsNull() )
624 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
625 getQuantityColorVal( theColor.green() ),
626 getQuantityColorVal( theColor.blue() ),
629 if( !myTopoShape.IsNull() )
631 if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode
633 myShape->SetColor( aBorderColor );
635 else if ( myDisplayMode == AIS_Shaded )
637 if ( theColor.alpha() == 0 )
639 anAttributes->SetFaceBoundaryDraw( false );
643 anAttributes->SetFaceBoundaryDraw( true );
645 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
646 if ( !aBoundaryAspect.IsNull() )
648 aBoundaryAspect->SetColor( aBorderColor );
649 anAttributes->SetFaceBoundaryAspect( aBoundaryAspect );
651 Handle(Prs3d_LineAspect) aWireAspect = anAttributes->WireAspect();
652 if ( !aWireAspect.IsNull() )
654 aWireAspect->SetColor( aBorderColor );
655 anAttributes->SetWireAspect( aWireAspect );
659 else if ( myDisplayMode == AIS_WireFrame )
661 myShape->SetColor( aBorderColor );
666 void HYDROGUI_Shape::setDisplayMode( int theDisplayMode )
668 myDisplayMode = theDisplayMode;