Salome HOME
BoxFeature is rewritten + small corrections in APIs
[modules/shaper.git] / src / PythonFeaturesPlugin / PythonFeaturesPlugin_Box.py
1 import ModelAPI
2 import sketch
3 import extrusion
4 from SketchResult import SketchResult
5
6
7 class PythonFeaturesPlugin_Box(ModelAPI.ModelAPI_Feature):
8
9     "Feature to create a box by drawing a sketch and extruding it"
10
11     def __init__(self):
12         ModelAPI.ModelAPI_Feature.__init__(self)
13
14     @staticmethod
15     def ID():
16         return "Box"
17
18     @staticmethod
19     def WIDTH_ID():
20         return "box_width"
21
22     @staticmethod
23     def LENGTH_ID():
24         return "box_length"
25
26     @staticmethod
27     def HEIGHT_ID():
28         return "box_height"
29
30     @staticmethod
31     def WIDTH_REF_ID():
32         return "box_ref_width"
33
34     @staticmethod
35     def LENGTH_REF_ID():
36         return "box_ref_length"
37
38     @staticmethod
39     def HEIGHT_REF_ID():
40         return "box_ref_height"
41
42     def getKind(self):
43         return PythonFeaturesPlugin_Box.ID()
44
45     def initAttributes(self):
46         # C++ static methods (in example "type()" of the ModelAPI_AttributeDouble
47         # should be called like this: moduleName.ClassName_staticMethod()
48         self.data().addAttribute(self.WIDTH_ID(), ModelAPI.ModelAPI_AttributeDouble_type())
49         self.data().addAttribute(self.LENGTH_ID(), ModelAPI.ModelAPI_AttributeDouble_type())
50         self.data().addAttribute(self.HEIGHT_ID(), ModelAPI.ModelAPI_AttributeDouble_type())
51         self.data().addAttribute(self.WIDTH_REF_ID(), ModelAPI.ModelAPI_AttributeReference_type())
52         self.data().addAttribute(self.LENGTH_REF_ID(), ModelAPI.ModelAPI_AttributeReference_type())
53         self.data().addAttribute(self.HEIGHT_REF_ID(), ModelAPI.ModelAPI_AttributeReference_type())
54         aSession = ModelAPI.ModelAPI_Session.get()
55         aSession.validators().registerNotObligatory(self.getKind(), self.WIDTH_REF_ID());
56         aSession.validators().registerNotObligatory(self.getKind(), self.LENGTH_REF_ID());
57         aSession.validators().registerNotObligatory(self.getKind(), self.HEIGHT_REF_ID());
58
59     def execute(self):
60         aWidth = self.real(self.WIDTH_ID()).value()
61         aLength = self.real(self.LENGTH_ID()).value()
62         aHeight = self.real(self.HEIGHT_ID()).value()
63         aWidthFeature = ModelAPI.modelAPI_Feature(self.reference(self.WIDTH_REF_ID()).value())
64         aLengthFeature = ModelAPI.modelAPI_Feature(self.reference(self.LENGTH_REF_ID()).value())
65         aHeightFeature = ModelAPI.modelAPI_Feature(self.reference(self.HEIGHT_REF_ID()).value())
66         aResult = None
67         if not all((aWidthFeature, aLengthFeature, aLengthFeature)):
68             aResult = extrusion.getBody(self.makeBox(aLength, aWidth, aHeight))
69         else:
70             aWidthFeature.real("ConstraintValue").setValue(aWidth)
71             aLengthFeature.real("ConstraintValue").setValue(aLength)
72             aHeightFeature.real("extrusion_size").setValue(aHeight)
73             aResult = extrusion.getBody(aHeightFeature)
74         self.setResult(aResult)
75
76     def makeBox(self, aWidth, aLength, aHeight):
77         aSession = ModelAPI.ModelAPI_Session.get()
78         aPart = aSession.activeDocument()
79         # Starting the Sketch
80         aSketch = sketch.addTo(aPart)
81         sketch.setXOYPlane(aSketch)
82         # Creating the lines
83         l1 = sketch.addLine(10, 10, 10, 50, aSketch)
84         l2 = sketch.addLine(10, 50, 60, 60, aSketch)
85         l3 = sketch.addLine(60, 60, 50, 10, aSketch)
86         l4 = sketch.addLine(50, 10, 10, 10, aSketch)
87         aSketch.execute()
88         # Creating the constraints
89         sketch.makeCoincident(sketch.getEndPoint(l1), sketch.getStartPoint(l2), aSketch)
90         sketch.makeCoincident(sketch.getEndPoint(l2), sketch.getStartPoint(l3), aSketch)
91         sketch.makeCoincident(sketch.getEndPoint(l3), sketch.getStartPoint(l4), aSketch)
92         sketch.makeCoincident(sketch.getEndPoint(l4), sketch.getStartPoint(l1), aSketch)
93         sketch.makeParallel(sketch.getGeometry(l1), sketch.getGeometry(l3), aSketch)
94         sketch.makeParallel(sketch.getGeometry(l2), sketch.getGeometry(l4), aSketch)
95         sketch.makePerpendicular(sketch.getGeometry(l1), sketch.getGeometry(l4), aSketch)
96         # Set to 0X and 0Y lines defined length
97         aWidthFeature = sketch.makeConstantLength(sketch.getGeometry(l4), aWidth, aSketch)
98         aLengthFeature = sketch.makeConstantLength(sketch.getGeometry(l1), aLength, aSketch)
99         # Finalisation of the operation
100         builder = SketchResult(aSketch)
101         # Creating a feature Extrusion
102         aHeightFeature = extrusion.addNew(builder, aHeight, aPart)
103         # Store features...
104         self.reference(self.WIDTH_REF_ID()).setValue(aWidthFeature)
105         self.reference(self.LENGTH_REF_ID()).setValue(aLengthFeature)
106         self.reference(self.HEIGHT_REF_ID()).setValue(aHeightFeature)
107         return aHeightFeature
108
109
110 # TEST
111 """
112 if __name__=='__main__':
113   session = ModelAPI.ModelAPI_Session.get()
114   part = session.activeDocument()
115   session.startOperation()
116   feature = part.addFeature('Box')
117   feature.real('box_width').setValue(10)
118   feature.real('box_length').setValue(10)
119   feature.real('box_height').setValue(10)
120   feature.execute()
121   session.finishOperation()
122 """