1 """Common tools for other modules.
2 Author: Sergey Pokhodenko
3 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
13 # from .sketcher.sketch import Sketch
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()
23 """Class for selection.
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
34 def __init__(self, *args):
35 """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
38 self.args = (None, None)
41 if len(args) == 1 and isinstance(args[0], basestring):
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))
55 def fill_attribute(attribute, value):
56 """Set value to attribure.
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
63 if (isinstance(attribute, ModelAPI.ModelAPI_AttributeBoolean) or
64 isinstance(attribute, ModelAPI.ModelAPI_AttributeDocRef) or
65 isinstance(attribute, ModelAPI.ModelAPI_AttributeInteger) or
66 isinstance(attribute, ModelAPI.ModelAPI_AttributeReference)
68 attribute.setValue(value)
70 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeString):
71 attribute.setValue(str(value))
73 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeDouble):
74 if isinstance(value, basestring):
75 attribute.setText(value)
77 attribute.setValue(value)
79 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeIntArray):
80 attrubute.setSize(len(value))
81 for i in range(len(value)):
82 attrubute.setValue(i, value[i])
84 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeRefAttr):
85 assert(isinstance(value, ModelAPI.ModelAPI_Attribute) or
86 isinstance(value, ModelAPI.ModelAPI_Object))
87 if isinstance(value, ModelAPI.ModelAPI_Attribute):
88 attribute.setAttr(value)
89 elif isinstance(value, ModelAPI.ModelAPI_Object):
90 attribute.setObject(value)
92 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeRefList):
97 assert(isinstance(value, collections.Iterable))
99 assert(isinstance(item, ModelAPI.ModelAPI_Object))
100 attribute.append(item)
102 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeSelection):
104 attribute.setValue(None, None)
107 assert(isinstance(value, Selection))
108 attribute.setValue(*value.args)
110 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeSelectionList):
115 assert(isinstance(value, collections.Iterable))
117 assert(isinstance(item, Selection))
118 attribute.append(*item.args)
120 elif (isinstance(attribute, GeomDataAPI.GeomDataAPI_Dir) or
121 isinstance(attribute, GeomDataAPI.GeomDataAPI_Point) or
122 isinstance(attribute, GeomDataAPI.GeomDataAPI_Point2D)
124 assert(isinstance(value, collections.Iterable))
125 attribute.setValue(*value)
128 raise AssertionError("Wrong attribute type: %s" % type(attribute))