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];
65 Events_InfoMessage("Shape type defined in XML is not implemented!").send();
69 std::string getShapeTypeDescription(const GeomValidators_ShapeType::TypeOfShape& theType)
71 std::string aValue = "";
73 if (MyShapeTypes.size() != 0) {
74 std::map<std::string, GeomValidators_ShapeType::TypeOfShape>::const_iterator
75 anIt = MyShapeTypes.begin(), aLast = MyShapeTypes.end();
76 for (; anIt != aLast; anIt++) {
77 if (anIt->second == theType)
85 bool GeomValidators_ShapeType::isValid(const AttributePtr& theAttribute,
86 const std::list<std::string>& theArguments,
87 Events_InfoMessage& theError) const
91 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
92 // returns true if the attribute satisfies at least one of given arguments
93 for (; anIt != aLast; anIt++) {
94 TypeOfShape aShapeType = shapeType(*anIt);
95 // if arguments contain any shape type value, the validator returns true
96 if (aShapeType == AnyShape) {
100 if (isValidAttribute(theAttribute, aShapeType, theError)) {
105 if (!aValid && theError.empty()) {
107 std::list<std::string>::const_iterator anIt = theArguments.begin(), aLast = theArguments.end();
108 // returns true if the attribute satisfies at least one of given arguments
109 for (; anIt != aLast; anIt++) {
114 theError = "It does not contain element with acceptable shape type. "
115 "The type should be one of the next: %1";
116 theError.arg(aTypes);
122 bool GeomValidators_ShapeType::isValidAttribute(const AttributePtr& theAttribute,
123 const TypeOfShape theShapeType,
124 Events_InfoMessage& theError) const
128 std::string anAttributeType = theAttribute->attributeType();
129 if (anAttributeType == ModelAPI_AttributeSelection::typeId()) {
130 AttributeSelectionPtr anAttr =
131 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
132 GeomShapePtr aShape = anAttr->value();
134 aValid = isValidShape(aShape, theShapeType, anAttr->isGeometricalSelection(), theError);
136 if (anAttr->context().get())
137 aValid = isValidObject(anAttr->context(),
139 anAttr->isGeometricalSelection(),
142 aValid = isValidObject(anAttr->contextFeature(),
144 anAttr->isGeometricalSelection(),
148 else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
149 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
150 if (anAttr->isObject()) {
151 aValid = isValidObject(anAttr->object(),
156 else if (theShapeType == Vertex) {
157 AttributePtr aRefAttr = anAttr->attr();
158 if (!aRefAttr.get()){
160 theError = "It has reference to an empty attribute";
163 std::string anAttributeType = aRefAttr->attributeType();
164 aValid = anAttributeType == GeomDataAPI_Point2D::typeId();
167 theError = "Shape type is \"%1\", it should be \"%2\"";
168 theError.arg(anAttributeType).arg(getShapeTypeDescription(theShapeType));
174 else if (anAttributeType == ModelAPI_AttributeReference::typeId()) {
175 AttributeReferencePtr anAttr =
176 std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
177 aValid = isValidObject(anAttr->value(), theShapeType, false, theError);
179 else if (anAttributeType == ModelAPI_AttributeSelectionList::typeId()) {
180 AttributeSelectionListPtr aListAttr =
181 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
182 // the Empty value means that the attribute selection list is valid if it is empty
183 if (aListAttr->size() == 0 && theShapeType == Empty) {
186 aValid = false; // the list should have elements if the shape type is not Empty
187 for (int i = 0; i < aListAttr->size(); i++) {
188 aValid = isValidAttribute(aListAttr->value(i), theShapeType, theError);
189 if (!aValid) // if at least one attribute is invalid, the result is false
196 theError = "The attribute with the %1 type is not processed";
197 theError.arg(anAttributeType);
205 bool GeomValidators_ShapeType::isValidObject(const ObjectPtr& theObject,
206 const TypeOfShape theShapeType,
207 const bool theIsGeometricalSelection,
208 Events_InfoMessage& theError) const
211 if (!theObject.get()) {
212 if(theShapeType != Empty) {
214 theError = "The object is empty";
218 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
220 if (theShapeType == Plane)
222 ResultConstructionPtr aResultConstruction =
223 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
224 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
225 const std::string& aKind = aFeature->getKind();
226 return aResult.get() != NULL && aKind == "Plane";
228 if (!aResult.get()) {
230 theError = "The result is empty";
232 aValid = isValidShape(aResult->shape(), theShapeType, theIsGeometricalSelection, theError);
235 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
236 if (aFeature.get() && (theShapeType == CompSolid))
240 theError = "The feature has to produce a compsolid";
247 bool GeomValidators_ShapeType::isValidShape(const GeomShapePtr theShape,
248 const TypeOfShape theShapeType,
249 const bool theIsGeometricalSelection,
250 Events_InfoMessage& theError) const
254 if (!theShape.get()) {
256 theError = "The shape is empty";
259 switch (theShapeType) {
261 aValid = theShape->isVertex();
264 aValid = theShape->isEdge();
267 if (theIsGeometricalSelection && theShape->isCompound()) {
269 for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
270 if (!anIt.current()->isEdge() || !GeomAPI_Curve(anIt.current()).isLine()) {
277 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isLine();
282 aValid = theShape->isEdge() && GeomAPI_Curve(theShape).isCircle();
285 aValid = theShape->shapeType() == GeomAPI_Shape::WIRE;
288 aValid = theShape->isFace();
291 aValid = theShape->shapeType() == GeomAPI_Shape::SHELL;
294 aValid = theShape->isSolid() || theShape->isCompSolid() ||
295 theShape->isCompoundOfSolids();
298 aValid = theShape->shapeType() == GeomAPI_Shape::COMPSOLID;
301 aValid = theShape->isCompound();