From e0c6f6e3a67f383162a2ece24377d3a006eeca03 Mon Sep 17 00:00:00 2001 From: spo Date: Mon, 26 Oct 2015 14:20:47 +0300 Subject: [PATCH] Add tools --- src/PythonAPI/model/tools.py | 76 +++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/src/PythonAPI/model/tools.py b/src/PythonAPI/model/tools.py index 35bc3195b..2a4a6ed73 100644 --- a/src/PythonAPI/model/tools.py +++ b/src/PythonAPI/model/tools.py @@ -4,16 +4,23 @@ Copyright (C) 2014-20xx CEA/DEN, EDF R&D """ import re +import collections + import ModelAPI +import GeomAPI + +# from .sketcher.sketch import Sketch + def convert_to_underscore(name): """Convert CamelCase to underscore_case.""" s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() + def get_value(attribute): """Return simple type value from ModelAPI attribute if possible. - + ModelAPI_AttributeDouble -> double ModelAPI_AttributeString -> str """ @@ -23,3 +30,70 @@ def get_value(attribute): print type(attribute) return attribute + + +class Selection: + """Class for storing selection.""" + + def __init__(self, *args): + """Initialize selection.""" + if not args: + self.args = (None, None) + return + + if len(args) == 1 and isinstance(args[0], basestring): + self.args = args + return + + assert(len(args) > 1 and len(args) < 4) + assert(isinstance(args[0], basestring) or + isinstance(args[0], ModelAPI.ModelAPI_Result)) +# "args[0] should be str or ModelAPI_Result (%s given)." % +# type(args[0])) + if isinstance(args[0], basestring): + assert(isinstance(args[1], basestring)) +# , +# "args[1] should be str (%s given)." % +# type(args[1])) + elif isinstance(args[0], ModelAPI.ModelAPI_Result): + assert(isinstance(args[1], GeomAPI.GeomAPI_Shape)) +# , +# "args[1] should be GeomAPI_Shape (%s given)." % +# type(args[1])) + self.args = args + + +def fill_attribute(attribute, value, *args): + """Set value to attribure. + + This function processes complex cases. + ModelAPI_AttributeSelectionList can accept string, result + shape, list of + strings and [result + shape]... + ModelAPI_AttributeDouble can accept float and string + """ + if isinstance(attribute, ModelAPI.ModelAPI_AttributeSelectionList): + attribute.clear() + if not value: + return + + assert(isinstance(value, collections.Iterable)) + for item in value: + assert(isinstance(item, Selection)) + attribute.append(*item.args) + + elif isinstance(attribute, ModelAPI.ModelAPI_AttributeSelection): + if value is None: + attribute.setValue(None, None) + return + + assert(isinstance(value, Selection)) + attribute.setValue(*value.args) + + elif isinstance(attribute, ModelAPI.ModelAPI_AttributeDouble): + if isinstance(value, basestring): + attribute.setText(value) + else: + attribute.setValue(value) + + else: + raise AssertionError("Wrong attribute type: %s" % type(attribute)) -- 2.39.2