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