Salome HOME
Issue #2535: Intersection - Include into sketch result does not work
[modules/shaper.git] / src / GeomValidators / GeomValidators_ZeroOffset.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <GeomValidators_ZeroOffset.h>
22
23 #include <Events_InfoMessage.h>
24
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_ResultConstruction.h>
30
31 #include <GeomAPI_Dir.h>
32 #include <GeomAPI_Face.h>
33 #include <GeomAPI_Shape.h>
34 #include <GeomAPI_Pln.h>
35 #include <GeomAPI_Pnt.h>
36
37 //=================================================================================================
38 bool GeomValidators_ZeroOffset::isValid(const std::shared_ptr<ModelAPI_Feature>& theFeature,
39                                         const std::list<std::string>& theArguments,
40                                         Events_InfoMessage& theError) const
41 {
42   if(theArguments.size() != 9) {
43     theError = "Wrong number of validator arguments in xml(expected 9).";
44     return false;
45   }
46
47   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
48
49   std::string aSelectedMethod;
50   if(theFeature->string(*anIt)) {
51     aSelectedMethod = theFeature->string(*anIt)->value();
52   }
53   anIt++;
54   std::string aCreationMethod = *anIt;
55   anIt++;
56
57   ListOfShape aFacesList;
58   if(theFeature->selection(*anIt)) {
59     AttributeSelectionPtr aFaceSelection = theFeature->selection(*anIt);
60     ResultConstructionPtr aConstruction =
61       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFaceSelection->context());
62     if(aConstruction.get()) {
63       int aSketchFacesNum = aConstruction->facesNum();
64       for(int aFaceIndex = 0; aFaceIndex < aSketchFacesNum; aFaceIndex++) {
65         std::shared_ptr<GeomAPI_Shape> aFace =
66           std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
67         if(aFace->isFace() && aFace->isPlanar()) {
68           aFacesList.push_back(aFace);
69         }
70       }
71     }
72   } else if(theFeature->selectionList(*anIt)) {
73     AttributeSelectionListPtr aFacesSelectionList = theFeature->selectionList(*anIt);
74     for(int anIndex = 0; anIndex < aFacesSelectionList->size(); anIndex++) {
75       AttributeSelectionPtr aFaceSel = aFacesSelectionList->value(anIndex);
76       std::shared_ptr<GeomAPI_Shape> aFaceShape = aFaceSel->value();
77       if(aFaceShape.get() && !aFaceShape->isNull()) { // Getting face.
78         if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
79           aFacesList.push_back(aFaceShape);
80         }
81       } else { // This may be the whole sketch result selected, check and get faces.
82         ResultPtr aContext = aFaceSel->context();
83         std::shared_ptr<GeomAPI_Shape> aContextShape = aContext->shape();
84         if(!aContextShape.get()) {
85           break;
86         }
87         ResultConstructionPtr aConstruction =
88           std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
89         if(!aConstruction.get()) {
90           break;
91         }
92         int aFacesNum = aConstruction->facesNum();
93         for(int aFaceIndex = 0; aFaceIndex < aFacesNum || aFacesNum == -1; aFaceIndex++) {
94           aFaceShape = std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
95           if(aFaceShape->isFace() && aFaceShape->isPlanar()) {
96             aFacesList.push_back(aFaceShape);
97           }
98         }
99       }
100     }
101   }
102   anIt++;
103
104   double aToSize = 0.0;
105   double aFromSize = 0.0;
106
107   if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
108     aToSize = theFeature->real(*anIt)->value();
109   }
110   anIt++;
111   if(theFeature->real(*anIt) && theFeature->real(*anIt)->isInitialized()) {
112     aFromSize = theFeature->real(*anIt)->value();
113   }
114   anIt++;
115
116   if(aSelectedMethod == aCreationMethod) {
117     if(aToSize == -aFromSize) {
118       theError = "ToSize = -FromSize.";
119       return false;
120     } else {
121       return true;
122     }
123   }
124
125   std::shared_ptr<GeomAPI_Shape> aToShape;
126   std::shared_ptr<GeomAPI_Shape> aFromShape;
127
128   std::shared_ptr<ModelAPI_AttributeSelection> anAttrSel = theFeature->selection(*anIt);
129   if(anAttrSel && anAttrSel->isInitialized()) {
130     aToShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
131     if(aToShape.get() == NULL && anAttrSel->context().get() != NULL) {
132       aToShape =  anAttrSel->context()->shape();
133     }
134   }
135   anIt++;
136
137   std::shared_ptr<ModelAPI_AttributeDouble> anAttrDouble = theFeature->real(*anIt);
138   if(anAttrDouble && anAttrDouble->isInitialized()) {
139     aToSize = anAttrDouble->value();
140   }
141   anIt++;
142
143   anAttrSel = theFeature->selection(*anIt);
144   if(anAttrSel && anAttrSel->isInitialized()) {
145     aFromShape = std::dynamic_pointer_cast<GeomAPI_Shape>(anAttrSel->value());
146     if(aFromShape.get() == NULL && anAttrSel->context().get() != NULL) {
147       aFromShape = anAttrSel->context()->shape();
148     }
149   }
150   anIt++;
151
152   anAttrDouble = theFeature->real(*anIt);
153   if(anAttrDouble && anAttrDouble->isInitialized()) {
154     aFromSize = anAttrDouble->value();
155   }
156
157   bool isPlanesCoincident = false;
158   if(!aFromShape.get() && !aToShape.get()) {
159     isPlanesCoincident = true;
160   } else if(aFromShape.get() && aToShape.get()) {
161     std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aFromShape));
162     if (aFromFace->isNull()) {
163       theError = "From face selection is invalid.";
164       return false;
165     }
166     std::shared_ptr<GeomAPI_Pln>  aFromPln = aFromFace->getPlane();
167
168     std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aToShape));
169     if (aToFace->isNull()) {
170       theError = "To face selection is invalid.";
171       return false;
172     }
173     std::shared_ptr<GeomAPI_Pln>  aToPln = aToFace->getPlane();
174
175     if(aFromPln.get()) {
176       isPlanesCoincident = aFromPln->isCoincident(aToPln);
177     }
178   } else {
179     std::shared_ptr<GeomAPI_Face> aFace;
180     if(aFromShape.get()) {
181       aFace.reset(new GeomAPI_Face(aFromShape));
182       if (aFace->isNull()) {
183         theError = "From face selection is invalid.";
184         return false;
185       }
186     } else {
187       aFace.reset(new GeomAPI_Face(aToShape));
188       if (aFace->isNull()) {
189         theError = "To face selection is invalid.";
190         return false;
191       }
192     }
193     std::shared_ptr<GeomAPI_Pln> aPln = aFace->getPlane();
194     if(aPln.get()) {
195       for(ListOfShape::const_iterator
196           anIter = aFacesList.cbegin(); anIter != aFacesList.cend(); anIter++) {
197         std::shared_ptr<GeomAPI_Shape> aSketchShape = *anIter;
198         std::shared_ptr<GeomAPI_Face> aSketchFace(new GeomAPI_Face(aSketchShape));
199         std::shared_ptr<GeomAPI_Pln>  aSketchPln = aSketchFace->getPlane();
200         if(aPln->isCoincident(aSketchPln)) {
201           isPlanesCoincident = true;
202           break;
203         }
204       }
205     }
206   }
207
208   if(isPlanesCoincident && aFromSize == -aToSize) {
209     theError = "FromSize = -ToSize and bounding planes are coincident.";
210     return false;
211   }
212
213   return true;
214 }
215
216 //=================================================================================================
217 bool GeomValidators_ZeroOffset::isNotObligatory(std::string theFeature, std::string theAttribute)
218 {
219   if(theAttribute == "from_object" || theAttribute == "to_object") {
220     return true;
221   }
222
223   return false;
224 }