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