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