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