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