1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "ModuleBase_ISelection.h"
22 #include "ModuleBase_ViewerPrs.h"
24 #include <StdSelect_BRepOwner.hxx>
25 #include <TopoDS_Vertex.hxx>
27 #include <BRep_Tool.hxx>
28 #include <GeomAPI_Pnt.h>
30 //********************************************************************
31 void ModuleBase_ISelection::appendSelected(const QList<ModuleBase_ViewerPrsPtr> theValues,
32 QList<ModuleBase_ViewerPrsPtr>& theValuesTo)
34 // collect the objects from the viewer
35 QObjectPtrList anExistedObjects;
36 QList<ModuleBase_ViewerPrsPtr>::const_iterator aPrsIt = theValuesTo.begin(),
37 aPrsLast = theValuesTo.end();
38 for (; aPrsIt != aPrsLast; aPrsIt++) {
39 if ((*aPrsIt)->owner() && (*aPrsIt)->object())
40 anExistedObjects.push_back((*aPrsIt)->object());
44 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(),
45 aLast = theValues.end();
46 for (; anIt != aLast; anIt++) {
47 ObjectPtr anObject = (*anIt)->object();
48 if (anObject.get() != NULL && !anExistedObjects.contains(anObject)) {
49 theValuesTo.append(std::shared_ptr<ModuleBase_ViewerPrs>(
50 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
56 //********************************************************************
57 ResultPtr ModuleBase_ISelection::getResult(const ModuleBase_ViewerPrsPtr& thePrs)
61 if (thePrs->object().get())
62 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
63 else if (!thePrs->owner().IsNull()) {
64 ObjectPtr anObject = getSelectableObject(thePrs->owner());
65 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
71 //********************************************************************
72 GeomShapePtr ModuleBase_ISelection::getShape(const ModuleBase_ViewerPrsPtr& thePrs)
76 const GeomShapePtr& aPrsShape = thePrs->shape();
77 // if only result is selected, an empty shape is set to the model
78 if (!aPrsShape.get() || aPrsShape->isNull()) {
82 // If the result object is built on the same shape, the result shape here is empty one
83 ResultPtr aResult = getResult(thePrs);
84 if (aResult.get() && aShape->isEqual(aResult->shape()))
85 aShape = GeomShapePtr();
90 //********************************************************************
91 QList<ModuleBase_ViewerPrsPtr> ModuleBase_ISelection::getViewerPrs(const QObjectPtrList& theObjects)
93 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs;
94 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
95 for (; anIt != aLast; anIt++) {
96 ObjectPtr anObject = *anIt;
97 if (anObject.get() != NULL) {
98 aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
99 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
105 //********************************************************************
106 void ModuleBase_ISelection::filterSelectionOnEqualPoints
107 (QList<ModuleBase_ViewerPrsPtr>& theSelected)
109 QList<ModuleBase_ViewerPrsPtr> aCandidatesToRemove;
110 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theSelected.begin(),
111 aLast = theSelected.end();
112 QList<ModuleBase_ViewerPrsPtr>::const_iterator aSubIt;
114 std::set<std::shared_ptr<GeomAPI_Vertex> > aVerticesMap;
115 for (; anIt != aLast; anIt++) {
116 ModuleBase_ViewerPrsPtr aPrs = *anIt;
117 std::shared_ptr<GeomAPI_Vertex> aGeomPrsVertex = getPresentationVertex(aPrs);
118 if (aGeomPrsVertex.get()) {
119 const TopoDS_Vertex& aPrsVertex = aGeomPrsVertex->impl<TopoDS_Vertex>();
120 std::set<std::shared_ptr<GeomAPI_Vertex> >::const_iterator anIt = aVerticesMap.begin(),
121 aLast = aVerticesMap.end();
123 for (; anIt != aLast && !aFound; anIt++) {
124 std::shared_ptr<GeomAPI_Vertex> aGeomVertex = *anIt;
125 const TopoDS_Vertex& aVertex = aGeomVertex->impl<TopoDS_Vertex>();
126 gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex);
127 gp_Pnt aPoint2 = BRep_Tool::Pnt(aPrsVertex);
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 aFound = aPnt1->isEqual(aPnt2);
136 aCandidatesToRemove.append(aPrs);
139 aVerticesMap.insert(aGeomPrsVertex);
142 QList<ModuleBase_ViewerPrsPtr>::const_iterator aRemIt = aCandidatesToRemove.begin(),
143 aRemLast = aCandidatesToRemove.end();
144 for (; aRemIt != aRemLast; aRemIt++) {
145 theSelected.removeAll(*aRemIt);
149 std::shared_ptr<GeomAPI_Vertex> ModuleBase_ISelection::getPresentationVertex(
150 const ModuleBase_ViewerPrsPtr& thePrs)
152 std::shared_ptr<GeomAPI_Vertex> aGeomVertex;
153 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(thePrs->owner());
155 if (!anOwner.IsNull() && anOwner->HasShape()) {
156 const TopoDS_Shape& aShape = anOwner->Shape();
157 if (aShape.ShapeType() == TopAbs_VERTEX) {
158 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
159 if (!aVertex.IsNull()) {
160 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
161 aGeomVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aPoint.X(), aPoint.Y(),