]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomValidators/GeomValidators_ZeroOffset.cpp
Salome HOME
Compilation fix. Validator fix.
[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     std::shared_ptr<GeomAPI_Pln>  aFromPln = aFromFace->getPlane();
144
145     std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
146     std::shared_ptr<GeomAPI_Pln>  aToPln = aToFace->getPlane();
147
148     if(aFromPln.get()) {
149       isPlanesCoincident = aFromPln->isCoincident(aToPln);
150     }
151   } else {
152     std::shared_ptr<GeomAPI_Face> aFace;
153     if(aFromShape.get()) {
154       aFace.reset(new GeomAPI_Face(aFromShape));
155     } else {
156       aFace.reset(new GeomAPI_Face(aToShape));
157     }
158     std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
159     if(aPln.get()) {
160       for(ListOfShape::const_iterator anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
161         std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
162         std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
163         std::shared_ptr<GeomAPI_Pln>  aSketchPln = aSketchFace->getPlane();
164         if(aPln->isCoincident(aSketchPln)) {
165           isPlanesCoincident = true;
166           break;
167         }
168       }
169     }
170   }
171
172   if(isPlanesCoincident && aFromSize == -aToSize) {
173     theError = "FromSize = -ToSize and bounding planes are coincident.";
174     return false;
175   }
176
177   return true;
178 }
179
180 //=================================================================================================
181 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
182 {
183   if(theAttribute == "from_object" || theAttribute == "to_object") {
184     return true;
185   }
186
187   return false;
188 }