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