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