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