Salome HOME
Rigid constraint should process selection of vertex, lines and circles. So, a new...
[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
13 #include <Events_Error.h>
14
15 #include <string>
16 #include <map>
17
18
19 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
20 static EdgeTypes MyEdgeTypes;
21
22 GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType)
23 {
24   if (MyEdgeTypes.size() == 0) {
25     MyEdgeTypes["vertex"] = Vertex;
26     MyEdgeTypes["line"]   = Line;
27     MyEdgeTypes["circle"] = Circle;
28   }
29   std::string aType = std::string(theType.c_str());
30   if (MyEdgeTypes.find(aType) != MyEdgeTypes.end())
31     return MyEdgeTypes[aType];
32   
33   Events_Error::send("Edge type defined in XML is not implemented!");
34   return AnyShape;
35 }
36
37 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
38                                        const std::list<std::string>& theArguments) const
39 {
40   bool aValid = false;
41
42   std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
43   for (; anIt != aLast && !aValid; anIt++) {
44     aValid = isValidArgument(theAttribute, *anIt);
45   }
46
47   return aValid;
48 }
49
50 bool GeomValidators_ShapeType::isValidArgument(const AttributePtr& theAttribute,
51                                                const std::string& theArgument) const
52 {
53   bool aValid = false;
54   TypeOfShape aShapeType = shapeType(theArgument);
55   if (aShapeType == AnyShape)
56     return true;
57
58   ObjectPtr anObject = GeomValidators_Tools::getObject(theAttribute);
59   if (anObject.get() != NULL) {
60     FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
61     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
62     if (aResult.get() != NULL) {
63       GeomShapePtr aShape = aResult->shape();
64       if (aShape.get() != NULL) {
65         switch (aShapeType) {
66           case Line:
67             aValid = aShape->isEdge() && !GeomAPI_Curve(aShape).isCircle();
68           break;
69           case Circle:
70             aValid = aShape->isEdge() && GeomAPI_Curve(aShape).isCircle();
71           break;
72           case Vertex:
73             aValid = aShape->isVertex();
74           break;
75           default: break;
76         }
77       }
78     }
79   }
80   else {
81     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
82     if (anAttr.get() != NULL) {
83       if (aShapeType == Vertex) {
84         AttributePtr aRefAttr = anAttr->attr();
85         aValid = aRefAttr.get() != NULL && aRefAttr->attributeType() == GeomDataAPI_Point2D::typeId();
86       }
87     }
88   }
89   return aValid;
90 }