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