Salome HOME
Merge branch 'master' into Dev_1.1.0
[modules/shaper.git] / src / FeaturesPlugin / Test / TestBoolean.py
1 """
2       TestBoolean.py
3       Unit test of FeaturesPlugin_Boolean class
4       
5       class FeaturesPlugin_Boolean
6         static const std::string MY_ID("Boolean");
7         static const std::string MY_OBJECT_ID("main_object");
8         static const std::string MY_TOOL_ID("tool_object");
9         static const std::string MY_TYPE_ID("bool_type");
10         
11         data()->addAttribute(FeaturesPlugin_Boolean::OBJECT_ID(), ModelAPI_AttributeReference::typeId());
12         data()->addAttribute(FeaturesPlugin_Boolean::TOOL_ID(), ModelAPI_AttributeReference::typeId());
13         data()->addAttribute(FeaturesPlugin_Boolean::TYPE_ID(), ModelAPI_AttributeInteger::typeId());
14 """
15 #=========================================================================
16 # Initialization of the test
17 #=========================================================================
18 from ModelAPI import *
19 from GeomDataAPI import *
20 from GeomAlgoAPI import *
21 from GeomAPI import *
22
23 __updated__ = "2014-12-16"
24
25 aSession = ModelAPI_Session.get()
26 # Create a part for extrusions & boolean
27 aSession.startOperation()
28 aPartFeature = aSession.moduleDocument().addFeature("Part")
29 aPart = aSession.activeDocument()
30 aSession.finishOperation()
31 #=========================================================================
32 # Create a sketch with circle to extrude
33 #=========================================================================
34 aSession.startOperation()
35 aCircleSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
36 origin = geomDataAPI_Point(aCircleSketchFeature.attribute("Origin"))
37 origin.setValue(0, 0, 0)
38 dirx = geomDataAPI_Dir(aCircleSketchFeature.attribute("DirX"))
39 dirx.setValue(1, 0, 0)
40 diry = geomDataAPI_Dir(aCircleSketchFeature.attribute("DirY"))
41 diry.setValue(0, 1, 0)
42 norm = geomDataAPI_Dir(aCircleSketchFeature.attribute("Norm"))
43 norm.setValue(0, 0, 1)
44 aSketchCircle = aCircleSketchFeature.addFeature("SketchCircle")
45 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter"))
46 aCircleRadius = aSketchCircle.real("CircleRadius")
47 anCircleCentr.setValue(10., 10.)
48 aCircleRadius.setValue(50.)
49 aSession.finishOperation()
50 #=========================================================================
51 # Create a sketch with triangle to extrude
52 #=========================================================================
53 aSession.startOperation()
54 aTriangleSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
55 origin = geomDataAPI_Point(aTriangleSketchFeature.attribute("Origin"))
56 origin.setValue(0, 0, 0)
57 dirx = geomDataAPI_Dir(aTriangleSketchFeature.attribute("DirX"))
58 dirx.setValue(1, 0, 0)
59 diry = geomDataAPI_Dir(aTriangleSketchFeature.attribute("DirY"))
60 diry.setValue(0, 1, 0)
61 norm = geomDataAPI_Dir(aTriangleSketchFeature.attribute("Norm"))
62 norm.setValue(0, 0, 1)
63 aSketchLineA = aTriangleSketchFeature.addFeature("SketchLine")
64 aSketchLineB = aTriangleSketchFeature.addFeature("SketchLine")
65 aSketchLineC = aTriangleSketchFeature.addFeature("SketchLine")
66 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
67 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
68 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
69 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
70 aLineCStartPoint = geomDataAPI_Point2D(aSketchLineC.attribute("StartPoint"))
71 aLineCEndPoint = geomDataAPI_Point2D(aSketchLineC.attribute("EndPoint"))
72 aLineAStartPoint.setValue(25., 25.)
73 aLineAEndPoint.setValue(100., 25.)
74 aLineBStartPoint.setValue(100., 25.)
75 aLineBEndPoint.setValue(60., 75.)
76 aLineCStartPoint.setValue(60., 75.)
77 aLineCEndPoint.setValue(25., 25.)
78 aSession.finishOperation()
79 #=========================================================================
80 # Make extrusion on circle (cylinder) and triangle (prism)
81 #=========================================================================
82 # Build shape from sketcher results
83 aSession.startOperation()
84 extrudedObjects = []
85 for eachSketchFeature in [aCircleSketchFeature, aTriangleSketchFeature]:
86     # Build sketch faces
87     aSketchResult = eachSketchFeature.firstResult()
88     aSketchEdges = modelAPI_ResultConstruction(aSketchResult).shape()
89     origin = geomDataAPI_Point(eachSketchFeature.attribute("Origin")).pnt()
90     dirX = geomDataAPI_Dir(eachSketchFeature.attribute("DirX")).dir()
91     dirY = geomDataAPI_Dir(eachSketchFeature.attribute("DirY")).dir()
92     norm = geomDataAPI_Dir(eachSketchFeature.attribute("Norm")).dir()
93     aSketchFaces = ShapeList()
94     GeomAlgoAPI_SketchBuilder.createFaces(
95         origin, dirX, dirY, norm, aSketchEdges, aSketchFaces)
96     # Create extrusion on them
97     anExtrusionFt = aPart.addFeature("Extrusion")
98     anExtrusionFt.selectionList("base").append(
99         aSketchResult, aSketchFaces[0])
100     anExtrusionFt.real("size").setValue(50)
101     anExtrusionFt.boolean("reverse").setValue(False)
102     anExtrusionFt.execute()
103     extrudedObjects.append(modelAPI_ResultBody(anExtrusionFt.firstResult()))
104 aSession.finishOperation()
105 #=========================================================================
106 # Create a pacman as boolean cut of the prism from the cylinder
107 #=========================================================================
108 aSession.startOperation()
109 aBooleanFt = aPart.addFeature("Boolean")
110 aBooleanFt.reference("main_object").setValue(extrudedObjects[0])
111 aBooleanFt.reference("tool_object").setValue(extrudedObjects[1])
112 kBooleanTypeCut = 0
113 aBooleanFt.integer("bool_type").setValue(kBooleanTypeCut)
114 aBooleanFt.execute()
115 aSession.finishOperation()
116
117 assert (len(aBooleanFt.results()) > 0)
118 aBooleanResult = modelAPI_ResultBody(aBooleanFt.firstResult())
119 assert (aBooleanResult is not None)
120 #=========================================================================
121 # End of test
122 #=========================================================================