3 from SketchResult import SketchResult
8 class PythonFeaturesPlugin_Box(ModelAPI.ModelAPI_Feature):
10 "Feature to create a box by drawing a sketch and extruding it"
13 ModelAPI.ModelAPI_Feature.__init__(self)
33 return "box_ref_width"
37 return "box_ref_length"
41 return "box_ref_height"
44 return PythonFeaturesPlugin_Box.ID()
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())
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 aWidthRefValue = self.reference(self.WIDTH_REF_ID()).value()
66 aLengthRefValue = self.reference(self.LENGTH_REF_ID()).value()
67 aHeightRefValue = self.reference(self.HEIGHT_REF_ID()).value()
69 if not all((aWidthRefValue, aLengthRefValue, aHeightRefValue)):
70 aResult = extrusion.getBody(self.makeBox(aLength, aWidth, aHeight))
72 aHeightProxyResult = ModelAPI.modelAPI_Result(aHeightRefValue)
73 aWidthFeature = ModelAPI.modelAPI_Feature(aWidthRefValue)
74 aLengthFeature = ModelAPI.modelAPI_Feature(aLengthRefValue)
75 aHeightResult = ModelAPI.modelAPI_ResultBody(aHeightProxyResult)
76 aWidthFeature.real("ConstraintValue").setValue(aWidth)
77 aLengthFeature.real("ConstraintValue").setValue(aLength)
78 if aHeightResult is not None:
79 aHeightFeature = aHeightResult.document().feature(aHeightResult)
80 aHeightFeature.real("extrusion_size").setValue(aHeight)
81 aResult = extrusion.getBody(aHeightFeature)
82 self.setResult(aResult)
84 def makeBox(self, aWidth, aLength, aHeight):
85 aSession = ModelAPI.ModelAPI_Session.get()
86 aPart = aSession.activeDocument()
88 aSketch = sketch.addTo(aPart)
89 sketch.setXOYPlane(aSketch)
91 l1 = sketch.addLine(10, 10, 10, 50, aSketch)
92 l2 = sketch.addLine(10, 50, 60, 60, aSketch)
93 l3 = sketch.addLine(60, 60, 50, 10, aSketch)
94 l4 = sketch.addLine(50, 10, 10, 10, aSketch)
96 # Creating the constraints
97 sketch.makeCoincident(sketch.getEndPoint(l1), sketch.getStartPoint(l2), aSketch)
98 sketch.makeCoincident(sketch.getEndPoint(l2), sketch.getStartPoint(l3), aSketch)
99 sketch.makeCoincident(sketch.getEndPoint(l3), sketch.getStartPoint(l4), aSketch)
100 sketch.makeCoincident(sketch.getEndPoint(l4), sketch.getStartPoint(l1), aSketch)
101 sketch.makeParallel(sketch.getGeometry(l1), sketch.getGeometry(l3), aSketch)
102 sketch.makeParallel(sketch.getGeometry(l2), sketch.getGeometry(l4), aSketch)
103 sketch.makePerpendicular(sketch.getGeometry(l1), sketch.getGeometry(l4), aSketch)
104 # Set to 0X and 0Y lines defined length
105 aWidthFeature = sketch.makeConstantLength(sketch.getGeometry(l4), aWidth, aSketch)
106 aLengthFeature = sketch.makeConstantLength(sketch.getGeometry(l1), aLength, aSketch)
107 # Finalisation of the operation
108 builder = SketchResult(aSketch)
109 # Creating a feature Extrusion
110 aHeightFeature = extrusion.addNew(builder, aHeight, aPart)
112 self.reference(self.WIDTH_REF_ID()).setValue(aWidthFeature)
113 self.reference(self.LENGTH_REF_ID()).setValue(aLengthFeature)
114 self.reference(self.HEIGHT_REF_ID()).setValue(aHeightFeature.firstResult())
115 return aHeightFeature
120 if __name__=='__main__':
121 session = ModelAPI.ModelAPI_Session.get()
122 part = session.activeDocument()
123 session.startOperation()
124 feature = part.addFeature('Box')
125 feature.real('box_width').setValue(10)
126 feature.real('box_length').setValue(10)
127 feature.real('box_height').setValue(10)
129 session.finishOperation()