]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Add automatic generated getters.
authorspo <sergey.pokhodenko@opencascade.com>
Thu, 22 Oct 2015 09:14:41 +0000 (12:14 +0300)
committerspo <sergey.pokhodenko@opencascade.com>
Thu, 22 Oct 2015 09:14:41 +0000 (12:14 +0300)
src/PythonAPI/Test/TestFeaturesExtrusion.py
src/PythonAPI/model/roots.py
src/PythonAPI/model/tools.py [new file with mode: 0644]

index 7d08ee9010cb68260755a312ed730d689c681879..2a6b07c116bef1db9aded2b02ba3d70883d3bca2 100644 (file)
@@ -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
index dd239d3428f38b92932fe73a70a3633439128003..b721821b4abfd0dcd66e08c1f22a6b882a9572c2 100644 (file)
@@ -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 (file)
index 0000000..35bc319
--- /dev/null
@@ -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