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 if isinstance(args[0], basestring):
52 assert(isinstance(args[1], basestring))
53 elif isinstance(args[0], ModelAPI.ModelAPI_Result) or args[0] is None:
54 assert(isinstance(args[1], GeomAPI.GeomAPI_Shape))
58 def fill_attribute(attribute, value):
59 """Set value to attribure.
61 This function processes complex cases.
62 ModelAPI_AttributeSelectionList can accept string, result + shape, list of
63 strings and [result + shape]...
64 ModelAPI_AttributeDouble can accept float and string
66 if isinstance(attribute, ModelAPI.ModelAPI_AttributeSelectionList):
71 assert(isinstance(value, collections.Iterable))
73 assert(isinstance(item, Selection))
74 attribute.append(*item.args)
76 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeSelection):
78 attribute.setValue(None, None)
81 assert(isinstance(value, Selection))
82 attribute.setValue(*value.args)
84 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeDouble):
85 if isinstance(value, basestring):
86 attribute.setText(value)
88 attribute.setValue(value)
91 raise AssertionError("Wrong attribute type: %s" % type(attribute))