Salome HOME
bos #19181: Result appears when creating a parameter in the sketch
[modules/shaper.git] / src / ModuleBase / ModuleBase_ResultPrs.cpp
index 11ab42dc67af2d85f29e2814faea8d6f9850fd64..9d3a5c616c0662116967b4c7d7057910804bef7e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2020  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 
 #include "ModuleBase_ResultPrs.h"
+#include "ModuleBase_IViewer.h"
 
 #include <GeomAPI_PlanarEdges.h>
+#include <GeomAPI_Edge.h>
 
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Tools.h>
@@ -32,6 +34,7 @@
 
 #include <Events_InfoMessage.h>
 #include <Events_Loop.h>
+#include <Config_PropManager.h>
 
 #include <AIS_ColoredDrawer.hxx>
 #include <AIS_InteractiveContext.hxx>
@@ -44,6 +47,7 @@
 #include <Prs3d_PointAspect.hxx>
 #include <Prs3d_IsoAspect.hxx>
 #include <Prs3d_ShadingAspect.hxx>
+#include <Prs3d_PlaneAspect.hxx>
 #include <SelectMgr_SequenceOfOwner.hxx>
 #include <SelectMgr_EntityOwner.hxx>
 #include <SelectMgr_SelectionManager.hxx>
@@ -54,6 +58,8 @@
 #include <TopExp_Explorer.hxx>
 #include <TopoDS.hxx>
 #include <TopoDS_Builder.hxx>
+#include <TopoDS_Edge.hxx>
+#include <BRepMesh_IncrementalMesh.hxx>
 
 //*******************************************************************************************
 
@@ -63,38 +69,95 @@ IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
 
 //********************************************************************
 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
-  : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
-  myTransparency(1)
+  : ViewerData_AISShape(TopoDS_Shape()),
+    myResult(theResult),
+    myIsSubstituted(false),
+    myTransparency(1),
+    myAdditionalSelectionPriority(0)
 {
-  std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
+
+  GeomShapePtr aShapePtr = ModelAPI_Tools::shape(theResult);
   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
+  // Workaround for Sketch subshapes which has no discrete representation
+  // until sketch faces are built and displayed.
+  // Thus, perform discretization of such edges.
+  if (theResult->groupName() == ModelAPI_ResultConstruction::group() &&
+    aShape.ShapeType() == TopAbs_EDGE) {
+    GeomEdgePtr anEdgePtr = GeomEdgePtr(new GeomAPI_Edge(aShapePtr));
+    if (anEdgePtr->isCircle() || anEdgePtr->isArc()) {
+      TopoDS_Edge anEdge = TopoDS::Edge(aShape);
+      TopLoc_Location aLoc;
+      Handle(Poly_Polygon3D) aPoly3D = BRep_Tool::Polygon3D(anEdge, aLoc);
+      if (aPoly3D.IsNull()) {
+        double aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
+        BRepMesh_IncrementalMesh(aShape, aDeflection);
+      }
+    }
+  }
   Set(aShape);
 
+  // Activate individual repaintng if this is a part of compsolid
+  ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
+  SetAutoHilight(aResOwner.get() == NULL);
+
   // Set own free boundaries aspect in order to have free
   // and unfree boundaries with different colors
   Handle(Prs3d_Drawer) aDrawer = Attributes();
-  Handle(Prs3d_LineAspect) aFreeBndAspect =
-    new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1);
-  aDrawer->SetFreeBoundaryAspect(aFreeBndAspect);
+  aDrawer->SetUnFreeBoundaryAspect(
+    new Prs3d_LineAspect(Quantity_NOC_YELLOW, Aspect_TOL_SOLID, 1));
+  aDrawer->SetFreeBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1));
+  aDrawer->SetFaceBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1));
+
+  Quantity_Color aColor;
+  Color(aColor);
+
+  std::vector<int> aIsoValues;
+  bool isIsoVisible;
+  ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
+  if (isIsoVisible) {
+    if (aIsoValues.size() == 0) {
+      aIsoValues.push_back(1);
+      aIsoValues.push_back(1);
+    }
+  }
+  else {
+    aIsoValues.push_back(0);
+    aIsoValues.push_back(0);
+  }
+  myUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[0]);
+  myVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[1]);
+  aDrawer->SetUIsoAspect(myUIsoAspect);
+  aDrawer->SetVIsoAspect(myVIsoAspect);
 
   if (aDrawer->HasOwnPointAspect())
     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
   else
     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
 
-  // Activate individual repaintng if this is a part of compsolid
-  ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
-  SetAutoHilight(aResOwner.get() == NULL);
+  aDrawer = DynamicHilightAttributes();
+  if (aDrawer.IsNull()) {
+    if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
+      aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
+      aDrawer->SetUIsoAspect(myUIsoAspect);
+      aDrawer->SetVIsoAspect(myVIsoAspect);
+      SetDynamicHilightAttributes(aDrawer);
+    }
+  }
 
-  myHiddenSubShapesDrawer = new AIS_ColoredDrawer (myDrawer);
+  myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
   Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
   aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
+  aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
   myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
 
   ModuleBase_Tools::setPointBallHighlighting(this);
 
   // Define colors for wireframe mode
   setEdgesDefaultColor();
+
+  ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), DynamicHilightAttributes());
+  ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
+  Attributes()->UpdatePreviousDeviationCoefficient();
 }
 
 //********************************************************************
@@ -109,76 +172,50 @@ void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
   ViewerData_AISShape::SetColor(theColor);
   myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
   setEdgesDefaultColor();
+  myUIsoAspect->SetColor(theColor);
+  myVIsoAspect->SetColor(theColor);
 }
 
 void ModuleBase_ResultPrs::setEdgesDefaultColor()
 {
-  AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
-  bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
+  if (myResult.get()) {
+    AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
+    bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
 
-  if (!aHasColor) {
     Handle(Prs3d_Drawer) aDrawer = Attributes();
-    Handle(Prs3d_LineAspect) anAspect; // = aDrawer->LineAspect();
-    //anAspect->SetColor(Quantity_NOC_YELLOW);
-    //aDrawer->SetLineAspect(anAspect);
-
-    // - unfree boundaries color
-    anAspect = aDrawer->UnFreeBoundaryAspect();
-    anAspect->SetColor(Quantity_NOC_YELLOW);
-    aDrawer->SetUnFreeBoundaryAspect(anAspect);
-    aDrawer->SetUnFreeBoundaryDraw(true);
-
-    // - free boundaries color
-    anAspect = aDrawer->FreeBoundaryAspect();
-    anAspect->SetColor(Quantity_NOC_GREEN);
-    aDrawer->SetFreeBoundaryAspect(anAspect);
-    aDrawer->SetFreeBoundaryDraw(true);
-
-    // - standalone edges color
-    anAspect = aDrawer->WireAspect();
-    anAspect->SetColor(Quantity_NOC_RED);
-    aDrawer->SetWireAspect(anAspect);
+    aDrawer->SetFaceBoundaryDraw(Standard_True);
+    aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
+
+    if (!aHasColor) {
+      aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
+      aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
+      aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
+
+      aDrawer->SetUnFreeBoundaryDraw(Standard_True);
+      aDrawer->SetFreeBoundaryDraw(Standard_True);
+      aDrawer->SetWireDraw(Standard_True);
+    }
   }
 }
 
 
 //********************************************************************
-bool ModuleBase_ResultPrs::setSubShapeHidden(const NCollection_List<TopoDS_Shape>& theShapes)
+void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& 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;
-    }
+
+  myHiddenSubShapes = theShapes;
+  collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
+  myVisibleCompound = aCompound;
+
+  aBBuilder.MakeCompound (aCompound);
+  TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
+  for (; aIt.More(); aIt.Next()) {
+    aBBuilder.Add(aCompound, aIt.Value());
   }
   myHiddenCompound = aCompound;
-  return isModified;
 }
 
 //********************************************************************
@@ -188,9 +225,8 @@ bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
     return false;
 
   // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
-  for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(myHiddenSubShapes); aShapeIt.More();
-    aShapeIt.Next())
-  {
+  TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
+  for (; aShapeIt.More(); aShapeIt.Next()) {
     if (theShape.IsSame(aShapeIt.Value()))
       return true;
   }
@@ -200,14 +236,15 @@ bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
 
 //********************************************************************
 bool ModuleBase_ResultPrs::hasSubShapeVisible(
-  const NCollection_List<TopoDS_Shape>& theShapesToSkip)
+  const TopoDS_ListOfShape& theShapesToSkip)
 {
   TopoDS_Compound aCompound;
   BRep_Builder aBuilder;
   aBuilder.MakeCompound (aCompound);
-  NCollection_List<TopoDS_Shape> aShapesToSkip;
-  aShapesToSkip.Append(myHiddenSubShapes);
-  for (NCollection_List<TopoDS_Shape>::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
+  TopoDS_ListOfShape aShapesToSkip;
+  TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
+  aShapesToSkip.Append(aHiddenCopy);
+  for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
     aShapesToSkip.Append(anIt.Value());
 
   collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
@@ -231,27 +268,25 @@ void ModuleBase_ResultPrs::Compute(
           const Handle(Prs3d_Presentation)& thePresentation,
           const Standard_Integer theMode)
 {
-  std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
+  std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
   bool aReadyToDisplay = aShapePtr.get();
   if (aReadyToDisplay) {
     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
     if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
-      if (!myOriginalShape.IsNull())
+      if (!myOriginalShape.IsNull()) {
         Set(myOriginalShape);
+        myIsSubstituted = false;
+      }
     }
     else { // convert shape into SHELL
-      TopoDS_Compound aCompound;
-      BRep_Builder aBuilder;
-      aBuilder.MakeCompound (aCompound);
-      collectSubShapes(aBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
-      bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
-      Set(aCompound);
+      bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
+      Set(myVisibleCompound);
+      myIsSubstituted = true;
       if (isEmptyShape)
         aReadyToDisplay = false;
     }
   }
   // change deviation coefficient to provide more precise circle
-  //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
   try {
     AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
   }
@@ -260,24 +295,26 @@ void ModuleBase_ResultPrs::Compute(
   }
 
   // visualize hidden sub-shapes transparent
-  if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
-  {
-    StdPrs_ShadedShape::Add (thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
-    aReadyToDisplay = true;
-  }
+  if (myResult.get()) {
+    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();
-    static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
-    ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
+    if (!aReadyToDisplay) {
+      Events_InfoMessage("ModuleBase_ResultPrs",
+        "An empty AIS presentation: ModuleBase_ResultPrs").send();
+      static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
+      ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
+    }
   }
 }
 
 //********************************************************************
 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
   TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
-  const NCollection_List<TopoDS_Shape>& theHiddenSubShapes)
+  const TopoDS_ListOfShape& theHiddenSubShapes)
 {
   switch (theShape.ShapeType()) {
     case TopAbs_COMPSOLID:
@@ -377,14 +414,14 @@ void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& a
       Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
       Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
       if (!aBasicsOwner.IsNull())
-        aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
+        aBasicsOwner->SetPriority(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
     }
   }
 }
 
 //********************************************************************
 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
-                                                 const Standard_Integer theMode)
+                                                 const Standard_Integer /*theMode*/)
 {
   if (Shape().ShapeType() == TopAbs_VERTEX) {
     const TopoDS_Shape& aShape = Shape();
@@ -405,10 +442,8 @@ bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selectio
         anIt.More();
         anIt.Next()) {
       Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
-      Handle(SelectMgr_EntityOwner) anOwner =
-        Handle(SelectMgr_EntityOwner)
-        ::DownCast(anEntity->BaseSensitive()->OwnerId());
-      anOwner->Set(this);
+      Handle(SelectMgr_EntityOwner) anOwner = anEntity->BaseSensitive()->OwnerId();
+      anOwner->SetSelectable(this);
     }
 
     return true;
@@ -466,3 +501,30 @@ void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_Presentatio
       thePM->AddToImmediateList(aHilightPrs);
   }
 }
+
+
+//********************************************************************
+void ModuleBase_ResultPrs::updateIsoLines()
+{
+  std::vector<int> aIsoValues;
+  bool isIsoVisible;
+  ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
+  if (isIsoVisible) {
+    if (aIsoValues.size() == 0) {
+      aIsoValues.push_back(1);
+      aIsoValues.push_back(1);
+    }
+  }
+  else {
+    if (aIsoValues.size() == 0) {
+      aIsoValues.push_back(0);
+      aIsoValues.push_back(0);
+    }
+    else {
+      aIsoValues[0] = 0;
+      aIsoValues[1] = 0;
+    }
+  }
+  myUIsoAspect->SetNumber(aIsoValues[0]);
+  myVIsoAspect->SetNumber(aIsoValues[1]);
+}