Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / ModuleBase / ModuleBase_ISelection.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_ISelection.h"
21
22 #include "ModuleBase_ViewerPrs.h"
23
24 #include <StdSelect_BRepOwner.hxx>
25 #include <TopoDS_Vertex.hxx>
26 #include <TopoDS.hxx>
27 #include <BRep_Tool.hxx>
28 #include <GeomAPI_Pnt.h>
29
30 //********************************************************************
31 void ModuleBase_ISelection::appendSelected(const QList<ModuleBase_ViewerPrsPtr> theValues,
32                                            QList<ModuleBase_ViewerPrsPtr>& theValuesTo)
33 {
34   // collect the objects from the viewer
35   QObjectPtrList anExistedObjects;
36   foreach(ModuleBase_ViewerPrsPtr aPrs, theValuesTo) {
37     if (aPrs->owner() && aPrs->object())
38       anExistedObjects.append(aPrs->object());
39   }
40
41   ObjectPtr anObject;
42   foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
43     anObject = aPrs->object();
44     if (anObject.get() != NULL && !anExistedObjects.contains(anObject)) {
45       theValuesTo.append(std::shared_ptr<ModuleBase_ViewerPrs>(
46                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
47     }
48   }
49 }
50
51 //********************************************************************
52 ResultPtr ModuleBase_ISelection::getResult(const ModuleBase_ViewerPrsPtr& thePrs)
53 {
54   ResultPtr aResult;
55
56   if (thePrs->object().get())
57     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
58   else if (!thePrs->owner().IsNull()) {
59     ObjectPtr anObject = getSelectableObject(thePrs->owner());
60     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
61   }
62
63   return aResult;
64 }
65
66 //********************************************************************
67 GeomShapePtr ModuleBase_ISelection::getShape(const ModuleBase_ViewerPrsPtr& thePrs)
68 {
69   GeomShapePtr aShape;
70
71   const GeomShapePtr& aPrsShape = thePrs->shape();
72   // if only result is selected, an empty shape is set to the model
73   if (!aPrsShape.get() || aPrsShape->isNull()) {
74   }
75   else {
76     aShape = aPrsShape;
77     // If the result object is built on the same shape, the result shape here is empty one
78     ResultPtr aResult = getResult(thePrs);
79     if (aResult.get() && aShape->isEqual(aResult->shape()))
80       aShape = GeomShapePtr();
81   }
82   return aShape;
83 }
84
85 //********************************************************************
86 QList<ModuleBase_ViewerPrsPtr> ModuleBase_ISelection::getViewerPrs(const QObjectPtrList& theObjects)
87 {
88   QList<ModuleBase_ViewerPrsPtr> aSelectedPrs;
89   QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
90   for (; anIt != aLast; anIt++) {
91     ObjectPtr anObject = *anIt;
92     if (anObject.get() != NULL) {
93       aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
94                new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
95     }
96   }
97   return aSelectedPrs;
98 }
99
100 //********************************************************************
101 void ModuleBase_ISelection::filterSelectionOnEqualPoints
102                                               (QList<ModuleBase_ViewerPrsPtr>& theSelected)
103 {
104   QList<ModuleBase_ViewerPrsPtr> aCandidatesToRemove;
105   QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theSelected.begin(),
106                                               aLast = theSelected.end();
107   QList<ModuleBase_ViewerPrsPtr>::const_iterator aSubIt;
108
109   std::set<std::shared_ptr<GeomAPI_Vertex> > aVerticesMap;
110   for (; anIt != aLast; anIt++) {
111     ModuleBase_ViewerPrsPtr aPrs = *anIt;
112     std::shared_ptr<GeomAPI_Vertex> aGeomPrsVertex = getPresentationVertex(aPrs);
113     if (aGeomPrsVertex.get()) {
114       const TopoDS_Vertex& aPrsVertex = aGeomPrsVertex->impl<TopoDS_Vertex>();
115       std::set<std::shared_ptr<GeomAPI_Vertex> >::const_iterator anIt = aVerticesMap.begin(),
116                                                                  aLast = aVerticesMap.end();
117       bool aFound = false;
118       for (; anIt != aLast && !aFound; anIt++) {
119         std::shared_ptr<GeomAPI_Vertex> aGeomVertex = *anIt;
120         const TopoDS_Vertex& aVertex = aGeomVertex->impl<TopoDS_Vertex>();
121         gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex);
122         gp_Pnt aPoint2 = BRep_Tool::Pnt(aPrsVertex);
123
124         std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>
125                         (new GeomAPI_Pnt(aPoint1.X(), aPoint1.Y(), aPoint1.Z()));
126         std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>
127                         (new GeomAPI_Pnt(aPoint2.X(), aPoint2.Y(), aPoint2.Z()));
128         aFound = aPnt1->isEqual(aPnt2);
129       }
130       if (aFound) {
131         aCandidatesToRemove.append(aPrs);
132         continue;
133       }
134       aVerticesMap.insert(aGeomPrsVertex);
135     }
136   }
137   QList<ModuleBase_ViewerPrsPtr>::const_iterator aRemIt = aCandidatesToRemove.begin(),
138                                               aRemLast = aCandidatesToRemove.end();
139   for (; aRemIt != aRemLast; aRemIt++) {
140     theSelected.removeAll(*aRemIt);
141   }
142 }
143
144 std::shared_ptr<GeomAPI_Vertex> ModuleBase_ISelection::getPresentationVertex(
145                                                          const ModuleBase_ViewerPrsPtr& thePrs)
146 {
147   std::shared_ptr<GeomAPI_Vertex> aGeomVertex;
148   Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(thePrs->owner());
149
150   if (!anOwner.IsNull() && anOwner->HasShape()) {
151     const TopoDS_Shape& aShape = anOwner->Shape();
152     if (aShape.ShapeType() == TopAbs_VERTEX) {
153       const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
154       if (!aVertex.IsNull())  {
155         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
156         aGeomVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aPoint.X(), aPoint.Y(),
157                                                                          aPoint.Z()));
158       }
159     }
160   }
161   return aGeomVertex;
162 }
163