Salome HOME
0cf8ae2740fe8425cbb6d61a533a61e820f7b795
[modules/shaper.git] / src / PythonAPI / model / roots.py
1 """Abstract root classes of user-defined Python features producing a Body
2 Author: Daniel Brunier-Coulin
3 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 """
5
6 import ModelAPI
7
8 from .tools import get_value, convert_to_underscore
9
10
11 class Feature(ModelAPI.ModelAPI_Feature):
12     """Base class of user-defined Python features."""
13
14     def __init__(self):
15         ModelAPI.ModelAPI_Feature.__init__(self)
16
17     def addRealInput (self, inputid):
18         self.data().addAttribute(inputid,
19                                  ModelAPI.ModelAPI_AttributeDouble_typeId())
20
21     def getRealInput (self, inputid):
22         return self.data().real(inputid).value()
23
24     def addResult (self, result):
25         shape = result.shape()
26         body = self.document().createBody(self.data())
27         body.store(shape)
28         self.setResult(body)
29
30
31 class Interface():
32     """Base class of high level Python interfaces to features."""
33
34     def __init__(self, feature):
35         self._feature = feature
36
37     def __getattr__(self, name):
38         """Process missing attributes.
39
40         Add get*() methods for access feature attributes.
41         Redirect missing attributes to the feature.
42         """
43         if name.startswith("get"):
44             possible_names = [
45                 "_" + name[3:],
46                 "_" + convert_to_underscore(name[3:]),
47                 ]
48             for possible_name in possible_names:
49                 if hasattr(self, possible_name):
50                     def getter():
51                         return get_value(getattr(self, possible_name))
52                     return getter
53
54         return self._feature.__getattribute__(name)
55
56     def setRealInput(self, inputid, value):
57         self._feature.data().real(inputid).setValue(value)
58
59     def areInputValid(self):
60         validators = ModelAPI.ModelAPI_Session.get().validators()
61         return validators.validate(self._feature)
62
63     def execute(self):
64         self._feature.execute()