Salome HOME
Merge branch 'master' into Dev_1.1.0
[modules/shaper.git] / src / FeaturesPlugin / Test / TestMultiBoolean.py
1 """
2       TestExtrusion.py
3       Unit test of FeaturesPlugin_Boolean class: many Boolean operations performance
4       
5       class FeaturesPlugin_Extrusion : public ModelAPI_Feature
6         static const std::string MY_EXTRUSION_ID("Extrusion");
7         static const std::string MY_FACE_ID("base");
8         static const std::string MY_SIZE_ID("size");
9         static const std::string MY_REVERSE_ID("reverse");
10           
11         data()->addAttribute(FeaturesPlugin_Extrusion::FACE_ID(), ModelAPI_AttributeSelection::typeId());
12         data()->addAttribute(FeaturesPlugin_Extrusion::SIZE_ID(), ModelAPI_AttributeDouble::typeId());
13         data()->addAttribute(FeaturesPlugin_Extrusion::REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
14 """
15
16 # Number rows and columns of cylinders that cuts the big box. Number of Boolena operations is N*N
17 N = 5
18
19 #=========================================================================
20 # Initialization of the test
21 #=========================================================================
22 from GeomAPI import *
23 from GeomAlgoAPI import *
24 from GeomDataAPI import *
25 from ModelAPI import *
26
27
28 __updated__ = "2015-03-26"
29
30 aSession = ModelAPI_Session.get()
31 aDocument = aSession.moduleDocument()
32 # Create a part for extrusion
33 aSession.startOperation()
34 aPartFeature = aDocument.addFeature("Part")
35 aSession.finishOperation()
36 assert (len(aPartFeature.results()) == 1)
37 # Another way is:
38 # aPart = aSession.activeDocument()
39 aPartResult = modelAPI_ResultPart(aPartFeature.firstResult())
40 aPart = aPartResult.partDoc()
41
42 #=========================================================================
43 # Create a list of sketches with one circle inside of each to extrude
44 #=========================================================================
45 step = 99. / N
46 radius = 95. / N / 2.
47
48 aSession.startOperation()
49 aSketchFeatures = []
50 for i in xrange(0, N):
51     for j in xrange(0, N):
52         # Create circle
53         aSketchFeature = modelAPI_CompositeFeature(aPart.addFeature("Sketch"))
54         origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
55         origin.setValue(0, 0, 0)
56         dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
57         dirx.setValue(1, 0, 0)
58         diry = geomDataAPI_Dir(aSketchFeature.attribute("DirY"))
59         diry.setValue(0, 1, 0)
60         norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
61         norm.setValue(0, 0, 1)
62         aSketchCircle = aSketchFeature.addFeature("SketchCircle")
63         anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter"))
64         aCircleRadius = aSketchCircle.real("CircleRadius")
65         anCircleCentr.setValue(0.5 + step * (0.5 + i), 0.5 + step * (0.5 + j))
66         aCircleRadius.setValue(radius)
67         aSketchFeatures.append(aSketchFeature)
68
69 aSession.finishOperation()
70
71 #=========================================================================
72 # Make extrusions on circles
73 #=========================================================================
74 # Build shape from sketcher results
75
76 # Create extrusions
77 aSession.startOperation()
78
79 anExtrusions = []
80 for i in xrange(0, N * N):
81     aSketchResult = aSketchFeatures[i].firstResult()
82     aSketchEdges = modelAPI_ResultConstruction(aSketchResult).shape()
83     origin = geomDataAPI_Point(aSketchFeatures[i].attribute("Origin")).pnt()
84     dirX = geomDataAPI_Dir(aSketchFeatures[i].attribute("DirX")).dir()
85     dirY = geomDataAPI_Dir(aSketchFeatures[i].attribute("DirY")).dir()
86     norm = geomDataAPI_Dir(aSketchFeatures[i].attribute("Norm")).dir()
87     aSketchFaces = ShapeList()
88     GeomAlgoAPI_SketchBuilder.createFaces(
89         origin, dirX, dirY, norm, aSketchEdges, aSketchFaces)
90
91     anExtrusionFt = aPart.addFeature("Extrusion")
92     assert (anExtrusionFt.getKind() == "Extrusion")
93
94     anExtrusionFt.selectionList("base").append(
95         aSketchResult, aSketchFaces[0])
96     anExtrusionFt.real("size").setValue(10)
97     anExtrusionFt.boolean("reverse").setValue(False)
98     # v1.0.2 from master
99     # anExtrusionFt.selection("extrusion_face").setValue(
100     #    aSketchResult, aSketchFaces[0])
101     # anExtrusionFt.real("extrusion_size").setValue(10)
102     # anExtrusionFt.boolean("extrusion_reverse").setValue(False)
103     anExtrusions.append(anExtrusionFt)
104
105 aSession.finishOperation()
106
107 #=========================================================================
108 # Make rectangle sketch: base for the box, size 100x100
109 #=========================================================================
110 aSession.startOperation()
111 aQuadrangleSketchFeature = modelAPI_CompositeFeature(aPart.addFeature("Sketch"))
112 origin = geomDataAPI_Point(aQuadrangleSketchFeature.attribute("Origin"))
113 origin.setValue(0, 0, 0)
114 dirx = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("DirX"))
115 dirx.setValue(1, 0, 0)
116 diry = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("DirY"))
117 diry.setValue(0, 1, 0)
118 norm = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("Norm"))
119 norm.setValue(0, 0, 1)
120 aSketchLineA = aQuadrangleSketchFeature.addFeature("SketchLine")
121 aSketchLineB = aQuadrangleSketchFeature.addFeature("SketchLine")
122 aSketchLineC = aQuadrangleSketchFeature.addFeature("SketchLine")
123 aSketchLineD = aQuadrangleSketchFeature.addFeature("SketchLine")
124 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
125 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
126 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
127 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
128 aLineCStartPoint = geomDataAPI_Point2D(aSketchLineC.attribute("StartPoint"))
129 aLineCEndPoint = geomDataAPI_Point2D(aSketchLineC.attribute("EndPoint"))
130 aLineDStartPoint = geomDataAPI_Point2D(aSketchLineD.attribute("StartPoint"))
131 aLineDEndPoint = geomDataAPI_Point2D(aSketchLineD.attribute("EndPoint"))
132 aLineAStartPoint.setValue(0., 0.)
133 aLineAEndPoint.setValue(0., 100.)
134 aLineBStartPoint.setValue(0., 100.)
135 aLineBEndPoint.setValue(100., 100.)
136 aLineCStartPoint.setValue(100., 100.)
137 aLineCEndPoint.setValue(100., 0.)
138 aLineDStartPoint.setValue(100., 0.)
139 aLineDEndPoint.setValue(0., 0.)
140
141 aSession.finishOperation()
142 aSession.startOperation()
143
144 #=========================================================================
145 # Build a big box extrusion
146 #=========================================================================
147 aSketchResult = aQuadrangleSketchFeature.firstResult()
148 aSketchEdges = modelAPI_ResultConstruction(aSketchResult).shape()
149 origin = geomDataAPI_Point(aQuadrangleSketchFeature.attribute("Origin")).pnt()
150 dirX = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("DirX")).dir()
151 dirY = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("DirY")).dir()
152 norm = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("Norm")).dir()
153 aSketchFaces = ShapeList()
154 GeomAlgoAPI_SketchBuilder.createFaces(
155     origin, dirX, dirY, norm, aSketchEdges, aSketchFaces)
156 # Create extrusion on them
157 aBox = aPart.addFeature("Extrusion")
158 aBox.selectionList("base").append(
159     aSketchResult, aSketchFaces[0])
160 aBox.real("size").setValue(10)
161 aBox.boolean("reverse").setValue(False)
162 # v 1.0.2 from master
163 # aBox.selection("extrusion_face").setValue(
164 #     aSketchResult, aSketchFaces[0])
165 # aBox.real("extrusion_size").setValue(10)
166 # aBox.boolean("extrusion_reverse").setValue(False)
167
168 aSession.finishOperation()
169
170
171 #=========================================================================
172 # Create a boolean cut of cylinders from the box:
173 # result of Boolean is the first argument of the next Boolean
174 #=========================================================================
175 aCurrentResult = aBox.firstResult()
176 aSession.startOperation()
177 for i in xrange(0, N * N):
178     aBooleanFt = aPart.addFeature("Boolean")
179     aBooleanFt.reference("main_object").setValue(modelAPI_ResultBody(aCurrentResult))
180     aBooleanFt.reference("tool_object").setValue(modelAPI_ResultBody(anExtrusions[i].firstResult()))
181     kBooleanTypeCut = 0
182     aBooleanFt.integer("bool_type").setValue(kBooleanTypeCut)
183     aBooleanFt.execute()
184     aCurrentResult = aBooleanFt.firstResult()
185 aSession.finishOperation()
186
187 #=========================================================================
188 # End of test
189 #=========================================================================