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,
46 std::string& theError) const
50 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
51 for (; anIt != aLast && !aValid; anIt++) {
52 aValid = isValidArgument(theAttribute, *anIt);
58 bool GeomValidators_ShapeType::isValidArgument(const AttributePtr& theAttribute,
59 const std::string& theArgument) const
62 TypeOfShape aShapeType = shapeType(theArgument);
63 if (aShapeType == AnyShape)
66 AttributeSelectionListPtr aListAttr =
67 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
68 if (aListAttr.get()) {
69 if(aListAttr->size() == 0 && shapeType(theArgument) == Empty) {
72 for (int i = 0; i < aListAttr->size(); i++) {
73 aValid = isValidAttribute(aListAttr->value(i), aShapeType);
74 if (!aValid) // if at least one attribute is invalid, the result is false
79 aValid = isValidAttribute(theAttribute, aShapeType);
84 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
85 const TypeOfShape theShapeType) const
89 std::string anAttributeType = theAttribute->attributeType();
91 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
92 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
93 GeomShapePtr aShape = anAttr->value();
95 aValid = isValidShape(aShape, theShapeType);
97 aValid = isValidObject(anAttr->context(), theShapeType);
99 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
100 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
101 if (anAttr.get() != NULL) {
102 if (anAttr->isObject()) {
103 aValid = isValidObject(anAttr->object(), theShapeType);
105 else if (theShapeType == Vertex) {
106 AttributePtr aRefAttr = anAttr->attr();
107 aValid = aRefAttr.get() != NULL && aRefAttr->attributeType() == GeomDataAPI_Point2D::typeId();
111 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
112 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
113 if (anAttr.get() != NULL)
114 aValid = isValidObject(anAttr->value(), theShapeType);
119 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
120 const TypeOfShape theShapeType) const
123 if (theObject.get() != NULL) {
124 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
125 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
126 if (aResult.get() != NULL) {
127 GeomShapePtr aShape = aResult->shape();
128 aValid = isValidShape(aShape, theShapeType);
134 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
135 const TypeOfShape theShapeType) const
139 if (theShape.get() != NULL) {
140 switch (theShapeType) {
142 aValid = theShape->isEdge();
145 aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
148 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
151 aValid = theShape->isVertex();
154 aValid = theShape->isSolid() || theShape->isCompoundOfSolids();
157 aValid = theShape->isFace();
160 aValid = theShape->isCompound();