Salome HOME
ModuleBase_ViewerPrs is wrapped into shared_ptr: remove include of this class from...
[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   for (; anIt != aLast; anIt++) {
97     aSubIt = anIt;
98     aSubIt++;
99     for (; aSubIt != aLast; aSubIt++) {
100       if (isEqualVertices(*anIt, *aSubIt)) {
101         aCandidatesToRemove.append(*aSubIt);
102         break;
103       }
104     }
105   }
106   QList<ModuleBase_ViewerPrsPtr>::const_iterator aRemIt = aCandidatesToRemove.begin(),
107                                               aRemLast = aCandidatesToRemove.end();
108   for (; aRemIt != aRemLast; aRemIt++) {
109     theSelected.removeAll(*aRemIt);
110   }
111 }
112
113 bool ModuleBase_ISelection::isEqualVertices(const ModuleBase_ViewerPrsPtr thePrs1,
114                                             const ModuleBase_ViewerPrsPtr thePrs2)
115 {
116   bool isEqual = false;
117   Handle(StdSelect_BRepOwner) anOwner1 = Handle(StdSelect_BRepOwner)::DownCast(thePrs1->owner());
118   Handle(StdSelect_BRepOwner) anOwner2 = Handle(StdSelect_BRepOwner)::DownCast(thePrs2->owner());
119
120   if (!anOwner1.IsNull() && anOwner1->HasShape() &&
121       !anOwner2.IsNull() && anOwner2->HasShape()) {
122     const TopoDS_Shape& aShape1 = anOwner1->Shape();
123     const TopoDS_Shape& aShape2 = anOwner2->Shape();
124     //TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
125     if (aShape1.ShapeType() == TopAbs_VERTEX &&
126         aShape2.ShapeType() == TopAbs_VERTEX) {
127       const TopoDS_Vertex& aVertex1 = TopoDS::Vertex(aShape1);
128       const TopoDS_Vertex& aVertex2 = TopoDS::Vertex(aShape2);
129       if (!aVertex1.IsNull() && !aVertex2.IsNull())  {
130         gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex1);
131         gp_Pnt aPoint2 = BRep_Tool::Pnt(aVertex2);
132
133         std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>
134                         (new GeomAPI_Pnt(aPoint1.X(), aPoint1.Y(), aPoint1.Z()));
135         std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>
136                         (new GeomAPI_Pnt(aPoint2.X(), aPoint2.Y(), aPoint2.Z()));
137         isEqual = aPnt1->isEqual(aPnt2);
138       }
139     }
140   }
141
142   return isEqual;
143 }