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