Salome HOME
7e87ace83ac5cc411f4e2862e5d371f10ba6489a
[modules/shaper.git] / src / PythonAPI / model / features / 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 model.roots import Interface
8 from model import Selection
9
10 def addExtrusion(part, *args):
11     """Add an Extrusion feature to the Part and return Extrusion.
12
13     This function has *3 signatures*:
14
15     .. function:: addExtrusion(base, size)
16
17     Arguments:
18         base(str, Sketch or list): base object(s)
19         size(double): size of the extrusion, the side is decided by the sign
20
21     .. function:: addExtrusion(base, to_size, from_size)
22
23     Arguments:
24         base(str, Sketch or list): base object(s)
25         to_size(double): upper size of the extrusion
26         from_size(double): lower size of the extrusion
27
28     .. function:: addExtrusion(base, to_object, to_offset, from_object, from_offset)
29
30     Arguments:
31         base(str, Sketch or list): base object(s)
32         to_object(plane): upper plane
33         to_offset(double): offset from upper object
34         from_object(plane): lower plane
35         from_offset(double): offset from lower plane
36
37     In all three cases the function returns an extrusion object
38
39     Returns:
40         Extrusion: extrusion object
41     """
42     assert(args)
43     feature = part.addFeature("Extrusion")
44     return Extrusion(feature, *args)
45
46
47 class Extrusion(Interface):
48     """Interface class for Extrusion feature.
49     """
50
51     def __init__(self, feature, *args):
52         """
53         Extrusion(feature) -> feature interface without initialization
54         Extrusion(feature, base, size) ->
55             feature interface initialized from arguments:
56             - base -- name, sketch or list of names and sketches
57             - size -- if positive -> to_size, if negative -> from_size
58         Extrusion(feature, base, to_size, from_size) ->
59             feature interface initialized from arguments:
60             - base -- name, sketch or list of names and sketches
61             - to_size -- upper size
62             - from_size -- lower size
63         Extrusion(feature, base, to_object, to_offset, from_object, from_offset) ->
64             feature interface initialized from arguments:
65             - base -- name, sketch or list of names and sketches
66             - to_object -- upper object (plane)
67             - to_offset -- offset from upper object
68             - from_object -- lower object (plane)
69             - from_offset -- offset from lower object
70         """
71         Interface.__init__(self, feature)
72         assert(self._feature.getKind() == "Extrusion")
73
74         self._base = self._feature.data().selectionList("base")
75         self._CreationMethod = self._feature.string("CreationMethod")
76         self._to_size = self._feature.data().real("to_size")
77         self._from_size = self._feature.data().real("from_size")
78         self._to_object = self._feature.data().selection("to_object")
79         self._to_offset = self._feature.data().real("to_offset")
80         self._from_object = self._feature.data().selection("from_object")
81         self._from_offset = self._feature.data().real("from_offset")
82
83         assert(self._base)
84         assert(self._CreationMethod)
85         assert(self._to_size)
86         assert(self._from_size)
87         assert(self._to_object)
88         assert(self._to_offset)
89         assert(self._from_object)
90         assert(self._from_offset)
91
92         if not args:
93             return
94
95         assert(len(args) in (2, 3, 5))
96         base = args[0]
97         args = args[1:]
98
99         self.setBase(base)
100
101         if len(args) == 4:
102             self.setPlanesAndOffsets(*args)
103         elif len(args) == 2:
104             self.setSizes(*args)
105         elif len(args) == 1:
106             self.setSize(args[0])
107
108         self.execute()
109         pass
110
111     def __clear(self):
112         self._CreationMethod.setValue("BySizes")
113         self._fillAttribute(self._to_size, 0)
114         self._fillAttribute(self._from_size, 0)
115         self._fillAttribute(self._to_object, None)
116         self._fillAttribute(self._to_offset, 0)
117         self._fillAttribute(self._from_object, None)
118         self._fillAttribute(self._from_offset, 0)
119         pass
120
121     def setBase(self, base):
122         """Modify base attribute of the feature.
123
124         See __init__.
125         """
126         self._fillAttribute(self._base, base)
127         pass
128
129     def setSizes(self, to_size, from_size, to_size_text="", from_size_text=""):
130         """Modify the to_size, from_size attributes of the feature.
131
132         See __init__.
133         """
134         # MPV: with "clear" calling here the extrusion all the time becomes modificed (height is set to 
135         # zero and then to actual value, but this function is used in macro Bax, that causes "modified"
136         # values without changes that causes cyclic dependency
137         #self.__clear()
138         self._fillAttribute(self._CreationMethod, "BySizes")
139         if to_size_text == "":
140             self._fillAttribute(self._to_size, to_size)
141         else:
142             self._fillAttribute(self._to_size, to_size_text)
143
144         if from_size_text == "":
145             self._fillAttribute(self._from_size, from_size)
146         else:
147             self._fillAttribute(self._to_size, from_size_text)
148         pass
149
150     def setSize(self, size, text=""):
151         """Modify the size of the feature.
152
153         If size is positive then initialize to_size with size.
154         If size is negative then initialize from_size with -size.
155         """
156         to_size, from_size = 0, 0
157         to_size_text, from_size_text = "", ""
158         if size >= 0:
159             to_size = size
160             to_size_text = text
161         else:
162             from_size = -size
163             from_size_text = text
164
165         self.setSizes(to_size, from_size, to_size_text, from_size_text)
166
167         pass
168
169     def setPlanesAndOffsets(self, to_object, to_offset,
170                             from_object, from_offset):
171         """Modify planes and offsets attributes of the feature.
172
173         See __init__.
174         """
175         self.__clear()
176         self._CreationMethod.setValue("ByPlanesAndOffsets")
177         self._fillAttribute(self._to_object, to_object)
178         self._fillAttribute(self._to_offset, to_offset)
179         self._fillAttribute(self._from_object, from_object)
180         self._fillAttribute(self._from_offset, from_offset)
181         pass
182
183     def result(self):
184         """F.result() -> list of Selection objects"""
185         return [Selection(result, result.shape()) for result in (self.firstResult(),)]