Salome HOME
Add revolution and repackage Features plugin interface.
[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
9
10 def addExtrusion(part, *args):
11     """Add an Extrusion feature to the Part and return Extrusion.
12
13     Pass all args to Extrusion __init__ function.
14     """
15     assert(len(args) > 0 and args[0] is not None)
16     feature = part.addFeature("Extrusion")
17     return Extrusion(feature, *args)
18
19
20 class Extrusion(Interface):
21     """Interface on an Extrusion feature."""
22
23     def __init__(self, feature, base=None, *args):
24         """Initialize an Extrusion feature with given parameters.
25
26         Expected arguments for all modes:
27         feature -- an Extrusion feature
28
29         Expected arguments for initializing the feature:
30         base -- name, sketch or list of names and sketches.
31         If base is None then don't change the feature.
32
33         For BySize mode (expect 2 arguments):
34         to_size -- upper size
35         from_size -- lower size
36
37         For ByPlanesAndOffsets mode (expect 4 arguments):
38         to_object -- upper object (plane)
39         to_offset -- offset from upper object
40         from_object -- lower object (plane)
41         from_offset -- offset from lower object
42         """
43         Interface.__init__(self, feature)
44         assert(self._feature.getKind() == "Extrusion")
45
46         self._base = self._feature.data().selectionList("base")
47         self._CreationMethod = self._feature.string("CreationMethod")
48         self._to_size = self._feature.data().real("to_size")
49         self._from_size = self._feature.data().real("from_size")
50         self._to_object = self._feature.data().selection("to_object")
51         self._to_offset = self._feature.data().real("to_offset")
52         self._from_object = self._feature.data().selection("from_object")
53         self._from_offset = self._feature.data().real("from_offset")
54
55         if base is None:
56             return
57
58         self.__setBase(base)
59
60         if len(args) == 4:
61             self.__createByPlanesAndOffsets(*args)
62         elif len(args) == 2:
63             self.__createBySizes(*args)
64         else:
65             raise AssertionError(
66                 "Extrusion takes 4 or 6 arguments (%s given)." % (len(args) + 2)
67                 )
68
69         self.__execute()
70         pass
71
72     def __setBase(self, base):
73         self._fill_attribute(self._base, base)
74         pass
75
76     def __clear(self):
77         self._CreationMethod.setValue("BySizes")
78         self._fill_attribute(self._to_size, 0)
79         self._fill_attribute(self._from_size, 0)
80         self._fill_attribute(self._to_object, None)
81         self._fill_attribute(self._to_offset, 0)
82         self._fill_attribute(self._from_object, None)
83         self._fill_attribute(self._from_offset, 0)
84         pass
85
86     def __createBySizes(self, to_size, from_size):
87         self.__clear()
88         self._CreationMethod.setValue("BySizes")
89         self._fill_attribute(self._to_size, to_size)
90         self._fill_attribute(self._from_size, from_size)
91         pass
92
93     def __createByPlanesAndOffsets(self, to_object, to_offset,
94                                    from_object, from_offset):
95         self.__clear()
96         self._CreationMethod.setValue("ByPlanesAndOffsets")
97         self._fill_attribute(self._to_object, to_object)
98         self._fill_attribute(self._to_offset, to_offset)
99         self._fill_attribute(self._from_object, from_object)
100         self._fill_attribute(self._from_offset, from_offset)
101         pass
102
103     def __execute(self):
104         if self.areInputValid():
105             self.execute()
106         else:
107             raise Exception("Cannot execute Extrusion: %s" %
108                             self._feature.error())
109
110     def setBase(self, base):
111         """Modify base attribute of the feature.
112
113         See __init__.
114         """
115         self.__setBase(base)
116         self.__execute()
117         pass
118
119     def setSizes(self, *args):
120         """Modify the to_size, from_size attributes of the feature.
121
122         See __init__.
123         """
124         self.__createBySizes(*args)
125         self.__execute()
126         pass
127
128     def setPlanesAndOffsets(self, *args):
129         """Modify planes and offsets attributes of the feature.
130
131         See __init__.
132         """
133         self.__createByPlanesAndOffsets(*args)
134         self.__execute()
135         pass