Salome HOME
b600746dad0624386ec3b19c1131fa0198d4bb71
[modules/shaper.git] / src / FiltersAPI / FiltersAPI_Selection.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "FiltersAPI_Selection.h"
21
22 #include "GeomAPI_Edge.h"
23 #include "ModelAPI_Session.h"
24 #include "ModelAPI_FiltersFactory.h"
25 #include "ModelHighAPI_Services.h"
26
27 FiltersAPI_Selection::FiltersAPI_Selection(const FiltersPtr & theFeature)
28 {
29   myVariantType = VT_Filtering;
30   myFilterFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theFeature->feature());
31 }
32
33 FiltersAPI_Selection::~FiltersAPI_Selection()
34 {
35 }
36
37 FiltersFeaturePtr FiltersAPI_Selection::feature() const
38 {
39   return myFilterFeature;
40 }
41
42 std::list<ModelHighAPI_Selection> FiltersAPI_Selection::select
43                                   (const std::string theShapeType) const
44 {
45   return select(GeomAPI_Shape::shapeTypeByStr(theShapeType));
46 }
47
48 std::list<ModelHighAPI_Selection> FiltersAPI_Selection::select
49                                   (const GeomAPI_Shape::ShapeType theShapeType) const
50 {
51   // finish operation to make sure the selection is done on the current state of the history
52   apply();
53
54   std::list<ModelHighAPI_Selection> aSelList;
55   static SessionPtr aSession = ModelAPI_Session::get();
56   std::list< std::pair<ResultPtr, GeomShapePtr> > aResList =
57     aSession->filters()->select(myFilterFeature, theShapeType);
58
59   std::list< std::pair<ResultPtr, GeomShapePtr> >::const_iterator itSelected = aResList.cbegin();
60   for (; itSelected != aResList.cend(); itSelected++) {
61     ResultPtr aCurRes = (*itSelected).first;
62     GeomShapePtr aSubShape = (*itSelected).second;
63     aSelList.push_back(ModelHighAPI_Selection(aCurRes, aSubShape));
64   }
65
66   return aSelList;
67 }
68
69 // ================================================================================================
70 FiltersAPI_Selection filters(const std::shared_ptr<ModelAPI_Document>& thePart,
71                              const std::list<FilterAPIPtr>& theFilters)
72 {
73   FeaturePtr aFeature = thePart->addFeature(FiltersPlugin_Selection::ID());
74   FiltersPtr aFiltersFeature(new FiltersAPI_Feature(aFeature));
75   aFiltersFeature->setFilters(theFilters);
76   return FiltersAPI_Selection(aFiltersFeature);
77 }