Salome HOME
sketch.selectFace() returns Selection
[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) or args[0] is None:
59             assert(isinstance(args[1], GeomAPI.GeomAPI_Shape))
60
61 #                    ,
62 #                    "args[1] should be GeomAPI_Shape (%s given)." %
63 #                    type(args[1]))
64         self.args = args
65
66
67 def fill_attribute(attribute, value):
68     """Set value to attribure.
69
70     This function processes complex cases.
71     ModelAPI_AttributeSelectionList can accept string, result + shape, list of
72     strings and [result + shape]...
73     ModelAPI_AttributeDouble can accept float and string
74     """
75     if isinstance(attribute, ModelAPI.ModelAPI_AttributeSelectionList):
76         attribute.clear()
77         if not value:
78             return
79
80         assert(isinstance(value, collections.Iterable))
81         for item in value:
82             assert(isinstance(item, Selection))
83             attribute.append(*item.args)
84
85     elif isinstance(attribute, ModelAPI.ModelAPI_AttributeSelection):
86         if value is None:
87             attribute.setValue(None, None)
88             return
89
90         assert(isinstance(value, Selection))
91         attribute.setValue(*value.args)
92
93     elif isinstance(attribute, ModelAPI.ModelAPI_AttributeDouble):
94         if isinstance(value, basestring):
95             attribute.setText(value)
96         else:
97             attribute.setValue(value)
98
99     else:
100         raise AssertionError("Wrong attribute type: %s" % type(attribute))