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 static EdgeTypes MyShapeTypes;
25 GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType)
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;
35 MyShapeTypes["plane"] = Plane;
37 std::string aType = std::string(theType.c_str());
38 if (MyShapeTypes.find(aType) != MyShapeTypes.end())
39 return MyShapeTypes[aType];
41 Events_Error::send("Shape type defined in XML is not implemented!");
45 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
47 std::string aValue = "";
49 if (MyShapeTypes.size() != 0) {
50 std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator anIt = MyShapeTypes.begin(),
51 aLast = MyShapeTypes.end();
52 for (; anIt != aLast; anIt++) {
53 if (anIt->second == theType)
61 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
62 const std::list<std::string>& theArguments,
63 std::string& theError) const
67 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
68 // returns true if the attribute satisfies at least one of given arguments
69 for (; anIt != aLast; anIt++) {
70 TypeOfShape aShapeType = shapeType(*anIt);
71 // if arguments contain any shape type value, the validator returns true
72 if (aShapeType == AnyShape) {
76 if (isValidAttribute(theAttribute, aShapeType, theError)) {
81 if (!aValid && theError.empty()) {
83 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
84 // returns true if the attribute satisfies at least one of given arguments
85 for (; anIt != aLast; anIt++) {
89 theError = "It does not contain element with acceptable shape type. The type should be one of the next: "
96 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
97 const TypeOfShape theShapeType,
98 std::string& theError) const
102 std::string anAttributeType = theAttribute->attributeType();
103 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
104 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
105 GeomShapePtr aShape = anAttr->value();
107 aValid = isValidShape(aShape, theShapeType, theError);
109 aValid = isValidObject(anAttr->context(), theShapeType, theError);
111 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
112 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
113 if (anAttr->isObject()) {
114 aValid = isValidObject(anAttr->object(), theShapeType, theError);
116 else if (theShapeType == Vertex) {
117 AttributePtr aRefAttr = anAttr->attr();
118 if (!aRefAttr.get()){
120 theError = "It has reference to an empty attribute";
123 std::string anAttributeType = aRefAttr->attributeType();
124 aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
126 theError = "Shape type is \"" + anAttributeType +
127 "\", it should be \"" + getShapeTypeDescription(theShapeType) + "\"";
131 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
132 AttributeReferencePtr anAttr =
133 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
134 aValid = isValidObject(anAttr->value(), theShapeType, theError);
136 else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
137 AttributeSelectionListPtr aListAttr =
138 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
139 // the Empty value means that the attribute selection list is valid if it is empty
140 if (aListAttr->size() == 0 && theShapeType == Empty) {
143 aValid = false; // the list should have elements if the shape type is not Empty
144 for (int i = 0; i < aListAttr->size(); i++) {
145 aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
146 if (!aValid) // if at least one attribute is invalid, the result is false
152 theError = "The attribute with the " + anAttributeType + " type is not processed";
157 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
158 const TypeOfShape theShapeType,
159 std::string& theError) const
162 if (!theObject.get()) {
164 theError = "The object is empty";
167 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
168 if( theShapeType==Plane )
170 ResultConstructionPtr aResultConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
171 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
172 const std::string& aKind = aFeature->getKind();
173 return aResult.get() != NULL && aKind == "Plane";
175 if (!aResult.get()) {
177 theError = "The result is empty";
180 aValid = isValidShape(aResult->shape(), theShapeType, theError);
186 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
187 const TypeOfShape theShapeType,
188 std::string& theError) const
192 if (!theShape.get()) {
194 theError = "The shape is empty";
197 switch (theShapeType) {
199 aValid = theShape->isEdge();
202 aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
205 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
208 aValid = theShape->isVertex();
211 aValid = theShape->isSolid() || theShape->isCompSolid() ||
212 theShape->isCompoundOfSolids();
215 aValid = theShape->isFace();
218 aValid = theShape->isCompound();