Salome HOME
Add white list for redirect some missing attributes to the feature.
[modules/shaper.git] / src / PythonAPI / model / extrusion.py
1 """Extrusion Interface
2 Author: Daniel Brunier-Coulin with contribution by Mikhail Ponikarov
3         and Sergey Pokhodenko
4 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
5 """
6
7 from .roots import Interface
8 from .sketcher.sketch import Sketch
9
10
11 def addExtrusion(part, *args):
12     """Add an Extrusion feature to the Part and return Extrusion.
13
14     Pass all args to Extrusion __init__ function.
15     """
16     assert(len(args) > 0 and args[0] is not None)
17     feature = part.addFeature("Extrusion")
18     return Extrusion(feature, *args)
19
20
21 class Extrusion(Interface):
22     """Interface on an Extrusion feature."""
23
24     def __init__(self, feature, base=None, *args):
25         """Initialize an Extrusion feature with given parameters.
26
27         Expected arguments for all modes:
28         feature -- an Extrusion feature
29
30         Expected arguments for initializing the feature:
31         base -- name, sketch or list of names and sketches.
32         If base is None then don't change the feature.
33
34         For BySize mode (expect 2 arguments):
35         to_size -- upper size
36         from_size -- lower size
37
38         For ByPlanesAndOffsets mode (expect 4 arguments):
39         to_object -- upper object (plane)
40         to_offset -- offset from upper object
41         from_object -- lower object (plane)
42         from_offset -- offset from lower object
43         """
44         Interface.__init__(self, feature)
45         assert(self._feature.getKind() == "Extrusion")
46
47         self._base = self._feature.data().selectionList("base")
48         self._CreationMethod = self._feature.string("CreationMethod")
49         self._to_size = self._feature.data().real("to_size")
50         self._from_size = self._feature.data().real("from_size")
51         self._to_object = self._feature.data().selection("to_object")
52         self._to_offset = self._feature.data().real("to_offset")
53         self._from_object = self._feature.data().selection("from_object")
54         self._from_offset = self._feature.data().real("from_offset")
55
56         if base is None:
57             return
58
59         self.__setBase(base)
60
61         if len(args) == 4:
62             self.__createByPlanesAndOffsets(*args)
63         elif len(args) == 2:
64             self.__createBySize(*args)
65         else:
66             raise Exception("cannot create the Extrusion")
67
68         self.__execute()
69         pass
70
71     def __setBase(self, base):
72         """Initialize base attribute of the feature.
73
74         Expected arguments:
75         base -- could be object of this types:
76         - string - topology name of object
77         - Sketch - sketch object
78         - iterable - list or tuple with string and/or Sketch objects
79         """
80         self._base.clear()
81         if isinstance(base, basestring):
82             self._base.append(base)
83         elif isinstance(base, Sketch):
84             self._base.append(base.result(), base.buildShape())
85         else:
86             for item in base:
87                 if isinstance(item, basestring):
88                     self._base.append(item)
89                 else:
90                     self._base.append(item.result(), item.buildShape())
91         pass
92
93     def __clear(self):
94         self._CreationMethod.setValue("BySizes")
95         self._to_size.setValue(0)
96         self._from_size.setValue(0)
97         self._to_object.setValue(None, None)
98         self._to_offset.setValue(0)
99         self._from_object.setValue(None, None)
100         self._from_offset.setValue(0)
101         pass
102
103     def __createBySize(self, to_size, from_size):
104         self.__clear()
105         self._CreationMethod.setValue("BySizes")
106         self._to_size.setValue(to_size)
107         self._from_size.setValue(from_size)
108         pass
109
110     def __createByPlanesAndOffsets(self, to_object, to_offset,
111                                    from_object, from_offset):
112         self.__clear()
113         self._CreationMethod.setValue("ByPlanesAndOffsets")
114         self._to_object.setValue(to_object.result(), to_object.buildShape())
115         self._to_offset.setValue(to_offset)
116         self._from_object.setValue(from_object.result(), from_object.buildShape())
117         self._from_offset.setValue(from_offset)
118         pass
119
120     def __execute(self):
121         if self.areInputValid():
122           self.execute()
123         else:
124           raise Exception("cannot execute Extrusion")
125
126     def setBase(self, base):
127         """Modify base attribute of the feature.
128
129         See __init__.
130         """
131         self.__setBase(base)
132         self.__execute()
133         pass
134
135     def setSize(self, *args):
136         """Modify the to_size, from_size attributes of the feature.
137
138         See __init__.
139         """
140         self.__createBySize(*args)
141         self.__execute()
142         pass
143
144     def setPlanesAndOffsets(self, *args):
145         """Modify planes and offsets attributes of the feature.
146
147         See __init__.
148         """
149         self.__createByPlanesAndOffsets(*args)
150         self.__execute()
151         pass