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
8 from model import tools
11 class Feature(ModelAPI.ModelAPI_Feature):
12 """Base class of user-defined Python features."""
15 """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
16 ModelAPI.ModelAPI_Feature.__init__(self)
18 def addRealInput(self, inputid):
19 """F.addRealInput(str) -- add real attribute"""
20 self.data().addAttribute(inputid,
21 ModelAPI.ModelAPI_AttributeDouble_typeId())
23 def getRealInput(self, inputid):
24 """F.getRealInput(str) -- get real value of the attribute"""
25 return self.data().real(inputid).value()
27 def addResult(self, result):
28 """F.addResult(ModelAPI_Result) -- add ModelAPI_Result shape as a result"""
29 shape = result.shape()
30 body = self.document().createBody(self.data())
36 """Base class of high level Python interfaces to features."""
38 def __init__(self, feature):
39 """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
40 self._feature = feature
42 def __getattr__(self, name):
43 """Process missing attributes.
45 Add get*() methods for access feature attributes.
47 if name.startswith("get"):
50 "_" + tools.convert_to_underscore(name[3:]),
52 for possible_name in possible_names:
53 if hasattr(self, possible_name):
55 return getattr(self, possible_name)
58 raise AttributeError()
60 def _fillAttribute(self, attribute, value):
61 """Fill ModelAPI_Attribute* with value."""
62 tools.fill_attribute(attribute, value)
65 """Return ModelAPI_Feature."""
69 """Return the unique kind of the feature"""
70 return self._feature.getKind()
73 """Return current results of the feature"""
74 return self._feature.results()
76 def firstResult(self):
77 """Return the first result in the list of results"""
78 return self._feature.firstResult()
81 """Return the last result in the list of results"""
82 return self._feature.lastResult()
84 def setRealInput(self, inputid, value):
85 """I.setRealInput(str, float) -- set real value to the attribute"""
86 self._feature.data().real(inputid).setValue(value)
88 def areInputValid(self):
89 """I.areInputValid() -> True or False validation result"""
90 validators = ModelAPI.ModelAPI_Session.get().validators()
91 return validators.validate(self._feature)
94 """I.execute() -- validate and execute the feature.
96 Raises RuntimeError if validation fails.
98 if self.areInputValid():
99 self._feature.execute()
101 raise RuntimeError("Can not execute %s: %s" %
102 (self._feature.getKind(), self._feature.error())