Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / GeomValidators / GeomValidators_BooleanArguments.cpp
1 // Copyright (C) 2014-2017  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<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include <GeomValidators_BooleanArguments.h>
21
22 #include <Events_InfoMessage.h>
23
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_ResultCompSolid.h>
27 #include <ModelAPI_Tools.h>
28
29 //=================================================================================================
30 bool GeomValidators_BooleanArguments::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
31                                               const std::list<std::string>& theArguments,
32                                               Events_InfoMessage& theError) const
33 {
34   if(theArguments.size() != 3) {
35     theError = "Wrong number of arguments (expected 3).";
36     return false;
37   }
38
39   int anObjectsNb = 0, aToolsNb = 0;
40   int anOperationType = 0;
41
42   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
43
44   bool isAllInSameCompSolid = true;
45   ResultCompSolidPtr aCompSolid;
46
47   AttributeSelectionListPtr anAttrSelList = theFeature->selectionList(*anIt);
48   if(anAttrSelList) {
49     anObjectsNb = anAttrSelList->size();
50     for(int anIndex = 0; anIndex < anObjectsNb; ++anIndex) {
51       AttributeSelectionPtr anAttr = anAttrSelList->value(anIndex);
52       ResultPtr aContext = anAttr->context();
53       ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
54       if(aResCompSolidPtr.get()) {
55         if(aCompSolid.get()) {
56           isAllInSameCompSolid = aCompSolid == aResCompSolidPtr;
57         } else {
58           aCompSolid = aResCompSolidPtr;
59         }
60       } else {
61         isAllInSameCompSolid = false;
62         break;
63       }
64     }
65   }
66   anIt++;
67
68
69   anAttrSelList = theFeature->selectionList(*anIt);
70   if(anAttrSelList) {
71     aToolsNb = anAttrSelList->size();
72     if(isAllInSameCompSolid) {
73       for(int anIndex = 0; anIndex < aToolsNb; ++anIndex) {
74         AttributeSelectionPtr anAttr = anAttrSelList->value(anIndex);
75         ResultPtr aContext = anAttr->context();
76         ResultCompSolidPtr aResCompSolidPtr = ModelAPI_Tools::compSolidOwner(aContext);
77         if(aResCompSolidPtr.get()) {
78           if(aCompSolid.get()) {
79             isAllInSameCompSolid = aCompSolid == aResCompSolidPtr;
80           } else {
81             aCompSolid = aResCompSolidPtr;
82           }
83         } else {
84           isAllInSameCompSolid = false;
85           break;
86         }
87       }
88     }
89   }
90   anIt++;
91
92   std::shared_ptr<ModelAPI_AttributeInteger> anAttrInt = theFeature->integer(*anIt);
93   if(anAttrInt) {
94     anOperationType = anAttrInt->value();
95   }
96
97   if(anOperationType == 1) {
98     // Fuse operation
99     if(anObjectsNb + aToolsNb < 2) {
100       theError = "Not enough arguments for Fuse operation.";
101       return false;
102     } else if(isAllInSameCompSolid) {
103       theError = "Operations only between sub-shapes of the same shape not allowed.";
104       return false;
105     }
106   } else {
107     if(anObjectsNb < 1) {
108       theError = "Objects not selected.";
109       return false;
110     }
111     if(aToolsNb < 1) {
112       theError = "Tools not selected.";
113       return false;
114     }
115     if(isAllInSameCompSolid) {
116       theError = "Operations only between sub-shapes of the same shape not allowed.";
117       return false;
118     }
119   }
120
121   return true;
122 }
123
124 //=================================================================================================
125 bool GeomValidators_BooleanArguments::isNotObligatory(std::string theFeature,
126                                                       std::string theAttribute)
127 {
128   if(theAttribute == "main_objects" || theAttribute == "tool_objects") {
129     return true;
130   }
131
132   return false;
133 }