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