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_AttributeReference)
67 attribute.setValue(value)
69 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeString):
70 attribute.setValue(str(value))
72 elif (isinstance(attribute, ModelAPI.ModelAPI_AttributeDouble) or
73 isinstance(attribute, ModelAPI.ModelAPI_AttributeInteger)
75 if isinstance(value, basestring):
76 attribute.setText(value)
78 attribute.setValue(value)
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])
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)
93 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeRefList):
98 assert(isinstance(value, collections.Iterable))
100 assert(isinstance(item, ModelAPI.ModelAPI_Object))
101 attribute.append(item)
103 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeRefAttrList):
108 assert(isinstance(value, collections.Iterable))
110 assert(isinstance(item, ModelAPI.ModelAPI_Attribute))
111 attribute.append(item)
113 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeSelection):
115 attribute.setValue(None, None)
118 assert(isinstance(value, Selection))
119 attribute.setValue(*value.args)
121 elif isinstance(attribute, ModelAPI.ModelAPI_AttributeSelectionList):
126 assert(isinstance(value, collections.Iterable))
128 assert(isinstance(item, Selection))
129 attribute.append(*item.args)
131 elif (isinstance(attribute, GeomDataAPI.GeomDataAPI_Dir) or
132 isinstance(attribute, GeomDataAPI.GeomDataAPI_Point) or
133 isinstance(attribute, GeomDataAPI.GeomDataAPI_Point2D)
135 assert(isinstance(value, collections.Iterable))
136 attribute.setValue(*value)
139 raise AssertionError("Wrong attribute type: %s" % type(attribute))