1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GeomValidators_ShapeType.h"
21 #include "GeomValidators_Tools.h"
23 #include <GeomAPI_Curve.h>
24 #include <GeomAPI_ShapeIterator.h>
25 #include <GeomDataAPI_Point2D.h>
27 #include <ModelAPI_Result.h>
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeSelectionList.h>
32 #include <ModelAPI_AttributeReference.h>
34 #include <Events_InfoMessage.h>
41 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
43 static EdgeTypes MyShapeTypes;
44 GeomValidators_ShapeType::TypeOfShape
45 GeomValidators_ShapeType::shapeType(const std::string& theType)
47 if (MyShapeTypes.size() == 0) {
48 MyShapeTypes["empty"] = Empty;
49 MyShapeTypes["vertex"] = Vertex;
50 MyShapeTypes["vertices"] = Vertex;
51 MyShapeTypes["edge"] = Edge;
52 MyShapeTypes["edges"] = Edge;
53 MyShapeTypes["line"] = Line;
54 MyShapeTypes["circle"] = Circle;
55 MyShapeTypes["wire"] = Wire;
56 MyShapeTypes["face"] = Face;
57 MyShapeTypes["faces"] = Face;
58 MyShapeTypes["plane"] = Plane;
59 MyShapeTypes["shell"] = Shell;
60 MyShapeTypes["solid"] = Solid;
61 MyShapeTypes["solids"] = Solid;
62 MyShapeTypes["compsolid"] = CompSolid;
63 MyShapeTypes["compound"] = Compound;
65 std::string aType = std::string(theType.c_str());
66 std::transform(aType.begin(), aType.end(), aType.begin(),
67 [](char c) { return static_cast<char>(::tolower(c)); });
68 if (MyShapeTypes.find(aType) != MyShapeTypes.end())
69 return MyShapeTypes[aType];
72 //Events_InfoMessage("Shape type defined in XML is not implemented!").send();
78 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
80 std::string aValue = "";
82 if (MyShapeTypes.size() != 0) {
83 std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
84 anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
85 for (; anIt != aLast; anIt++)
86 if (anIt->second == theType) {
95 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
96 const std::list<std::string>& theArguments,
97 Events_InfoMessage& theError) const
101 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
102 // returns true if the attribute satisfies at least one of given arguments
103 for (; anIt != aLast; anIt++) {
104 TypeOfShape aShapeType = shapeType(*anIt);
105 // if arguments contain any shape type value, the validator returns true
106 if (aShapeType == AnyShape) {
110 if (isValidAttribute(theAttribute, aShapeType, theError)) {
115 if (!aValid && theError.empty()) {
117 // returns true if the attribute satisfies at least one of given arguments
118 for (anIt = theArguments.begin(); anIt != aLast; anIt++) {
123 theError = "It does not contain element with acceptable shape type. "
124 "The type should be one of the next: %1";
125 theError.arg(aTypes);
131 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
132 const TypeOfShape theShapeType,
133 Events_InfoMessage& theError) const
137 std::string anAttributeType = theAttribute->attributeType();
138 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
139 AttributeSelectionPtr anAttr =
140 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
141 GeomShapePtr aShape = anAttr->value();
143 aValid = isValidShape(aShape, theShapeType, anAttr->isGeometricalSelection(), theError);
145 if (anAttr->context().get())
146 aValid = isValidObject(anAttr->context(),
148 anAttr->isGeometricalSelection(),
151 aValid = isValidObject(anAttr->contextFeature(),
153 anAttr->isGeometricalSelection(),
157 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
158 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
159 if (anAttr->isObject()) {
160 aValid = isValidObject(anAttr->object(),
165 else if (theShapeType == Vertex) {
166 AttributePtr aRefAttr = anAttr->attr();
167 if (!aRefAttr.get()){
169 theError = "It has reference to an empty attribute";
172 std::string anAttrType = aRefAttr->attributeType();
173 aValid = anAttrType == GeomDataAPI_Point2D::typeId();
176 theError = "Shape type is \"%1\", it should be \"%2\"";
177 theError.arg(anAttrType).arg(getShapeTypeDescription(theShapeType));
185 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
186 AttributeReferencePtr anAttr =
187 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
188 aValid = isValidObject(anAttr->value(), theShapeType, false, theError);
190 else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
191 AttributeSelectionListPtr aListAttr =
192 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
193 // the Empty value means that the attribute selection list is valid if it is empty
194 if (aListAttr->size() == 0 && theShapeType == Empty) {
197 aValid = false; // the list should have elements if the shape type is not Empty
198 for (int i = 0; i < aListAttr->size(); i++) {
199 aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
200 if (!aValid) // if at least one attribute is invalid, the result is false
207 theError = "The attribute with the %1 type is not processed";
208 theError.arg(anAttributeType);
216 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
217 const TypeOfShape theShapeType,
218 const bool theIsGeometricalSelection,
219 Events_InfoMessage& theError) const
222 if (!theObject.get()) {
223 if(theShapeType != Empty) {
225 theError = "The object is empty";
229 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
231 if (theShapeType == Plane)
233 ResultConstructionPtr aResultConstruction =
234 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
235 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
236 const std::string& aKind = aFeature->getKind();
237 return aResult.get() != NULL && aKind == "Plane";
239 if (!aResult.get()) {
241 theError = "The result is empty";
243 aValid = isValidShape(aResult->shape(), theShapeType, theIsGeometricalSelection, theError);
246 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
247 if (aFeature.get() && (theShapeType == CompSolid))
251 theError = "The feature has to produce a compsolid";
258 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
259 const TypeOfShape theShapeType,
260 const bool theIsGeometricalSelection,
261 Events_InfoMessage& theError) const
265 if (!theShape.get()) {
267 theError = "The shape is empty";
270 switch (theShapeType) {
272 aValid = theShape->isVertex();
275 aValid = theShape->isEdge();
278 if (theIsGeometricalSelection && theShape->isCompound()) {
280 for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
281 if (!anIt.current()->isEdge() || !GeomAPI_Curve(anIt.current()).isLine()) {
288 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine();
293 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
296 aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
299 aValid = theShape->isFace();
302 aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
305 aValid = theShape->isPlanar();
308 aValid = theShape->isSolid() || theShape->isCompSolid() ||
309 theShape->isCompoundOfSolids();
312 aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
315 aValid = theShape->isCompound();