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_ResultConstruction.h>
12 #include <ModelAPI_AttributeRefAttr.h>
13 #include <ModelAPI_AttributeSelectionList.h>
14 #include <ModelAPI_AttributeReference.h>
16 #include <Events_Error.h>
22 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
24 GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType)
26 static EdgeTypes MyShapeTypes;
28 if (MyShapeTypes.size() == 0) {
29 MyShapeTypes["empty"] = Empty;
30 MyShapeTypes["vertex"] = Vertex;
31 MyShapeTypes["edge"] = Edge;
32 MyShapeTypes["line"] = Line;
33 MyShapeTypes["circle"] = Circle;
34 MyShapeTypes["face"] = Face;
35 MyShapeTypes["solid"] = Solid;
36 MyShapeTypes["plane"] = Plane;
38 std::string aType = std::string(theType.c_str());
39 if (MyShapeTypes.find(aType) != MyShapeTypes.end())
40 return MyShapeTypes[aType];
42 Events_Error::send("Shape type defined in XML is not implemented!");
46 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
47 const std::list<std::string>& theArguments,
48 std::string& theError) const
52 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
53 for (; anIt != aLast && !aValid; anIt++) {
54 aValid = isValidArgument(theAttribute, *anIt);
60 bool GeomValidators_ShapeType::isValidArgument(const AttributePtr& theAttribute,
61 const std::string& theArgument) const
64 TypeOfShape aShapeType = shapeType(theArgument);
65 if (aShapeType == AnyShape)
68 AttributeSelectionListPtr aListAttr =
69 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
70 if (aListAttr.get()) {
71 if(aListAttr->size() == 0 && shapeType(theArgument) == Empty) {
74 for (int i = 0; i < aListAttr->size(); i++) {
75 aValid = isValidAttribute(aListAttr->value(i), aShapeType);
76 if (!aValid) // if at least one attribute is invalid, the result is false
81 aValid = isValidAttribute(theAttribute, aShapeType);
86 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
87 const TypeOfShape theShapeType) const
91 std::string anAttributeType = theAttribute->attributeType();
93 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
94 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
95 GeomShapePtr aShape = anAttr->value();
97 aValid = isValidShape(aShape, theShapeType);
99 aValid = isValidObject(anAttr->context(), theShapeType);
101 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
102 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
103 if (anAttr.get() != NULL) {
104 if (anAttr->isObject()) {
105 aValid = isValidObject(anAttr->object(), theShapeType);
107 else if (theShapeType == Vertex) {
108 AttributePtr aRefAttr = anAttr->attr();
109 aValid = aRefAttr.get() != NULL && aRefAttr->attributeType() == GeomDataAPI_Point2D::typeId();
113 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
114 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
115 if (anAttr.get() != NULL)
116 aValid = isValidObject(anAttr->value(), theShapeType);
121 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
122 const TypeOfShape theShapeType) const
125 if (theObject.get() != NULL) {
126 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
127 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
128 if( theShapeType==Plane )
130 ResultConstructionPtr aResultConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
131 const std::string& aKind = aFeature->getKind();
132 return aResult.get() != NULL && aKind == "Plane";
135 if (aResult.get() != NULL) {
136 GeomShapePtr aShape = aResult->shape();
137 aValid = isValidShape(aShape, theShapeType);
143 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
144 const TypeOfShape theShapeType) const
148 if (theShape.get() != NULL) {
149 switch (theShapeType) {
151 aValid = theShape->isEdge();
154 aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
157 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
160 aValid = theShape->isVertex();
163 aValid = theShape->isSolid() || theShape->isCompoundOfSolids();
166 aValid = theShape->isFace();
169 aValid = theShape->isCompound();