Salome HOME
657a504d242938211ad83b347b4f81ff95689e08
[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 aSession.finishOperation()
30 aPart = aSession.activeDocument()
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 norm = geomDataAPI_Dir(aCircleSketchFeature.attribute("Norm"))
41 norm.setValue(0, 0, 1)
42 aSketchCircle = aCircleSketchFeature.addFeature("SketchCircle")
43 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter"))
44 aCircleRadius = aSketchCircle.real("CircleRadius")
45 anCircleCentr.setValue(10., 10.)
46 aCircleRadius.setValue(50.)
47 aSession.finishOperation()
48 #=========================================================================
49 # Create a sketch with triangle to extrude
50 #=========================================================================
51 aSession.startOperation()
52 aTriangleSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
53 origin = geomDataAPI_Point(aTriangleSketchFeature.attribute("Origin"))
54 origin.setValue(0, 0, 0)
55 dirx = geomDataAPI_Dir(aTriangleSketchFeature.attribute("DirX"))
56 dirx.setValue(1, 0, 0)
57 norm = geomDataAPI_Dir(aTriangleSketchFeature.attribute("Norm"))
58 norm.setValue(0, 0, 1)
59 aSketchLineA = aTriangleSketchFeature.addFeature("SketchLine")
60 aSketchLineB = aTriangleSketchFeature.addFeature("SketchLine")
61 aSketchLineC = aTriangleSketchFeature.addFeature("SketchLine")
62 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
63 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
64 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
65 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
66 aLineCStartPoint = geomDataAPI_Point2D(aSketchLineC.attribute("StartPoint"))
67 aLineCEndPoint = geomDataAPI_Point2D(aSketchLineC.attribute("EndPoint"))
68 aLineAStartPoint.setValue(25., 25.)
69 aLineAEndPoint.setValue(100., 25.)
70 aLineBStartPoint.setValue(100., 25.)
71 aLineBEndPoint.setValue(60., 75.)
72 aLineCStartPoint.setValue(60., 75.)
73 aLineCEndPoint.setValue(25., 25.)
74 aSession.finishOperation()
75 #=========================================================================
76 # Make extrusion on circle (cylinder) and triangle (prism)
77 #=========================================================================
78 # Build shape from sketcher results
79 aSession.startOperation()
80 extrudedObjects = []
81 for eachSketchFeature in [aCircleSketchFeature, aTriangleSketchFeature]:
82     # Build sketch faces
83     aSketchResult = eachSketchFeature.firstResult()
84     aSketchEdges = modelAPI_ResultConstruction(aSketchResult).shape()
85     origin = geomDataAPI_Point(eachSketchFeature.attribute("Origin")).pnt()
86     dirX = geomDataAPI_Dir(eachSketchFeature.attribute("DirX")).dir()
87     norm = geomDataAPI_Dir(eachSketchFeature.attribute("Norm")).dir()
88     aSketchFaces = ShapeList()
89     GeomAlgoAPI_SketchBuilder.createFaces(
90         origin, dirX, norm, aSketchEdges, aSketchFaces)
91     # Create extrusion on them
92     anExtrusionFt = aPart.addFeature("Extrusion")
93     anExtrusionFt.selectionList("base").append(
94         aSketchResult, aSketchFaces[0])
95     anExtrusionFt.real("from_size").setValue(0)
96     anExtrusionFt.real("to_size").setValue(50)
97     anExtrusionFt.execute()
98     extrudedObjects.append(modelAPI_ResultBody(anExtrusionFt.firstResult()))
99 aSession.finishOperation()
100 #=========================================================================
101 # Create a pacman as boolean cut of the prism from the cylinder
102 #=========================================================================
103 aSession.startOperation()
104 aBooleanFt = aPart.addFeature("Boolean")
105 aBooleanFt.reference("main_object").setValue(extrudedObjects[0])
106 aBooleanFt.reference("tool_object").setValue(extrudedObjects[1])
107 kBooleanTypeCut = 0
108 aBooleanFt.integer("bool_type").setValue(kBooleanTypeCut)
109 aBooleanFt.execute()
110 aSession.finishOperation()
111
112 assert (len(aBooleanFt.results()) > 0)
113 aBooleanResult = modelAPI_ResultBody(aBooleanFt.firstResult())
114 assert (aBooleanResult is not None)
115 #=========================================================================
116 # End of test
117 #=========================================================================