Salome HOME
bos #29479 Show edges directions
[modules/shaper.git] / src / ModuleBase / ModuleBase_ResultPrs.cpp
index e58f460dae4da316eb218daa967bad8a790ca0fc..ccba3a2071f008092dd64616e85add91ddb700c8 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2022  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>
 #include <TopExp_Explorer.hxx>
 #include <TopoDS.hxx>
 #include <TopoDS_Builder.hxx>
+#include <TopoDS_Edge.hxx>
+#include <BRepMesh_IncrementalMesh.hxx>
+#include <Standard_Version.hxx>
+#include <Prs3d_Arrow.hxx>
+#include <GeomAdaptor_Curve.hxx>
+#include <TopExp.hxx>
+#include <GCPnts_AbscissaPoint.hxx>
+
+#if OCC_VERSION_HEX > 0x070400
+#include <StdPrs_ToolTriangulatedShape.hxx>
+#endif
 
 //*******************************************************************************************
 
@@ -63,41 +78,97 @@ IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
 
 //********************************************************************
 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
-  : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
-  myTransparency(1), myIsSubstituted(false)
+  : 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);
+  // VSV: bos22744: The AutoHilight mode is swithced off because it produces different
+  // behaviour of selection for simple shape and compound. For example when selection mode
+  // is Vertex the shape is selected by vertex, but compound is selected by whole shape
+  //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->VIsoAspect()->SetNumber(0);
-  aDrawer->UIsoAspect()->SetNumber(0);
+  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.));
 
+  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);
   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();
 }
 
 //********************************************************************
@@ -112,6 +183,8 @@ 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()
@@ -120,28 +193,18 @@ void ModuleBase_ResultPrs::setEdgesDefaultColor()
     AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
     bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
 
+    Handle(Prs3d_Drawer) aDrawer = Attributes();
+    aDrawer->SetFaceBoundaryDraw(Standard_True);
+    aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
+
     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->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);
     }
   }
 }
@@ -150,8 +213,6 @@ void ModuleBase_ResultPrs::setEdgesDefaultColor()
 //********************************************************************
 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
 {
-  bool isModified = false;
-
   TopoDS_Compound aCompound;
   BRep_Builder aBBuilder;
   aBBuilder.MakeCompound (aCompound);
@@ -236,12 +297,56 @@ void ModuleBase_ResultPrs::Compute(
         aReadyToDisplay = false;
     }
   }
+  // change deviation coefficient to provide more precise circle
   try {
     AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
   }
   catch (...) {
     return;
   }
+  if (myResult.get() && ModelAPI_Tools::isShowEdgesDirection(myResult))
+  {
+    TopExp_Explorer Exp(myshape, TopAbs_EDGE);
+    for (; Exp.More(); Exp.Next()) {
+      TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
+      if (anEdgeE.IsNull())
+        continue;
+
+      // draw curve direction (issue 0021087)
+      anEdgeE.Orientation(TopAbs_FORWARD);
+
+      TopoDS_Vertex aV1, aV2;
+      TopExp::Vertices(anEdgeE, aV1, aV2);
+      gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
+      gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
+
+      double fp, lp;
+      gp_Vec aDirVec;
+      Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE, fp, lp);
+
+      if (C.IsNull()) continue;
+
+      if (anEdgeE.Orientation() == TopAbs_FORWARD)
+        C->D1(lp, aP2, aDirVec);
+      else {
+        C->D1(fp, aP1, aDirVec);
+        aP2 = aP1;
+      }
+      GeomAdaptor_Curve aAdC;
+      aAdC.Load(C, fp, lp);
+      Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
+
+      if (aDist > gp::Resolution()) {
+        gp_Dir aDir;
+        if (anEdgeE.Orientation() == TopAbs_FORWARD)
+          aDir = aDirVec;
+        else
+          aDir = -aDirVec;
+
+        Prs3d_Arrow::Draw(thePresentation->CurrentGroup(), aP2, aDir, M_PI / 180.*5., aDist / 10.);
+      }
+    }
+  }
 
   // visualize hidden sub-shapes transparent
   if (myResult.get()) {
@@ -363,27 +468,30 @@ 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();
 
     int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
+#if OCC_VERSION_HEX > 0x070400
+    double aDeflection = StdPrs_ToolTriangulatedShape::GetDeflection(aShape, myDrawer);
+#else
     double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
-
+#endif
     /// The cause of this method is the last parameter of BRep owner setting into True.
     /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
     /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
     Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
     StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
-                                                  aDeflection, myDrawer->HLRAngle(), 9, 500);
+                                                  aDeflection, myDrawer->DeviationAngle(), 9, 500);
 
 
     NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
@@ -391,10 +499,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;
@@ -452,3 +558,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]);
+}