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