Salome HOME
Improvement for key "Delete" processing: should be done only on possible objects...
[modules/shaper.git] / src / ModuleBase / ModuleBase_ISelection.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "ModuleBase_ISelection.h"
4
5 #include "ModuleBase_ViewerPrs.h"
6
7 #include <StdSelect_BRepOwner.hxx>
8 #include <TopoDS_Vertex.hxx>
9 #include <TopoDS.hxx>
10 #include <BRep_Tool.hxx>
11 #include <GeomAPI_Pnt.h>
12
13 //********************************************************************
14 void ModuleBase_ISelection::appendSelected(const QList<ModuleBase_ViewerPrsPtr> theValues,
15                                            QList<ModuleBase_ViewerPrsPtr>& theValuesTo)
16 {
17   // collect the objects from the viewer
18   QObjectPtrList anExistedObjects;
19   QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = theValuesTo.begin(),
20                                               aPrsLast = theValuesTo.end();
21   for (; aPrsIt != aPrsLast; aPrsIt++) {
22     if ((*aPrsIt)->owner() && (*aPrsIt)->object())
23       anExistedObjects.push_back((*aPrsIt)->object());
24   }
25
26
27   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(),
28                                               aLast = theValues.end();
29   for (; anIt != aLast; anIt++) {
30     ObjectPtr anObject = (*anIt)->object();
31     if (anObject.get() != NULL && !anExistedObjects.contains(anObject)) {
32       theValuesTo.append(std::shared_ptr<ModuleBase_ViewerPrs>(
33                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
34     }
35   }
36
37 }
38
39 //********************************************************************
40 ResultPtr ModuleBase_ISelection::getResult(const ModuleBase_ViewerPrsPtr& thePrs)
41 {
42   ResultPtr aResult;
43
44   if (thePrs->object().get())
45     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
46   else if (!thePrs->owner().IsNull()) {
47     ObjectPtr anObject = getSelectableObject(thePrs->owner());
48     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
49   }
50
51   return aResult;
52 }
53
54 //********************************************************************
55 GeomShapePtr ModuleBase_ISelection::getShape(const ModuleBase_ViewerPrsPtr& thePrs)
56 {
57   GeomShapePtr aShape;
58
59   const GeomShapePtr& aPrsShape = thePrs->shape();
60   // if only result is selected, an empty shape is set to the model
61   if (!aPrsShape.get() || aPrsShape->isNull()) {
62   }
63   else {
64     aShape = aPrsShape;
65     // If the result object is built on the same shape, the result shape here is empty one
66     ResultPtr aResult = getResult(thePrs);
67     if (aResult.get() && aShape->isEqual(aResult->shape()))
68       aShape = GeomShapePtr();
69   }
70   return aShape;
71 }
72
73 //********************************************************************
74 QList<ModuleBase_ViewerPrsPtr> ModuleBase_ISelection::getViewerPrs(const QObjectPtrList& theObjects)
75 {
76   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs;
77   QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
78   for (; anIt != aLast; anIt++) {
79     ObjectPtr anObject = *anIt;
80     if (anObject.get() != NULL) {
81       aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
82                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
83     }
84   }
85   return aSelectedPrs;
86 }
87
88 //********************************************************************
89 void ModuleBase_ISelection::filterSelectionOnEqualPoints
90                                               (QList<ModuleBase_ViewerPrsPtr>& theSelected)
91 {
92   QList<ModuleBase_ViewerPrsPtr> aCandidatesToRemove;
93   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theSelected.begin(),
94                                               aLast = theSelected.end();
95   QList<ModuleBase_ViewerPrsPtr>::const_iterator aSubIt;
96
97   std::set<std::shared_ptr<GeomAPI_Vertex> > aVerticesMap;
98   for (; anIt != aLast; anIt++) {
99     ModuleBase_ViewerPrsPtr aPrs = *anIt;
100     std::shared_ptr<GeomAPI_Vertex> aGeomPrsVertex = getPresentationVertex(aPrs);
101     if (aGeomPrsVertex.get()) {
102       const TopoDS_Vertex& aPrsVertex = aGeomPrsVertex->impl<TopoDS_Vertex>();
103       std::set<std::shared_ptr<GeomAPI_Vertex> >::const_iterator anIt = aVerticesMap.begin(),
104                                                                  aLast = aVerticesMap.end();
105       bool aFound = false;
106       for (; anIt != aLast && !aFound; anIt++) {
107         std::shared_ptr<GeomAPI_Vertex> aGeomVertex = *anIt;
108         const TopoDS_Vertex& aVertex = aGeomVertex->impl<TopoDS_Vertex>();
109         gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex);
110         gp_Pnt aPoint2 = BRep_Tool::Pnt(aPrsVertex);
111
112         std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>
113                         (new GeomAPI_Pnt(aPoint1.X(), aPoint1.Y(), aPoint1.Z()));
114         std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>
115                         (new GeomAPI_Pnt(aPoint2.X(), aPoint2.Y(), aPoint2.Z()));
116         aFound = aPnt1->isEqual(aPnt2);
117       }
118       if (aFound) {
119         aCandidatesToRemove.append(aPrs);
120         continue;
121       }
122       aVerticesMap.insert(aGeomPrsVertex);
123     }
124   }
125   QList<ModuleBase_ViewerPrsPtr>::const_iterator aRemIt = aCandidatesToRemove.begin(),
126                                               aRemLast = aCandidatesToRemove.end();
127   for (; aRemIt != aRemLast; aRemIt++) {
128     theSelected.removeAll(*aRemIt);
129   }
130 }
131
132 std::shared_ptr<GeomAPI_Vertex> ModuleBase_ISelection::getPresentationVertex(
133                                                          const ModuleBase_ViewerPrsPtr& thePrs)
134 {
135   std::shared_ptr<GeomAPI_Vertex> aGeomVertex;
136   Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(thePrs->owner());
137
138   if (!anOwner.IsNull() && anOwner->HasShape()) {
139     const TopoDS_Shape& aShape = anOwner->Shape();
140     if (aShape.ShapeType() == TopAbs_VERTEX) {
141       const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
142       if (!aVertex.IsNull())  {
143         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
144         aGeomVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aPoint.X(), aPoint.Y(),
145                                                                          aPoint.Z()));
146       }
147     }
148   }
149   return aGeomVertex;
150 }
151