1 """Common tools for other modules.
2 Author: Sergey Pokhodenko
3 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
12 # from .sketcher.sketch import Sketch
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()
21 def get_value(attribute):
22 """Return simple type value from ModelAPI attribute if possible.
24 ModelAPI_AttributeDouble -> double
25 ModelAPI_AttributeString -> str
27 if (isinstance(attribute, ModelAPI.ModelAPI_AttributeDouble) or
28 isinstance(attribute, ModelAPI.ModelAPI_AttributeString)):
29 return attribute.value()
36 """Class for storing selection."""
38 def __init__(self, *args):
39 """Initialize selection."""
41 self.args = (None, None)
44 if len(args) == 1 and isinstance(args[0], basestring):
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)." %
53 if isinstance(args[0], basestring):
54 assert(isinstance(args[1], basestring))
56 # "args[1] should be str (%s given)." %
58 elif isinstance(args[0], ModelAPI.ModelAPI_Result) or args[0] is None:
59 assert(isinstance(args[1], GeomAPI.GeomAPI_Shape))
62 # "args[1] should be GeomAPI_Shape (%s given)." %
67 def fill_attribute(attribute, value):
68 """Set value to attribure.
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
75 if isinstance(attribute, ModelAPI.ModelAPI_AttributeSelectionList):
80 assert(isinstance(value, collections.Iterable))
82 assert(isinstance(item, Selection))
83 attribute.append(*item.args)
85 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeSelection):
87 attribute.setValue(None, None)
90 assert(isinstance(value, Selection))
91 attribute.setValue(*value.args)
93 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeDouble):
94 if isinstance(value, basestring):
95 attribute.setText(value)
97 attribute.setValue(value)
100 raise AssertionError("Wrong attribute type: %s" % type(attribute))