]> SALOME platform Git repositories - modules/shaper.git/blob - src/CollectionPlugin/CollectionPlugin_Validators.cpp
Salome HOME
Task 2.5. Combination operations on Groups (issue #2935)
[modules/shaper.git] / src / CollectionPlugin / CollectionPlugin_Validators.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 "CollectionPlugin_Validators.h"
21 #include "CollectionPlugin_Group.h"
22 #include "CollectionPlugin_Field.h"
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultGroup.h>
25 #include <Events_InfoMessage.h>
26
27
28 bool CollectionPlugin_FieldValidator::isValid(const FeaturePtr& theFeature,
29   const std::list<std::string>& theArguments,
30   Events_InfoMessage& theError) const
31 {
32   AttributeSelectionListPtr aSelList =
33     theFeature->selectionList(CollectionPlugin_Field::SELECTED_ID());
34   std::string aTypeStr = aSelList->selectionType();
35   if (aTypeStr == "part")
36     return true;
37   if (aSelList->isInitialized()) {
38     int aSize = aSelList->size();
39     bool aIsDefined = aSize > 0;
40     if (!aIsDefined)
41       theError = "Selection list is not initialized";
42     return aIsDefined;
43   }
44   theError = "Selection list is not initialized";
45   return false;
46 }
47
48 bool CollectionPlugin_GroupOperationAttributeValidator::isValid(
49     const AttributePtr& theAttribute,
50     const std::list<std::string>& theArguments,
51     Events_InfoMessage& theError) const
52 {
53   AttributeSelectionListPtr aSelList =
54       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
55   if (!aSelList) {
56     theError = "Error: This validator can only work with selection list of attributes";
57     return false;
58   }
59
60   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
61   // check selected items
62   std::string aType;
63   for (int anIndex = 0; anIndex < aSelList->size(); ++anIndex) {
64     AttributeSelectionPtr aCurSelection = aSelList->value(anIndex);
65     // applicable the groups only
66     ResultPtr aGroupResult = aCurSelection->context();
67     if (aGroupResult->groupName() != ModelAPI_ResultGroup::group()) {
68       theError = "Error: Groups can be selected only.";
69       return false;
70     }
71     // groups of same type can be selected only
72     FeaturePtr aGroupFeature = ModelAPI_Feature::feature(aGroupResult->data()->owner());
73     std::string aGroupType =
74         aGroupFeature->selectionList(CollectionPlugin_Group::LIST_ID())->selectionType();
75     if (aType.empty())
76       aType = aGroupType;
77     else if (aType != aGroupType) {
78       theError = "Error: Groups should have same type";
79       return false;
80     }
81   }
82   return true;
83 }