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