Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomValidators / GeomValidators_Face.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomValidators_Face.h"
21 #include "GeomValidators_Tools.h"
22
23 #include "ModelAPI_AttributeSelection.h"
24
25 #include <GeomAPI_Face.h>
26 #include <GeomAPI_ShapeIterator.h>
27
28 #include <GeomAbs_SurfaceType.hxx>
29
30 #include <Events_InfoMessage.h>
31
32 typedef std::map<std::string, GeomAbs_SurfaceType> FaceTypes;
33 static FaceTypes MyFaceTypes;
34
35 GeomAbs_SurfaceType faceType(const std::string& theType)
36 {
37   if (MyFaceTypes.size() == 0) {
38     MyFaceTypes["plane"] = GeomAbs_Plane;
39     MyFaceTypes["cylinder"] = GeomAbs_Cylinder;
40   }
41   std::string aType = std::string(theType.c_str());
42   if (MyFaceTypes.find(aType) != MyFaceTypes.end())
43     return MyFaceTypes[aType];
44
45 // LCOV_EXCL_START
46   Events_InfoMessage("GeomValidators_Face", "Face type defined in XML is not implemented!").send();
47   return GeomAbs_Plane;
48 // LCOV_EXCL_STOP
49 }
50
51 bool isValidFace(const GeomShapePtr theShape,
52                  const GeomAbs_SurfaceType theFaceType,
53                  Events_InfoMessage& theError)
54 {
55   GeomFacePtr aGeomFace = theShape->face();
56
57   if (!aGeomFace.get()) {
58     theError = "The shape is not a face.";
59       return false;
60   }
61
62   bool aValid = true;
63
64   switch (theFaceType) {
65     case GeomAbs_Plane: {
66       aValid = aGeomFace->isPlanar();
67       if (!aValid) theError = "The shape is not a plane.";
68       break;
69     }
70     case GeomAbs_Cylinder: {
71       aValid = aGeomFace->isCylindrical();
72       if (!aValid) theError = "The shape is not a cylinder.";
73       break;
74     }
75     default: {
76       aValid = false;
77       theError = "The shape is not an available face.";
78       break;
79     }
80   }
81
82   return aValid;
83 }
84
85 bool GeomValidators_Face::isValid(const AttributePtr& theAttribute,
86                                   const std::list<std::string>& theArguments,
87                                   Events_InfoMessage& theError) const
88 {
89   std::string anAttributeType = theAttribute->attributeType();
90   if (anAttributeType != ModelAPI_AttributeSelection::typeId()) {
91 // LCOV_EXCL_START
92     theError = "The attribute with the %1 type is not processed";
93     theError.arg(theAttribute->attributeType());
94     return false;
95 // LCOV_EXCL_STOP
96   }
97
98   bool aValid = true;
99   ObjectPtr anObject = GeomValidators_Tools::getObject(theAttribute);
100   if (!anObject.get()) {
101     aValid = true; // an empty face selected is valid.
102   }
103   else {
104     AttributeSelectionPtr aSelectionAttr =
105       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
106     std::shared_ptr<GeomAPI_Shape> aGeomShape = aSelectionAttr->value();
107     if (!aGeomShape.get()) {
108       // if the shape is empty, apply the validator to the shape of result
109       aGeomShape = aSelectionAttr->context()->shape();
110     }
111     // it is necessary to check whether the shape is face in order to set in selection a value
112     // with any type and check the type in this validator
113     // It is realized to select any object in OB and filter it in this validator (sketch plane)
114     GeomAbs_SurfaceType aFaceType = GeomAbs_Plane;
115     if (theArguments.size() == 1)
116       aFaceType = faceType(theArguments.front());
117     if (aGeomShape->isFace()) {
118       aValid = isValidFace(aGeomShape, aFaceType, theError);
119     }
120     else if (aSelectionAttr->isGeometricalSelection() && aGeomShape->isCompound()) {
121       for (GeomAPI_ShapeIterator anIt(aGeomShape); anIt.more() && aValid; anIt.next()) {
122         aValid = isValidFace(anIt.current(), aFaceType, theError);
123       }
124     }
125     else {
126       aValid = false;
127       theError = "The shape is not a face.";
128     }
129   }
130   return aValid;
131 }