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