]> SALOME platform Git repositories - modules/shaper.git/blob - src/PythonAPI/model/roots.py
Salome HOME
Fix model.services.defaultPlane()
[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         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
16         ModelAPI.ModelAPI_Feature.__init__(self)
17
18     def addRealInput(self, inputid):
19         """F.addRealInput(str) -- add real attribute"""
20         self.data().addAttribute(inputid,
21                                  ModelAPI.ModelAPI_AttributeDouble_typeId())
22
23     def getRealInput(self, inputid):
24         """F.getRealInput(str) -- get real value of the attribute"""
25         return self.data().real(inputid).value()
26
27     def getTextInput(self, inputid):
28         """F.getTextInput(str) -- get text value of the attribute"""
29         return self.data().real(inputid).text()
30
31     def addResult(self, result):
32         """F.addResult(ModelAPI_Result) -- add ModelAPI_Result shape as a result"""
33         shape = result.shape()
34         body = self.document().createBody(self.data())
35         body.store(shape)
36         self.setResult(body)
37
38
39 class Interface():
40     """Base class of high level Python interfaces to features."""
41
42     def __init__(self, feature):
43         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
44         self._feature = feature
45
46     def __getattr__(self, name):
47         """Process missing attributes.
48
49         Add get*() methods for access feature attributes.
50         """
51         if name.startswith("get"):
52             possible_names = [
53                 "_" + name[3:],
54                 "_" + tools.convert_to_underscore(name[3:]),
55                 ]
56             for possible_name in possible_names:
57                 if hasattr(self, possible_name):
58                     def getter():
59                         return getattr(self, possible_name)
60                     return getter
61
62         raise AttributeError()
63
64     def _fillAttribute(self, attribute, value):
65         """Fill ModelAPI_Attribute* with value."""
66         tools.fill_attribute(attribute, value)
67
68     def feature(self):
69         """Return ModelAPI_Feature."""
70         return self._feature
71
72     def getKind(self):
73         """Return the unique kind of the feature"""
74         return self._feature.getKind()
75
76     def results(self):
77         """Return current results of the feature"""
78         return self._feature.results()
79
80     def firstResult(self):
81         """Return the first result in the list of results"""
82         return self._feature.firstResult()
83
84     def lastResult(self):
85         """Return the last result in the list of results"""
86         return self._feature.lastResult()
87
88     def setRealInput(self, inputid, value):
89         """I.setRealInput(str, float) -- set real value to the attribute"""
90         self._feature.data().real(inputid).setValue(value)
91
92     def areInputValid(self):
93         """I.areInputValid() -> True or False validation result"""
94         validators = ModelAPI.ModelAPI_Session.get().validators()
95         return validators.validate(self._feature)
96
97     def execute(self):
98         """I.execute() -- validate and execute the feature.
99
100         Raises RuntimeError if validation fails.
101         """
102         if self.areInputValid():
103             self._feature.execute()
104         else:
105             raise RuntimeError("Can not execute %s: %s" %
106                                (self._feature.getKind(), self._feature.error())
107                                )