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;
22 static EdgeTypes MyEdgeTypes;
24 GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType)
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;
34 std::string aType = std::string(theType.c_str());
35 if (MyEdgeTypes.find(aType) != MyEdgeTypes.end())
36 return MyEdgeTypes[aType];
38 Events_Error::send("Shape type defined in XML is not implemented!");
42 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
43 const std::list<std::string>& theArguments) const
47 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
48 for (; anIt != aLast && !aValid; anIt++) {
49 aValid = isValidArgument(theAttribute, *anIt);
55 bool GeomValidators_ShapeType::isValidArgument(const AttributePtr& theAttribute,
56 const std::string& theArgument) const
59 TypeOfShape aShapeType = shapeType(theArgument);
60 if (aShapeType == AnyShape)
63 AttributeSelectionListPtr aListAttr =
64 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
65 if (aListAttr.get()) {
66 for (int i = 0; i < aListAttr->size(); i++) {
67 aValid = isValidAttribute(aListAttr->value(i), aShapeType);
68 if (!aValid) // if at least one attribute is invalid, the result is false
73 aValid = isValidAttribute(theAttribute, aShapeType);
78 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
79 const TypeOfShape theShapeType) const
83 std::string anAttributeType = theAttribute->attributeType();
85 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
86 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
87 GeomShapePtr aShape = anAttr->value();
89 aValid = isValidShape(aShape, theShapeType);
91 aValid = isValidObject(anAttr->context(), theShapeType);
93 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
94 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
95 if (anAttr.get() != NULL) {
96 if (anAttr->isObject()) {
97 aValid = isValidObject(anAttr->object(), theShapeType);
99 else if (theShapeType == Vertex) {
100 AttributePtr aRefAttr = anAttr->attr();
101 aValid = aRefAttr.get() != NULL && aRefAttr->attributeType() == GeomDataAPI_Point2D::typeId();
105 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
106 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
107 if (anAttr.get() != NULL)
108 aValid = isValidObject(anAttr->value(), theShapeType);
113 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
114 const TypeOfShape theShapeType) const
117 if (theObject.get() != NULL) {
118 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
119 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
120 if (aResult.get() != NULL) {
121 GeomShapePtr aShape = aResult->shape();
122 aValid = isValidShape(aShape, theShapeType);
128 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
129 const TypeOfShape theShapeType) const
133 if (theShape.get() != NULL) {
134 switch (theShapeType) {
136 aValid = theShape->isEdge();
139 aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
142 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
145 aValid = theShape->isVertex();
148 aValid = theShape->isSolid();
151 aValid = theShape->isFace();
154 aValid = theShape->isCompound();