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