]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomValidators/GeomValidators_ShapeType.cpp
Salome HOME
[Code coverage GeomValidators]: Exclude "fool-tolerance" validations
[modules/shaper.git] / src / GeomValidators / GeomValidators_ShapeType.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_ShapeType.h"
22 #include "GeomValidators_Tools.h"
23
24 #include <GeomAPI_Curve.h>
25 #include <GeomAPI_ShapeIterator.h>
26 #include <GeomDataAPI_Point2D.h>
27
28 #include <ModelAPI_Result.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_ResultConstruction.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_AttributeSelectionList.h>
33 #include <ModelAPI_AttributeReference.h>
34
35 #include <Events_InfoMessage.h>
36
37 #include <string>
38 #include <map>
39
40
41 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
42
43 static EdgeTypes MyShapeTypes;
44 GeomValidators_ShapeType::TypeOfShape
45   GeomValidators_ShapeType::shapeType(const std::string& theType)
46 {
47   if (MyShapeTypes.size() == 0) {
48     MyShapeTypes["empty"]     = Empty;
49     MyShapeTypes["vertex"]    = Vertex;
50     MyShapeTypes["edge"]      = Edge;
51     MyShapeTypes["line"]      = Line;
52     MyShapeTypes["circle"]    = Circle;
53     MyShapeTypes["wire"]      = Wire;
54     MyShapeTypes["face"]      = Face;
55     MyShapeTypes["plane"]     = Plane;
56     MyShapeTypes["shell"]     = Shell;
57     MyShapeTypes["solid"]     = Solid;
58     MyShapeTypes["compsolid"] = CompSolid;
59     MyShapeTypes["compound"]  = Compound;
60   }
61   std::string aType = std::string(theType.c_str());
62   if (MyShapeTypes.find(aType) != MyShapeTypes.end())
63     return MyShapeTypes[aType];
64
65   Events_InfoMessage("Shape type defined in XML is not implemented!").send();
66   return AnyShape;
67 }
68
69 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
70 {
71   std::string aValue = "";
72
73   if (MyShapeTypes.size() != 0) {
74     std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
75       anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
76     for (; anIt != aLast; anIt++) {
77       if (anIt->second == theType)
78         aValue = anIt->first;
79         break;
80     }
81   }
82   return aValue;
83 }
84
85 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
86                                        const std::list<std::string>& theArguments,
87                                        Events_InfoMessage& theError) const
88 {
89   bool aValid = false;
90
91   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
92   // returns true if the attribute satisfies at least one of given arguments
93   for (; anIt != aLast; anIt++) {
94     TypeOfShape aShapeType = shapeType(*anIt);
95     // if arguments contain any shape type value, the validator returns true
96     if (aShapeType == AnyShape) {
97       aValid = true;
98       break;
99     }
100     if (isValidAttribute(theAttribute, aShapeType, theError)) {
101       aValid = true;
102       break;
103     }
104   }
105   if (!aValid && theError.empty()) {
106     std::string aTypes;
107     std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
108     // returns true if the attribute satisfies at least one of given arguments
109     for (; anIt != aLast; anIt++) {
110       if (!aTypes.empty())
111         aTypes += ", ";
112       aTypes += *anIt;
113     }
114     theError = "It does not contain element with acceptable shape type. "
115                "The type should be one of the next: %1";
116     theError.arg(aTypes);
117   }
118
119   return aValid;
120 }
121
122 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
123                                                 const TypeOfShape theShapeType,
124                                                 Events_InfoMessage& theError) const
125 {
126   bool aValid = true;
127
128   std::string anAttributeType = theAttribute->attributeType();
129   if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
130     AttributeSelectionPtr anAttr =
131       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
132     GeomShapePtr aShape = anAttr->value();
133     if (aShape.get())
134       aValid = isValidShape(aShape, theShapeType, anAttr->isGeometricalSelection(), theError);
135     else {
136       if (anAttr->context().get())
137         aValid = isValidObject(anAttr->context(),
138                                theShapeType,
139                                anAttr->isGeometricalSelection(),
140                                theError);
141       else
142         aValid = isValidObject(anAttr->contextFeature(),
143                                theShapeType,
144                                anAttr->isGeometricalSelection(),
145                                theError);
146     }
147   }
148   else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
149     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
150     if (anAttr->isObject()) {
151       aValid = isValidObject(anAttr->object(),
152                              theShapeType,
153                              false,
154                              theError);
155     }
156     else if (theShapeType == Vertex) {
157       AttributePtr aRefAttr = anAttr->attr();
158       if (!aRefAttr.get()){
159         aValid = false;
160         theError = "It has reference to an empty attribute";
161       }
162       else {
163         std::string anAttributeType = aRefAttr->attributeType();
164         aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
165         if (!aValid) {
166 // LCOV_EXCL_START
167           theError = "Shape type is \"%1\", it should be \"%2\"";
168           theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
169 // LCOV_EXCL_STOP
170         }
171       }
172     }
173   }
174   else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
175     AttributeReferencePtr anAttr =
176                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
177     aValid = isValidObject(anAttr->value(), theShapeType, false, theError);
178   }
179   else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
180     AttributeSelectionListPtr aListAttr =
181                           std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
182     // the Empty value means that the attribute selection list is valid if it is empty
183     if (aListAttr->size() == 0 && theShapeType == Empty) {
184       return true;
185     }
186     aValid = false; // the list should have elements if the shape type is not Empty
187     for (int i = 0; i < aListAttr->size(); i++) {
188       aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
189       if (!aValid) // if at least one attribute is invalid, the result is false
190         break;
191     }
192   }
193   else {
194 // LCOV_EXCL_START
195     aValid = false;
196     theError = "The attribute with the %1 type is not processed";
197     theError.arg(anAttributeType);
198 // LCOV_EXCL_STOP
199   }
200   if (aValid)
201     theError = "";
202   return aValid;
203 }
204
205 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
206                                              const TypeOfShape theShapeType,
207                                              const bool theIsGeometricalSelection,
208                                              Events_InfoMessage& theError) const
209 {
210   bool aValid = true;
211   if (!theObject.get()) {
212     if(theShapeType != Empty) {
213       aValid = false;
214       theError = "The object is empty";
215     }
216   }
217   else {
218     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
219     if (aResult.get()) {
220       if (theShapeType == Plane)
221       {
222         ResultConstructionPtr aResultConstruction =
223           std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
224         FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
225         const std::string& aKind = aFeature->getKind();
226         return aResult.get() != NULL && aKind == "Plane";
227       }
228       if (!aResult.get()) {
229         aValid = false;
230         theError = "The result is empty";
231       } else {
232         aValid = isValidShape(aResult->shape(), theShapeType, theIsGeometricalSelection, theError);
233       }
234     } else {
235       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
236       if (aFeature.get() && (theShapeType == CompSolid))
237         return aValid;
238       else {
239         aValid = false;
240         theError = "The feature has to produce a compsolid";
241       }
242     }
243   }
244   return aValid;
245 }
246
247 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
248                                             const TypeOfShape theShapeType,
249                                             const bool theIsGeometricalSelection,
250                                             Events_InfoMessage& theError) const
251 {
252   bool aValid = true;
253
254   if (!theShape.get()) {
255     aValid = false;
256     theError = "The shape is empty";
257   }
258   else {
259     switch (theShapeType) {
260     case Vertex:
261       aValid = theShape->isVertex();
262       break;
263     case Edge:
264       aValid = theShape->isEdge();
265       break;
266     case Line: {
267       if (theIsGeometricalSelection && theShape->isCompound()) {
268         aValid = true;
269         for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
270           if (!anIt.current()->isEdge() || !GeomAPI_Curve(anIt.current()).isLine()) {
271             aValid = false;
272             break;
273           }
274         }
275       }
276       else {
277         aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine();
278       }
279       break;
280     }
281     case Circle:
282       aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
283       break;
284     case Wire:
285       aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
286       break;
287     case Face:
288       aValid = theShape->isFace();
289       break;
290     case Shell:
291       aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
292       break;
293     case Solid:
294       aValid = theShape->isSolid() || theShape->isCompSolid() ||
295                theShape->isCompoundOfSolids();
296       break;
297     case CompSolid:
298       aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
299       break;
300     case Compound:
301       aValid = theShape->isCompound();
302       break;
303     default:
304       aValid = false;
305       break;
306     }
307   }
308   return aValid;
309 }