From 8772e2a0380ba30d8b49f0016f518cda167dffbb Mon Sep 17 00:00:00 2001 From: spo Date: Thu, 22 Oct 2015 12:14:41 +0300 Subject: [PATCH] Add automatic generated getters. --- src/PythonAPI/Test/TestFeaturesExtrusion.py | 40 +++++++++++++++++++++ src/PythonAPI/model/roots.py | 13 +++++++ src/PythonAPI/model/tools.py | 25 +++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 src/PythonAPI/model/tools.py diff --git a/src/PythonAPI/Test/TestFeaturesExtrusion.py b/src/PythonAPI/Test/TestFeaturesExtrusion.py index 7d08ee901..2a6b07c11 100644 --- a/src/PythonAPI/Test/TestFeaturesExtrusion.py +++ b/src/PythonAPI/Test/TestFeaturesExtrusion.py @@ -53,6 +53,14 @@ class FeaturesAddExtrusionTestCase(FeaturesAddExtrusionFixture): sketch.selectFace(circle.result()), 10, 0) + self.assertEqual(extrusion.getCreationMethod(), "BySizes") + self.assertEqual(extrusion.getToSize(), 10) + self.assertEqual(extrusion.getFromSize(), 0) + self.assertEqual(extrusion.getToObject().context(), None) + self.assertEqual(extrusion.getToOffset(), 0) + self.assertEqual(extrusion.getFromObject().context(), None) + self.assertEqual(extrusion.getFromOffset(), 0) + def test_add_extrusion_by_face_and_planes(self): # base base_sketch = model.addSketch(self.part, model.defaultPlane("XOY")) @@ -76,6 +84,16 @@ class FeaturesAddExtrusionTestCase(FeaturesAddExtrusionFixture): to_sketch, 15, from_sketch, 20) + self.assertEqual(extrusion.getCreationMethod(), "ByPlanesAndOffsets") + self.assertEqual(extrusion.getToSize(), 0) + self.assertEqual(extrusion.getFromSize(), 0) +# self.assertEqual(extrusion.getToObject().context(), +# to_sketch.result()) + self.assertEqual(extrusion.getToOffset(), 15) +# self.assertEqual(extrusion.getFromObject().context(), +# from_sketch.result()) + self.assertEqual(extrusion.getFromOffset(), 20) + class FeaturesExtrusionTestCase(FeaturesExtrusionFixture): @@ -83,8 +101,30 @@ class FeaturesExtrusionTestCase(FeaturesExtrusionFixture): # call method of the feature self.assertEqual(self.extrusion.getKind(), "Extrusion") + def test_extrusion_get_attribute(self): + # call method of the feature + self.assertTrue(isinstance(self.extrusion.getBase(), + ModelAPI.ModelAPI_AttributeSelectionList)) + self.assertTrue(isinstance(self.extrusion.getCreationMethod(), + basestring)) + self.assertTrue(isinstance(self.extrusion.getToSize(), float)) + self.assertTrue(isinstance(self.extrusion.getFromSize(), float)) + self.assertTrue(isinstance(self.extrusion.getToObject(), + ModelAPI.ModelAPI_AttributeSelection)) + self.assertTrue(isinstance(self.extrusion.getToOffset(), float)) + self.assertTrue(isinstance(self.extrusion.getFromObject(), + ModelAPI.ModelAPI_AttributeSelection)) + self.assertTrue(isinstance(self.extrusion.getFromOffset(), float)) + def test_extrusion_set_sizes(self): self.extrusion.setSize(15, 20) + self.assertEqual(self.extrusion.getCreationMethod(), "BySizes") + self.assertEqual(self.extrusion.getToSize(), 15) + self.assertEqual(self.extrusion.getFromSize(), 20) + self.assertEqual(self.extrusion.getToObject().context(), None) + self.assertEqual(self.extrusion.getToOffset(), 0) + self.assertEqual(self.extrusion.getFromObject().context(), None) + self.assertEqual(self.extrusion.getFromOffset(), 0) def test_extrusion_set_planes_and_offsets(self): # base diff --git a/src/PythonAPI/model/roots.py b/src/PythonAPI/model/roots.py index dd239d342..b721821b4 100644 --- a/src/PythonAPI/model/roots.py +++ b/src/PythonAPI/model/roots.py @@ -5,6 +5,7 @@ Copyright (C) 2014-20xx CEA/DEN, EDF R&D import ModelAPI +from .tools import get_value, convert_to_underscore class Feature(ModelAPI.ModelAPI_Feature): @@ -36,8 +37,20 @@ class Interface(): def __getattr__(self, name): """Process missing attributes. + Add get*() methods for access feature attributes. Redirect missing attributes to the feature. """ + if name.startswith("get"): + possible_names = [ + "_" + name[3:], + "_" + convert_to_underscore(name[3:]), + ] + for possible_name in possible_names: + if hasattr(self, possible_name): + def getter(): + return get_value(getattr(self, possible_name)) + return getter + return self._feature.__getattribute__(name) def setRealInput (self, inputid, value): diff --git a/src/PythonAPI/model/tools.py b/src/PythonAPI/model/tools.py new file mode 100644 index 000000000..35bc3195b --- /dev/null +++ b/src/PythonAPI/model/tools.py @@ -0,0 +1,25 @@ +"""Common tools for other modules. +Author: Sergey Pokhodenko +Copyright (C) 2014-20xx CEA/DEN, EDF R&D +""" + +import re +import ModelAPI + +def convert_to_underscore(name): + """Convert CamelCase to underscore_case.""" + s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) + return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() + +def get_value(attribute): + """Return simple type value from ModelAPI attribute if possible. + + ModelAPI_AttributeDouble -> double + ModelAPI_AttributeString -> str + """ + if (isinstance(attribute, ModelAPI.ModelAPI_AttributeDouble) or + isinstance(attribute, ModelAPI.ModelAPI_AttributeString)): + return attribute.value() + + print type(attribute) + return attribute -- 2.39.2