Salome HOME
Issue #2309 Possibility to hide faces : display hidden objects by panel if it uses...
[modules/shaper.git] / src / ModuleBase / ModuleBase_ResultPrs.cpp
index 907fd6a6098ffb50752defe12d7ff04ddbc2d987..ad9ca2febdeae5b830ca78d4ee4c119f4215015a 100755 (executable)
 //
 
 #include "ModuleBase_ResultPrs.h"
-#include "ModuleBase_Tools.h"
+
+#include <GeomAPI_PlanarEdges.h>
 
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Tools.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_ResultCompSolid.h>
-#include <GeomAPI_PlanarEdges.h>
+
+#include "ModuleBase_Tools.h"
+#include "ModuleBase_BRepOwner.h"
 
 #include <Events_InfoMessage.h>
 #include <Events_Loop.h>
 
+#include <AIS_ColoredDrawer.hxx>
+#include <AIS_InteractiveContext.hxx>
+#include <AIS_Selection.hxx>
 #include <BRep_Builder.hxx>
+#include <Graphic3d_AspectMarker3d.hxx>
 #include <Prs3d_Drawer.hxx>
 #include <Prs3d.hxx>
 #include <Prs3d_PointAspect.hxx>
 #include <Prs3d_IsoAspect.hxx>
-#include <TopoDS_Builder.hxx>
-#include <TopoDS.hxx>
+#include <Prs3d_ShadingAspect.hxx>
 #include <SelectMgr_SequenceOfOwner.hxx>
 #include <SelectMgr_EntityOwner.hxx>
 #include <SelectMgr_SelectionManager.hxx>
 #include <StdPrs_WFShape.hxx>
+#include <StdPrs_ShadedShape.hxx>
 #include <StdSelect_BRepSelectionTool.hxx>
-#include <AIS_InteractiveContext.hxx>
-#include <AIS_Selection.hxx>
 #include <TColStd_ListIteratorOfListOfInteger.hxx>
-#include <Graphic3d_AspectMarker3d.hxx>
 #include <TopExp_Explorer.hxx>
-
-IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_BRepOwner, StdSelect_BRepOwner);
+#include <TopoDS.hxx>
+#include <TopoDS_Builder.hxx>
 
 //*******************************************************************************************
 
@@ -56,8 +60,10 @@ IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
 
 
 
+//********************************************************************
 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
-  : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0)
+  : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
+  myTransparency(1)
 {
   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
@@ -72,14 +78,98 @@ ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
   ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
   SetAutoHilight(aCompSolid.get() == NULL);
 
+  myHiddenSubShapesDrawer = new AIS_ColoredDrawer (myDrawer);
+  Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
+  aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
+  myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
+
   ModuleBase_Tools::setPointBallHighlighting(this);
 }
 
+//********************************************************************
 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
 {
   myAdditionalSelectionPriority = thePriority;
 }
 
+//********************************************************************
+void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
+{
+  ViewerData_AISShape::SetColor(theColor);
+  myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
+}
+
+//********************************************************************
+bool ModuleBase_ResultPrs::setSubShapeHidden(const NCollection_List<TopoDS_Shape>& theShapes)
+{
+  bool isModified = false;
+
+  TopoDS_Compound aCompound;
+  BRep_Builder aBBuilder;
+  aBBuilder.MakeCompound (aCompound);
+  // restore hidden shapes if there are not the shapes in parameter container
+  NCollection_List<TopoDS_Shape> aVisibleSubShapes;
+  for (NCollection_List<TopoDS_Shape>::Iterator aHiddenIt(myHiddenSubShapes); aHiddenIt.More();
+       aHiddenIt.Next()) {
+    if (!theShapes.Contains(aHiddenIt.Value()))
+      aVisibleSubShapes.Append(aHiddenIt.Value());
+    else
+      aBBuilder.Add (aCompound, aHiddenIt.Value());
+  }
+  isModified = !aVisibleSubShapes.IsEmpty();
+  for (NCollection_List<TopoDS_Shape>::Iterator aVisibleIt(aVisibleSubShapes); aVisibleIt.More();
+       aVisibleIt.Next())
+    myHiddenSubShapes.Remove(aVisibleIt.Value());
+
+  // append hidden shapes into internal container if there are not these shapes
+  for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(theShapes); aShapeIt.More();
+    aShapeIt.Next())
+  {
+    if (aShapeIt.Value().ShapeType() != TopAbs_FACE) // only face shape can be hidden
+      continue;
+
+    if (!myHiddenSubShapes.Contains(aShapeIt.Value())) {
+      myHiddenSubShapes.Append(aShapeIt.Value());
+      aBBuilder.Add (aCompound, aShapeIt.Value());
+      isModified = true;
+    }
+  }
+  myHiddenCompound = aCompound;
+  return isModified;
+}
+
+//********************************************************************
+bool ModuleBase_ResultPrs::hasSubShapeVisible(const TopoDS_Shape& theShape)
+{
+  int aNbOfHiddenSubShapes = myHiddenSubShapes.Size();
+
+  if (!myHiddenSubShapes.Contains(theShape))
+    aNbOfHiddenSubShapes++; // the shape to be hidden later
+
+  TopExp_Explorer anExp(myOriginalShape, TopAbs_FACE);
+  bool aHasVisibleShape = false;
+  for(TopExp_Explorer anExp(myOriginalShape, TopAbs_FACE); anExp.More() && !aHasVisibleShape;
+      anExp.Next())
+  {
+    aNbOfHiddenSubShapes--;
+    if (aNbOfHiddenSubShapes < 0)
+      aHasVisibleShape = true;
+  }
+  return aHasVisibleShape;
+}
+
+//********************************************************************
+bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
+{
+  if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
+    return false;
+
+  myTransparency = theTransparency;
+  myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
+  return true;
+}
+
+//********************************************************************
 void ModuleBase_ResultPrs::Compute(
           const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
           const Handle(Prs3d_Presentation)& thePresentation,
@@ -89,13 +179,37 @@ void ModuleBase_ResultPrs::Compute(
   bool aReadyToDisplay = aShapePtr.get();
   if (aReadyToDisplay) {
     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
-    if (!myOriginalShape.IsNull())
-      Set(myOriginalShape);
+    if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
+      if (!myOriginalShape.IsNull())
+        Set(myOriginalShape);
+    }
+    else { // convert shape into SHELL
+      TopoDS_Shell aShell;
+      BRep_Builder aShellBuilder;
+      aShellBuilder.MakeShell(aShell);
+      bool isEmptyShape = true;
+      for(TopExp_Explorer anExp(myOriginalShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
+        if (myHiddenSubShapes.Contains(anExp.Current()))
+          continue;
+        aShellBuilder.Add(aShell, anExp.Current());
+        isEmptyShape = false;
+      }
+      Set(aShell);
+      if (isEmptyShape)
+        aReadyToDisplay = false;
+    }
   }
   // change deviation coefficient to provide more precise circle
   //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
   AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
 
+  // visualize hidden sub-shapes transparent
+  if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
+  {
+    StdPrs_ShadedShape::Add (thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
+    aReadyToDisplay = true;
+  }
+
   if (!aReadyToDisplay) {
     Events_InfoMessage("ModuleBase_ResultPrs",
                        "An empty AIS presentation: ModuleBase_ResultPrs").send();
@@ -104,6 +218,7 @@ void ModuleBase_ResultPrs::Compute(
   }
 }
 
+//********************************************************************
 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
                                             const Standard_Integer theMode)
 {
@@ -169,6 +284,7 @@ void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& a
   }
 }
 
+//********************************************************************
 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
                                                  const Standard_Integer theMode)
 {
@@ -196,6 +312,7 @@ bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selectio
   return false;
 }
 
+//********************************************************************
 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
                                            const SelectMgr_SequenceOfOwner& theOwners)
 {
@@ -222,6 +339,7 @@ void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManag
   }
 }
 
+//********************************************************************
 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
                                                  const Handle(Graphic3d_HighlightStyle)& theStyle,
                                                  const Handle(SelectMgr_EntityOwner)& theOwner)