X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FGeomValidators%2FGeomValidators_ShapeType.cpp;h=80e6af9d02cbda14a32a29932da767a012202dae;hb=3205d0f18200948632155bbe7b640bc1e482243d;hp=cefd7a592548388b114db60ebe376b574065cf18;hpb=4ca3fd41634c37f840d0cbb3f896fa97c2d3c457;p=modules%2Fshaper.git diff --git a/src/GeomValidators/GeomValidators_ShapeType.cpp b/src/GeomValidators/GeomValidators_ShapeType.cpp index cefd7a592..80e6af9d0 100644 --- a/src/GeomValidators/GeomValidators_ShapeType.cpp +++ b/src/GeomValidators/GeomValidators_ShapeType.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -20,10 +21,9 @@ typedef std::map EdgeTypes; +static EdgeTypes MyShapeTypes; GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType) { - static EdgeTypes MyShapeTypes; - if (MyShapeTypes.size() == 0) { MyShapeTypes["empty"] = Empty; MyShapeTypes["vertex"] = Vertex; @@ -32,6 +32,7 @@ GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const MyShapeTypes["circle"] = Circle; MyShapeTypes["face"] = Face; MyShapeTypes["solid"] = Solid; + MyShapeTypes["plane"] = Plane; } std::string aType = std::string(theType.c_str()); if (MyShapeTypes.find(aType) != MyShapeTypes.end()) @@ -41,6 +42,22 @@ GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const return AnyShape; } +std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType) +{ + std::string aValue = ""; + + if (MyShapeTypes.size() != 0) { + std::map::const_iterator anIt = MyShapeTypes.begin(), + aLast = MyShapeTypes.end(); + for (; anIt != aLast; anIt++) { + if (anIt->second == theType) + aValue = anIt->first; + break; + } + } + return aValue; +} + bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute, const std::list& theArguments, std::string& theError) const @@ -48,98 +65,138 @@ bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute, bool aValid = false; std::list::const_iterator anIt = theArguments.begin(), aLast = theArguments.end(); - for (; anIt != aLast && !aValid; anIt++) { - aValid = isValidArgument(theAttribute, *anIt); - } - - return aValid; -} - -bool GeomValidators_ShapeType::isValidArgument(const AttributePtr& theAttribute, - const std::string& theArgument) const -{ - bool aValid = false; - TypeOfShape aShapeType = shapeType(theArgument); - if (aShapeType == AnyShape) - return true; - - AttributeSelectionListPtr aListAttr = - std::dynamic_pointer_cast(theAttribute); - if (aListAttr.get()) { - if(aListAttr->size() == 0 && shapeType(theArgument) == Empty) { - return true; + // returns true if the attribute satisfies at least one of given arguments + for (; anIt != aLast; anIt++) { + TypeOfShape aShapeType = shapeType(*anIt); + // if arguments contain any shape type value, the validator returns true + if (aShapeType == AnyShape) { + aValid = true; + break; } - for (int i = 0; i < aListAttr->size(); i++) { - aValid = isValidAttribute(aListAttr->value(i), aShapeType); - if (!aValid) // if at least one attribute is invalid, the result is false - break; + if (isValidAttribute(theAttribute, aShapeType, theError)) { + aValid = true; + break; } } - else { - aValid = isValidAttribute(theAttribute, aShapeType); + if (!aValid && theError.empty()) { + std::string aTypes; + std::list::const_iterator anIt = theArguments.begin(), aLast = theArguments.end(); + // returns true if the attribute satisfies at least one of given arguments + for (; anIt != aLast; anIt++) { + if (!aTypes.empty()) + aTypes += ", "; + } + theError = "It does not contain element with acceptable shape type. The type should be one of the next: " + + aTypes; } + return aValid; } bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute, - const TypeOfShape theShapeType) const + const TypeOfShape theShapeType, + std::string& theError) const { - bool aValid = false; + bool aValid = true; std::string anAttributeType = theAttribute->attributeType(); - if (anAttributeType == ModelAPI_AttributeSelection::typeId()) { AttributeSelectionPtr anAttr = std::dynamic_pointer_cast(theAttribute); GeomShapePtr aShape = anAttr->value(); if (aShape.get()) - aValid = isValidShape(aShape, theShapeType); + aValid = isValidShape(aShape, theShapeType, theError); else - aValid = isValidObject(anAttr->context(), theShapeType); + aValid = isValidObject(anAttr->context(), theShapeType, theError); } else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) { AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast(theAttribute); - if (anAttr.get() != NULL) { - if (anAttr->isObject()) { - aValid = isValidObject(anAttr->object(), theShapeType); + if (anAttr->isObject()) { + aValid = isValidObject(anAttr->object(), theShapeType, theError); + } + else if (theShapeType == Vertex) { + AttributePtr aRefAttr = anAttr->attr(); + if (!aRefAttr.get()){ + aValid = false; + theError = "It has reference to an empty attribute"; } - else if (theShapeType == Vertex) { - AttributePtr aRefAttr = anAttr->attr(); - aValid = aRefAttr.get() != NULL && aRefAttr->attributeType() == GeomDataAPI_Point2D::typeId(); + else { + std::string anAttributeType = aRefAttr->attributeType(); + aValid = anAttributeType == GeomDataAPI_Point2D::typeId(); + if (!aValid) + theError = "Shape type is \"" + anAttributeType + + "\", it should be \"" + getShapeTypeDescription(theShapeType) + "\""; } } } else if (anAttributeType == ModelAPI_AttributeReference::typeId()) { - AttributeReferencePtr anAttr = std::dynamic_pointer_cast(theAttribute); - if (anAttr.get() != NULL) - aValid = isValidObject(anAttr->value(), theShapeType); + AttributeReferencePtr anAttr = + std::dynamic_pointer_cast(theAttribute); + aValid = isValidObject(anAttr->value(), theShapeType, theError); + } + else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) { + AttributeSelectionListPtr aListAttr = + std::dynamic_pointer_cast(theAttribute); + // the Empty value means that the attribute selection list is valid if it is empty + if (aListAttr->size() == 0 && theShapeType == Empty) { + return true; + } + aValid = false; // the list should have elements if the shape type is not Empty + for (int i = 0; i < aListAttr->size(); i++) { + aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError); + if (!aValid) // if at least one attribute is invalid, the result is false + break; + } + } + else { + aValid = false; + theError = "The attribute with the " + anAttributeType + " type is not processed"; } return aValid; } bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject, - const TypeOfShape theShapeType) const + const TypeOfShape theShapeType, + std::string& theError) const { - bool aValid = false; - if (theObject.get() != NULL) { - FeaturePtr aFeature = ModelAPI_Feature::feature(theObject); + bool aValid = true; + if (!theObject.get()) { + aValid = false; + theError = "The object is empty"; + } + else { ResultPtr aResult = std::dynamic_pointer_cast(theObject); - if (aResult.get() != NULL) { - GeomShapePtr aShape = aResult->shape(); - aValid = isValidShape(aShape, theShapeType); + if( theShapeType==Plane ) + { + ResultConstructionPtr aResultConstruction = std::dynamic_pointer_cast(theObject); + FeaturePtr aFeature = ModelAPI_Feature::feature(theObject); + const std::string& aKind = aFeature->getKind(); + return aResult.get() != NULL && aKind == "Plane"; + } + if (!aResult.get()) { + aValid = false; + theError = "The result is empty"; + } + else { + aValid = isValidShape(aResult->shape(), theShapeType, theError); } } return aValid; } bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape, - const TypeOfShape theShapeType) const + const TypeOfShape theShapeType, + std::string& theError) const { - bool aValid = false; + bool aValid = true; - if (theShape.get() != NULL) { + if (!theShape.get()) { + aValid = false; + theError = "The shape is empty"; + } + else { switch (theShapeType) { - case Edge: - aValid = theShape->isEdge(); + case Edge: + aValid = theShape->isEdge(); break; case Line: aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle(); @@ -151,7 +208,8 @@ bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape, aValid = theShape->isVertex(); break; case Solid: - aValid = theShape->isSolid() || theShape->isCompoundOfSolids(); + aValid = theShape->isSolid() || theShape->isCompSolid() || + theShape->isCompoundOfSolids(); break; case Face: aValid = theShape->isFace(); @@ -159,7 +217,9 @@ bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape, case Compound: aValid = theShape->isCompound(); break; - default: break; + default: + aValid = false; + break; } } return aValid;