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>
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];
65 Events_InfoMessage("Shape type defined in XML is not implemented!").send();
71 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
73 std::string aValue = "";
75 if (MyShapeTypes.size() != 0) {
76 std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
77 anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
78 for (; anIt != aLast; anIt++) {
79 if (anIt->second == theType)
88 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
89 const std::list<std::string>& theArguments,
90 Events_InfoMessage& theError) const
94 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
95 // returns true if the attribute satisfies at least one of given arguments
96 for (; anIt != aLast; anIt++) {
97 TypeOfShape aShapeType = shapeType(*anIt);
98 // if arguments contain any shape type value, the validator returns true
99 if (aShapeType == AnyShape) {
103 if (isValidAttribute(theAttribute, aShapeType, theError)) {
108 if (!aValid && theError.empty()) {
110 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
111 // returns true if the attribute satisfies at least one of given arguments
112 for (; anIt != aLast; anIt++) {
117 theError = "It does not contain element with acceptable shape type. "
118 "The type should be one of the next: %1";
119 theError.arg(aTypes);
125 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
126 const TypeOfShape theShapeType,
127 Events_InfoMessage& theError) const
131 std::string anAttributeType = theAttribute->attributeType();
132 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
133 AttributeSelectionPtr anAttr =
134 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
135 GeomShapePtr aShape = anAttr->value();
137 aValid = isValidShape(aShape, theShapeType, anAttr->isGeometricalSelection(), theError);
139 if (anAttr->context().get())
140 aValid = isValidObject(anAttr->context(),
142 anAttr->isGeometricalSelection(),
145 aValid = isValidObject(anAttr->contextFeature(),
147 anAttr->isGeometricalSelection(),
151 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
152 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
153 if (anAttr->isObject()) {
154 aValid = isValidObject(anAttr->object(),
159 else if (theShapeType == Vertex) {
160 AttributePtr aRefAttr = anAttr->attr();
161 if (!aRefAttr.get()){
163 theError = "It has reference to an empty attribute";
166 std::string anAttributeType = aRefAttr->attributeType();
167 aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
170 theError = "Shape type is \"%1\", it should be \"%2\"";
171 theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
177 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
178 AttributeReferencePtr anAttr =
179 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
180 aValid = isValidObject(anAttr->value(), theShapeType, false, theError);
182 else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
183 AttributeSelectionListPtr aListAttr =
184 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
185 // the Empty value means that the attribute selection list is valid if it is empty
186 if (aListAttr->size() == 0 && theShapeType == Empty) {
189 aValid = false; // the list should have elements if the shape type is not Empty
190 for (int i = 0; i < aListAttr->size(); i++) {
191 aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
192 if (!aValid) // if at least one attribute is invalid, the result is false
199 theError = "The attribute with the %1 type is not processed";
200 theError.arg(anAttributeType);
208 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
209 const TypeOfShape theShapeType,
210 const bool theIsGeometricalSelection,
211 Events_InfoMessage& theError) const
214 if (!theObject.get()) {
215 if(theShapeType != Empty) {
217 theError = "The object is empty";
221 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
223 if (theShapeType == Plane)
225 ResultConstructionPtr aResultConstruction =
226 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
227 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
228 const std::string& aKind = aFeature->getKind();
229 return aResult.get() != NULL && aKind == "Plane";
231 if (!aResult.get()) {
233 theError = "The result is empty";
235 aValid = isValidShape(aResult->shape(), theShapeType, theIsGeometricalSelection, theError);
238 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
239 if (aFeature.get() && (theShapeType == CompSolid))
243 theError = "The feature has to produce a compsolid";
250 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
251 const TypeOfShape theShapeType,
252 const bool theIsGeometricalSelection,
253 Events_InfoMessage& theError) const
257 if (!theShape.get()) {
259 theError = "The shape is empty";
262 switch (theShapeType) {
264 aValid = theShape->isVertex();
267 aValid = theShape->isEdge();
270 if (theIsGeometricalSelection && theShape->isCompound()) {
272 for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
273 if (!anIt.current()->isEdge() || !GeomAPI_Curve(anIt.current()).isLine()) {
280 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine();
285 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
288 aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
291 aValid = theShape->isFace();
294 aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
297 aValid = theShape->isPlanar();
300 aValid = theShape->isSolid() || theShape->isCompSolid() ||
301 theShape->isCompoundOfSolids();
304 aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
307 aValid = theShape->isCompound();