1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 #include "GeomValidators_ShapeType.h"
5 #include "GeomValidators_Tools.h"
7 #include <GeomAPI_Curve.h>
8 #include <GeomDataAPI_Point2D.h>
10 #include <ModelAPI_Result.h>
11 #include <ModelAPI_AttributeRefAttr.h>
12 #include <ModelAPI_AttributeSelectionList.h>
13 #include <ModelAPI_AttributeReference.h>
15 #include <Events_Error.h>
21 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
23 GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType)
25 static EdgeTypes MyShapeTypes;
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;
36 std::string aType = std::string(theType.c_str());
37 if (MyShapeTypes.find(aType) != MyShapeTypes.end())
38 return MyShapeTypes[aType];
40 Events_Error::send("Shape type defined in XML is not implemented!");
44 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
45 const std::list<std::string>& theArguments) const
49 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
50 for (; anIt != aLast && !aValid; anIt++) {
51 aValid = isValidArgument(theAttribute, *anIt);
57 bool GeomValidators_ShapeType::isValidArgument(const AttributePtr& theAttribute,
58 const std::string& theArgument) const
61 TypeOfShape aShapeType = shapeType(theArgument);
62 if (aShapeType == AnyShape)
65 AttributeSelectionListPtr aListAttr =
66 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
67 if (aListAttr.get()) {
68 if(aListAttr->size() == 0 && shapeType(theArgument) == Empty) {
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
78 aValid = isValidAttribute(theAttribute, aShapeType);
83 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
84 const TypeOfShape theShapeType) const
88 std::string anAttributeType = theAttribute->attributeType();
90 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
91 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
92 GeomShapePtr aShape = anAttr->value();
94 aValid = isValidShape(aShape, theShapeType);
96 aValid = isValidObject(anAttr->context(), theShapeType);
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);
104 else if (theShapeType == Vertex) {
105 AttributePtr aRefAttr = anAttr->attr();
106 aValid = aRefAttr.get() != NULL && aRefAttr->attributeType() == GeomDataAPI_Point2D::typeId();
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);
118 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
119 const TypeOfShape theShapeType) const
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);
133 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
134 const TypeOfShape theShapeType) const
138 if (theShape.get() != NULL) {
139 switch (theShapeType) {
141 aValid = theShape->isEdge();
144 aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
147 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
150 aValid = theShape->isVertex();
153 aValid = theShape->isSolid();
156 aValid = theShape->isFace();
159 aValid = theShape->isCompound();