Salome HOME
Adjust unit tests
[modules/shaper.git] / src / PythonAddons / macros / box / feature.py
1 """Box macro-feature
2 Authors: Renaud Nedelec - Daniel Brunier-Coulin
3 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 """
5
6 import model
7 import geom
8
9
10 class BoxFeature(model.Feature):
11     """An example of Box feature implementation.
12
13     BoxFeature() -> Box
14     """
15
16 # Initializations
17
18     def __init__(self):
19         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
20         model.Feature.__init__(self)
21
22     @staticmethod
23     def ID():
24         """Return Id of the feature."""
25         return "Box_script"
26
27     @staticmethod
28     def WIDTH_ID():
29         """Returns ID of Width parameter."""
30         return "width"
31
32     @staticmethod
33     def LENGTH_ID():
34         """Returns ID of Length parameter."""
35         return "length"
36
37     @staticmethod
38     def HEIGHT_ID():
39         """Returns ID of Height parameter."""
40         return "height"
41
42     def getKind(self):
43         """Override Feature.getKind()"""
44         return BoxFeature.ID()
45
46
47 # Creation of the box at default size
48
49     def initAttributes(self):
50         """Override Feature.initAttributes()"""
51         # Creating the input arguments of the feature
52         self.addRealInput(self.WIDTH_ID())
53         self.addRealInput(self.LENGTH_ID())
54         self.addRealInput(self.HEIGHT_ID())
55
56         # Creating the base of the box with unit values
57         mypart = model.activeDocument()
58         xoy = model.defaultPlane("XOY")
59
60         # A base of the geometry
61         self.base = model.addSketch(mypart, xoy)
62
63         p1 = geom.Pnt2d(0, 0)
64         p2 = geom.Pnt2d(0, 1)
65         p3 = geom.Pnt2d(1, 1)
66         p4 = geom.Pnt2d(1, 0)
67
68         line = model.addPolygon(self.base, p1, p2, p3, p4)
69
70         # Setting the size of the base with default values
71         # Width
72         self.width = self.base.setLength(line[0], 50)  # Keeps the constraint for edition
73         # Length
74         self.length = self.base.setLength(line[3], 50)  # Keeps the constraint for edition
75
76         # Keeping the rectangle
77         self.base.setParallel(line[0], line[2])
78         self.base.setParallel(line[1], line[3])
79         self.base.setPerpendicular(line[0], line[3])
80
81         # execute sketch
82         model.do()
83
84         # Creating the extrusion (the box) at default size
85         # A box result
86         self.box = model.addExtrusion(mypart, self.base.selectFace(), 50)
87
88 # Edition of the box at user size
89
90     def execute(self):
91         """F.execute() -- execute the feature"""
92         # Retrieving the user inputs
93         width = self.real(self.WIDTH_ID())
94         length = self.real(self.LENGTH_ID())
95         height = self.real(self.HEIGHT_ID())
96
97         # Editing the box
98         if width.text() == "":
99             self.base.setValue(self.width, width.value())
100         else:
101             self.base.setValue(self.width, width.text())
102
103         if length.text() == "":
104             self.base.setValue(self.length, length.value())
105         else:
106             self.base.setValue(self.length, length.text())
107
108         if (height.text() == ""):
109             self.box.setSize(height.value())
110         else:
111             self.box.setSize(height.text())
112
113         # Publishing the result: not needed for Macro feature
114         # self.addResult( self.box.result() )
115
116     def isMacro(self):
117         """Override Feature.initAttributes().
118         F.isMacro() -> True
119
120         Box feature is macro: removes itself on the creation transaction
121         finish.
122         """
123         return True