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;
33 MyEdgeTypes["compound"] = Compound;
35 std::string aType = std::string(theType.c_str());
36 if (MyEdgeTypes.find(aType) != MyEdgeTypes.end())
37 return MyEdgeTypes[aType];
39 Events_Error::send("Shape type defined in XML is not implemented!");
43 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
44 const std::list<std::string>& theArguments) const
48 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
49 for (; anIt != aLast && !aValid; anIt++) {
50 aValid = isValidArgument(theAttribute, *anIt);
56 bool GeomValidators_ShapeType::isValidArgument(const AttributePtr& theAttribute,
57 const std::string& theArgument) const
60 TypeOfShape aShapeType = shapeType(theArgument);
61 if (aShapeType == AnyShape)
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
74 aValid = isValidAttribute(theAttribute, aShapeType);
79 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
80 const TypeOfShape theShapeType) const
84 std::string anAttributeType = theAttribute->attributeType();
86 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
87 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
88 GeomShapePtr aShape = anAttr->value();
90 aValid = isValidShape(aShape, theShapeType);
92 aValid = isValidObject(anAttr->context(), theShapeType);
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);
100 else if (theShapeType == Vertex) {
101 AttributePtr aRefAttr = anAttr->attr();
102 aValid = aRefAttr.get() != NULL && aRefAttr->attributeType() == GeomDataAPI_Point2D::typeId();
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);
114 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
115 const TypeOfShape theShapeType) const
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);
129 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
130 const TypeOfShape theShapeType) const
134 if (theShape.get() != NULL) {
135 switch (theShapeType) {
137 aValid = theShape->isEdge();
140 aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
143 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
146 aValid = theShape->isVertex();
149 aValid = theShape->isSolid();
152 aValid = theShape->isFace();
155 aValid = theShape->isCompound();