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