Salome HOME
Merge branch HELP_DOCUMENTATION_EDITING into master.
[modules/shaper.git] / src / PythonAddons / doc / examples / box / feature.py
1 ## Copyright (C) 2014-2017  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
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 """Box macro-feature
22 Authors: Renaud Nedelec - Daniel Brunier-Coulin
23 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
24 """
25
26 from salome.shaper import model
27 from salome.shaper import geom
28
29
30 class BoxFeature(model.Feature):
31     """An example of Box feature implementation.
32
33     BoxFeature() -> Box
34     """
35
36 # Initializations
37
38     def __init__(self):
39         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
40         model.Feature.__init__(self)
41
42     @staticmethod
43     def ID():
44         """Return Id of the feature."""
45         return "Box_script"
46
47     @staticmethod
48     def WIDTH_ID():
49         """Returns ID of Width parameter."""
50         return "width"
51
52     @staticmethod
53     def LENGTH_ID():
54         """Returns ID of Length parameter."""
55         return "length"
56
57     @staticmethod
58     def HEIGHT_ID():
59         """Returns ID of Height parameter."""
60         return "height"
61
62     def getKind(self):
63         """Override Feature.getKind()"""
64         return BoxFeature.ID()
65
66
67 # Creation of the box at default size
68
69     def initAttributes(self):
70         """Override Feature.initAttributes()"""
71         # Creating the input arguments of the feature
72         self.addRealInput(self.WIDTH_ID())
73         self.addRealInput(self.LENGTH_ID())
74         self.addRealInput(self.HEIGHT_ID())
75
76         # Creating the base of the box with unit values
77         mypart = model.activeDocument()
78         xoy = model.defaultPlane("XOY")
79
80         # A base of the geometry
81         self.base = model.addSketch(mypart, xoy)
82
83         p1 = geom.Pnt2d(0, 0)
84         p2 = geom.Pnt2d(0, 1)
85         p3 = geom.Pnt2d(1, 1)
86         p4 = geom.Pnt2d(1, 0)
87
88         line = model.addPolygon(self.base, p1, p2, p3, p4)
89
90         self.base.setFixed(line[0].startPoint())
91         self.base.setVertical(line[0])
92
93         # Setting the size of the base with default values
94         # Width
95         self.width = self.base.setLength(line[3], 50)  # Keeps the constraint for edition
96         # Length
97         self.length = self.base.setLength(line[0], 50)  # Keeps the constraint for edition
98
99         # Keeping the rectangle
100         self.base.setParallel(line[0], line[2])
101         self.base.setParallel(line[1], line[3])
102         self.base.setPerpendicular(line[0], line[3])
103
104         # execute sketch
105         mypart.setCurrentFeature(self.base.feature(), False)
106         model.updateFeatures()
107
108         # Creating the extrusion (the box) at default size
109         # A box result
110         self.box = model.addExtrusion(mypart, self.base.selectFace(), 50)
111
112 # Edition of the box at user size
113
114     def execute(self):
115         """F.execute() -- execute the feature"""
116         # Retrieving the user inputs
117         width = self.real(self.WIDTH_ID())
118         length = self.real(self.LENGTH_ID())
119         height = self.real(self.HEIGHT_ID())
120
121         # Editing the box
122         if width.text() == "":
123             self.base.setValue(self.width, width.value())
124         else:
125             self.base.setValue(self.width, width.text())
126
127         if length.text() == "":
128             self.base.setValue(self.length, length.value())
129         else:
130             self.base.setValue(self.length, length.text())
131
132         if (height.text() == ""):
133             self.box.setSize(height.value())
134         else:
135             self.box.setSize(height.text())
136
137         # Publishing the result: not needed for Macro feature
138         # self.addResult( self.box.result() )
139
140     def isMacro(self):
141         """Override Feature.initAttributes().
142         F.isMacro() -> True
143
144         Box feature is macro: removes itself on the creation transaction
145         finish.
146         """
147         return True