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_Face.h"
22 #include "GeomValidators_Tools.h"
24 #include "ModelAPI_AttributeSelection.h"
26 #include <GeomAPI_Face.h>
27 #include <GeomAPI_ShapeIterator.h>
29 #include <GeomAbs_SurfaceType.hxx>
31 #include <Events_InfoMessage.h>
33 typedef std::map<std::string, GeomAbs_SurfaceType> FaceTypes;
34 static FaceTypes MyFaceTypes;
36 GeomAbs_SurfaceType faceType(const std::string& theType)
38 if (MyFaceTypes.size() == 0) {
39 MyFaceTypes["plane"] = GeomAbs_Plane;
40 MyFaceTypes["cylinder"] = GeomAbs_Cylinder;
42 std::string aType = std::string(theType.c_str());
43 if (MyFaceTypes.find(aType) != MyFaceTypes.end())
44 return MyFaceTypes[aType];
46 Events_InfoMessage("GeomValidators_Face", "Face type defined in XML is not implemented!").send();
50 bool isValidFace(const GeomShapePtr theShape,
51 const GeomAbs_SurfaceType theFaceType,
52 Events_InfoMessage& theError)
54 GeomFacePtr aGeomFace = theShape->face();
56 if (!aGeomFace.get()) {
57 theError = "The shape is not a face.";
63 switch (theFaceType) {
65 aValid = aGeomFace->isPlanar();
66 if (!aValid) theError = "The shape is not a plane.";
69 case GeomAbs_Cylinder: {
70 aValid = aGeomFace->isCylindrical();
71 if (!aValid) theError = "The shape is not a cylinder.";
76 theError = "The shape is not an available face.";
84 bool GeomValidators_Face::isValid(const AttributePtr& theAttribute,
85 const std::list<std::string>& theArguments,
86 Events_InfoMessage& theError) const
88 std::string anAttributeType = theAttribute->attributeType();
89 if (anAttributeType != ModelAPI_AttributeSelection::typeId()) {
90 theError = "The attribute with the %1 type is not processed";
91 theError.arg(theAttribute->attributeType());
96 ObjectPtr anObject = GeomValidators_Tools::getObject(theAttribute);
97 if (!anObject.get()) {
98 aValid = true; // an empty face selected is valid.
101 AttributeSelectionPtr aSelectionAttr =
102 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
103 std::shared_ptr<GeomAPI_Shape> aGeomShape = aSelectionAttr->value();
104 if (!aGeomShape.get()) {
105 // if the shape is empty, apply the validator to the shape of result
106 aGeomShape = aSelectionAttr->context()->shape();
108 // it is necessary to check whether the shape is face in order to set in selection a value
109 // with any type and check the type in this validator
110 // It is realized to select any object in OB and filter it in this validator (sketch plane)
111 if (!aGeomShape->isFace()) {
113 theError = "The shape is not a face.";
116 GeomAbs_SurfaceType aFaceType = GeomAbs_Plane;
117 if (theArguments.size() == 1) aFaceType = faceType(theArguments.front());
118 if (aGeomShape->isFace()) {
119 isValidFace(aGeomShape, aFaceType, theError);
121 else if (aSelectionAttr->isGeometricalSelection() && aGeomShape->isCompound()) {
122 for (GeomAPI_ShapeIterator anIt(aGeomShape); anIt.more(); anIt.next()) {
123 if (!isValidFace(anIt.current(), aFaceType, theError)) {
130 theError = "The shape is not a face.";