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