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 ModelAPI.ModelAPI_Feature.__init__(self)
17 def addRealInput (self, inputid):
18 self.data().addAttribute(inputid,
19 ModelAPI.ModelAPI_AttributeDouble_typeId())
21 def getRealInput (self, inputid):
22 return self.data().real(inputid).value()
24 def addResult (self, result):
25 shape = result.shape()
26 body = self.document().createBody(self.data())
32 """Base class of high level Python interfaces to features."""
34 def __init__(self, feature):
35 self._feature = feature
36 self._attribute_white_list = [
38 "getKind", "results", "firstResult", "lastResult",
41 def __getattr__(self, name):
42 """Process missing attributes.
44 Add get*() methods for access feature attributes.
45 Redirect some missing attributes to the feature.
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 if name in self._attribute_white_list:
59 return getattr(self._feature, name)
61 raise AttributeError()
64 """Return ModelAPI_Feature."""
67 def _fill_attribute(self, attribute, value):
68 """Fill ModelAPI_Attribute* with value."""
69 tools.fill_attribute(attribute, value)
71 def setRealInput(self, inputid, value):
72 self._feature.data().real(inputid).setValue(value)
74 def areInputValid(self):
75 validators = ModelAPI.ModelAPI_Session.get().validators()
76 return validators.validate(self._feature)
79 if self.areInputValid():
80 self._feature.execute()
82 raise RuntimeError("Can not execute %s: %s" %
83 (self._feature.getKind(), self._feature.error())