1 // Copyright (C) 2014-2019 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(), ::tolower);
67 if (MyShapeTypes.find(aType) != MyShapeTypes.end())
68 return MyShapeTypes[aType];
71 Events_InfoMessage("Shape type defined in XML is not implemented!").send();
77 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
79 std::string aValue = "";
81 if (MyShapeTypes.size() != 0) {
82 std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
83 anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
84 for (; anIt != aLast; anIt++) {
85 if (anIt->second == theType)
94 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
95 const std::list<std::string>& theArguments,
96 Events_InfoMessage& theError) const
100 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
101 // returns true if the attribute satisfies at least one of given arguments
102 for (; anIt != aLast; anIt++) {
103 TypeOfShape aShapeType = shapeType(*anIt);
104 // if arguments contain any shape type value, the validator returns true
105 if (aShapeType == AnyShape) {
109 if (isValidAttribute(theAttribute, aShapeType, theError)) {
114 if (!aValid && theError.empty()) {
116 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
117 // returns true if the attribute satisfies at least one of given arguments
118 for (; 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 anAttributeType = aRefAttr->attributeType();
173 aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
176 theError = "Shape type is \"%1\", it should be \"%2\"";
177 theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
183 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
184 AttributeReferencePtr anAttr =
185 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
186 aValid = isValidObject(anAttr->value(), theShapeType, false, theError);
188 else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
189 AttributeSelectionListPtr aListAttr =
190 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
191 // the Empty value means that the attribute selection list is valid if it is empty
192 if (aListAttr->size() == 0 && theShapeType == Empty) {
195 aValid = false; // the list should have elements if the shape type is not Empty
196 for (int i = 0; i < aListAttr->size(); i++) {
197 aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
198 if (!aValid) // if at least one attribute is invalid, the result is false
205 theError = "The attribute with the %1 type is not processed";
206 theError.arg(anAttributeType);
214 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
215 const TypeOfShape theShapeType,
216 const bool theIsGeometricalSelection,
217 Events_InfoMessage& theError) const
220 if (!theObject.get()) {
221 if(theShapeType != Empty) {
223 theError = "The object is empty";
227 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
229 if (theShapeType == Plane)
231 ResultConstructionPtr aResultConstruction =
232 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
233 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
234 const std::string& aKind = aFeature->getKind();
235 return aResult.get() != NULL && aKind == "Plane";
237 if (!aResult.get()) {
239 theError = "The result is empty";
241 aValid = isValidShape(aResult->shape(), theShapeType, theIsGeometricalSelection, theError);
244 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
245 if (aFeature.get() && (theShapeType == CompSolid))
249 theError = "The feature has to produce a compsolid";
256 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
257 const TypeOfShape theShapeType,
258 const bool theIsGeometricalSelection,
259 Events_InfoMessage& theError) const
263 if (!theShape.get()) {
265 theError = "The shape is empty";
268 switch (theShapeType) {
270 aValid = theShape->isVertex();
273 aValid = theShape->isEdge();
276 if (theIsGeometricalSelection && theShape->isCompound()) {
278 for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
279 if (!anIt.current()->isEdge() || !GeomAPI_Curve(anIt.current()).isLine()) {
286 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine();
291 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
294 aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
297 aValid = theShape->isFace();
300 aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
303 aValid = theShape->isPlanar();
306 aValid = theShape->isSolid() || theShape->isCompSolid() ||
307 theShape->isCompoundOfSolids();
310 aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
313 aValid = theShape->isCompound();