Salome HOME
Using validator in multi selector control for the "type_choice" key.
[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_AttributeRefAttr.h>
12 #include <ModelAPI_AttributeSelectionList.h>
13 #include <ModelAPI_AttributeReference.h>
14
15 #include <Events_Error.h>
16
17 #include <string>
18 #include <map>
19
20
21 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
22 static EdgeTypes MyEdgeTypes;
23
24 GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType)
25 {
26   if (MyEdgeTypes.size() == 0) {
27     MyEdgeTypes["vertex"] = Vertex;
28     MyEdgeTypes["edge"]   = Edge;
29     MyEdgeTypes["line"]   = Line;
30     MyEdgeTypes["circle"] = Circle;
31     MyEdgeTypes["solid"] = Solid;
32     MyEdgeTypes["face"]  = Face;
33   }
34   std::string aType = std::string(theType.c_str());
35   if (MyEdgeTypes.find(aType) != MyEdgeTypes.end())
36     return MyEdgeTypes[aType];
37   
38   Events_Error::send("Shape type defined in XML is not implemented!");
39   return AnyShape;
40 }
41
42 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
43                                        const std::list<std::string>& theArguments) const
44 {
45   bool aValid = false;
46
47   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
48   for (; anIt != aLast && !aValid; anIt++) {
49     aValid = isValidArgument(theAttribute, *anIt);
50   }
51
52   return aValid;
53 }
54
55 bool GeomValidators_ShapeType::isValidArgument(const AttributePtr& theAttribute,
56                                                const std::string& theArgument) const
57 {
58   bool aValid = false;
59   TypeOfShape aShapeType = shapeType(theArgument);
60   if (aShapeType == AnyShape)
61     return true;
62
63   AttributeSelectionListPtr aListAttr = 
64            std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
65   if (aListAttr.get()) {
66     for (int i = 0; i < aListAttr->size(); i++) {
67       aValid = isValidAttribute(aListAttr->value(i), aShapeType);
68       if (!aValid) // if at least one attribute is invalid, the result is false
69         break;
70     }
71   }
72   else {
73     aValid = isValidAttribute(theAttribute, aShapeType);
74   }
75   return aValid;
76 }
77
78 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
79                                                 const TypeOfShape theShapeType) const
80 {
81   bool aValid = false;
82
83   std::string anAttributeType = theAttribute->attributeType();
84
85   if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
86     AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
87     GeomShapePtr aShape = anAttr->value();
88     if (aShape.get())
89       aValid = isValidShape(aShape, theShapeType);
90     else
91       aValid = isValidObject(anAttr->context(), theShapeType);
92   }
93   else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
94     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
95     if (anAttr.get() != NULL) {
96       if (anAttr->isObject()) {
97         aValid = isValidObject(anAttr->object(), theShapeType);
98       }
99       else if (theShapeType == Vertex) {
100         AttributePtr aRefAttr = anAttr->attr();
101         aValid = aRefAttr.get() != NULL && aRefAttr->attributeType() == GeomDataAPI_Point2D::typeId();
102       }
103     }
104   }
105   else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
106     AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
107     if (anAttr.get() != NULL)
108       aValid = isValidObject(anAttr->value(), theShapeType);
109   }
110   return aValid;
111 }
112
113 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
114                                              const TypeOfShape theShapeType) const
115 {
116   bool aValid = false;
117   if (theObject.get() != NULL) {
118     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
119     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
120     if (aResult.get() != NULL) {
121       GeomShapePtr aShape = aResult->shape();
122       aValid = isValidShape(aShape, theShapeType);
123     }
124   }
125   return aValid;
126 }
127
128 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
129                                             const TypeOfShape theShapeType) const
130 {
131   bool aValid = false;
132
133   if (theShape.get() != NULL) {
134     switch (theShapeType) {
135       case Edge:
136         aValid = theShape->isEdge();
137       break;
138       case Line:
139         aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
140       break;
141       case Circle:
142         aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
143       break;
144       case Vertex:
145         aValid = theShape->isVertex();
146       break;
147       case Solid:
148         aValid = theShape->isSolid();
149         break;
150       case Face:
151         aValid = theShape->isFace();
152         break;
153       default: break;
154     }
155   }
156   return aValid;
157 }