Salome HOME
aa36d86280812bc8b1492249212b0355ba9e5bee
[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 = featureToCompositeFeature(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         norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
59         norm.setValue(0, 0, 1)
60         aSketchCircle = aSketchFeature.addFeature("SketchCircle")
61         anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter"))
62         aCircleRadius = aSketchCircle.real("CircleRadius")
63         anCircleCentr.setValue(0.5 + step * (0.5 + i), 0.5 + step * (0.5 + j))
64         aCircleRadius.setValue(radius)
65         aSketchFeatures.append(aSketchFeature)
66
67 aSession.finishOperation()
68
69 #=========================================================================
70 # Make extrusions on circles
71 #=========================================================================
72 # Build shape from sketcher results
73
74 # Create extrusions
75 aSession.startOperation()
76
77 anExtrusions = []
78 for i in xrange(0, N * N):
79     aSketchResult = aSketchFeatures[i].firstResult()
80     aSketchEdges = modelAPI_ResultConstruction(aSketchResult).shape()
81     origin = geomDataAPI_Point(aSketchFeatures[i].attribute("Origin")).pnt()
82     dirX = geomDataAPI_Dir(aSketchFeatures[i].attribute("DirX")).dir()
83     norm = geomDataAPI_Dir(aSketchFeatures[i].attribute("Norm")).dir()
84     aSketchFaces = ShapeList()
85     GeomAlgoAPI_SketchBuilder.createFaces(
86         origin, dirX, norm, aSketchEdges, aSketchFaces)
87
88     anExtrusionFt = aPart.addFeature("Extrusion")
89     assert (anExtrusionFt.getKind() == "Extrusion")
90
91     anExtrusionFt.selectionList("base").append(
92         aSketchResult, aSketchFaces[0])
93     anExtrusionFt.string("CreationMethod").setValue("BySizes")
94     anExtrusionFt.real("from_size").setValue(0)
95     anExtrusionFt.real("to_size").setValue(10)
96     anExtrusionFt.real("to_offset").setValue(0) #TODO: remove
97     anExtrusionFt.real("from_offset").setValue(0) #TODO: remove 
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 = featureToCompositeFeature(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 norm = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("Norm"))
117 norm.setValue(0, 0, 1)
118 aSketchLineA = aQuadrangleSketchFeature.addFeature("SketchLine")
119 aSketchLineB = aQuadrangleSketchFeature.addFeature("SketchLine")
120 aSketchLineC = aQuadrangleSketchFeature.addFeature("SketchLine")
121 aSketchLineD = aQuadrangleSketchFeature.addFeature("SketchLine")
122 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
123 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
124 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
125 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
126 aLineCStartPoint = geomDataAPI_Point2D(aSketchLineC.attribute("StartPoint"))
127 aLineCEndPoint = geomDataAPI_Point2D(aSketchLineC.attribute("EndPoint"))
128 aLineDStartPoint = geomDataAPI_Point2D(aSketchLineD.attribute("StartPoint"))
129 aLineDEndPoint = geomDataAPI_Point2D(aSketchLineD.attribute("EndPoint"))
130 aLineAStartPoint.setValue(0., 0.)
131 aLineAEndPoint.setValue(0., 100.)
132 aLineBStartPoint.setValue(0., 100.)
133 aLineBEndPoint.setValue(100., 100.)
134 aLineCStartPoint.setValue(100., 100.)
135 aLineCEndPoint.setValue(100., 0.)
136 aLineDStartPoint.setValue(100., 0.)
137 aLineDEndPoint.setValue(0., 0.)
138
139 aSession.finishOperation()
140 aSession.startOperation()
141
142 #=========================================================================
143 # Build a big box extrusion
144 #=========================================================================
145 aSketchResult = aQuadrangleSketchFeature.firstResult()
146 aSketchEdges = modelAPI_ResultConstruction(aSketchResult).shape()
147 origin = geomDataAPI_Point(aQuadrangleSketchFeature.attribute("Origin")).pnt()
148 dirX = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("DirX")).dir()
149 norm = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("Norm")).dir()
150 aSketchFaces = ShapeList()
151 GeomAlgoAPI_SketchBuilder.createFaces(
152     origin, dirX, norm, aSketchEdges, aSketchFaces)
153 # Create extrusion on them
154 aBox = aPart.addFeature("Extrusion")
155 aBox.selectionList("base").append(
156     aSketchResult, aSketchFaces[0])
157 aBox.string("CreationMethod").setValue("BySizes")
158 aBox.real("from_size").setValue(0)
159 aBox.real("to_size").setValue(10)
160 aBox.real("to_offset").setValue(0) #TODO: remove
161 aBox.real("from_offset").setValue(0) #TODO: remove
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 #=========================================================================
174 aCurrentResult = modelAPI_ResultBody(aBox.firstResult())
175 aSession.startOperation()
176
177 aBooleanFt = aPart.addFeature("Boolean")
178 aBooleanFt.selectionList("main_objects").append(aCurrentResult, aCurrentResult.shape())
179 for i in xrange(0, N * N):
180     anExtrusionResult = modelAPI_ResultBody(anExtrusions[i].firstResult())
181     aBooleanFt.selectionList("tool_objects").append(anExtrusionResult, anExtrusionResult.shape())
182 kBooleanTypeCut = 0
183 aBooleanFt.integer("bool_type").setValue(kBooleanTypeCut)
184 aBooleanFt.execute()
185 aFactory = ModelAPI_Session.get().validators()
186 assert (aFactory.validate(aBooleanFt))
187 assert (len(aBooleanFt.results()) > 0)
188 aCurrentResult = modelAPI_ResultBody(aBooleanFt.firstResult())
189 assert (aCurrentResult is not None)
190 aSession.finishOperation()
191
192 #=========================================================================
193 # End of test
194 #=========================================================================
195
196 from salome.shaper import model
197 assert(model.checkPythonDump())