Salome HOME
Fix for the unit-tests that use not HighAPI pythion interface to create construction...
[modules/shaper.git] / src / ModelAPI / Test / TestFolder_Update.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 # Initialization of the test
23 #=========================================================================
24 from ModelAPI import *
25 from GeomDataAPI import *
26
27 __updated__ = "2017-11-23"
28
29 aSession = ModelAPI_Session.get()
30
31
32 def newPoint(theDocument, theX, theY, theZ):
33     aSession.startOperation()
34     aPoint = theDocument.addFeature("Point")
35     aPointData = aPoint.data()
36     assert(aPointData is not None)
37     geomDataAPI_Point(aPointData.attribute("point3d")).setValue(theX, theY, theZ)
38     aPointData.string("creation_method").setValue("by_xyz")
39     aSession.finishOperation()
40     return aPoint
41
42
43 #=========================================================================
44 # Test 1. Add a point into a folder above
45 #=========================================================================
46 aSession.startOperation()
47 aPart = aSession.moduleDocument().addFeature("Part")
48 aSession.finishOperation()
49
50 # add point and a folder before it
51 aPartDoc = aSession.activeDocument()
52 aPoint1 = newPoint(aPartDoc, 0., 0., 0.)
53
54 aSession.startOperation()
55 aFolder1 = aPartDoc.addFolder(aPoint1)
56 aSession.finishOperation()
57
58 NB_FEATURES_FULL = 2
59 NB_FEATURES_OUT  = 2
60
61 assert(aPartDoc.size("Folders") == 1), "Wrong number of folders: {}".format(aPartDoc.size("Folders"))
62 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}".format(aPartDoc.size("Features"))
63 FOLDER_NAME_EXPECTED = "Folder_1"
64 assert(aFolder1.name() == FOLDER_NAME_EXPECTED), "Actual name '{}', expected '{}'".format(aFolder1.name(), FOLDER_NAME_EXPECTED)
65
66 toFolder = FeatureList()
67 toFolder.append(aPoint1)
68
69 # move point to the folder
70 aSession.startOperation()
71 aFolder = aPartDoc.findFolderAbove(toFolder)
72 assert(aFolder is not None)
73 isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
74 aSession.finishOperation()
75 assert(isAdded)
76 NB_FEATURES_OUT -= 1
77 # full number of features
78 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
79 # number of features outside the folder
80 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
81
82 # check the index of the point in the folder
83 aFound = aPartDoc.findContainingFolder(aPoint1)
84 assert(aFound[0].data().isEqual(aFolder1.data()))
85 assert(aFound[1] == 0)
86
87 #=========================================================================
88 # Test 2. Add a point, check it is added to a folder, then move it out
89 #=========================================================================
90 aPoint2 = newPoint(aPartDoc, 10., 0., 0.)
91
92 NB_FEATURES_FULL += 1
93 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
94 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
95
96 fromFolder = FeatureList()
97 fromFolder.append(aPoint2)
98
99 aSession.startOperation()
100 isMovedOut = aPartDoc.removeFromFolder(fromFolder)
101 aSession.finishOperation()
102 assert(isMovedOut)
103
104 NB_FEATURES_OUT += 1
105 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
106 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
107
108 #=========================================================================
109 # Test 3. Add a point into a folder below
110 #=========================================================================
111 aPoint3 = newPoint(aPartDoc, 10., 10., 0.)
112
113 NB_FEATURES_FULL += 1
114 NB_FEATURES_OUT += 1
115
116 # add a folder
117 aSession.startOperation()
118 aFolder2 = aPartDoc.addFolder(aPoint3)
119 aSession.finishOperation()
120
121 NB_FEATURES_FULL += 1
122 NB_FEATURES_OUT += 1
123
124 assert(aPartDoc.size("Folders") == 2), "Wrong number of folders: {}".format(aPartDoc.size("Folders"))
125 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
126 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
127
128 toFolder = FeatureList()
129 toFolder.append(aPoint2)
130
131 # move point to the folder
132 aSession.startOperation()
133 aFolder = aPartDoc.findFolderBelow(toFolder)
134 assert(aFolder is not None)
135 isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
136 aSession.finishOperation()
137 assert(isAdded)
138
139 NB_FEATURES_OUT -= 1
140
141 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
142 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
143
144 # check the index of the point in the folder
145 aFound = aPartDoc.findContainingFolder(aPoint2)
146 assert(aFound[0].data().isEqual(aFolder2.data()))
147 assert(aFound[1] == 0)
148 aFound = aPartDoc.findContainingFolder(aPoint3)
149 assert(aFound == -1)
150
151 #=========================================================================
152 # Test 4. Add several points into a folder
153 #=========================================================================
154 aPoint4 = newPoint(aPartDoc, 0., 10., 0.)
155
156 NB_FEATURES_FULL += 1
157 NB_FEATURES_OUT += 1
158
159 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
160 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
161
162 # index without respect to foldering
163 assert(aPartDoc.index(aPoint4) == 5), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4))
164 # index according to folders
165 assert(aPartDoc.index(aPoint4, True) == 3), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
166
167 # add a folder
168 aSession.startOperation()
169 aFolder3 = aPartDoc.addFolder(aPoint3)
170 aSession.finishOperation()
171
172 NB_FEATURES_FULL += 1
173 NB_FEATURES_OUT += 1
174
175 assert(aPartDoc.size("Folders") == 3), "Wrong number of folders: {}".format(aPartDoc.size("Folders"))
176 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
177 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
178
179 # index without respect to foldering
180 assert(aPartDoc.index(aFolder3) == 4), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3))
181 assert(aPartDoc.index(aPoint4) == 6), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4))
182 # index according to folders
183 assert(aPartDoc.index(aFolder3, True) == 2), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
184 assert(aPartDoc.index(aPoint4, True) == 4), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
185
186 toFolder = FeatureList()
187 toFolder.append(aPoint3)
188 toFolder.append(aPoint4)
189
190 # move point to the folder
191 aSession.startOperation()
192 aFolder = aPartDoc.findFolderAbove(toFolder)
193 assert(aFolder is not None)
194 isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
195 aSession.finishOperation()
196 assert(isAdded)
197
198 NB_FEATURES_OUT -= 2
199
200 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
201 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
202
203 # index without respect to foldering
204 assert(aPartDoc.index(aFolder3) == 4), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3))
205 assert(aPartDoc.index(aPoint3) == 5), "Wrong index of the point: {}".format(aPartDoc.index(aPoint3))
206 assert(aPartDoc.index(aPoint4) == 6), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4))
207 # index according to folders
208 assert(aPartDoc.index(aFolder3, True) == 2), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
209 assert(aPartDoc.index(aPoint3, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint3, True))
210 assert(aPartDoc.index(aPoint4, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
211
212 # check the index of the point in the folder
213 aFound = aPartDoc.findContainingFolder(aPoint3)
214 assert(aFound[0].data().isEqual(aFolder3.data()))
215 assert(aFound[1] == 0)
216 aFound = aPartDoc.findContainingFolder(aPoint4)
217 assert(aFound[0].data().isEqual(aFolder3.data()))
218 assert(aFound[1] == 1)
219
220 aPoint5 = newPoint(aPartDoc, 0., 0., 10.)
221 fromFolder = FeatureList()
222 fromFolder.append(aPoint5)
223
224 aSession.startOperation()
225 isMovedOut = aPartDoc.removeFromFolder(fromFolder)
226 aSession.finishOperation()
227 assert(isMovedOut)
228
229 # add more points to the folder to move them out
230 aPoint6 = newPoint(aPartDoc, 10., 0., 10.)
231 aPoint7 = newPoint(aPartDoc, 10., 10., 10.)
232 aPoint8 = newPoint(aPartDoc, 0., 10., 10.)
233
234 toFolder = FeatureList()
235 toFolder.append(aPoint5)
236 toFolder.append(aPoint6)
237 toFolder.append(aPoint7)
238 toFolder.append(aPoint8)
239
240 # move point to the folder
241 aSession.startOperation()
242 aFolder = aPartDoc.findFolderAbove(toFolder)
243 assert(aFolder is not None)
244 isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
245 aSession.finishOperation()
246 assert(isAdded)
247
248 NB_FEATURES_FULL += 4
249 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
250 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
251
252 assert(aPartDoc.index(aPoint5, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint5, True))
253 assert(aPartDoc.index(aPoint6, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint6, True))
254 assert(aPartDoc.index(aPoint7, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint7, True))
255 assert(aPartDoc.index(aPoint8, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint8, True))
256
257 assert(aFolder3.reference("first_feature").value() is not None)
258 assert(aFolder3.reference("last_feature").value() is not None)
259
260 #=========================================================================
261 # Test 5. Remove a point from a folder before it
262 #=========================================================================
263 fromFolder = FeatureList()
264 fromFolder.append(aPoint3)
265
266 aSession.startOperation()
267 isMovedOut = aPartDoc.removeFromFolder(fromFolder)
268 aSession.finishOperation()
269 assert(isMovedOut)
270
271 NB_FEATURES_OUT += 1
272 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
273 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
274
275 assert(aPartDoc.index(aPoint3, True) == 2), "Wrong index of the point: {}".format(aPartDoc.index(aPoint3, True))
276 assert(aPartDoc.index(aFolder3, True) == 3), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
277
278 assert(aFolder3.reference("first_feature").value() is not None)
279 assert(aFolder3.reference("last_feature").value() is not None)
280
281 #=========================================================================
282 # Test 6. Remove a point from a folder after it
283 #=========================================================================
284 fromFolder = FeatureList()
285 fromFolder.append(aPoint8)
286
287 aSession.startOperation()
288 isMovedOut = aPartDoc.removeFromFolder(fromFolder)
289 aSession.finishOperation()
290 assert(isMovedOut)
291
292 NB_FEATURES_OUT += 1
293 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
294 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
295
296 assert(aPartDoc.index(aPoint8, True) == 4), "Wrong index of the point: {}".format(aPartDoc.index(aPoint8, True))
297 assert(aPartDoc.index(aFolder3, True) == 3), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
298
299 assert(aFolder3.reference("first_feature").value() is not None)
300 assert(aFolder3.reference("last_feature").value() is not None)
301
302 #=========================================================================
303 # Test 7. Try to remove several points which are not start nor end in a folder
304 #=========================================================================
305 fromFolder = FeatureList()
306 fromFolder.append(aPoint5)
307 fromFolder.append(aPoint6)
308
309 aSession.startOperation()
310 isMovedOut = aPartDoc.removeFromFolder(fromFolder)
311 aSession.finishOperation()
312 assert(isMovedOut is False)
313
314 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
315 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
316
317 assert(aPartDoc.index(aFolder3, True) == 3), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
318 assert(aPartDoc.index(aPoint5, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint5, True))
319 assert(aPartDoc.index(aPoint6, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint6, True))
320 assert(aPartDoc.index(aPoint8, True) == 4), "Wrong index of the point: {}".format(aPartDoc.index(aPoint8, True))
321
322 assert(aFolder3.reference("first_feature").value() is not None)
323 assert(aFolder3.reference("last_feature").value() is not None)
324
325 #=========================================================================
326 # Test 8. Remove several points from a folder after it
327 #=========================================================================
328 fromFolder = FeatureList()
329 fromFolder.append(aPoint6)
330 fromFolder.append(aPoint7)
331
332 aSession.startOperation()
333 isMovedOut = aPartDoc.removeFromFolder(fromFolder)
334 aSession.finishOperation()
335 assert(isMovedOut)
336
337 NB_FEATURES_OUT += 2
338 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
339 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
340
341 assert(aPartDoc.index(aFolder3, True) == 3), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
342 assert(aPartDoc.index(aPoint6, True) == 4), "Wrong index of the point: {}".format(aPartDoc.index(aPoint6, True))
343 assert(aPartDoc.index(aPoint7, True) == 5), "Wrong index of the point: {}".format(aPartDoc.index(aPoint7, True))
344 assert(aPartDoc.index(aPoint8, True) == 6), "Wrong index of the point: {}".format(aPartDoc.index(aPoint8, True))
345
346 assert(aFolder3.reference("first_feature").value() is not None)
347 assert(aFolder3.reference("last_feature").value() is not None)
348
349 #=========================================================================
350 # Test 9. Remove all remaining points from a folder after it
351 #=========================================================================
352 fromFolder = FeatureList()
353 fromFolder.append(aPoint4)
354 fromFolder.append(aPoint5)
355
356 aSession.startOperation()
357 isMovedOut = aPartDoc.removeFromFolder(fromFolder, False)
358 aSession.finishOperation()
359 assert(isMovedOut)
360
361 NB_FEATURES_OUT += 2
362 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
363 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
364
365 assert(aPartDoc.index(aFolder3, True) == 3), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
366 assert(aPartDoc.index(aPoint4, True) == 4), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
367 assert(aPartDoc.index(aPoint5, True) == 5), "Wrong index of the point: {}".format(aPartDoc.index(aPoint5, True))
368 assert(aPartDoc.index(aPoint6, True) == 6), "Wrong index of the point: {}".format(aPartDoc.index(aPoint6, True))
369 assert(aPartDoc.index(aPoint7, True) == 7), "Wrong index of the point: {}".format(aPartDoc.index(aPoint7, True))
370 assert(aPartDoc.index(aPoint8, True) == 8), "Wrong index of the point: {}".format(aPartDoc.index(aPoint8, True))
371
372 # folder is empty
373 assert(aFolder3.reference("first_feature").value() is None)
374 assert(aFolder3.reference("last_feature").value() is None)
375 # remove empty folder
376 aSession.startOperation()
377 aPartDoc.removeFolder(aFolder3)
378 aSession.finishOperation()
379
380 from salome.shaper import model
381 assert(model.checkPythonDump())