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