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