]> SALOME platform Git repositories - modules/shaper.git/blob - src/PythonAPI/model/roots.py
Salome HOME
Issue #1648: Dump Python in the High Level Parameterized Geometry API
[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 class Feature(ModelAPI.ModelAPI_Feature):
9     """Base class of user-defined Python features."""
10
11     def __init__(self):
12         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
13         ModelAPI.ModelAPI_Feature.__init__(self)
14
15     def addRealInput(self, inputid):
16         """F.addRealInput(str) -- add real attribute"""
17         self.data().addAttribute(inputid,
18                                  ModelAPI.ModelAPI_AttributeDouble_typeId())
19
20     def getRealInput(self, inputid):
21         """F.getRealInput(str) -- get real value of the attribute"""
22         return self.data().real(inputid).value()
23
24     def getTextInput(self, inputid):
25         """F.getTextInput(str) -- get text value of the attribute"""
26         return self.data().real(inputid).text()
27
28     def addResult(self, result):
29         """F.addResult(ModelAPI_Result) -- add ModelAPI_Result shape as a result"""
30         shape = result.shape()
31         body = self.document().createBody(self.data())
32         body.store(shape)
33         self.setResult(body)
34
35
36 class Interface():
37     """Base class of high level Python interfaces to features."""
38
39     def __init__(self, feature):
40         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
41         self._feature = feature
42
43     def feature(self):
44         """Return ModelAPI_Feature."""
45         return self._feature
46
47     def getKind(self):
48         """Return the unique kind of the feature"""
49         return self._feature.getKind()
50
51     def results(self):
52         """Return current results of the feature"""
53         return self._feature.results()
54
55     def firstResult(self):
56         """Return the first result in the list of results"""
57         return self._feature.firstResult()
58
59     def lastResult(self):
60         """Return the last result in the list of results"""
61         return self._feature.lastResult()
62
63     def setRealInput(self, inputid, value):
64         """I.setRealInput(str, float) -- set real value to the attribute"""
65         self._feature.data().real(inputid).setValue(value)
66
67     def areInputValid(self):
68         """I.areInputValid() -> True or False validation result"""
69         validators = ModelAPI.ModelAPI_Session.get().validators()
70         return validators.validate(self._feature)
71
72     def execute(self):
73         """I.execute() -- validate and execute the feature.
74
75         Raises RuntimeError if validation fails.
76         """
77         if self.areInputValid():
78             self._feature.execute()
79         else:
80             raise RuntimeError("Can not execute %s: %s" %
81                                (self._feature.getKind(), self._feature.error())
82                                )