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