1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomValidators_ShapeType.h"
22 #include "GeomValidators_Tools.h"
24 #include <GeomAPI_Curve.h>
25 #include <GeomDataAPI_Point2D.h>
27 #include <ModelAPI_Result.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_AttributeRefAttr.h>
30 #include <ModelAPI_AttributeSelectionList.h>
31 #include <ModelAPI_AttributeReference.h>
33 #include <Events_InfoMessage.h>
39 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
41 static EdgeTypes MyShapeTypes;
42 GeomValidators_ShapeType::TypeOfShape
43 GeomValidators_ShapeType::shapeType(const std::string& theType)
45 if (MyShapeTypes.size() == 0) {
46 MyShapeTypes["empty"] = Empty;
47 MyShapeTypes["vertex"] = Vertex;
48 MyShapeTypes["edge"] = Edge;
49 MyShapeTypes["line"] = Line;
50 MyShapeTypes["circle"] = Circle;
51 MyShapeTypes["wire"] = Wire;
52 MyShapeTypes["face"] = Face;
53 MyShapeTypes["plane"] = Plane;
54 MyShapeTypes["shell"] = Shell;
55 MyShapeTypes["solid"] = Solid;
56 MyShapeTypes["compsolid"] = CompSolid;
57 MyShapeTypes["compound"] = Compound;
59 std::string aType = std::string(theType.c_str());
60 if (MyShapeTypes.find(aType) != MyShapeTypes.end())
61 return MyShapeTypes[aType];
63 Events_InfoMessage("Shape type defined in XML is not implemented!").send();
67 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
69 std::string aValue = "";
71 if (MyShapeTypes.size() != 0) {
72 std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
73 anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
74 for (; anIt != aLast; anIt++) {
75 if (anIt->second == theType)
83 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
84 const std::list<std::string>& theArguments,
85 Events_InfoMessage& theError) const
89 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
90 // returns true if the attribute satisfies at least one of given arguments
91 for (; anIt != aLast; anIt++) {
92 TypeOfShape aShapeType = shapeType(*anIt);
93 // if arguments contain any shape type value, the validator returns true
94 if (aShapeType == AnyShape) {
98 if (isValidAttribute(theAttribute, aShapeType, theError)) {
103 if (!aValid && theError.empty()) {
105 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
106 // returns true if the attribute satisfies at least one of given arguments
107 for (; anIt != aLast; anIt++) {
112 theError = "It does not contain element with acceptable shape type. "
113 "The type should be one of the next: %1";
114 theError.arg(aTypes);
120 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
121 const TypeOfShape theShapeType,
122 Events_InfoMessage& theError) const
126 std::string anAttributeType = theAttribute->attributeType();
127 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
128 AttributeSelectionPtr anAttr =
129 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
130 GeomShapePtr aShape = anAttr->value();
132 aValid = isValidShape(aShape, theShapeType, theError);
134 aValid = isValidObject(anAttr->context(), theShapeType, theError);
136 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
137 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
138 if (anAttr->isObject()) {
139 aValid = isValidObject(anAttr->object(), theShapeType, theError);
141 else if (theShapeType == Vertex) {
142 AttributePtr aRefAttr = anAttr->attr();
143 if (!aRefAttr.get()){
145 theError = "It has reference to an empty attribute";
148 std::string anAttributeType = aRefAttr->attributeType();
149 aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
151 theError = "Shape type is \"%1\", it should be \"%2\"";
152 theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
157 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
158 AttributeReferencePtr anAttr =
159 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
160 aValid = isValidObject(anAttr->value(), theShapeType, theError);
162 else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
163 AttributeSelectionListPtr aListAttr =
164 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
165 // the Empty value means that the attribute selection list is valid if it is empty
166 if (aListAttr->size() == 0 && theShapeType == Empty) {
169 aValid = false; // the list should have elements if the shape type is not Empty
170 for (int i = 0; i < aListAttr->size(); i++) {
171 aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
172 if (!aValid) // if at least one attribute is invalid, the result is false
178 theError = "The attribute with the %1 type is not processed";
179 theError.arg(anAttributeType);
184 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
185 const TypeOfShape theShapeType,
186 Events_InfoMessage& theError) const
189 if (!theObject.get()) {
190 if(theShapeType != Empty) {
192 theError = "The object is empty";
196 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
197 if( theShapeType==Plane )
199 ResultConstructionPtr aResultConstruction =
200 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
201 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
202 const std::string& aKind = aFeature->getKind();
203 return aResult.get() != NULL && aKind == "Plane";
205 if (!aResult.get()) {
207 theError = "The result is empty";
210 aValid = isValidShape(aResult->shape(), theShapeType, theError);
216 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
217 const TypeOfShape theShapeType,
218 Events_InfoMessage& theError) const
222 if (!theShape.get()) {
224 theError = "The shape is empty";
227 switch (theShapeType) {
229 aValid = theShape->isVertex();
232 aValid = theShape->isEdge();
235 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine();
238 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
241 aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
244 aValid = theShape->isFace();
247 aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
250 aValid = theShape->isSolid() || theShape->isCompSolid() ||
251 theShape->isCompoundOfSolids();
254 aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
257 aValid = theShape->isCompound();