Salome HOME
Add tools
[modules/shaper.git] / src / PythonAPI / model / tools.py
1 """Common tools for other modules.
2 Author: Sergey Pokhodenko
3 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 """
5
6 import re
7 import collections
8
9 import ModelAPI
10 import GeomAPI
11
12 # from .sketcher.sketch import Sketch
13
14
15 def convert_to_underscore(name):
16     """Convert CamelCase to underscore_case."""
17     s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
18     return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
19
20
21 def get_value(attribute):
22     """Return simple type value from ModelAPI attribute if possible.
23
24     ModelAPI_AttributeDouble -> double
25     ModelAPI_AttributeString -> str
26     """
27     if (isinstance(attribute, ModelAPI.ModelAPI_AttributeDouble) or
28             isinstance(attribute, ModelAPI.ModelAPI_AttributeString)):
29         return attribute.value()
30
31     print type(attribute)
32     return attribute
33
34
35 class Selection:
36     """Class for storing selection."""
37
38     def __init__(self, *args):
39         """Initialize selection."""
40         if not args:
41             self.args = (None, None)
42             return
43
44         if len(args) == 1 and isinstance(args[0], basestring):
45             self.args = args
46             return
47
48         assert(len(args) > 1 and len(args) < 4)
49         assert(isinstance(args[0], basestring) or
50                isinstance(args[0], ModelAPI.ModelAPI_Result))
51 #                 "args[0] should be str or ModelAPI_Result (%s given)." %
52 #                 type(args[0]))
53         if isinstance(args[0], basestring):
54             assert(isinstance(args[1], basestring))
55 #             ,
56 #                    "args[1] should be str (%s given)." %
57 #                    type(args[1]))
58         elif isinstance(args[0], ModelAPI.ModelAPI_Result):
59             assert(isinstance(args[1], GeomAPI.GeomAPI_Shape))
60 #                    ,
61 #                    "args[1] should be GeomAPI_Shape (%s given)." %
62 #                    type(args[1]))
63         self.args = args
64
65
66 def fill_attribute(attribute, value, *args):
67     """Set value to attribure.
68
69     This function processes complex cases.
70     ModelAPI_AttributeSelectionList can accept string, result + shape, list of
71     strings and [result + shape]...
72     ModelAPI_AttributeDouble can accept float and string
73     """
74     if isinstance(attribute, ModelAPI.ModelAPI_AttributeSelectionList):
75         attribute.clear()
76         if not value:
77             return
78
79         assert(isinstance(value, collections.Iterable))
80         for item in value:
81             assert(isinstance(item, Selection))
82             attribute.append(*item.args)
83
84     elif isinstance(attribute, ModelAPI.ModelAPI_AttributeSelection):
85         if value is None:
86             attribute.setValue(None, None)
87             return
88
89         assert(isinstance(value, Selection))
90         attribute.setValue(*value.args)
91
92     elif isinstance(attribute, ModelAPI.ModelAPI_AttributeDouble):
93         if isinstance(value, basestring):
94             attribute.setText(value)
95         else:
96             attribute.setValue(value)
97
98     else:
99         raise AssertionError("Wrong attribute type: %s" % type(attribute))