Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomValidators / GeomValidators_MinObjectsSelected.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 <GeomValidators_MinObjectsSelected.h>
21
22 #include <Events_InfoMessage.h>
23
24 #include <BuildPlugin_Interpolation.h>
25
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29
30 //=================================================================================================
31 bool GeomValidators_MinObjectsSelected::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
32                                                 const std::list<std::string>& theArguments,
33                                                 Events_InfoMessage& theError) const
34 {
35   if (theArguments.size() != 2) {
36 // LCOV_EXCL_START
37     theError =
38       "Error: Wrong number of arguments (expected 2): selection list id and min number of objects";
39     return false;
40 // LCOV_EXCL_STOP
41   }
42   //"Interpolation"
43   if (theFeature->name().substr(0, 6) == L"Interp")
44   {
45     AttributeStringPtr anAttr =theFeature->string(BuildPlugin_Interpolation::CREATION_METHOD_ID());
46     if (anAttr->isInitialized())
47       if (anAttr->value() == BuildPlugin_Interpolation::CREATION_METHOD_ANALYTICAL_ID())
48         return true;
49   }
50
51   std::string aSelectionListId = theArguments.front();
52   AttributeSelectionListPtr anAttrSelList = theFeature->selectionList(aSelectionListId);
53   if (!anAttrSelList.get()) {
54 // LCOV_EXCL_START
55     theError = "Error: Could not get attribute \"%1\".";
56     theError.arg(aSelectionListId);
57     return false;
58 // LCOV_EXCL_STOP
59   }
60   int anObjectsNb = anAttrSelList->size();
61
62   int aMinObjectsNb = atoi(theArguments.back().c_str());
63
64   if (anObjectsNb < aMinObjectsNb) {
65     theError = "Error: Attribute \"%1\" should contain at least %2 items.";
66     theError.arg(aSelectionListId).arg(aMinObjectsNb);
67     return false;
68   }
69
70   return true;
71 }