Salome HOME
Porting to SALOME_8.2.0
[modules/shaper.git] / src / GeomValidators / GeomValidators_Face.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3
4 #include "GeomValidators_Face.h"
5 #include "GeomValidators_Tools.h"
6
7 #include "ModelAPI_AttributeSelection.h"
8
9 #include <GeomAPI_Face.h>
10
11 #include <GeomAbs_SurfaceType.hxx>
12
13 #include <Events_InfoMessage.h>
14
15 typedef std::map<std::string, GeomAbs_SurfaceType> FaceTypes;
16 static FaceTypes MyFaceTypes;
17
18 GeomAbs_SurfaceType faceType(const std::string& theType)
19 {
20   if (MyFaceTypes.size() == 0) {
21     MyFaceTypes["plane"] = GeomAbs_Plane;
22     MyFaceTypes["cylinder"] = GeomAbs_Cylinder;
23   }
24   std::string aType = std::string(theType.c_str());
25   if (MyFaceTypes.find(aType) != MyFaceTypes.end())
26     return MyFaceTypes[aType];
27
28   Events_InfoMessage("GeomValidators_Face", "Face type defined in XML is not implemented!").send();
29   return GeomAbs_Plane;
30 }
31
32 bool GeomValidators_Face::isValid(const AttributePtr& theAttribute,
33                                   const std::list<std::string>& theArguments,
34                                   Events_InfoMessage& theError) const
35 {
36   std::string anAttributeType = theAttribute->attributeType();
37   if (anAttributeType != ModelAPI_AttributeSelection::typeId()) {
38     theError = "The attribute with the %1 type is not processed";
39     theError.arg(theAttribute->attributeType());
40     return false;
41   }
42
43   bool aValid = true;
44   ObjectPtr anObject = GeomValidators_Tools::getObject(theAttribute);
45   if (!anObject.get()) {
46     aValid = true; // an empty face selected is valid.
47   }
48   else {
49     AttributeSelectionPtr aSelectionAttr =
50       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
51     std::shared_ptr<GeomAPI_Shape> aGeomShape = aSelectionAttr->value();
52     if (!aGeomShape.get()) {
53       // if the shape is empty, apply the validator to the shape of result
54       aGeomShape = aSelectionAttr->context()->shape();
55     }
56     // it is necessary to check whether the shape is face in order to set in selection a value
57     // with any type and check the type in this validator
58     // It is realized to select any object in OB and filter it in this validator (sketch plane)
59     if (!aGeomShape->isFace()) {
60       aValid = false;
61       theError = "The shape is not a face.";
62     }
63     else {
64       std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
65       if (!aGeomFace.get()) {
66         aValid = false;
67         theError = "The shape is not a face.";
68       }
69       else {
70         GeomAbs_SurfaceType aFaceType = GeomAbs_Plane;
71         if (theArguments.size() == 1)
72           aFaceType = faceType(theArguments.front());
73
74         switch (aFaceType) {
75           case GeomAbs_Plane: {
76             aValid = aGeomFace->isPlanar();
77             if (!aValid)
78               theError = "The shape is not a plane.";
79           }
80           break;
81           case GeomAbs_Cylinder:{
82             aValid = aGeomFace->isCylindrical();
83             if (!aValid)
84               theError = "The shape is not a cylinder.";
85           }
86           break;
87           default: {
88             aValid = false;
89             theError = "The shape is not an available face.";
90             break;
91           }
92         }
93       }
94     }
95   }
96   return aValid;
97 }