Salome HOME
Issue #1019: Provide CompSolid selection mode for composite results
[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, TopoDS_Shape(), 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 TopoDS_Shape& aTDSShape = thePrs.shape();
57   // if only result is selected, an empty shape is set to the model
58   if (aTDSShape.IsNull()) {
59   }
60   else {
61     aShape = GeomShapePtr(new GeomAPI_Shape());
62     aShape->setImpl(new TopoDS_Shape(aTDSShape));
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_ViewerPrs> ModuleBase_ISelection::getViewerPrs(const QObjectPtrList& theObjects)
73 {
74   QList<ModuleBase_ViewerPrs> 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(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));
80     }
81   }
82   return aSelectedPrs;
83 }
84
85 //********************************************************************
86 void ModuleBase_ISelection::filterSelectionOnEqualPoints
87                                               (QList<ModuleBase_ViewerPrs>& theSelected)
88 {
89   QList<ModuleBase_ViewerPrs> aCandidatesToRemove;
90   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theSelected.begin(),
91                                               aLast = theSelected.end();
92   QList<ModuleBase_ViewerPrs>::const_iterator aSubIt;
93   for (; anIt != aLast; anIt++) {
94     aSubIt = anIt;
95     aSubIt++;
96     for (; aSubIt != aLast; aSubIt++) {
97       if (isEqualVertices(*anIt, *aSubIt)) {
98         aCandidatesToRemove.append(*aSubIt);
99         break;
100       }
101     }
102   }
103   QList<ModuleBase_ViewerPrs>::const_iterator aRemIt = aCandidatesToRemove.begin(),
104                                               aRemLast = aCandidatesToRemove.end();
105   for (; aRemIt != aRemLast; aRemIt++) {
106     theSelected.removeAll(*aRemIt);
107   }
108 }
109
110 bool ModuleBase_ISelection::isEqualVertices(const ModuleBase_ViewerPrs thePrs1,
111                                             const ModuleBase_ViewerPrs thePrs2)
112 {
113   bool isEqual = false;
114   Handle(StdSelect_BRepOwner) anOwner1 = Handle(StdSelect_BRepOwner)::DownCast(thePrs1.owner());
115   Handle(StdSelect_BRepOwner) anOwner2 = Handle(StdSelect_BRepOwner)::DownCast(thePrs2.owner());
116
117   if (!anOwner1.IsNull() && anOwner1->HasShape() &&
118       !anOwner2.IsNull() && anOwner2->HasShape()) {
119     const TopoDS_Shape& aShape1 = anOwner1->Shape();
120     const TopoDS_Shape& aShape2 = anOwner2->Shape();
121     //TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
122     if (aShape1.ShapeType() == TopAbs_VERTEX &&
123         aShape2.ShapeType() == TopAbs_VERTEX) {
124       const TopoDS_Vertex& aVertex1 = TopoDS::Vertex(aShape1);
125       const TopoDS_Vertex& aVertex2 = TopoDS::Vertex(aShape2);
126       if (!aVertex1.IsNull() && !aVertex2.IsNull())  {
127         gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex1);
128         gp_Pnt aPoint2 = BRep_Tool::Pnt(aVertex2);
129
130         std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>
131                         (new GeomAPI_Pnt(aPoint1.X(), aPoint1.Y(), aPoint1.Z()));
132         std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>
133                         (new GeomAPI_Pnt(aPoint2.X(), aPoint2.Y(), aPoint2.Z()));
134         isEqual = aPnt1->isEqual(aPnt2);
135       }
136     }
137   }
138
139   return isEqual;
140 }