Salome HOME
Add ConnectorAPI
[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 import GeomDataAPI
12
13 # from .sketcher.sketch import Sketch
14
15
16 def convert_to_underscore(name):
17     """Convert CamelCase to underscore_case."""
18     s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
19     return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
20
21
22 class Selection:
23     """Class for selection.
24
25     Selection() -> empty selection
26     Selection(name, type) -> selection initialized with arguments:
27         - name -- topological name
28         - type -- type of the object
29     Selection(context, shape) -> selection initialized with arguments:
30         - context -- ModelAPI_Result object
31         - shape -- GeomAPI_Shape shape
32     """
33
34     def __init__(self, *args):
35         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
36
37         if not args:
38             self.args = (None, None)
39             return
40
41         if len(args) == 1 and isinstance(args[0], basestring):
42             self.args = args
43             return
44
45         assert(len(args) > 1 and len(args) < 4)
46         assert(isinstance(args[0], basestring) or
47                isinstance(args[0], ModelAPI.ModelAPI_Result))
48         if isinstance(args[0], basestring):
49             assert(isinstance(args[1], basestring))
50         elif isinstance(args[0], ModelAPI.ModelAPI_Result) or args[0] is None:
51             assert(isinstance(args[1], GeomAPI.GeomAPI_Shape))
52         self.args = args
53
54
55 def fill_attribute(attribute, value):
56     """Set value to attribure.
57
58     This function processes complex cases.
59     ModelAPI_AttributeSelectionList can accept string, result + shape, list of
60     strings and [result + shape]...
61     ModelAPI_AttributeDouble can accept float and string
62     """
63     if (isinstance(attribute, ModelAPI.ModelAPI_AttributeBoolean) or
64         isinstance(attribute, ModelAPI.ModelAPI_AttributeDocRef) or
65         isinstance(attribute, ModelAPI.ModelAPI_AttributeReference)
66         ):
67         attribute.setValue(value)
68
69     elif isinstance(attribute, ModelAPI.ModelAPI_AttributeString):
70         attribute.setValue(str(value))
71
72     elif (isinstance(attribute, ModelAPI.ModelAPI_AttributeDouble) or
73           isinstance(attribute, ModelAPI.ModelAPI_AttributeInteger)
74           ):
75         if isinstance(value, basestring):
76             attribute.setText(value)
77         else:
78             attribute.setValue(value)
79
80     elif isinstance(attribute, ModelAPI.ModelAPI_AttributeIntArray):
81         attrubute.setSize(len(value))
82         for i in range(len(value)):
83             attrubute.setValue(i, value[i])
84
85     elif isinstance(attribute, ModelAPI.ModelAPI_AttributeRefAttr):
86         assert(isinstance(value, ModelAPI.ModelAPI_Attribute) or
87                isinstance(value, ModelAPI.ModelAPI_Object))
88         if isinstance(value, ModelAPI.ModelAPI_Attribute):
89             attribute.setAttr(value)
90         elif isinstance(value, ModelAPI.ModelAPI_Object):
91             attribute.setObject(value)
92
93     elif isinstance(attribute, ModelAPI.ModelAPI_AttributeRefList):
94         attribute.clear()
95         if not value:
96             return
97
98         assert(isinstance(value, collections.Iterable))
99         for item in value:
100             assert(isinstance(item, ModelAPI.ModelAPI_Object))
101             attribute.append(item)
102
103     elif isinstance(attribute, ModelAPI.ModelAPI_AttributeRefAttrList):
104         attribute.clear()
105         if not value:
106             return
107
108         assert(isinstance(value, collections.Iterable))
109         for item in value:
110             assert(isinstance(item, ModelAPI.ModelAPI_Attribute))
111             attribute.append(item)
112
113     elif isinstance(attribute, ModelAPI.ModelAPI_AttributeSelection):
114         if value is None:
115             attribute.setValue(None, None)
116             return
117
118         assert(isinstance(value, Selection))
119         attribute.setValue(*value.args)
120
121     elif isinstance(attribute, ModelAPI.ModelAPI_AttributeSelectionList):
122         attribute.clear()
123         if not value:
124             return
125
126         assert(isinstance(value, collections.Iterable))
127         for item in value:
128             assert(isinstance(item, Selection))
129             attribute.append(*item.args)
130
131     elif (isinstance(attribute, GeomDataAPI.GeomDataAPI_Dir) or
132           isinstance(attribute, GeomDataAPI.GeomDataAPI_Point) or
133           isinstance(attribute, GeomDataAPI.GeomDataAPI_Point2D)
134           ):
135         assert(isinstance(value, collections.Iterable))
136         attribute.setValue(*value)
137
138     else:
139         raise AssertionError("Wrong attribute type: %s" % type(attribute))