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