Salome HOME
Merge branch 'V9_2_2_BR'
[modules/shaper.git] / src / GeomValidators / GeomValidators_MinObjectsSelected.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
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 <GeomValidators_MinObjectsSelected.h>
21
22 #include <Events_InfoMessage.h>
23
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26
27 //=================================================================================================
28 bool GeomValidators_MinObjectsSelected::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
29                                                 const std::list<std::string>& theArguments,
30                                                 Events_InfoMessage& theError) const
31 {
32   if(theArguments.size() != 2) {
33 // LCOV_EXCL_START
34     theError =
35       "Error: Wrong number of arguments (expected 2): selection list id and min number of objects";
36     return false;
37 // LCOV_EXCL_STOP
38   }
39
40   std::string aSelectionListId = theArguments.front();
41   AttributeSelectionListPtr anAttrSelList = theFeature->selectionList(aSelectionListId);
42   if(!anAttrSelList.get()) {
43 // LCOV_EXCL_START
44     theError = "Error: Could not get attribute \"%1\".";
45     theError.arg(aSelectionListId);
46     return false;
47 // LCOV_EXCL_STOP
48   }
49   int anObjectsNb = anAttrSelList->size();
50
51   int aMinObjectsNb = atoi(theArguments.back().c_str());
52
53   if(anObjectsNb < aMinObjectsNb) {
54     theError = "Error: Attribute \"%1\" should contain at least %2 items.";
55     theError.arg(aSelectionListId).arg(theArguments.back());
56     return false;
57   }
58
59   return true;
60 }