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