Salome HOME
roots use tools
[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 model import tools
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         self._attribute_white_list = [
37             "getKind", "results", "firstResult", "lastResult",
38             ]
39
40     def __getattr__(self, name):
41         """Process missing attributes.
42
43         Add get*() methods for access feature attributes.
44         Redirect some missing attributes to the feature.
45         """
46         if name.startswith("get"):
47             possible_names = [
48                 "_" + name[3:],
49                 "_" + tools.convert_to_underscore(name[3:]),
50                 ]
51             for possible_name in possible_names:
52                 if hasattr(self, possible_name):
53                     def getter():
54                         return getattr(self, possible_name)
55                     return getter
56
57         if name in self._attribute_white_list:
58             return getattr(self._feature, name)
59
60         raise AttributeError()
61
62     def feature(self):
63         """Return ModelAPI_Feature."""
64         return self._feature
65
66     def _fill_attribute(self, attribute, value, *args):
67         """Fill ModelAPI_Attribute* with value."""
68         tools.fill_attribute(attribute, value, *args)
69
70     def setRealInput(self, inputid, value):
71         self._feature.data().real(inputid).setValue(value)
72
73     def areInputValid(self):
74         validators = ModelAPI.ModelAPI_Session.get().validators()
75         return validators.validate(self._feature)
76
77     def execute(self):
78         self._feature.execute()