Salome HOME
Issue #1664: In the Sketcher, add the function Split a segment. Correction of circle...
[modules/shaper.git] / src / PartSet / PartSet_ResultSketchPrs.cpp
index ede7d144d93d8789e347d74cfc40f2f374ed4881..ccf60f8ae03f377030bd71253211166e982b2e4e 100755 (executable)
@@ -8,14 +8,18 @@
 #include "PartSet_Tools.h"
 #include "ModuleBase_Tools.h"
 
+#include <ModelAPI_Events.h>
 #include <ModelAPI_Tools.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <GeomAPI_PlanarEdges.h>
 
-#include <Events_Error.h>
+#include <Events_InfoMessage.h>
+#include <Events_Loop.h>
 
 #include <SketchPlugin_SketchEntity.h>
 
+#include <SketcherPrs_Tools.h>
+
 #include <Config_PropManager.h>
 
 #include <BRep_Builder.hxx>
@@ -28,6 +32,7 @@
 #include <StdSelect_BRepSelectionTool.hxx>
 #include <Graphic3d_AspectLine3d.hxx>
 #include <Prs3d_LineAspect.hxx>
+#include <Graphic3d_AspectMarker3d.hxx>
 
 #define DEBUG_WIRE
 
@@ -57,112 +62,49 @@ PartSet_ResultSketchPrs::PartSet_ResultSketchPrs(ResultPtr theResult)
   // Activate individual repaintng if this is a part of compsolid
   ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
   SetAutoHilight(aCompSolid.get() == NULL);
-}
 
-bool PartSet_ResultSketchPrs::isValidShapeType(const TopAbs_ShapeEnum& theBaseType,
-                                            const TopAbs_ShapeEnum& theCheckedType)
-{
-  bool aValid = theBaseType == theCheckedType;
-  if (!aValid) {
-    // currently this functionality is for all, as we have no separate wire selection mode
-    // lately it should be corrected to have the following check only for sketch presentations
-    aValid = theBaseType == TopAbs_FACE && theCheckedType == TopAbs_WIRE;
-  }
-  return aValid;
+  ModuleBase_Tools::setPointBallHighlighting(this);
 }
 
 void PartSet_ResultSketchPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
-                                   const Handle(Prs3d_Presentation)& thePresentation, 
-                                   const Standard_Integer theMode)
+                                      const Handle(Prs3d_Presentation)& thePresentation, 
+                                      const Standard_Integer theMode)
 {
   thePresentation->Clear();
 
-  std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
-  if (!aShapePtr) {
-    Events_Error::throwException("An empty AIS presentation: PartSet_ResultSketchPrs");
-    return;
-  }
+  TopoDS_Shape aResultShape;
+  TopoDS_Compound anAuxiliaryCompound;
+  NCollection_List<TopoDS_Shape> aFaceList;
+  fillShapes(aResultShape, anAuxiliaryCompound, mySketchFaceList);
 
-  setAuxiliaryPresentationStyle(false);
+  bool aReadyToDisplay = !aResultShape.IsNull() || !anAuxiliaryCompound.IsNull();
 
-  myFacesList.clear();
-  ResultConstructionPtr aConstruction = 
-    std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
-  if (aConstruction.get()) {
-    int aFacesNum = aConstruction->facesNum();
-    for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
-      myFacesList.push_back(aConstruction->face(aFaceIndex));
+  if (aReadyToDisplay) {
+    if (!aResultShape.IsNull()) {
+      myOriginalShape = aResultShape;
+      if (!myOriginalShape.IsNull())
+        Set(myOriginalShape);
     }
+    myAuxiliaryCompound = anAuxiliaryCompound;
   }
-  myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
-  if (!myOriginalShape.IsNull()) {
-    Set(myOriginalShape);
 
-    // change deviation coefficient to provide more precise circle
-    ModuleBase_Tools::setDefaultDeviationCoefficient(myOriginalShape, Attributes());
-    AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
-  }
-  else
-    Events_Error::throwException("An empty AIS presentation: PartSet_ResultSketchPrs");
+  setAuxiliaryPresentationStyle(false);
 
-  // visualize auxiliary shapes and sketch construction elements(point, center of a circle)
-  FeaturePtr aResultFeature = ModelAPI_Feature::feature(myResult);
-  CompositeFeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
-                                                                          (aResultFeature);
-  std::list<ResultPtr> anAuxiliaryResults;
-  /// append auxiliary shapes
-  for (int i = 0; i < aSketchFeature->numberOfSubs(); i++) {
-    FeaturePtr aFeature = aSketchFeature->subFeature(i);
-    if (PartSet_Tools::isAuxiliarySketchEntity(aFeature)) {
-      std::list<ResultPtr> aResults = aFeature->results();
-      std::list<ResultPtr>::const_iterator aIt;
-      for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
-        ResultPtr aResult = *aIt;
-        if (aResult.get() && aResult->shape().get())
-          anAuxiliaryResults.push_back(aResult);
-      }
-    }
-    else {
-    /// append not-edges shapes, e.g. center of a circle, an arc, a point feature
-      const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
-      std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
-      for (; aResIter != aRes.cend(); aResIter++) {
-        std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
-            ModelAPI_ResultConstruction>(*aResIter);
-        if (aConstr) {
-          std::shared_ptr<GeomAPI_Shape> aGeomShape = aConstr->shape();
-          if (aGeomShape.get()) {
-            const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
-            if (aShape.ShapeType() != TopAbs_EDGE)
-              anAuxiliaryResults.push_back(aConstr);
-          }
-        }
-      }
-    }
-  }
+  // change deviation coefficient to provide more precise circle
+  ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
+  AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
 
-  if (anAuxiliaryResults.size() > 0) {
+  if (!myAuxiliaryCompound.IsNull()) {
     setAuxiliaryPresentationStyle(true);
 
-    BRep_Builder aBuilder;
-    TopoDS_Compound aComp;
-    aBuilder.MakeCompound(aComp);
-    std::list<ResultPtr>::const_iterator anIt = anAuxiliaryResults.begin(),
-                                         aLast = anAuxiliaryResults.end();
-    for (; anIt != aLast; anIt++) {
-      ResultPtr aResult = *anIt;
-      if (aResult.get()) {
-        GeomShapePtr aGeomShape = aResult->shape();
-        if (aGeomShape.get()) {
-          const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
-          if (!aShape.IsNull())
-            aBuilder.Add(aComp, aShape);
-        }
-      }
-    }
-    myAuxiliaryCompound = aComp;
     Handle(Prs3d_Drawer) aDrawer = Attributes();
-    StdPrs_WFDeflectionShape::Add(thePresentation, aComp, aDrawer);
+    StdPrs_WFDeflectionShape::Add(thePresentation, myAuxiliaryCompound, aDrawer);
+  }
+
+  if (!aReadyToDisplay) {
+    Events_InfoMessage("PartSet_ResultSketchPrs", "An empty AIS presentation: PartSet_ResultSketchPrs").send();
+    static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
+    ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
   }
 }
 
@@ -183,13 +125,21 @@ void debugInfo(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType)
 #endif
 
 void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
-                                            const Standard_Integer aMode)
+                                            const Standard_Integer theMode)
 {
-  if (aMode > 8)
+  int aMode = theMode;
+
+  if (aMode > 8 &&
+      aMode != SketcherPrs_Tools::Sel_Sketch_Face &&
+      aMode != SketcherPrs_Tools::Sel_Sketch_Wire)
     // In order to avoid using custom selection modes
     return;
 
-  if (aMode == AIS_Shape::SelectionMode(TopAbs_FACE)) {
+  bool aShapeIsChanged = false;
+  if (aMode == SketcherPrs_Tools::Sel_Sketch_Face ||
+      aMode == SketcherPrs_Tools::Sel_Sketch_Wire) {
+    aMode = (aMode == SketcherPrs_Tools::Sel_Sketch_Face) ? AIS_Shape::SelectionMode(TopAbs_FACE)
+                                                          : AIS_Shape::SelectionMode(TopAbs_WIRE);
 #ifdef DEBUG_WIRE
     const TopoDS_Shape& aShape = Shape();
     debugInfo(aShape, TopAbs_VERTEX); // 24
@@ -201,13 +151,10 @@ void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)
     TopoDS_Compound aComp;
     aBuilder.MakeCompound(aComp);
     aBuilder.Add(aComp, myOriginalShape);
-    std::list<std::shared_ptr<GeomAPI_Shape>>::const_iterator aIt;
-    for (aIt = myFacesList.cbegin(); aIt != myFacesList.cend(); ++aIt) {
-      TopoDS_Shape aFace = (*aIt)->impl<TopoDS_Shape>();
+    
+    for(NCollection_List<TopoDS_Shape>::Iterator anIt(mySketchFaceList); anIt.More(); anIt.Next()) {
+      const TopoDS_Shape& aFace = anIt.Value();
       aBuilder.Add(aComp, aFace);
-      // for sketch presentation in the face mode wires should be selectable also
-      // accoring to #1343 Improvement of Extrusion and Revolution operations
-      appendShapeSelection(aSelection, aFace, TopAbs_WIRE);
     }
 #ifdef DEBUG_WIRE
     debugInfo(aComp, TopAbs_VERTEX); // 24
@@ -216,18 +163,23 @@ void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)
     debugInfo(aComp, TopAbs_FACE); // 2
 #endif
     Set(aComp);
-  } else
+    aShapeIsChanged = true;
+  }
+  else
     Set(myOriginalShape);
 
   // append auxiliary compound to selection of edges/vertices
   if (aMode == AIS_Shape::SelectionMode(TopAbs_EDGE) ||
       aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX)) {
 
-        bool isVertex = aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX);
+    bool isVertex = aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX);
     appendShapeSelection(aSelection, myAuxiliaryCompound, isVertex ? TopAbs_VERTEX : TopAbs_EDGE);
   }
 
   AIS_Shape::ComputeSelection(aSelection, aMode);
+
+  if (aShapeIsChanged)
+    Set(myOriginalShape);
 }
 
 void PartSet_ResultSketchPrs::appendShapeSelection(const Handle(SelectMgr_Selection)& theSelection,
@@ -261,9 +213,9 @@ void PartSet_ResultSketchPrs::setAuxiliaryPresentationStyle(const bool isAuxilia
     aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
   }
   else {
-    aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color",
-                                         SKETCH_AUXILIARY_COLOR);
-    aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH_AUXILIARY();
+    aColor = Config_PropManager::color("Visualization", "result_construction_color",
+                                        ModelAPI_ResultConstruction::DEFAULT_COLOR());
+    aWidth = PartSet_Tools::getAISDefaultWidth();// default width value
     aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY();
   }
 
@@ -291,3 +243,82 @@ void PartSet_ResultSketchPrs::setAuxiliaryPresentationStyle(const bool isAuxilia
     aLineAspect->SetTypeOfLine(aType);
   }
 }
+
+void PartSet_ResultSketchPrs::fillShapes(TopoDS_Shape& theResultShape,
+                                         TopoDS_Compound& theAuxiliaryCompound,
+                                         NCollection_List<TopoDS_Shape>& theFaceList)
+{
+  //if (!aResultShape.IsNull() || !anAuxiliaryCompound.IsNull())
+  std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
+  if (!aShapePtr)
+    return;
+
+  theFaceList.Clear();
+  ResultConstructionPtr aConstruction = 
+    std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
+  if (aConstruction.get()) {
+    int aFacesNum = aConstruction->facesNum();
+    for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
+      std::shared_ptr<GeomAPI_Face> aFaceShape = aConstruction->face(aFaceIndex);
+      if (aFaceShape.get()) {
+        TopoDS_Shape aFace = (aFaceShape)->impl<TopoDS_Shape>();
+        theFaceList.Append(aFace);
+      }
+    }
+  }
+  theResultShape = aShapePtr->impl<TopoDS_Shape>();
+
+  /// find auxiliary shapes
+  FeaturePtr aResultFeature = ModelAPI_Feature::feature(myResult);
+  CompositeFeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
+                                                                          (aResultFeature);
+  std::list<ResultPtr> anAuxiliaryResults;
+  for (int i = 0; i < aSketchFeature->numberOfSubs(); i++) {
+    FeaturePtr aFeature = aSketchFeature->subFeature(i);
+    if (PartSet_Tools::isAuxiliarySketchEntity(aFeature)) {
+      std::list<ResultPtr> aResults = aFeature->results();
+      std::list<ResultPtr>::const_iterator aIt;
+      for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
+        ResultPtr aResult = *aIt;
+        if (aResult.get() && aResult->shape().get())
+          anAuxiliaryResults.push_back(aResult);
+      }
+    }
+    else {
+    /// append not-edges shapes, e.g. center of a circle, an arc, a point feature
+      const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
+      std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
+      for (; aResIter != aRes.cend(); aResIter++) {
+        std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
+            ModelAPI_ResultConstruction>(*aResIter);
+        if (aConstr) {
+          std::shared_ptr<GeomAPI_Shape> aGeomShape = aConstr->shape();
+          if (aGeomShape.get()) {
+            const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
+            if (aShape.ShapeType() != TopAbs_EDGE)
+              anAuxiliaryResults.push_back(aConstr);
+          }
+        }
+      }
+    }
+  }
+
+  if (anAuxiliaryResults.size() > 0) {
+    BRep_Builder aBuilder;
+    //TopoDS_Compound aComp;
+    aBuilder.MakeCompound(theAuxiliaryCompound);
+    std::list<ResultPtr>::const_iterator anIt = anAuxiliaryResults.begin(),
+                                         aLast = anAuxiliaryResults.end();
+    for (; anIt != aLast; anIt++) {
+      ResultPtr aResult = *anIt;
+      if (aResult.get()) {
+        GeomShapePtr aGeomShape = aResult->shape();
+        if (aGeomShape.get()) {
+          const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
+          if (!aShape.IsNull())
+            aBuilder.Add(theAuxiliaryCompound, aShape);
+        }
+      }
+    }
+  }
+}