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_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>
40 typedef std::map<std::string, GeomValidators_ShapeType::TypeOfShape> EdgeTypes;
42 static EdgeTypes MyShapeTypes;
43 GeomValidators_ShapeType::TypeOfShape
44 GeomValidators_ShapeType::shapeType(const std::string& theType)
46 if (MyShapeTypes.size() == 0) {
47 MyShapeTypes["empty"] = Empty;
48 MyShapeTypes["vertex"] = Vertex;
49 MyShapeTypes["edge"] = Edge;
50 MyShapeTypes["line"] = Line;
51 MyShapeTypes["circle"] = Circle;
52 MyShapeTypes["wire"] = Wire;
53 MyShapeTypes["face"] = Face;
54 MyShapeTypes["plane"] = Plane;
55 MyShapeTypes["shell"] = Shell;
56 MyShapeTypes["solid"] = Solid;
57 MyShapeTypes["compsolid"] = CompSolid;
58 MyShapeTypes["compound"] = Compound;
60 std::string aType = std::string(theType.c_str());
61 if (MyShapeTypes.find(aType) != MyShapeTypes.end())
62 return MyShapeTypes[aType];
64 Events_InfoMessage("Shape type defined in XML is not implemented!").send();
68 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
70 std::string aValue = "";
72 if (MyShapeTypes.size() != 0) {
73 std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
74 anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
75 for (; anIt != aLast; anIt++) {
76 if (anIt->second == theType)
84 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
85 const std::list<std::string>& theArguments,
86 Events_InfoMessage& theError) const
90 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
91 // returns true if the attribute satisfies at least one of given arguments
92 for (; anIt != aLast; anIt++) {
93 TypeOfShape aShapeType = shapeType(*anIt);
94 // if arguments contain any shape type value, the validator returns true
95 if (aShapeType == AnyShape) {
99 if (isValidAttribute(theAttribute, aShapeType, theError)) {
104 if (!aValid && theError.empty()) {
106 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
107 // returns true if the attribute satisfies at least one of given arguments
108 for (; anIt != aLast; anIt++) {
113 theError = "It does not contain element with acceptable shape type. "
114 "The type should be one of the next: %1";
115 theError.arg(aTypes);
121 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
122 const TypeOfShape theShapeType,
123 Events_InfoMessage& theError) const
127 std::string anAttributeType = theAttribute->attributeType();
128 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
129 AttributeSelectionPtr anAttr =
130 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
131 GeomShapePtr aShape = anAttr->value();
133 aValid = isValidShape(aShape, theShapeType, theError);
135 if (anAttr->context().get())
136 aValid = isValidObject(anAttr->context(), theShapeType, theError);
138 aValid = isValidObject(anAttr->contextFeature(), theShapeType, theError);
141 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
142 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
143 if (anAttr->isObject()) {
144 aValid = isValidObject(anAttr->object(), theShapeType, theError);
146 else if (theShapeType == Vertex) {
147 AttributePtr aRefAttr = anAttr->attr();
148 if (!aRefAttr.get()){
150 theError = "It has reference to an empty attribute";
153 std::string anAttributeType = aRefAttr->attributeType();
154 aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
156 theError = "Shape type is \"%1\", it should be \"%2\"";
157 theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
162 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
163 AttributeReferencePtr anAttr =
164 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
165 aValid = isValidObject(anAttr->value(), theShapeType, theError);
167 else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
168 AttributeSelectionListPtr aListAttr =
169 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
170 // the Empty value means that the attribute selection list is valid if it is empty
171 if (aListAttr->size() == 0 && theShapeType == Empty) {
174 aValid = false; // the list should have elements if the shape type is not Empty
175 for (int i = 0; i < aListAttr->size(); i++) {
176 aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
177 if (!aValid) // if at least one attribute is invalid, the result is false
183 theError = "The attribute with the %1 type is not processed";
184 theError.arg(anAttributeType);
191 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
192 const TypeOfShape theShapeType,
193 Events_InfoMessage& theError) const
196 if (!theObject.get()) {
197 if(theShapeType != Empty) {
199 theError = "The object is empty";
203 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
205 if (theShapeType == Plane)
207 ResultConstructionPtr aResultConstruction =
208 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
209 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
210 const std::string& aKind = aFeature->getKind();
211 return aResult.get() != NULL && aKind == "Plane";
213 if (!aResult.get()) {
215 theError = "The result is empty";
217 aValid = isValidShape(aResult->shape(), theShapeType, theError);
220 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
221 if (aFeature.get() && (theShapeType == CompSolid))
225 theError = "The feature has to produce a compsolid";
232 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
233 const TypeOfShape theShapeType,
234 Events_InfoMessage& theError) const
238 if (!theShape.get()) {
240 theError = "The shape is empty";
243 switch (theShapeType) {
245 aValid = theShape->isVertex();
248 aValid = theShape->isEdge();
251 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine();
254 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
257 aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
260 aValid = theShape->isFace();
263 aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
266 aValid = theShape->isSolid() || theShape->isCompSolid() ||
267 theShape->isCompoundOfSolids();
270 aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
273 aValid = theShape->isCompound();