Salome HOME
Update tests that create sketch faces by themselves and use these faces as arguments...
[modules/shaper.git] / src / FeaturesPlugin / Test / TestSerialBoolean.py
1 ## Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 ##
3 ## This library is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU Lesser General Public
5 ## License as published by the Free Software Foundation; either
6 ## version 2.1 of the License, or (at your option) any later version.
7 ##
8 ## This library is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ## Lesser General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU Lesser General Public
14 ## License along with this library; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 ##
17 ## See http:##www.salome-platform.org/ or
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 """
22       TestExtrusion.py
23       Unit test of FeaturesPlugin_Boolean class: many Boolean operations performance
24
25       class FeaturesPlugin_Extrusion : public ModelAPI_Feature
26         static const std::string MY_EXTRUSION_ID("Extrusion");
27         static const std::string MY_FACE_ID("base");
28         static const std::string MY_SIZE_ID("size");
29         static const std::string MY_REVERSE_ID("reverse");
30
31         data()->addAttribute(FeaturesPlugin_Extrusion::FACE_ID(), ModelAPI_AttributeSelection::typeId());
32         data()->addAttribute(FeaturesPlugin_Extrusion::SIZE_ID(), ModelAPI_AttributeDouble::typeId());
33         data()->addAttribute(FeaturesPlugin_Extrusion::REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
34 """
35
36 # Number rows and columns of cylinders that cuts the big box. Number of Boolena operations is N*N
37 N = 5
38
39 #=========================================================================
40 # Initialization of the test
41 #=========================================================================
42 from GeomAPI import *
43 from GeomAlgoAPI import *
44 from GeomDataAPI import *
45 from ModelAPI import *
46
47
48 __updated__ = "2015-03-26"
49
50 aSession = ModelAPI_Session.get()
51 aDocument = aSession.moduleDocument()
52 # Create a part for extrusion
53 aSession.startOperation()
54 aPartFeature = aDocument.addFeature("Part")
55 aSession.finishOperation()
56 assert (len(aPartFeature.results()) == 1)
57 # Another way is:
58 # aPart = aSession.activeDocument()
59 aPartResult = modelAPI_ResultPart(aPartFeature.firstResult())
60 aPart = aPartResult.partDoc()
61
62 #=========================================================================
63 # Create a list of sketches with one circle inside of each to extrude
64 #=========================================================================
65 step = 99. / N
66 radius = 95. / N / 2.
67
68 aSession.startOperation()
69 aSketchFeatures = []
70 for i in range(0, N):
71     for j in range(0, N):
72         # Create circle
73         aSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
74         origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
75         origin.setValue(0, 0, 0)
76         dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
77         dirx.setValue(1, 0, 0)
78         norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
79         norm.setValue(0, 0, 1)
80         aSketchCircle = aSketchFeature.addFeature("SketchCircle")
81         anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("circle_center"))
82         aCircleRadius = aSketchCircle.real("circle_radius")
83         anCircleCentr.setValue(0.5 + step * (0.5 + i), 0.5 + step * (0.5 + j))
84         aCircleRadius.setValue(radius)
85         aSketchFeatures.append(aSketchFeature)
86
87 aSession.finishOperation()
88
89 #=========================================================================
90 # Make extrusions on circles
91 #=========================================================================
92 # Build shape from sketcher results
93
94 # Create extrusions
95 aSession.startOperation()
96
97 anExtrusions = []
98 for i in range(0, N * N):
99     aSketchResult = modelAPI_ResultConstruction(aSketchFeatures[i].firstResult())
100     anExtrusionFt = aPart.addFeature("Extrusion")
101     assert (anExtrusionFt.getKind() == "Extrusion")
102
103     anExtrusionFt.selectionList("base").append(
104         aSketchResult, aSketchResult.face(0))
105     anExtrusionFt.string("CreationMethod").setValue("BySizes")
106     anExtrusionFt.real("from_size").setValue(0)
107     anExtrusionFt.real("to_size").setValue(10)
108     anExtrusionFt.real("to_offset").setValue(0) #TODO: remove
109     anExtrusionFt.real("from_offset").setValue(0) #TODO: remove
110     anExtrusions.append(anExtrusionFt)
111
112 aSession.finishOperation()
113
114 #=========================================================================
115 # Make rectangle sketch: base for the box, size 100x100
116 #=========================================================================
117 aSession.startOperation()
118 aQuadrangleSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
119 origin = geomDataAPI_Point(aQuadrangleSketchFeature.attribute("Origin"))
120 origin.setValue(0, 0, 0)
121 dirx = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("DirX"))
122 dirx.setValue(1, 0, 0)
123 norm = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("Norm"))
124 norm.setValue(0, 0, 1)
125 aSketchLineA = aQuadrangleSketchFeature.addFeature("SketchLine")
126 aSketchLineB = aQuadrangleSketchFeature.addFeature("SketchLine")
127 aSketchLineC = aQuadrangleSketchFeature.addFeature("SketchLine")
128 aSketchLineD = aQuadrangleSketchFeature.addFeature("SketchLine")
129 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
130 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
131 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
132 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
133 aLineCStartPoint = geomDataAPI_Point2D(aSketchLineC.attribute("StartPoint"))
134 aLineCEndPoint = geomDataAPI_Point2D(aSketchLineC.attribute("EndPoint"))
135 aLineDStartPoint = geomDataAPI_Point2D(aSketchLineD.attribute("StartPoint"))
136 aLineDEndPoint = geomDataAPI_Point2D(aSketchLineD.attribute("EndPoint"))
137 aLineAStartPoint.setValue(0., 0.)
138 aLineAEndPoint.setValue(0., 100.)
139 aLineBStartPoint.setValue(0., 100.)
140 aLineBEndPoint.setValue(100., 100.)
141 aLineCStartPoint.setValue(100., 100.)
142 aLineCEndPoint.setValue(100., 0.)
143 aLineDStartPoint.setValue(100., 0.)
144 aLineDEndPoint.setValue(0., 0.)
145
146 aSession.finishOperation()
147 aSession.startOperation()
148
149 #=========================================================================
150 # Build a big box extrusion
151 #=========================================================================
152 aSketchResult = modelAPI_ResultConstruction(aQuadrangleSketchFeature.firstResult())
153 # Create extrusion on them
154 aBox = aPart.addFeature("Extrusion")
155 aBox.selectionList("base").append(
156     aSketchResult, aSketchResult.face(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
163 aSession.finishOperation()
164
165
166 #=========================================================================
167 # Create a boolean cut of cylinders from the box:
168 # result of Boolean is the first argument of the next Boolean
169 #=========================================================================
170 aCurrentResult = modelAPI_ResultBody(aBox.firstResult())
171 aSession.startOperation()
172 aFactory = ModelAPI_Session.get().validators()
173 for i in range(0, N * N):
174     anExtrusionResult = modelAPI_ResultBody(anExtrusions[i].firstResult())
175     aBooleanFt = aPart.addFeature("Cut")
176     aBooleanFt.selectionList("main_objects").append(aCurrentResult, aCurrentResult.shape())
177     aBooleanFt.selectionList("tool_objects").append(anExtrusionResult, anExtrusionResult.shape())
178     aBooleanFt.execute()
179     assert (aFactory.validate(aBooleanFt))
180     assert (len(aBooleanFt.results()) > 0)
181     aCurrentResult = modelAPI_ResultBody(aBooleanFt.firstResult())
182     assert (aCurrentResult is not None)
183 aSession.finishOperation()
184
185 #=========================================================================
186 # End of test
187 #=========================================================================
188
189 from salome.shaper import model
190 assert(model.checkPythonDump())