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