1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "ModuleBase_ISelection.h"
5 #include "ModuleBase_ViewerPrs.h"
7 #include <StdSelect_BRepOwner.hxx>
8 #include <TopoDS_Vertex.hxx>
10 #include <BRep_Tool.hxx>
11 #include <GeomAPI_Pnt.h>
13 //********************************************************************
14 void ModuleBase_ISelection::appendSelected(const QList<ModuleBase_ViewerPrsPtr> theValues,
15 QList<ModuleBase_ViewerPrsPtr>& theValuesTo)
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());
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)));
39 //********************************************************************
40 ResultPtr ModuleBase_ISelection::getResult(const ModuleBase_ViewerPrsPtr& thePrs)
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);
54 //********************************************************************
55 GeomShapePtr ModuleBase_ISelection::getShape(const ModuleBase_ViewerPrsPtr& thePrs)
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()) {
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();
73 //********************************************************************
74 QList<ModuleBase_ViewerPrsPtr> ModuleBase_ISelection::getViewerPrs(const QObjectPtrList& theObjects)
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)));
88 //********************************************************************
89 void ModuleBase_ISelection::filterSelectionOnEqualPoints
90 (QList<ModuleBase_ViewerPrsPtr>& theSelected)
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;
97 std::set<std::shared_ptr<GeomAPI_Vertex> > aVerticesMap;
98 for (; anIt != aLast; anIt++) {
99 ModuleBase_ViewerPrsPtr aPrs = *anIt;
100 std::shared_ptr<GeomAPI_Vertex> aGeomPrsVertex = getPresentationVertex(aPrs);
101 if (aGeomPrsVertex.get()) {
102 const TopoDS_Vertex& aPrsVertex = aGeomPrsVertex->impl<TopoDS_Vertex>();
103 std::set<std::shared_ptr<GeomAPI_Vertex> >::const_iterator anIt = aVerticesMap.begin(),
104 aLast = aVerticesMap.end();
106 for (; anIt != aLast && !aFound; anIt++) {
107 std::shared_ptr<GeomAPI_Vertex> aGeomVertex = *anIt;
108 const TopoDS_Vertex& aVertex = aGeomVertex->impl<TopoDS_Vertex>();
109 gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex);
110 gp_Pnt aPoint2 = BRep_Tool::Pnt(aPrsVertex);
112 std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>
113 (new GeomAPI_Pnt(aPoint1.X(), aPoint1.Y(), aPoint1.Z()));
114 std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>
115 (new GeomAPI_Pnt(aPoint2.X(), aPoint2.Y(), aPoint2.Z()));
116 aFound = aPnt1->isEqual(aPnt2);
119 aCandidatesToRemove.append(aPrs);
122 aVerticesMap.insert(aGeomPrsVertex);
125 QList<ModuleBase_ViewerPrsPtr>::const_iterator aRemIt = aCandidatesToRemove.begin(),
126 aRemLast = aCandidatesToRemove.end();
127 for (; aRemIt != aRemLast; aRemIt++) {
128 theSelected.removeAll(*aRemIt);
132 std::shared_ptr<GeomAPI_Vertex> ModuleBase_ISelection::getPresentationVertex(
133 const ModuleBase_ViewerPrsPtr& thePrs)
135 std::shared_ptr<GeomAPI_Vertex> aGeomVertex;
136 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(thePrs->owner());
138 if (!anOwner.IsNull() && anOwner->HasShape()) {
139 const TopoDS_Shape& aShape = anOwner->Shape();
140 if (aShape.ShapeType() == TopAbs_VERTEX) {
141 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
142 if (!aVertex.IsNull()) {
143 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
144 aGeomVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aPoint.X(), aPoint.Y(),