Salome HOME
ModuleBase_ViewerPrs should contain GeomShapePtr instead of TopoDS_Shape.
[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 <StdSelect_BRepOwner.hxx>
6 #include <TopoDS_Vertex.hxx>
7 #include <TopoDS.hxx>
8 #include <BRep_Tool.hxx>
9 #include <GeomAPI_Pnt.h>
10
11 //********************************************************************
12 void ModuleBase_ISelection::appendSelected(const QList<ModuleBase_ViewerPrs> theValues,
13                                            QList<ModuleBase_ViewerPrs>& theValuesTo)
14 {
15   // collect the objects from the viewer
16   QObjectPtrList anExistedObjects;
17   QList<ModuleBase_ViewerPrs>::const_iterator aPrsIt = theValuesTo.begin(),
18                                               aPrsLast = theValuesTo.end();
19   for (; aPrsIt != aPrsLast; aPrsIt++) {
20     if ((*aPrsIt).owner() && (*aPrsIt).object())
21       anExistedObjects.push_back((*aPrsIt).object());
22   }
23
24
25   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(),
26                                               aLast = theValues.end();
27   for (; anIt != aLast; anIt++) {
28     ObjectPtr anObject = (*anIt).object();
29     if (anObject.get() != NULL && !anExistedObjects.contains(anObject)) {
30       theValuesTo.append(ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL));
31     }
32   }
33
34 }
35
36 //********************************************************************
37 ResultPtr ModuleBase_ISelection::getResult(const ModuleBase_ViewerPrs& thePrs)
38 {
39   ResultPtr aResult;
40
41   if (thePrs.object().get())
42     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
43   else if (!thePrs.owner().IsNull()) {
44     ObjectPtr anObject = getSelectableObject(thePrs.owner());
45     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
46   }
47
48   return aResult;
49 }
50
51 //********************************************************************
52 GeomShapePtr ModuleBase_ISelection::getShape(const ModuleBase_ViewerPrs& thePrs)
53 {
54   GeomShapePtr aShape;
55
56   const GeomShapePtr& aPrsShape = thePrs.shape();
57   // if only result is selected, an empty shape is set to the model
58   if (!aPrsShape.get() || aPrsShape->isNull()) {
59   }
60   else {
61     aShape = aPrsShape;
62     // If the result object is built on the same shape, the result shape here is empty one
63     ResultPtr aResult = getResult(thePrs);
64     if (aResult.get() && aShape->isEqual(aResult->shape()))
65       aShape = GeomShapePtr();
66   }
67   return aShape;
68 }
69
70 //********************************************************************
71 QList<ModuleBase_ViewerPrs> ModuleBase_ISelection::getViewerPrs(const QObjectPtrList& theObjects)
72 {
73   QList<ModuleBase_ViewerPrs> aSelectedPrs;
74   QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
75   for (; anIt != aLast; anIt++) {
76     ObjectPtr anObject = *anIt;
77     if (anObject.get() != NULL) {
78       aSelectedPrs.append(ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL));
79     }
80   }
81   return aSelectedPrs;
82 }
83
84 //********************************************************************
85 void ModuleBase_ISelection::filterSelectionOnEqualPoints
86                                               (QList<ModuleBase_ViewerPrs>& theSelected)
87 {
88   QList<ModuleBase_ViewerPrs> aCandidatesToRemove;
89   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theSelected.begin(),
90                                               aLast = theSelected.end();
91   QList<ModuleBase_ViewerPrs>::const_iterator aSubIt;
92   for (; anIt != aLast; anIt++) {
93     aSubIt = anIt;
94     aSubIt++;
95     for (; aSubIt != aLast; aSubIt++) {
96       if (isEqualVertices(*anIt, *aSubIt)) {
97         aCandidatesToRemove.append(*aSubIt);
98         break;
99       }
100     }
101   }
102   QList<ModuleBase_ViewerPrs>::const_iterator aRemIt = aCandidatesToRemove.begin(),
103                                               aRemLast = aCandidatesToRemove.end();
104   for (; aRemIt != aRemLast; aRemIt++) {
105     theSelected.removeAll(*aRemIt);
106   }
107 }
108
109 bool ModuleBase_ISelection::isEqualVertices(const ModuleBase_ViewerPrs thePrs1,
110                                             const ModuleBase_ViewerPrs thePrs2)
111 {
112   bool isEqual = false;
113   Handle(StdSelect_BRepOwner) anOwner1 = Handle(StdSelect_BRepOwner)::DownCast(thePrs1.owner());
114   Handle(StdSelect_BRepOwner) anOwner2 = Handle(StdSelect_BRepOwner)::DownCast(thePrs2.owner());
115
116   if (!anOwner1.IsNull() && anOwner1->HasShape() &&
117       !anOwner2.IsNull() && anOwner2->HasShape()) {
118     const TopoDS_Shape& aShape1 = anOwner1->Shape();
119     const TopoDS_Shape& aShape2 = anOwner2->Shape();
120     //TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
121     if (aShape1.ShapeType() == TopAbs_VERTEX &&
122         aShape2.ShapeType() == TopAbs_VERTEX) {
123       const TopoDS_Vertex& aVertex1 = TopoDS::Vertex(aShape1);
124       const TopoDS_Vertex& aVertex2 = TopoDS::Vertex(aShape2);
125       if (!aVertex1.IsNull() && !aVertex2.IsNull())  {
126         gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex1);
127         gp_Pnt aPoint2 = BRep_Tool::Pnt(aVertex2);
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         isEqual = aPnt1->isEqual(aPnt2);
134       }
135     }
136   }
137
138   return isEqual;
139 }