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