X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FGeomValidators%2FGeomValidators_ShapeType.cpp;h=ba89aa91b32cdaa6ad885120760d4dd000de6d37;hb=fc72d43b677baa05ae7fd317346fd8b723b799ed;hp=50c4bcdb232b93fa3d077c2e057713a41ed6b842;hpb=ead20a0065c5c4552f3e02c7d6f304110dc0e4e6;p=modules%2Fshaper.git diff --git a/src/GeomValidators/GeomValidators_ShapeType.cpp b/src/GeomValidators/GeomValidators_ShapeType.cpp index 50c4bcdb2..ba89aa91b 100644 --- a/src/GeomValidators/GeomValidators_ShapeType.cpp +++ b/src/GeomValidators/GeomValidators_ShapeType.cpp @@ -1,159 +1,322 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D - +// Copyright (C) 2014-2023 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// #include "GeomValidators_ShapeType.h" #include "GeomValidators_Tools.h" #include +#include #include #include +#include +#include #include #include #include -#include +#include +#include #include #include typedef std::map EdgeTypes; -static EdgeTypes MyEdgeTypes; -GeomValidators_ShapeType::TypeOfShape GeomValidators_ShapeType::shapeType(const std::string& theType) +static EdgeTypes MyShapeTypes; +GeomValidators_ShapeType::TypeOfShape + GeomValidators_ShapeType::shapeType(const std::string& theType) { - if (MyEdgeTypes.size() == 0) { - MyEdgeTypes["vertex"] = Vertex; - MyEdgeTypes["edge"] = Edge; - MyEdgeTypes["line"] = Line; - MyEdgeTypes["circle"] = Circle; - MyEdgeTypes["solid"] = Solid; - MyEdgeTypes["face"] = Face; + if (MyShapeTypes.size() == 0) { + MyShapeTypes["empty"] = Empty; + MyShapeTypes["vertex"] = Vertex; + MyShapeTypes["vertices"] = Vertex; + MyShapeTypes["edge"] = Edge; + MyShapeTypes["edges"] = Edge; + MyShapeTypes["line"] = Line; + MyShapeTypes["circle"] = Circle; + MyShapeTypes["wire"] = Wire; + MyShapeTypes["face"] = Face; + MyShapeTypes["faces"] = Face; + MyShapeTypes["plane"] = Plane; + MyShapeTypes["shell"] = Shell; + MyShapeTypes["solid"] = Solid; + MyShapeTypes["solids"] = Solid; + MyShapeTypes["compsolid"] = CompSolid; + MyShapeTypes["compound"] = Compound; } std::string aType = std::string(theType.c_str()); - if (MyEdgeTypes.find(aType) != MyEdgeTypes.end()) - return MyEdgeTypes[aType]; - - Events_Error::send("Shape type defined in XML is not implemented!"); + std::transform(aType.begin(), aType.end(), aType.begin(), + [](char c) { return static_cast(::tolower(c)); }); + if (MyShapeTypes.find(aType) != MyShapeTypes.end()) + return MyShapeTypes[aType]; + +// LCOV_EXCL_START + //Events_InfoMessage("Shape type defined in XML is not implemented!").send(); return AnyShape; +// LCOV_EXCL_STOP } -bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute, - const std::list& theArguments) const +// LCOV_EXCL_START +std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType) { - bool aValid = false; + std::string aValue = ""; - std::list::const_iterator anIt = theArguments.begin(), aLast = theArguments.end(); - for (; anIt != aLast && !aValid; anIt++) { - aValid = isValidArgument(theAttribute, *anIt); + 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 aValid; + return aValue; } +// LCOV_EXCL_STOP -bool GeomValidators_ShapeType::isValidArgument(const AttributePtr& theAttribute, - const std::string& theArgument) const +bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute, + const std::list& theArguments, + Events_InfoMessage& theError) const { bool aValid = false; - TypeOfShape aShapeType = shapeType(theArgument); - if (aShapeType == AnyShape) - return true; - AttributeSelectionListPtr aListAttr = - std::dynamic_pointer_cast(theAttribute); - if (aListAttr.get()) { - 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; + 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++) { + TypeOfShape aShapeType = shapeType(*anIt); + // if arguments contain any shape type value, the validator returns true + if (aShapeType == AnyShape) { + aValid = true; + break; + } + if (isValidAttribute(theAttribute, aShapeType, theError)) { + aValid = true; + break; } } - else { - aValid = isValidAttribute(theAttribute, aShapeType); + if (!aValid && theError.empty()) { + std::string aTypes; + // returns true if the attribute satisfies at least one of given arguments + for (anIt = theArguments.begin(); anIt != aLast; anIt++) { + if (!aTypes.empty()) + aTypes += ", "; + aTypes += *anIt; + } + theError = "It does not contain element with acceptable shape type. " + "The type should be one of the next: %1"; + theError.arg(aTypes); } + return aValid; } bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute, - const TypeOfShape theShapeType) const + const TypeOfShape theShapeType, + Events_InfoMessage& 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); + AttributeSelectionPtr anAttr = + std::dynamic_pointer_cast(theAttribute); GeomShapePtr aShape = anAttr->value(); if (aShape.get()) - aValid = isValidShape(aShape, theShapeType); - else - aValid = isValidObject(anAttr->context(), theShapeType); + aValid = isValidShape(aShape, theShapeType, anAttr->isGeometricalSelection(), theError); + else { + if (anAttr->context().get()) + aValid = isValidObject(anAttr->context(), + theShapeType, + anAttr->isGeometricalSelection(), + theError); + else + aValid = isValidObject(anAttr->contextFeature(), + theShapeType, + anAttr->isGeometricalSelection(), + 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, + false, + 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 anAttrType = aRefAttr->attributeType(); + aValid = anAttrType == GeomDataAPI_Point2D::typeId(); + if (!aValid) { +// LCOV_EXCL_START + theError = "Shape type is \"%1\", it should be \"%2\""; + theError.arg(anAttrType).arg(getShapeTypeDescription(theShapeType)); +// LCOV_EXCL_STOP + } } } + else + aValid = false; } 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, false, 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 { +// LCOV_EXCL_START + aValid = false; + theError = "The attribute with the %1 type is not processed"; + theError.arg(anAttributeType); +// LCOV_EXCL_STOP } + if (aValid) + theError = ""; return aValid; } bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject, - const TypeOfShape theShapeType) const + const TypeOfShape theShapeType, + const bool theIsGeometricalSelection, + Events_InfoMessage& theError) const { - bool aValid = false; - if (theObject.get() != NULL) { - FeaturePtr aFeature = ModelAPI_Feature::feature(theObject); + bool aValid = true; + if (!theObject.get()) { + if(theShapeType != Empty) { + 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 (aResult.get()) { + 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, theIsGeometricalSelection, theError); + } + } else { + FeaturePtr aFeature = std::dynamic_pointer_cast(theObject); + if (aFeature.get() && (theShapeType == CompSolid)) + return aValid; + else { + aValid = false; + theError = "The feature has to produce a compsolid"; + } } } return aValid; } bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape, - const TypeOfShape theShapeType) const + const TypeOfShape theShapeType, + const bool theIsGeometricalSelection, + Events_InfoMessage& 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 Vertex: + aValid = theShape->isVertex(); break; - case Line: - aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle(); + case Edge: + aValid = theShape->isEdge(); break; - case Circle: - aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle(); + case Line: { + if (theIsGeometricalSelection && theShape->isCompound()) { + aValid = true; + for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) { + if (!anIt.current()->isEdge() || !GeomAPI_Curve(anIt.current()).isLine()) { + aValid = false; + break; + } + } + } + else { + aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine(); + } break; - case Vertex: - aValid = theShape->isVertex(); + } + case Circle: + aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle(); + break; + case Wire: + aValid = theShape->shapeType() == GeomAPI_Shape::WIRE; + break; + case Face: + aValid = theShape->isFace(); + break; + case Shell: + aValid = theShape->shapeType() == GeomAPI_Shape::SHELL; + break; + case Plane: + aValid = theShape->isPlanar(); + break; + case Solid: + aValid = theShape->isSolid() || theShape->isCompSolid() || + theShape->isCompoundOfSolids(); + break; + case CompSolid: + aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID; + break; + case Compound: + aValid = theShape->isCompound(); + break; + default: + aValid = false; break; - case Solid: - aValid = theShape->isSolid(); - break; - case Face: - aValid = theShape->isFace(); - break; - case Compound: - aValid = theShape->isCompound(); - break; - default: break; } } return aValid;