Salome HOME
Fix for the issue #19932 : Fillet fails in python with all in features Fuse inputs
[modules/shaper.git] / src / ModuleBase / ModuleBase_ISelection.cpp
index 05bc1d6c1dbde197c76e1c23cf8e0dcd3d2cf051..5edf3334ce9f08102a0b0010d564da7ddf57384a 100644 (file)
@@ -1,7 +1,27 @@
-// Copyright (C) 2014-20xx 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
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "ModuleBase_ISelection.h"
 
+#include "ModuleBase_ViewerPrs.h"
+#include "ModelAPI_Feature.h"
+
 #include <StdSelect_BRepOwner.hxx>
 #include <TopoDS_Vertex.hxx>
 #include <TopoDS.hxx>
@@ -14,24 +34,19 @@ void ModuleBase_ISelection::appendSelected(const QList<ModuleBase_ViewerPrsPtr>
 {
   // collect the objects from the viewer
   QObjectPtrList anExistedObjects;
-  QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = theValuesTo.begin(),
-                                              aPrsLast = theValuesTo.end();
-  for (; aPrsIt != aPrsLast; aPrsIt++) {
-    if ((*aPrsIt)->owner() && (*aPrsIt)->object())
-      anExistedObjects.push_back((*aPrsIt)->object());
+  foreach(ModuleBase_ViewerPrsPtr aPrs, theValuesTo) {
+    if (aPrs->owner() && aPrs->object())
+      anExistedObjects.append(aPrs->object());
   }
 
-
-  QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(),
-                                              aLast = theValues.end();
-  for (; anIt != aLast; anIt++) {
-    ObjectPtr anObject = (*anIt)->object();
+  ObjectPtr anObject;
+  foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
+    anObject = aPrs->object();
     if (anObject.get() != NULL && !anExistedObjects.contains(anObject)) {
       theValuesTo.append(std::shared_ptr<ModuleBase_ViewerPrs>(
                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
     }
   }
-
 }
 
 //********************************************************************
@@ -39,8 +54,10 @@ ResultPtr ModuleBase_ISelection::getResult(const ModuleBase_ViewerPrsPtr& thePrs
 {
   ResultPtr aResult;
 
-  if (thePrs->object().get())
-    aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
+  if (thePrs->object().get()) {
+    ObjectPtr aObject = thePrs->object();
+    aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
+  }
   else if (!thePrs->owner().IsNull()) {
     ObjectPtr anObject = getSelectableObject(thePrs->owner());
     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
@@ -91,14 +108,33 @@ void ModuleBase_ISelection::filterSelectionOnEqualPoints
   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theSelected.begin(),
                                               aLast = theSelected.end();
   QList<ModuleBase_ViewerPrsPtr>::const_iterator aSubIt;
+
+  std::set<std::shared_ptr<GeomAPI_Vertex> > aVerticesMap;
   for (; anIt != aLast; anIt++) {
-    aSubIt = anIt;
-    aSubIt++;
-    for (; aSubIt != aLast; aSubIt++) {
-      if (isEqualVertices(*anIt, *aSubIt)) {
-        aCandidatesToRemove.append(*aSubIt);
-        break;
+    ModuleBase_ViewerPrsPtr aPrs = *anIt;
+    std::shared_ptr<GeomAPI_Vertex> aGeomPrsVertex = getPresentationVertex(aPrs);
+    if (aGeomPrsVertex.get()) {
+      const TopoDS_Vertex& aPrsVertex = aGeomPrsVertex->impl<TopoDS_Vertex>();
+      std::set<std::shared_ptr<GeomAPI_Vertex> >::const_iterator aVIt = aVerticesMap.begin(),
+                                                                 aVLast = aVerticesMap.end();
+      bool aFound = false;
+      for (; aVIt != aVLast && !aFound; aVIt++) {
+        std::shared_ptr<GeomAPI_Vertex> aGeomVertex = *aVIt;
+        const TopoDS_Vertex& aVertex = aGeomVertex->impl<TopoDS_Vertex>();
+        gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex);
+        gp_Pnt aPoint2 = BRep_Tool::Pnt(aPrsVertex);
+
+        std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>
+                        (new GeomAPI_Pnt(aPoint1.X(), aPoint1.Y(), aPoint1.Z()));
+        std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>
+                        (new GeomAPI_Pnt(aPoint2.X(), aPoint2.Y(), aPoint2.Z()));
+        aFound = aPnt1->isEqual(aPnt2);
+      }
+      if (aFound) {
+        aCandidatesToRemove.append(aPrs);
+        continue;
       }
+      aVerticesMap.insert(aGeomPrsVertex);
     }
   }
   QList<ModuleBase_ViewerPrsPtr>::const_iterator aRemIt = aCandidatesToRemove.begin(),
@@ -108,34 +144,23 @@ void ModuleBase_ISelection::filterSelectionOnEqualPoints
   }
 }
 
-bool ModuleBase_ISelection::isEqualVertices(const ModuleBase_ViewerPrsPtr thePrs1,
-                                            const ModuleBase_ViewerPrsPtr thePrs2)
+std::shared_ptr<GeomAPI_Vertex> ModuleBase_ISelection::getPresentationVertex(
+                                                         const ModuleBase_ViewerPrsPtr& thePrs)
 {
-  bool isEqual = false;
-  Handle(StdSelect_BRepOwner) anOwner1 = Handle(StdSelect_BRepOwner)::DownCast(thePrs1->owner());
-  Handle(StdSelect_BRepOwner) anOwner2 = Handle(StdSelect_BRepOwner)::DownCast(thePrs2->owner());
-
-  if (!anOwner1.IsNull() && anOwner1->HasShape() &&
-      !anOwner2.IsNull() && anOwner2->HasShape()) {
-    const TopoDS_Shape& aShape1 = anOwner1->Shape();
-    const TopoDS_Shape& aShape2 = anOwner2->Shape();
-    //TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
-    if (aShape1.ShapeType() == TopAbs_VERTEX &&
-        aShape2.ShapeType() == TopAbs_VERTEX) {
-      const TopoDS_Vertex& aVertex1 = TopoDS::Vertex(aShape1);
-      const TopoDS_Vertex& aVertex2 = TopoDS::Vertex(aShape2);
-      if (!aVertex1.IsNull() && !aVertex2.IsNull())  {
-        gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex1);
-        gp_Pnt aPoint2 = BRep_Tool::Pnt(aVertex2);
-
-        std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>
-                        (new GeomAPI_Pnt(aPoint1.X(), aPoint1.Y(), aPoint1.Z()));
-        std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>
-                        (new GeomAPI_Pnt(aPoint2.X(), aPoint2.Y(), aPoint2.Z()));
-        isEqual = aPnt1->isEqual(aPnt2);
+  std::shared_ptr<GeomAPI_Vertex> aGeomVertex;
+  Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(thePrs->owner());
+
+  if (!anOwner.IsNull() && anOwner->HasShape()) {
+    const TopoDS_Shape& aShape = anOwner->Shape();
+    if (aShape.ShapeType() == TopAbs_VERTEX) {
+      const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
+      if (!aVertex.IsNull())  {
+        gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
+        aGeomVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aPoint.X(), aPoint.Y(),
+                                                                         aPoint.Z()));
       }
     }
   }
-
-  return isEqual;
+  return aGeomVertex;
 }
+