Salome HOME
It provides error messages for validators.
[modules/shaper.git] / src / GeomValidators / GeomValidators_ShapeType.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3
4 #include "GeomValidators_ShapeType.h"
5 #include "GeomValidators_Tools.h"
6
7 #include <GeomAPI_Curve.h>
8 #include <GeomDataAPI_Point2D.h>
9
10 #include <ModelAPI_Result.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <ModelAPI_AttributeRefAttr.h>
13 #include <ModelAPI_AttributeSelectionList.h>
14 #include <ModelAPI_AttributeReference.h>
15
16 #include <Events_Error.h>
17
18 #include <string>
19 #include <map>
20
21
22 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
23
24 static EdgeTypes MyShapeTypes;
25 GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType)
26 {
27   if (MyShapeTypes.size() == 0) {
28     MyShapeTypes["empty"]  = Empty;
29     MyShapeTypes["vertex"] = Vertex;
30     MyShapeTypes["edge"]   = Edge;
31     MyShapeTypes["line"]   = Line;
32     MyShapeTypes["circle"] = Circle;
33     MyShapeTypes["face"]   = Face;
34     MyShapeTypes["solid"]  = Solid;
35     MyShapeTypes["plane"]  = Plane;
36   }
37   std::string aType = std::string(theType.c_str());
38   if (MyShapeTypes.find(aType) != MyShapeTypes.end())
39     return MyShapeTypes[aType];
40   
41   Events_Error::send("Shape type defined in XML is not implemented!");
42   return AnyShape;
43 }
44
45 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
46 {
47   std::string aValue = "";
48
49   if (MyShapeTypes.size() != 0) {
50     std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator anIt = MyShapeTypes.begin(),
51                                                                                  aLast = MyShapeTypes.end();
52     for (; anIt != aLast; anIt++) {
53       if (anIt->second == theType)
54         aValue = anIt->first;
55         break;
56     }
57   }
58   return aValue;
59 }
60
61 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
62                                        const std::list<std::string>& theArguments,
63                                        std::string& theError) const
64 {
65   bool aValid = false;
66
67   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
68   // returns true if the attribute satisfies at least one of given arguments
69   for (; anIt != aLast; anIt++) {
70     TypeOfShape aShapeType = shapeType(*anIt);
71     // if arguments contain any shape type value, the validator returns true
72     if (aShapeType == AnyShape) {
73       aValid = true;
74       break;
75     }
76     if (isValidAttribute(theAttribute, aShapeType, theError)) {
77       aValid = true;
78       break;
79     }
80   }
81   if (!aValid && theError.empty()) {
82     std::string aTypes;
83     std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
84     // returns true if the attribute satisfies at least one of given arguments
85     for (; anIt != aLast; anIt++) {
86       if (!aTypes.empty())
87         aTypes += ", ";
88     }
89     theError = "It does not contain element with acceptable shape type. The type should be one of the next: "
90                + aTypes;
91   }
92
93   return aValid;
94 }
95
96 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
97                                                 const TypeOfShape theShapeType,
98                                                 std::string& theError) const
99 {
100   bool aValid = true;
101
102   std::string anAttributeType = theAttribute->attributeType();
103   if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
104     AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
105     GeomShapePtr aShape = anAttr->value();
106     if (aShape.get())
107       aValid = isValidShape(aShape, theShapeType, theError);
108     else
109       aValid = isValidObject(anAttr->context(), theShapeType, theError);
110   }
111   else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
112     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
113     if (anAttr->isObject()) {
114       aValid = isValidObject(anAttr->object(), theShapeType, theError);
115     }
116     else if (theShapeType == Vertex) {
117       AttributePtr aRefAttr = anAttr->attr();
118       if (!aRefAttr.get()){
119         aValid = false;
120         theError = "It has reference to an empty attribute";
121       }
122       else {
123         std::string anAttributeType = aRefAttr->attributeType();
124         aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
125         if (!aValid)
126           theError = "Shape type is \"" + anAttributeType +
127                      "\", it should be \"" + getShapeTypeDescription(theShapeType) + "\"";
128       }
129     }
130   }
131   else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
132     AttributeReferencePtr anAttr =
133                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
134     aValid = isValidObject(anAttr->value(), theShapeType, theError);
135   }
136   else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
137     AttributeSelectionListPtr aListAttr =
138                           std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
139     // the Empty value means that the attribute selection list is valid if it is empty
140     if (aListAttr->size() == 0 && theShapeType == Empty) {
141       return true;
142     }
143     aValid = false; // the list should have elements if the shape type is not Empty
144     for (int i = 0; i < aListAttr->size(); i++) {
145       aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
146       if (!aValid) // if at least one attribute is invalid, the result is false
147         break;
148     }
149   }
150   else {
151     aValid = false;
152     theError = "The attribute with the " + anAttributeType  + " type is not processed";
153   }
154   return aValid;
155 }
156
157 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
158                                              const TypeOfShape theShapeType,
159                                              std::string& theError) const
160 {
161   bool aValid = true;
162   if (!theObject.get()) {
163     aValid = false;
164     theError = "The object is empty";
165   }
166   else {
167     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
168     if( theShapeType==Plane )
169     {
170       ResultConstructionPtr aResultConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
171       FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
172       const std::string& aKind = aFeature->getKind();
173       return aResult.get() != NULL && aKind == "Plane";
174     }
175     if (!aResult.get()) {
176       aValid = false;
177       theError = "The result is empty";
178     }
179     else {
180       aValid = isValidShape(aResult->shape(), theShapeType, theError);
181     }
182   }
183   return aValid;
184 }
185
186 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
187                                             const TypeOfShape theShapeType,
188                                             std::string& theError) const
189 {
190   bool aValid = true;
191
192   if (!theShape.get()) {
193     aValid = false;
194     theError = "The shape is empty";
195   }
196   else {
197     switch (theShapeType) {
198     case Edge:
199       aValid = theShape->isEdge();
200       break;
201       case Line:
202         aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
203       break;
204       case Circle:
205         aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
206       break;
207       case Vertex:
208         aValid = theShape->isVertex();
209       break;
210       case Solid:
211         aValid = theShape->isSolid() || theShape->isCompSolid() ||
212                  theShape->isCompoundOfSolids();
213         break;
214       case Face:
215         aValid = theShape->isFace();
216         break;
217       case Compound:
218         aValid = theShape->isCompound();
219         break;
220       default:
221         aValid = false;
222         break;
223     }
224   }
225   return aValid;
226 }