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