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 <GeomAPI_ShapeIterator.h>
26 #include <GeomDataAPI_Point2D.h>
28 #include <ModelAPI_Result.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_ResultConstruction.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_AttributeSelectionList.h>
33 #include <ModelAPI_AttributeReference.h>
35 #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["edge"] = Edge;
51 MyShapeTypes["line"] = Line;
52 MyShapeTypes["circle"] = Circle;
53 MyShapeTypes["wire"] = Wire;
54 MyShapeTypes["face"] = Face;
55 MyShapeTypes["plane"] = Plane;
56 MyShapeTypes["shell"] = Shell;
57 MyShapeTypes["solid"] = Solid;
58 MyShapeTypes["compsolid"] = CompSolid;
59 MyShapeTypes["compound"] = Compound;
61 std::string aType = std::string(theType.c_str());
62 if (MyShapeTypes.find(aType) != MyShapeTypes.end())
63 return MyShapeTypes[aType];
66 Events_InfoMessage("Shape type defined in XML is not implemented!").send();
72 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
74 std::string aValue = "";
76 if (MyShapeTypes.size() != 0) {
77 std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
78 anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
79 for (; anIt != aLast; anIt++) {
80 if (anIt->second == theType)
89 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
90 const std::list<std::string>& theArguments,
91 Events_InfoMessage& theError) const
95 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
96 // returns true if the attribute satisfies at least one of given arguments
97 for (; anIt != aLast; anIt++) {
98 TypeOfShape aShapeType = shapeType(*anIt);
99 // if arguments contain any shape type value, the validator returns true
100 if (aShapeType == AnyShape) {
104 if (isValidAttribute(theAttribute, aShapeType, theError)) {
109 if (!aValid && theError.empty()) {
111 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
112 // returns true if the attribute satisfies at least one of given arguments
113 for (; anIt != aLast; anIt++) {
118 theError = "It does not contain element with acceptable shape type. "
119 "The type should be one of the next: %1";
120 theError.arg(aTypes);
126 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
127 const TypeOfShape theShapeType,
128 Events_InfoMessage& theError) const
132 std::string anAttributeType = theAttribute->attributeType();
133 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
134 AttributeSelectionPtr anAttr =
135 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
136 GeomShapePtr aShape = anAttr->value();
138 aValid = isValidShape(aShape, theShapeType, anAttr->isGeometricalSelection(), theError);
140 if (anAttr->context().get())
141 aValid = isValidObject(anAttr->context(),
143 anAttr->isGeometricalSelection(),
146 aValid = isValidObject(anAttr->contextFeature(),
148 anAttr->isGeometricalSelection(),
152 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
153 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
154 if (anAttr->isObject()) {
155 aValid = isValidObject(anAttr->object(),
160 else if (theShapeType == Vertex) {
161 AttributePtr aRefAttr = anAttr->attr();
162 if (!aRefAttr.get()){
164 theError = "It has reference to an empty attribute";
167 std::string anAttributeType = aRefAttr->attributeType();
168 aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
171 theError = "Shape type is \"%1\", it should be \"%2\"";
172 theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
178 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
179 AttributeReferencePtr anAttr =
180 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
181 aValid = isValidObject(anAttr->value(), theShapeType, false, theError);
183 else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
184 AttributeSelectionListPtr aListAttr =
185 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
186 // the Empty value means that the attribute selection list is valid if it is empty
187 if (aListAttr->size() == 0 && theShapeType == Empty) {
190 aValid = false; // the list should have elements if the shape type is not Empty
191 for (int i = 0; i < aListAttr->size(); i++) {
192 aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
193 if (!aValid) // if at least one attribute is invalid, the result is false
200 theError = "The attribute with the %1 type is not processed";
201 theError.arg(anAttributeType);
209 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
210 const TypeOfShape theShapeType,
211 const bool theIsGeometricalSelection,
212 Events_InfoMessage& theError) const
215 if (!theObject.get()) {
216 if(theShapeType != Empty) {
218 theError = "The object is empty";
222 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
224 if (theShapeType == Plane)
226 ResultConstructionPtr aResultConstruction =
227 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
228 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
229 const std::string& aKind = aFeature->getKind();
230 return aResult.get() != NULL && aKind == "Plane";
232 if (!aResult.get()) {
234 theError = "The result is empty";
236 aValid = isValidShape(aResult->shape(), theShapeType, theIsGeometricalSelection, theError);
239 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
240 if (aFeature.get() && (theShapeType == CompSolid))
244 theError = "The feature has to produce a compsolid";
251 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
252 const TypeOfShape theShapeType,
253 const bool theIsGeometricalSelection,
254 Events_InfoMessage& theError) const
258 if (!theShape.get()) {
260 theError = "The shape is empty";
263 switch (theShapeType) {
265 aValid = theShape->isVertex();
268 aValid = theShape->isEdge();
271 if (theIsGeometricalSelection && theShape->isCompound()) {
273 for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
274 if (!anIt.current()->isEdge() || !GeomAPI_Curve(anIt.current()).isLine()) {
281 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine();
286 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
289 aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
292 aValid = theShape->isFace();
295 aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
298 aValid = theShape->isSolid() || theShape->isCompSolid() ||
299 theShape->isCompoundOfSolids();
302 aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
305 aValid = theShape->isCompound();