Salome HOME
Make preview of python Box feature working correctly
[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_CompositeFeature):
9     """Feature to create a box by drawing a sketch and extruding it
10     """
11
12     def __init__(self):
13         ModelAPI.ModelAPI_CompositeFeature.__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_typeId())
50         self.data().addAttribute(self.LENGTH_ID(), ModelAPI.ModelAPI_AttributeDouble_typeId())
51         self.data().addAttribute(self.HEIGHT_ID(), ModelAPI.ModelAPI_AttributeDouble_typeId())
52         self.data().addAttribute(self.WIDTH_REF_ID(), ModelAPI.ModelAPI_AttributeReference_typeId())
53         self.data().addAttribute(self.LENGTH_REF_ID(), ModelAPI.ModelAPI_AttributeReference_typeId())
54         self.data().addAttribute(self.HEIGHT_REF_ID(), ModelAPI.ModelAPI_AttributeReference_typeId())
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         self.mySketch = None  # not yet initialized
61         self.myExtrusion = None  # not yet initialized
62
63     def execute(self):
64         aWidth = self.real(self.WIDTH_ID()).value()
65         aLength = self.real(self.LENGTH_ID()).value()
66         aHeight = self.real(self.HEIGHT_ID()).value()
67         aWidthRefValue = self.reference(self.WIDTH_REF_ID()).value()
68         aLengthRefValue = self.reference(self.LENGTH_REF_ID()).value()
69         aHeightRefValue = self.reference(self.HEIGHT_REF_ID()).value()
70         aResult = None
71         if not all((aWidthRefValue, aLengthRefValue, aHeightRefValue)):
72             aResult = extrusion.getBody(self.makeBox(aLength, aWidth, aHeight))
73         else:
74             aHeightProxyResult = ModelAPI.modelAPI_Result(aHeightRefValue)
75             aWidthFeature = ModelAPI.objectToFeature(aWidthRefValue)
76             aLengthFeature = ModelAPI.objectToFeature(aLengthRefValue)
77             aHeightResult = ModelAPI.modelAPI_ResultBody(aHeightProxyResult)
78             aWidthFeature.real("ConstraintValue").setValue(aWidth)
79             aLengthFeature.real("ConstraintValue").setValue(aLength)
80             if aHeightResult is not None:
81                 aHeightFeature = aHeightResult.document().feature(aHeightResult)
82                 aHeightFeature.real("extrusion_size").setValue(aHeight)
83                 aResult = extrusion.getBody(aHeightFeature)
84         # create a new result with copied shape from extrusion
85         aResultBody = self.document().createBody(self.data())
86         if not aResult is None:
87             aResultBody.store(aResult.shape())
88             self.setResult(aResultBody)
89         pass
90
91     def makeBox(self, aWidth, aLength, aHeight):
92         aSession = ModelAPI.ModelAPI_Session.get()
93         aPart = aSession.activeDocument()
94         # Starting the Sketch
95         aSketch = sketch.addTo(aPart)
96         self.mySketch = aSketch
97         sketch.setXOYPlane(aSketch)
98         # Creating the lines
99         l1 = sketch.addLine(10, 10, 10, 60, aSketch)
100         l2 = sketch.addLine(10, 60, 60, 60, aSketch)
101         l3 = sketch.addLine(60, 60, 60, 10, aSketch)
102         l4 = sketch.addLine(60, 10, 10, 10, aSketch)
103         aSketch.execute()
104         # Creating the constraints
105         sketch.makeCoincident(sketch.getEndPoint(l1), sketch.getStartPoint(l2), aSketch)
106         sketch.makeCoincident(sketch.getEndPoint(l2), sketch.getStartPoint(l3), aSketch)
107         sketch.makeCoincident(sketch.getEndPoint(l3), sketch.getStartPoint(l4), aSketch)
108         sketch.makeCoincident(sketch.getEndPoint(l4), sketch.getStartPoint(l1), aSketch)
109         sketch.makeParallel(sketch.getGeometry(l1), sketch.getGeometry(l3), aSketch)
110         sketch.makeParallel(sketch.getGeometry(l2), sketch.getGeometry(l4), aSketch)
111         sketch.makePerpendicular(sketch.getGeometry(l1), sketch.getGeometry(l4), aSketch)
112         # Set to 0X and 0Y lines defined length
113         aWidthFeature = sketch.makeConstantLength(sketch.getGeometry(l4), aWidth, aSketch)
114         aLengthFeature = sketch.makeConstantLength(sketch.getGeometry(l1), aLength, aSketch)
115         # Finalisation of the operation
116         builder = SketchResult(aSketch)
117         # Creating a feature Extrusion
118         aHeightFeature = extrusion.addNew(builder, aHeight, aPart)
119         self.myExtrusion = aHeightFeature
120         # Store features...
121         self.reference(self.WIDTH_REF_ID()).setValue(aWidthFeature)
122         self.reference(self.LENGTH_REF_ID()).setValue(aLengthFeature)
123         self.reference(self.HEIGHT_REF_ID()).setValue(aHeightFeature.firstResult())
124         return aHeightFeature
125
126     def addFeature(self, theID):
127         pass
128
129     def numberOfSubs(self):
130         subsCount = 0
131         if not self.mySketch is None:
132             subsCount += 1
133         if not self.myExtrusion is None:
134             subsCount += 1
135         # extrusion and sketch
136         return subsCount
137
138     def subFeature(self, theIndex):
139         if theIndex == 0: # sketch
140             return ModelAPI.compositeFeatureToFeature(self.mySketch)
141         return self.myExtrusion
142
143     def subFeatureId(self, theIndex):
144         return 0
145
146     def isSub(self, theFeature):
147         return theFeature == self.mySketch or theFeature == self.myExtrusion
148
149     def attributeChanged(self, theAttrID):
150         # on update of attributes values, transfer them to sub-features immideately to see good preview
151         # otherwise these features will be executed before execute of "Box" and with old parameters
152         aWidthRefValue = self.reference(self.WIDTH_REF_ID()).value()
153         aLengthRefValue = self.reference(self.LENGTH_REF_ID()).value()
154         aHeightRefValue = self.reference(self.HEIGHT_REF_ID()).value()
155         if all((aWidthRefValue, aLengthRefValue, aHeightRefValue)):
156           self.execute()
157
158 # TEST
159 """
160 if __name__=='__main__':
161   session = ModelAPI.ModelAPI_Session.get()
162   part = session.activeDocument()
163   session.startOperation()
164   feature = part.addFeature('Box')
165   feature.real('box_width').setValue(10)
166   feature.real('box_length').setValue(10)
167   feature.real('box_height').setValue(10)
168   feature.execute()
169   session.finishOperation()
170 """