Salome HOME
[Code coverage GeomValidators]: Exclude "fool-tolerance" validations
[modules/shaper.git] / src / GeomValidators / GeomValidators_ZeroOffset.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_ZeroOffset.h>
22
23 #include <Events_InfoMessage.h>
24
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_ResultConstruction.h>
30
31 #include <GeomAPI_Dir.h>
32 #include <GeomAPI_Face.h>
33 #include <GeomAPI_Shape.h>
34 #include <GeomAPI_ShapeIterator.h>
35 #include <GeomAPI_Pln.h>
36 #include <GeomAPI_Pnt.h>
37
38 //=================================================================================================
39 bool GeomValidators_ZeroOffset::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
40                                         const std::list<std::string>& theArguments,
41                                         Events_InfoMessage& theError) const
42 {
43 // LCOV_EXCL_START
44   if(theArguments.size() != 9) {
45     theError = "Wrong number of validator arguments in xml(expected 9).";
46     return false;
47   }
48 // LCOV_EXCL_STOP
49
50   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
51
52   std::string aSelectedMethod;
53   if(theFeature->string(*anIt)) {
54     aSelectedMethod = theFeature->string(*anIt)->value();
55   }
56   anIt++;
57   std::string aCreationMethod = *anIt;
58   anIt++;
59
60   ListOfShape aFacesList;
61   if(theFeature->selection(*anIt)) {
62     AttributeSelectionPtr aFaceSelection = theFeature->selection(*anIt);
63     ResultConstructionPtr aConstruction =
64       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFaceSelection->context());
65     if(aConstruction.get()) {
66       int aSketchFacesNum = aConstruction->facesNum();
67       for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
68         std::shared_ptr<GeomAPI_Shape> aFace =
69           std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
70         if(aFace->isFace() && aFace->isPlanar()) {
71           aFacesList.push_back(aFace);
72         }
73       }
74     }
75   } else if(theFeature->selectionList(*anIt)) {
76     AttributeSelectionListPtr aFacesSelectionList = theFeature->selectionList(*anIt);
77     for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
78       AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
79       std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
80       if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
81         if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
82           aFacesList.push_back(aFaceShape);
83         }
84       } else { // This may be the whole sketch result selected, check and get faces.
85         ResultPtr aContext = aFaceSel->context();
86         std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
87         if(!aContextShape.get()) {
88           break;
89         }
90         ResultConstructionPtr aConstruction =
91           std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
92         if(!aConstruction.get()) {
93           break;
94         }
95         int aFacesNum = aConstruction->facesNum();
96         for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
97           aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
98           if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
99             aFacesList.push_back(aFaceShape);
100           }
101         }
102       }
103     }
104   }
105   anIt++;
106
107   double aToSize = 0.0;
108   double aFromSize = 0.0;
109
110   if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
111     aToSize = theFeature->real(*anIt)->value();
112   }
113   anIt++;
114   if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
115     aFromSize = theFeature->real(*anIt)->value();
116   }
117   anIt++;
118
119   if(aSelectedMethod == aCreationMethod) {
120     if(aToSize == -aFromSize) {
121       theError = "ToSize = -FromSize.";
122       return false;
123     } else {
124       return true;
125     }
126   }
127
128   std::shared_ptr<GeomAPI_Shape> aToShape;
129   std::shared_ptr<GeomAPI_Shape> aFromShape;
130
131   std::shared_ptr<ModelAPI_AttributeSelection> anAttrSel = theFeature->selection(*anIt);
132   if(anAttrSel && anAttrSel->isInitialized()) {
133     aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
134     if(aToShape.get() == NULL && anAttrSel->context().get() != NULL) {
135       aToShape =  anAttrSel->context()->shape();
136     }
137     if (aToShape->isCompound()) {
138       GeomAPI_ShapeIterator anIt(aToShape);
139       aToShape = anIt.current();
140     }
141   }
142   anIt++;
143
144   std::shared_ptr<ModelAPI_AttributeDouble> anAttrDouble = theFeature->real(*anIt);
145   if(anAttrDouble && anAttrDouble->isInitialized()) {
146     aToSize = anAttrDouble->value();
147   }
148   anIt++;
149
150   anAttrSel = theFeature->selection(*anIt);
151   if(anAttrSel && anAttrSel->isInitialized()) {
152     aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
153     if(aFromShape.get() == NULL && anAttrSel->context().get() != NULL) {
154       aFromShape = anAttrSel->context()->shape();
155     }
156     if (aFromShape->isCompound()) {
157       GeomAPI_ShapeIterator anIt(aFromShape);
158       aFromShape = anIt.current();
159     }
160   }
161   anIt++;
162
163   anAttrDouble = theFeature->real(*anIt);
164   if(anAttrDouble && anAttrDouble->isInitialized()) {
165     aFromSize = anAttrDouble->value();
166   }
167
168   bool isPlanesCoincident = false;
169   if(!aFromShape.get() && !aToShape.get()) {
170     isPlanesCoincident = true;
171   } else if(aFromShape.get() && aToShape.get()) {
172     std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
173     if (aFromFace->isNull()) {
174       theError = "From face selection is invalid.";
175       return false;
176     }
177     std::shared_ptr<GeomAPI_Pln>  aFromPln = aFromFace->getPlane();
178
179     std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
180     if (aToFace->isNull()) {
181       theError = "To face selection is invalid.";
182       return false;
183     }
184     std::shared_ptr<GeomAPI_Pln>  aToPln = aToFace->getPlane();
185
186     if(aFromPln.get()) {
187       isPlanesCoincident = aFromPln->isCoincident(aToPln);
188     }
189   } else {
190     std::shared_ptr<GeomAPI_Face> aFace;
191     if(aFromShape.get()) {
192       aFace.reset(new GeomAPI_Face(aFromShape));
193       if (aFace->isNull()) {
194         theError = "From face selection is invalid.";
195         return false;
196       }
197     } else {
198       aFace.reset(new GeomAPI_Face(aToShape));
199       if (aFace->isNull()) {
200         theError = "To face selection is invalid.";
201         return false;
202       }
203     }
204     std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
205     if(aPln.get()) {
206       for(ListOfShape::const_iterator
207           anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
208         std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
209         std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
210         std::shared_ptr<GeomAPI_Pln>  aSketchPln = aSketchFace->getPlane();
211         if(aPln->isCoincident(aSketchPln)) {
212           isPlanesCoincident = true;
213           break;
214         }
215       }
216     }
217   }
218
219   if(isPlanesCoincident && aFromSize == -aToSize) {
220     theError = "FromSize = -ToSize and bounding planes are coincident.";
221     return false;
222   }
223
224   return true;
225 }
226
227 //=================================================================================================
228 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
229 {
230   if(theAttribute == "from_object" || theAttribute == "to_object") {
231     return true;
232   }
233
234   return false;
235 }