Salome HOME
fbc389ba86917a2485206f244f12c29d6f335d75
[modules/shaper.git] / src / ModuleBase / ModuleBase_ValidatorFace.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3
4 #include "ModuleBase_ValidatorFace.h"
5 #include "ModuleBase_WidgetShapeSelector.h"
6
7 #include "ModelAPI_AttributeSelection.h"
8
9 #include <GeomAPI_Face.h>
10
11 #include <Events_Error.h>
12
13 #include <QString>
14 #include <QMap>
15
16 typedef QMap<QString, GeomAbs_SurfaceType> FaceTypes;
17 static FaceTypes MyFaceTypes;
18
19 GeomAbs_SurfaceType ModuleBase_ValidatorFace::faceType(const std::string& theType)
20 {
21   if (MyFaceTypes.count() == 0) {
22     MyFaceTypes["plane"] = GeomAbs_Plane;
23     MyFaceTypes["cylinder"] = GeomAbs_Cylinder;
24   }
25   QString aType = QString(theType.c_str()).toLower();
26   if (MyFaceTypes.contains(aType))
27     return MyFaceTypes[aType];
28   
29   Events_Error::send("Face type defined in XML is not implemented!");
30   return GeomAbs_Plane;
31 }
32
33 bool ModuleBase_ValidatorFace::isValid(const AttributePtr& theAttribute,
34                                        const std::list<std::string>& theArguments) const
35 {
36   bool aValid = false;
37
38   GeomAbs_SurfaceType aFaceType = GeomAbs_Plane;
39   if (theArguments.size() == 1) {
40     std::string anArgument = theArguments.front();
41     aFaceType = faceType(anArgument);
42   }
43
44   ObjectPtr anObject = ModuleBase_WidgetShapeSelector::getObject(theAttribute);
45   if (anObject.get() != NULL) {
46     AttributeSelectionPtr aSelectionAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
47                                                                  (theAttribute);
48     std::shared_ptr<GeomAPI_Shape> aGeomShape = aSelectionAttr->value();
49     std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
50     if (aGeomFace.get() != NULL) {
51       switch(aFaceType) {
52           case GeomAbs_Plane:
53             aValid = aGeomFace->isPlanar();
54             break;
55           case GeomAbs_Cylinder:
56             break;
57           default:
58             break;
59       }
60     }
61   }
62   return aValid;
63 }