Salome HOME
Issue #1834: Fix length of lines
[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     }
94     theError = "It does not contain element with acceptable shape type. \
95  The type should be one of the next: %1";
96     theError.arg(aTypes);
97   }
98
99   return aValid;
100 }
101
102 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
103                                                 const TypeOfShape theShapeType,
104                                                 Events_InfoMessage& theError) const
105 {
106   bool aValid = true;
107
108   std::string anAttributeType = theAttribute->attributeType();
109   if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
110     AttributeSelectionPtr anAttr = 
111       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
112     GeomShapePtr aShape = anAttr->value();
113     if (aShape.get())
114       aValid = isValidShape(aShape, theShapeType, theError);
115     else
116       aValid = isValidObject(anAttr->context(), theShapeType, theError);
117   }
118   else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
119     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
120     if (anAttr->isObject()) {
121       aValid = isValidObject(anAttr->object(), theShapeType, theError);
122     }
123     else if (theShapeType == Vertex) {
124       AttributePtr aRefAttr = anAttr->attr();
125       if (!aRefAttr.get()){
126         aValid = false;
127         theError = "It has reference to an empty attribute";
128       }
129       else {
130         std::string anAttributeType = aRefAttr->attributeType();
131         aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
132         if (!aValid) {
133           theError = "Shape type is \"%1\", it should be \"%2\"";
134           theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
135         }
136       }
137     }
138   }
139   else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
140     AttributeReferencePtr anAttr =
141                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
142     aValid = isValidObject(anAttr->value(), theShapeType, theError);
143   }
144   else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
145     AttributeSelectionListPtr aListAttr =
146                           std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
147     // the Empty value means that the attribute selection list is valid if it is empty
148     if (aListAttr->size() == 0 && theShapeType == Empty) {
149       return true;
150     }
151     aValid = false; // the list should have elements if the shape type is not Empty
152     for (int i = 0; i < aListAttr->size(); i++) {
153       aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
154       if (!aValid) // if at least one attribute is invalid, the result is false
155         break;
156     }
157   }
158   else {
159     aValid = false;
160     theError = "The attribute with the %1 type is not processed";
161     theError.arg(anAttributeType);
162   }
163   return aValid;
164 }
165
166 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
167                                              const TypeOfShape theShapeType,
168                                              Events_InfoMessage& theError) const
169 {
170   bool aValid = true;
171   if (!theObject.get()) {
172     if(theShapeType != Empty) {
173       aValid = false;
174       theError = "The object is empty";
175     }
176   }
177   else {
178     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
179     if( theShapeType==Plane )
180     {
181       ResultConstructionPtr aResultConstruction = 
182         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
183       FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
184       const std::string& aKind = aFeature->getKind();
185       return aResult.get() != NULL && aKind == "Plane";
186     }
187     if (!aResult.get()) {
188       aValid = false;
189       theError = "The result is empty";
190     }
191     else {
192       aValid = isValidShape(aResult->shape(), theShapeType, theError);
193     }
194   }
195   return aValid;
196 }
197
198 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
199                                             const TypeOfShape theShapeType,
200                                             Events_InfoMessage& theError) const
201 {
202   bool aValid = true;
203
204   if (!theShape.get()) {
205     aValid = false;
206     theError = "The shape is empty";
207   }
208   else {
209     switch (theShapeType) {
210     case Vertex:
211       aValid = theShape->isVertex();
212       break;
213     case Edge:
214       aValid = theShape->isEdge();
215       break;
216     case Line:
217       aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
218       break;
219     case Circle:
220       aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
221       break;
222     case Wire:
223       aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
224       break;
225     case Face:
226       aValid = theShape->isFace();
227       break;
228     case Shell:
229       aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
230       break;
231     case Solid:
232       aValid = theShape->isSolid() || theShape->isCompSolid() ||
233                theShape->isCompoundOfSolids();
234       break;
235     case CompSolid:
236       aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
237       break;
238     case Compound:
239       aValid = theShape->isCompound();
240       break;
241     default:
242       aValid = false;
243       break;
244     }
245   }
246   return aValid;
247 }