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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "GeomValidators_ShapeType.h"
21 #include "GeomValidators_Tools.h"
23 #include <GeomAPI_Curve.h>
24 #include <GeomDataAPI_Point2D.h>
26 #include <ModelAPI_Result.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_AttributeRefAttr.h>
29 #include <ModelAPI_AttributeSelectionList.h>
30 #include <ModelAPI_AttributeReference.h>
32 #include <Events_InfoMessage.h>
38 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
40 static EdgeTypes MyShapeTypes;
41 GeomValidators_ShapeType::TypeOfShape
42 GeomValidators_ShapeType::shapeType(const std::string& theType)
44 if (MyShapeTypes.size() == 0) {
45 MyShapeTypes["empty"] = Empty;
46 MyShapeTypes["vertex"] = Vertex;
47 MyShapeTypes["edge"] = Edge;
48 MyShapeTypes["line"] = Line;
49 MyShapeTypes["circle"] = Circle;
50 MyShapeTypes["wire"] = Wire;
51 MyShapeTypes["face"] = Face;
52 MyShapeTypes["plane"] = Plane;
53 MyShapeTypes["shell"] = Shell;
54 MyShapeTypes["solid"] = Solid;
55 MyShapeTypes["compsolid"] = CompSolid;
56 MyShapeTypes["compound"] = Compound;
58 std::string aType = std::string(theType.c_str());
59 if (MyShapeTypes.find(aType) != MyShapeTypes.end())
60 return MyShapeTypes[aType];
62 Events_InfoMessage("Shape type defined in XML is not implemented!").send();
66 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
68 std::string aValue = "";
70 if (MyShapeTypes.size() != 0) {
71 std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
72 anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
73 for (; anIt != aLast; anIt++) {
74 if (anIt->second == theType)
82 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
83 const std::list<std::string>& theArguments,
84 Events_InfoMessage& theError) const
88 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
89 // returns true if the attribute satisfies at least one of given arguments
90 for (; anIt != aLast; anIt++) {
91 TypeOfShape aShapeType = shapeType(*anIt);
92 // if arguments contain any shape type value, the validator returns true
93 if (aShapeType == AnyShape) {
97 if (isValidAttribute(theAttribute, aShapeType, theError)) {
102 if (!aValid && theError.empty()) {
104 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
105 // returns true if the attribute satisfies at least one of given arguments
106 for (; anIt != aLast; anIt++) {
111 theError = "It does not contain element with acceptable shape type. "
112 "The type should be one of the next: %1";
113 theError.arg(aTypes);
119 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
120 const TypeOfShape theShapeType,
121 Events_InfoMessage& theError) const
125 std::string anAttributeType = theAttribute->attributeType();
126 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
127 AttributeSelectionPtr anAttr =
128 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
129 GeomShapePtr aShape = anAttr->value();
131 aValid = isValidShape(aShape, theShapeType, theError);
133 aValid = isValidObject(anAttr->context(), theShapeType, theError);
135 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
136 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
137 if (anAttr->isObject()) {
138 aValid = isValidObject(anAttr->object(), theShapeType, theError);
140 else if (theShapeType == Vertex) {
141 AttributePtr aRefAttr = anAttr->attr();
142 if (!aRefAttr.get()){
144 theError = "It has reference to an empty attribute";
147 std::string anAttributeType = aRefAttr->attributeType();
148 aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
150 theError = "Shape type is \"%1\", it should be \"%2\"";
151 theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
156 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
157 AttributeReferencePtr anAttr =
158 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
159 aValid = isValidObject(anAttr->value(), theShapeType, theError);
161 else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
162 AttributeSelectionListPtr aListAttr =
163 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
164 // the Empty value means that the attribute selection list is valid if it is empty
165 if (aListAttr->size() == 0 && theShapeType == Empty) {
168 aValid = false; // the list should have elements if the shape type is not Empty
169 for (int i = 0; i < aListAttr->size(); i++) {
170 aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
171 if (!aValid) // if at least one attribute is invalid, the result is false
177 theError = "The attribute with the %1 type is not processed";
178 theError.arg(anAttributeType);
183 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
184 const TypeOfShape theShapeType,
185 Events_InfoMessage& theError) const
188 if (!theObject.get()) {
189 if(theShapeType != Empty) {
191 theError = "The object is empty";
195 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
196 if( theShapeType==Plane )
198 ResultConstructionPtr aResultConstruction =
199 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
200 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
201 const std::string& aKind = aFeature->getKind();
202 return aResult.get() != NULL && aKind == "Plane";
204 if (!aResult.get()) {
206 theError = "The result is empty";
209 aValid = isValidShape(aResult->shape(), theShapeType, theError);
215 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
216 const TypeOfShape theShapeType,
217 Events_InfoMessage& theError) const
221 if (!theShape.get()) {
223 theError = "The shape is empty";
226 switch (theShapeType) {
228 aValid = theShape->isVertex();
231 aValid = theShape->isEdge();
234 aValid = theShape->isEdge() && !GeomAPI_Curve(theShape).isCircle();
237 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
240 aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
243 aValid = theShape->isFace();
246 aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
249 aValid = theShape->isSolid() || theShape->isCompSolid() ||
250 theShape->isCompoundOfSolids();
253 aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
256 aValid = theShape->isCompound();