1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <HYDROGUI_Shape.h>
24 #include <HYDROGUI_Tool.h>
25 #include <HYDROData_Channel.h>
26 #include <HYDROData_Document.h>
27 #include <HYDROData_DummyObject3D.h>
28 #include <HYDROData_ImmersibleZone.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 <HYDROGUI_PrsShape.h>
38 #include <AIS_Shape.hxx>
39 #include <BRep_Builder.hxx>
40 #include <BRepBuilderAPI_MakeFace.hxx>
41 #include <Graphic3d_AspectFillArea3d.hxx>
42 #include <Prs3d_IsoAspect.hxx>
43 #include <Prs3d_ShadingAspect.hxx>
45 #include <TopoDS_Face.hxx>
46 #include <TopoDS_Wire.hxx>
47 #include <TopExp_Explorer.hxx>
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 )
65 HYDROGUI_Shape::~HYDROGUI_Shape()
69 if ( !myShape.IsNull() )
73 Handle(AIS_InteractiveContext) HYDROGUI_Shape::getContext() const
78 Handle(HYDROData_Entity) HYDROGUI_Shape::getObject() const
83 TopoDS_Shape HYDROGUI_Shape::getTopoShape() const
88 bool HYDROGUI_Shape::getIsToUpdate() const
93 void HYDROGUI_Shape::setIsToUpdate( bool theState )
95 myIsToUpdate = theState;
98 bool HYDROGUI_Shape::isVisible() const
103 Handle(AIS_InteractiveObject) HYDROGUI_Shape::getAISObject() const
108 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
110 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
113 displayShape( theIsUpdateViewer );
116 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
118 if ( myContext.IsNull() || myShape.IsNull() )
121 myContext->Erase( myShape, theIsUpdateViewer );
124 void HYDROGUI_Shape::update( bool isUpdateViewer,
125 bool isDeactivateSelection )
128 setIsToUpdate( false );
130 if ( myContext.IsNull() )
133 // Try to retrieve information from object
134 if ( !myObject.IsNull() )
136 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
138 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
140 Handle(HYDROData_ImmersibleZone) aZoneObj =
141 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
143 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
144 if ( !aZoneShape.IsNull() ) {
145 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
146 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
147 setFace( aZoneFace, false, false, "" );
149 myTopoShape = aZoneShape;
150 //TODO: myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
151 myDisplayMode = AIS_Shaded;
154 updateShape( false, false );
158 QColor aFillingColor = aZoneObj->GetFillingColor();
159 QColor aBorderColor = aZoneObj->GetBorderColor();
161 setFillingColor( aFillingColor, false, false );
162 setBorderColor( aBorderColor, false, false );
164 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
166 Handle(HYDROData_PolylineXY) aPolyline =
167 Handle(HYDROData_PolylineXY)::DownCast( myObject );
169 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
171 if ( !aPolylineShape.IsNull() ) {
172 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
173 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
174 setWire( aPolylineWire, false, false );
176 myTopoShape = aPolylineShape;
177 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
178 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
179 myDisplayMode = AIS_Shaded;
182 updateShape( false, false );
186 QColor aWireColor = aPolyline->GetWireColor();
187 setBorderColor( aWireColor, false, false );
189 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
191 Handle(HYDROData_Polyline3D) aPolyline =
192 Handle(HYDROData_Polyline3D)::DownCast( myObject );
194 TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
196 if ( !aPolylineShape.IsNull() ) {
197 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
198 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
199 setWire( aPolylineWire, false, false );
201 myTopoShape = aPolylineShape;
202 // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
203 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
204 myDisplayMode = AIS_Shaded;
207 updateShape( false, false );
211 QColor aWireColor = aPolyline->GetBorderColor();
212 setBorderColor( aWireColor, false, false );
214 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
216 Handle(HYDROData_Zone) aZone =
217 Handle(HYDROData_Zone)::DownCast( myObject );
219 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
221 setFace( aZoneFace, false, false, "" );
222 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
224 // Red color for a zone with bathymetry conflict
225 setFillingColor( Qt::red );
229 // Set the filling color for zone
230 setFillingColor( aZone->GetColor(HYDROData_ImmersibleZone::DefaultFillingColor()) );
233 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
235 Handle(HYDROData_Profile) aProfile =
236 Handle(HYDROData_Profile)::DownCast( myObject );
238 TopoDS_Wire aProfileWire;
240 if ( aProfile->IsValid() ) {
241 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
243 if ( !aProfileShape.IsNull() &&
244 aProfileShape.ShapeType() == TopAbs_WIRE ) {
245 aProfileWire = TopoDS::Wire( aProfileShape );
249 setWire( aProfileWire, false, false );
251 QColor aWireColor = aProfile->GetBorderColor();
252 setBorderColor( aWireColor, false, false );
254 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
255 myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
256 myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
258 Handle(HYDROData_Object) aGeomObject =
259 Handle(HYDROData_Object)::DownCast( myObject );
261 TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
263 setShape( anObjShape, false, false );
265 QColor aFillingColor = aGeomObject->GetFillingColor();
266 QColor aBorderColor = aGeomObject->GetBorderColor();
268 setFillingColor( aFillingColor, false, false );
269 setBorderColor( aBorderColor, false, false );
271 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
273 Handle(HYDROData_DummyObject3D) anObject3D =
274 Handle(HYDROData_DummyObject3D)::DownCast( myObject );
275 TopoDS_Shape aShape3D = anObject3D->GetShape();
277 setShape( aShape3D, false, false );
279 QColor aFillingColor = anObject3D->GetFillingColor();
280 QColor aBorderColor = anObject3D->GetBorderColor();
282 setFillingColor( aFillingColor, false, false );
283 setBorderColor( aBorderColor, false, false );
285 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
287 Handle(HYDROData_ShapesGroup) aShapesGroup =
288 Handle(HYDROData_ShapesGroup)::DownCast( myObject );
290 TopTools_SequenceOfShape aShapes;
291 aShapesGroup->GetShapes( aShapes );
293 TopoDS_Compound aCompound;
294 BRep_Builder aCompoundBuilder;
295 aCompoundBuilder.MakeCompound( aCompound );
297 for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
299 const TopoDS_Shape& aShape = aShapes.Value( i );
300 aCompoundBuilder.Add( aCompound, aShape );
303 setShape( aCompound, false, false );
307 if ( myShape.IsNull() || !isVisible() )
310 displayShape( isUpdateViewer );
312 if (isDeactivateSelection)
313 myContext->Deactivate(myShape);
316 void HYDROGUI_Shape::setVisible( const bool theState,
317 const bool theIsUpdateViewer )
319 myIsVisible = theState;
321 if ( myShape.IsNull() )
324 if ( ( myIsVisible && myContext->IsDisplayed( myShape ) ) ||
325 ( !myIsVisible && !myContext->IsDisplayed( myShape ) ) )
329 displayShape( theIsUpdateViewer );
332 myContext->Erase( myShape, theIsUpdateViewer );
335 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
337 if ( myIsHighlight == theIsHighlight )
340 myIsHighlight = theIsHighlight;
342 if ( myContext.IsNull() || myShape.IsNull() )
345 colorShapeBorder( getActiveColor() );
346 displayShape( isUpdateViewer );
349 bool HYDROGUI_Shape::isHighlighted() const
351 return myIsHighlight;
354 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
355 const bool theToDisplay,
356 const bool theIsUpdateViewer )
358 myTopoShape = theWire;
359 // To avoid that hilight presentation is equal to "normal" object presentation.
360 // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
361 myDisplayMode = AIS_Shaded;
364 updateShape( theToDisplay, theIsUpdateViewer );
367 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
368 const bool theToDisplay,
369 const bool theIsUpdateViewer )
371 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
372 TopoDS_Compound aCompound;
373 BRep_Builder aBuilder;
374 aBuilder.MakeCompound( aCompound );
376 for ( ; anExp.More(); anExp.Next() ) {
377 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
378 if ( aWire.IsNull() ) {
382 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
384 if( aMakeFace.IsDone() ) {
385 aBuilder.Add( aCompound, aMakeFace.Face() );
389 myTopoShape = aCompound;
390 myDisplayMode = AIS_Shaded;
393 updateShape( theToDisplay, theIsUpdateViewer );
396 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
397 const bool theToDisplay,
398 const bool theIsUpdateViewer,
399 const QString& theTextureFileName )
401 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
402 aFaceBuilder.Build();
403 if( aFaceBuilder.IsDone() )
405 TopoDS_Face aFace = aFaceBuilder.Face();
406 setFace( aFace, theToDisplay, theIsUpdateViewer, theTextureFileName );
410 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
411 const bool theToDisplay,
412 const bool theIsUpdateViewer,
413 const QString& theTextureFileName )
415 myTopoShape = theFace;
416 myDisplayMode = theTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
417 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
418 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
421 updateShape( theToDisplay, theIsUpdateViewer );
424 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
425 const bool theToDisplay,
426 const bool theIsUpdateViewer )
428 myTopoShape = theShape;
429 myDisplayMode = AIS_Shaded;
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 ( !myShape.IsNull() && isVisible() && !myContext.IsNull() && myZLayer >= 0 )
478 myContext->SetZLayer( myShape, myZLayer );
481 Handle_AIS_InteractiveObject HYDROGUI_Shape::createShape() const
483 if( myTopoShape.IsNull() )
484 return Handle_AIS_InteractiveObject();
486 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) ||
487 myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D)) ||
488 myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ))
489 return new HYDROGUI_PrsShape(myTopoShape);
491 return new AIS_Shape( myTopoShape );
494 void HYDROGUI_Shape::buildShape()
496 // Erase previously created shape
499 myShape = createShape();
500 if( myShape.IsNull() )
503 Handle_AIS_Shape anAISShape = Handle_AIS_Shape::DownCast( myShape );
504 if( !anAISShape.IsNull() )
505 anAISShape ->SetHLRAngleAndDeviation( 0.001 );
507 if ( !myObject.IsNull() )
508 myShape->SetOwner( myObject );
510 myShape->SetTransparency( 0 );
511 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
513 // Init default params for shape
514 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
515 if ( !anAttributes.IsNull() )
517 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
518 if ( !anIsoAspect.IsNull() ) {
519 anIsoAspect->SetNumber( 0 );
520 anAttributes->SetUIsoAspect( anIsoAspect );
523 anIsoAspect = anAttributes->VIsoAspect();
524 if ( !anIsoAspect.IsNull() ) {
525 anIsoAspect->SetNumber( 0 );
526 anAttributes->SetVIsoAspect( anIsoAspect );
529 if ( myDisplayMode == AIS_Shaded )
531 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
532 if ( !aShadingAspect.IsNull() )
534 Graphic3d_MaterialAspect aMatAspect( Graphic3d_NOM_PLASTIC );
535 //aMatAspect.SetAmbient( 1 );
536 //aMatAspect.SetDiffuse( 0 );
538 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
539 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
542 else if ( myDisplayMode == AIS_WireFrame )
544 anAttributes->SetWireDraw( true );
549 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
550 const bool theIsUpdateViewer )
552 if ( myShape.IsNull() )
555 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
556 if ( !anAttributes.IsNull() )
558 if ( myDisplayMode == AIS_Shaded )
560 // Coloring face filling
561 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
562 if ( !aShadingAspect.IsNull() )
564 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
565 getQuantityColorVal( myFillingColor.green() ),
566 getQuantityColorVal( myFillingColor.blue() ),
569 aShadingAspect->SetColor( aFillingColor );
570 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
573 else if ( myDisplayMode == AIS_WireFrame )
578 colorShapeBorder( getActiveColor() );
581 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
584 displayShape( theIsUpdateViewer );
587 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
589 myContext->Display( myShape, Standard_False );
592 myContext->SetZLayer( myShape, myZLayer );
594 myContext->UpdateCurrentViewer();
597 QColor HYDROGUI_Shape::getActiveColor() const
599 return isHighlighted() ? myHighlightColor : myBorderColor;
602 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
604 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
607 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
609 if ( myShape.IsNull() )
612 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
613 if ( anAttributes.IsNull() )
616 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
617 getQuantityColorVal( theColor.green() ),
618 getQuantityColorVal( theColor.blue() ),
621 if( !myTopoShape.IsNull() )
623 if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode
625 myShape->SetColor( aBorderColor );
627 else if ( myDisplayMode == AIS_Shaded )
629 if ( theColor.alpha() == 0 )
631 anAttributes->SetFaceBoundaryDraw( false );
635 anAttributes->SetFaceBoundaryDraw( true );
637 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
638 if ( !aBoundaryAspect.IsNull() )
640 aBoundaryAspect->SetColor( aBorderColor );
641 anAttributes->SetFaceBoundaryAspect( aBoundaryAspect );
643 Handle(Prs3d_LineAspect) aWireAspect = anAttributes->WireAspect();
644 if ( !aWireAspect.IsNull() )
646 aWireAspect->SetColor( aBorderColor );
647 anAttributes->SetWireAspect( aWireAspect );
651 else if ( myDisplayMode == AIS_WireFrame )
653 myShape->SetColor( aBorderColor );
658 void HYDROGUI_Shape::setDisplayMode( int theDisplayMode )
660 myDisplayMode = theDisplayMode;