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