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