Salome HOME
Merge Dev_2.1.0 with PythonAPI branch
[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         aFacesList.push_back(aFace);
50       }
51     }
52   } else if(theFeature->selectionList(*anIt)) {
53     AttributeSelectionListPtr aFacesSelectionList = theFeature->selectionList(*anIt);
54     for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
55       AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
56       std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
57       if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
58         aFacesList.push_back(aFaceShape);
59       } else { // This may be the whole sketch result selected, check and get faces.
60         ResultPtr aContext = aFaceSel->context();
61         std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
62         if(!aContextShape.get()) {
63           break;
64         }
65         ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
66         if(!aConstruction.get()) {
67           break;
68         }
69         int aFacesNum = aConstruction->facesNum();
70         for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
71           aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
72           aFacesList.push_back(aFaceShape);
73         }
74       }
75     }
76   }
77   anIt++;
78
79   double aToSize = 0.0;
80   double aFromSize = 0.0;
81
82   if(theFeature->real(*anIt)) {
83     aToSize = theFeature->real(*anIt)->value();
84   }
85   anIt++;
86   if(theFeature->real(*anIt)) {
87     aFromSize = theFeature->real(*anIt)->value();
88   }
89   anIt++;
90
91   if(aSelectedMethod == aCreationMethod) {
92     if(aToSize == -aFromSize) {
93       theError = "ToSize = -FromSize.";
94       return false;
95     } else {
96       return true;
97     }
98   }
99
100   std::shared_ptr<GeomAPI_Shape> aToShape;
101   std::shared_ptr<GeomAPI_Shape> aFromShape;
102
103   std::shared_ptr<ModelAPI_AttributeSelection> anAttrSel = theFeature->selection(*anIt);
104   if(anAttrSel) {
105     aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
106     if(aToShape.get() == NULL && anAttrSel->context().get() != NULL) {
107       aToShape =  anAttrSel->context()->shape();
108     }
109   }
110   anIt++;
111
112   std::shared_ptr<ModelAPI_AttributeDouble> anAttrDouble = theFeature->real(*anIt);
113   if(anAttrDouble) {
114     aToSize = anAttrDouble->value();
115   }
116   anIt++;
117
118   anAttrSel = theFeature->selection(*anIt);
119   if(anAttrSel) {
120     aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
121     if(aFromShape.get() == NULL && anAttrSel->context().get() != NULL) {
122       aFromShape = anAttrSel->context()->shape();
123     }
124   }
125   anIt++;
126
127   anAttrDouble = theFeature->real(*anIt);
128   if(anAttrDouble) {
129     aFromSize = anAttrDouble->value();
130   }
131
132   bool isPlanesCoincident = false;
133   if(!aFromShape.get() && !aToShape.get()) {
134     isPlanesCoincident = true;
135   } else if(aFromShape.get() && aToShape.get()) {
136     std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
137     std::shared_ptr<GeomAPI_Pln>  aFromPln = aFromFace->getPlane();
138
139     std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
140     std::shared_ptr<GeomAPI_Pln>  aToPln = aToFace->getPlane();
141
142     if(aFromPln.get()) {
143       isPlanesCoincident = aFromPln->isCoincident(aToPln);
144     }
145   } else {
146     std::shared_ptr<GeomAPI_Face> aFace;
147     if(aFromShape.get()) {
148       aFace.reset(new GeomAPI_Face(aFromShape));
149     } else {
150       aFace.reset(new GeomAPI_Face(aToShape));
151     }
152     std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
153     if(aPln.get()) {
154       for(ListOfShape::const_iterator anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
155         std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
156         std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
157         std::shared_ptr<GeomAPI_Pln>  aSketchPln = aSketchFace->getPlane();
158         if(aPln->isCoincident(aSketchPln)) {
159           isPlanesCoincident = true;
160           break;
161         }
162       }
163     }
164   }
165
166   if(isPlanesCoincident && aFromSize == -aToSize) {
167     theError = "FromSize = -ToSize and bounding planes are coincident.";
168     return false;
169   }
170
171   return true;
172 }
173
174 //=================================================================================================
175 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
176 {
177   if(theAttribute == "from_object" || theAttribute == "to_object") {
178     return true;
179   }
180
181   return false;
182 }