1 // Copyright (C) 2014-2019 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
20 #include "ModuleBase_ISelection.h"
22 #include "ModuleBase_ViewerPrs.h"
23 #include "ModelAPI_Feature.h"
25 #include <StdSelect_BRepOwner.hxx>
26 #include <TopoDS_Vertex.hxx>
28 #include <BRep_Tool.hxx>
29 #include <GeomAPI_Pnt.h>
31 //********************************************************************
32 void ModuleBase_ISelection::appendSelected(const QList<ModuleBase_ViewerPrsPtr> theValues,
33 QList<ModuleBase_ViewerPrsPtr>& theValuesTo)
35 // collect the objects from the viewer
36 QObjectPtrList anExistedObjects;
37 foreach(ModuleBase_ViewerPrsPtr aPrs, theValuesTo) {
38 if (aPrs->owner() && aPrs->object())
39 anExistedObjects.append(aPrs->object());
43 foreach(ModuleBase_ViewerPrsPtr aPrs, theValues) {
44 anObject = aPrs->object();
45 if (anObject.get() != NULL && !anExistedObjects.contains(anObject)) {
46 theValuesTo.append(std::shared_ptr<ModuleBase_ViewerPrs>(
47 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
52 //********************************************************************
53 ResultPtr ModuleBase_ISelection::getResult(const ModuleBase_ViewerPrsPtr& thePrs)
57 if (thePrs->object().get()) {
58 ObjectPtr aObject = thePrs->object();
59 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
61 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
63 aResult = aFeature->firstResult();
67 else if (!thePrs->owner().IsNull()) {
68 ObjectPtr anObject = getSelectableObject(thePrs->owner());
69 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
75 //********************************************************************
76 GeomShapePtr ModuleBase_ISelection::getShape(const ModuleBase_ViewerPrsPtr& thePrs)
80 const GeomShapePtr& aPrsShape = thePrs->shape();
81 // if only result is selected, an empty shape is set to the model
82 if (!aPrsShape.get() || aPrsShape->isNull()) {
86 // If the result object is built on the same shape, the result shape here is empty one
87 ResultPtr aResult = getResult(thePrs);
88 if (aResult.get() && aShape->isEqual(aResult->shape()))
89 aShape = GeomShapePtr();
94 //********************************************************************
95 QList<ModuleBase_ViewerPrsPtr> ModuleBase_ISelection::getViewerPrs(const QObjectPtrList& theObjects)
97 QList<ModuleBase_ViewerPrsPtr> aSelectedPrs;
98 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
99 for (; anIt != aLast; anIt++) {
100 ObjectPtr anObject = *anIt;
101 if (anObject.get() != NULL) {
102 aSelectedPrs.append(std::shared_ptr<ModuleBase_ViewerPrs>(
103 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
109 //********************************************************************
110 void ModuleBase_ISelection::filterSelectionOnEqualPoints
111 (QList<ModuleBase_ViewerPrsPtr>& theSelected)
113 QList<ModuleBase_ViewerPrsPtr> aCandidatesToRemove;
114 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theSelected.begin(),
115 aLast = theSelected.end();
116 QList<ModuleBase_ViewerPrsPtr>::const_iterator aSubIt;
118 std::set<std::shared_ptr<GeomAPI_Vertex> > aVerticesMap;
119 for (; anIt != aLast; anIt++) {
120 ModuleBase_ViewerPrsPtr aPrs = *anIt;
121 std::shared_ptr<GeomAPI_Vertex> aGeomPrsVertex = getPresentationVertex(aPrs);
122 if (aGeomPrsVertex.get()) {
123 const TopoDS_Vertex& aPrsVertex = aGeomPrsVertex->impl<TopoDS_Vertex>();
124 std::set<std::shared_ptr<GeomAPI_Vertex> >::const_iterator anIt = aVerticesMap.begin(),
125 aLast = aVerticesMap.end();
127 for (; anIt != aLast && !aFound; anIt++) {
128 std::shared_ptr<GeomAPI_Vertex> aGeomVertex = *anIt;
129 const TopoDS_Vertex& aVertex = aGeomVertex->impl<TopoDS_Vertex>();
130 gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex);
131 gp_Pnt aPoint2 = BRep_Tool::Pnt(aPrsVertex);
133 std::shared_ptr<GeomAPI_Pnt> aPnt1 = std::shared_ptr<GeomAPI_Pnt>
134 (new GeomAPI_Pnt(aPoint1.X(), aPoint1.Y(), aPoint1.Z()));
135 std::shared_ptr<GeomAPI_Pnt> aPnt2 = std::shared_ptr<GeomAPI_Pnt>
136 (new GeomAPI_Pnt(aPoint2.X(), aPoint2.Y(), aPoint2.Z()));
137 aFound = aPnt1->isEqual(aPnt2);
140 aCandidatesToRemove.append(aPrs);
143 aVerticesMap.insert(aGeomPrsVertex);
146 QList<ModuleBase_ViewerPrsPtr>::const_iterator aRemIt = aCandidatesToRemove.begin(),
147 aRemLast = aCandidatesToRemove.end();
148 for (; aRemIt != aRemLast; aRemIt++) {
149 theSelected.removeAll(*aRemIt);
153 std::shared_ptr<GeomAPI_Vertex> ModuleBase_ISelection::getPresentationVertex(
154 const ModuleBase_ViewerPrsPtr& thePrs)
156 std::shared_ptr<GeomAPI_Vertex> aGeomVertex;
157 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(thePrs->owner());
159 if (!anOwner.IsNull() && anOwner->HasShape()) {
160 const TopoDS_Shape& aShape = anOwner->Shape();
161 if (aShape.ShapeType() == TopAbs_VERTEX) {
162 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
163 if (!aVertex.IsNull()) {
164 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
165 aGeomVertex = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(aPoint.X(), aPoint.Y(),