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