Salome HOME
f29c1cfdc10f6259a82211dd4273aa00d4a7da93
[modules/shaper.git] / src / FeaturesPlugin / Test / TestSerialBoolean.py
1 # Copyright (C) 2014-2023  CEA, EDF
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 email : webmaster.salome@opencascade.com
18 #
19
20 """
21       TestExtrusion.py
22       Unit test of FeaturesPlugin_Boolean class: many Boolean operations performance
23
24       class FeaturesPlugin_Extrusion : public ModelAPI_Feature
25         static const std::string MY_EXTRUSION_ID("Extrusion");
26         static const std::string MY_FACE_ID("base");
27         static const std::string MY_SIZE_ID("size");
28         static const std::string MY_REVERSE_ID("reverse");
29
30         data()->addAttribute(FeaturesPlugin_Extrusion::FACE_ID(), ModelAPI_AttributeSelection::typeId());
31         data()->addAttribute(FeaturesPlugin_Extrusion::SIZE_ID(), ModelAPI_AttributeDouble::typeId());
32         data()->addAttribute(FeaturesPlugin_Extrusion::REVERSE_ID(), ModelAPI_AttributeBoolean::typeId());
33 """
34
35 # Number rows and columns of cylinders that cuts the big box. Number of Boolena operations is N*N
36 N = 5
37
38 #=========================================================================
39 # Initialization of the test
40 #=========================================================================
41 from GeomAPI import *
42 from GeomAlgoAPI import *
43 from GeomDataAPI import *
44 from ModelAPI import *
45
46
47 __updated__ = "2015-03-26"
48
49 aSession = ModelAPI_Session.get()
50 aDocument = aSession.moduleDocument()
51 # Create a part for extrusion
52 aSession.startOperation()
53 aPartFeature = aDocument.addFeature("Part")
54 aSession.finishOperation()
55 assert (len(aPartFeature.results()) == 1)
56 # Another way is:
57 # aPart = aSession.activeDocument()
58 aPartResult = modelAPI_ResultPart(aPartFeature.firstResult())
59 aPart = aPartResult.partDoc()
60
61 #=========================================================================
62 # Create a list of sketches with one circle inside of each to extrude
63 #=========================================================================
64 step = 99. / N
65 radius = 95. / N / 2.
66
67 aSession.startOperation()
68 aSketchFeatures = []
69 for i in range(0, N):
70     for j in range(0, N):
71         # Create circle
72         aSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
73         origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
74         origin.setValue(0, 0, 0)
75         dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
76         dirx.setValue(1, 0, 0)
77         norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
78         norm.setValue(0, 0, 1)
79         aSketchCircle = aSketchFeature.addFeature("SketchCircle")
80         anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("circle_center"))
81         aCircleRadius = aSketchCircle.real("circle_radius")
82         anCircleCentr.setValue(0.5 + step * (0.5 + i), 0.5 + step * (0.5 + j))
83         aCircleRadius.setValue(radius)
84         aSketchFeatures.append(aSketchFeature)
85
86 aSession.finishOperation()
87
88 #=========================================================================
89 # Make extrusions on circles
90 #=========================================================================
91 # Build shape from sketcher results
92
93 # Create extrusions
94 aSession.startOperation()
95
96 anExtrusions = []
97 for i in range(0, N * N):
98     aSketchResult = modelAPI_ResultConstruction(aSketchFeatures[i].firstResult())
99     anExtrusionFt = aPart.addFeature("Extrusion")
100     assert (anExtrusionFt.getKind() == "Extrusion")
101
102     anExtrusionFt.selectionList("base").append(
103         aSketchResult, aSketchResult.face(0))
104     anExtrusionFt.string("CreationMethod").setValue("BySizes")
105     anExtrusionFt.real("from_size").setValue(0)
106     anExtrusionFt.real("to_size").setValue(10)
107     anExtrusionFt.real("to_offset").setValue(0) #TODO: remove
108     anExtrusionFt.real("from_offset").setValue(0) #TODO: remove
109     anExtrusions.append(anExtrusionFt)
110
111 aSession.finishOperation()
112
113 #=========================================================================
114 # Make rectangle sketch: base for the box, size 100x100
115 #=========================================================================
116 aSession.startOperation()
117 aQuadrangleSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
118 origin = geomDataAPI_Point(aQuadrangleSketchFeature.attribute("Origin"))
119 origin.setValue(0, 0, 0)
120 dirx = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("DirX"))
121 dirx.setValue(1, 0, 0)
122 norm = geomDataAPI_Dir(aQuadrangleSketchFeature.attribute("Norm"))
123 norm.setValue(0, 0, 1)
124 aSketchLineA = aQuadrangleSketchFeature.addFeature("SketchLine")
125 aSketchLineB = aQuadrangleSketchFeature.addFeature("SketchLine")
126 aSketchLineC = aQuadrangleSketchFeature.addFeature("SketchLine")
127 aSketchLineD = aQuadrangleSketchFeature.addFeature("SketchLine")
128 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
129 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
130 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
131 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
132 aLineCStartPoint = geomDataAPI_Point2D(aSketchLineC.attribute("StartPoint"))
133 aLineCEndPoint = geomDataAPI_Point2D(aSketchLineC.attribute("EndPoint"))
134 aLineDStartPoint = geomDataAPI_Point2D(aSketchLineD.attribute("StartPoint"))
135 aLineDEndPoint = geomDataAPI_Point2D(aSketchLineD.attribute("EndPoint"))
136 aLineAStartPoint.setValue(0., 0.)
137 aLineAEndPoint.setValue(0., 100.)
138 aLineBStartPoint.setValue(0., 100.)
139 aLineBEndPoint.setValue(100., 100.)
140 aLineCStartPoint.setValue(100., 100.)
141 aLineCEndPoint.setValue(100., 0.)
142 aLineDStartPoint.setValue(100., 0.)
143 aLineDEndPoint.setValue(0., 0.)
144
145 aSession.finishOperation()
146 aSession.startOperation()
147
148 #=========================================================================
149 # Build a big box extrusion
150 #=========================================================================
151 aSketchResult = modelAPI_ResultConstruction(aQuadrangleSketchFeature.firstResult())
152 # Create extrusion on them
153 aBox = aPart.addFeature("Extrusion")
154 aBox.selectionList("base").append(
155     aSketchResult, aSketchResult.face(0))
156 aBox.string("CreationMethod").setValue("BySizes")
157 aBox.real("from_size").setValue(0)
158 aBox.real("to_size").setValue(10)
159 aBox.real("to_offset").setValue(0) #TODO: remove
160 aBox.real("from_offset").setValue(0) #TODO: remove
161
162 aSession.finishOperation()
163
164
165 #=========================================================================
166 # Create a boolean cut of cylinders from the box:
167 # result of Boolean is the first argument of the next Boolean
168 #=========================================================================
169 aCurrentResult = modelAPI_ResultBody(aBox.firstResult())
170 aSession.startOperation()
171 aFactory = ModelAPI_Session.get().validators()
172 for i in range(0, N * N):
173     anExtrusionResult = modelAPI_ResultBody(anExtrusions[i].firstResult())
174     aBooleanFt = aPart.addFeature("Cut")
175     aBooleanFt.selectionList("main_objects").append(aCurrentResult, aCurrentResult.shape())
176     aBooleanFt.selectionList("tool_objects").append(anExtrusionResult, anExtrusionResult.shape())
177     aBooleanFt.execute()
178     assert (aFactory.validate(aBooleanFt))
179     assert (len(aBooleanFt.results()) > 0)
180     aCurrentResult = modelAPI_ResultBody(aBooleanFt.firstResult())
181     assert (aCurrentResult is not None)
182 aSession.finishOperation()
183
184 #=========================================================================
185 # End of test
186 #=========================================================================
187
188 from salome.shaper import model
189 assert(model.checkPythonDump())