Salome HOME
Update copyrights
[modules/shaper.git] / src / PythonAddons / macros / box / feature.py
1 # Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21 Box macro-feature
22 Authors: Renaud Nedelec - Daniel Brunier-Coulin
23 """
24
25 from salome.shaper import model
26 from salome.shaper import geom
27
28
29 class BoxFeature(model.Feature):
30     """An example of Box feature implementation.
31
32     BoxFeature() -> Box
33     """
34
35 # Initializations
36
37     def __init__(self):
38         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
39         model.Feature.__init__(self)
40
41     @staticmethod
42     def ID():
43         """Return Id of the feature."""
44         return "Box_script"
45
46     @staticmethod
47     def WIDTH_ID():
48         """Returns ID of Width parameter."""
49         return "width"
50
51     @staticmethod
52     def LENGTH_ID():
53         """Returns ID of Length parameter."""
54         return "length"
55
56     @staticmethod
57     def HEIGHT_ID():
58         """Returns ID of Height parameter."""
59         return "height"
60
61     def getKind(self):
62         """Override Feature.getKind()"""
63         return BoxFeature.ID()
64
65
66 # Creation of the box at default size
67
68     def initAttributes(self):
69         """Override Feature.initAttributes()"""
70         # Creating the input arguments of the feature
71         self.addRealInput(self.WIDTH_ID())
72         self.addRealInput(self.LENGTH_ID())
73         self.addRealInput(self.HEIGHT_ID())
74
75         # Creating the base of the box with unit values
76         mypart = model.activeDocument()
77         xoy = model.defaultPlane("XOY")
78
79         # A base of the geometry
80         self.base = model.addSketch(mypart, xoy)
81
82         p1 = geom.Pnt2d(0, 0)
83         p2 = geom.Pnt2d(0, 1)
84         p3 = geom.Pnt2d(1, 1)
85         p4 = geom.Pnt2d(1, 0)
86
87         line = model.addPolygon(self.base, p1, p2, p3, p4)
88
89         self.base.setFixed(line[0].startPoint())
90         self.base.setVertical(line[0])
91
92         # Setting the size of the base with default values
93         # Width
94         self.width = self.base.setLength(line[3], 50)  # Keeps the constraint for edition
95         # Length
96         self.length = self.base.setLength(line[0], 50)  # Keeps the constraint for edition
97
98         # Keeping the rectangle
99         self.base.setParallel(line[0], line[2])
100         self.base.setParallel(line[1], line[3])
101         self.base.setPerpendicular(line[0], line[3])
102
103         # execute sketch
104         mypart.setCurrentFeature(self.base.feature(), False)
105         model.updateFeatures()
106
107         # Creating the extrusion (the box) at default size
108         # A box result
109         self.box = model.addExtrusion(mypart, self.base.selectFace(), 50)
110
111 # Edition of the box at user size
112
113     def execute(self):
114         """F.execute() -- execute the feature"""
115         # Retrieving the user inputs
116         width = self.real(self.WIDTH_ID())
117         length = self.real(self.LENGTH_ID())
118         height = self.real(self.HEIGHT_ID())
119
120         # Editing the box
121         if width.text() == "":
122             self.base.setValue(self.width, width.value())
123         else:
124             self.base.setValue(self.width, width.text())
125
126         if length.text() == "":
127             self.base.setValue(self.length, length.value())
128         else:
129             self.base.setValue(self.length, length.text())
130
131         if (height.text() == ""):
132             self.box.setSize(height.value())
133         else:
134             self.box.setSize(height.text())
135
136         # Publishing the result: not needed for Macro feature
137         # self.addResult( self.box.result() )
138
139     def isMacro(self):
140         """Override Feature.initAttributes().
141         F.isMacro() -> True
142
143         Box feature is macro: removes itself on the creation transaction
144         finish.
145         """
146         return True