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