Salome HOME
Ability to get index of the objects according to foldering (Task 2.3. Ability to...
[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 #=========================================================================
84 # Test 2. Add a point into a folder below
85 #=========================================================================
86 aPoint2 = newPoint(aPartDoc, 10., 0., 0.)
87 aPoint3 = newPoint(aPartDoc, 10., 10., 0.)
88
89 NB_FEATURES_FULL += 2
90 NB_FEATURES_OUT += 2
91
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 # add a folder
96 aSession.startOperation()
97 aFolder2 = aPartDoc.addFolder(aPoint3)
98 aSession.finishOperation()
99
100 NB_FEATURES_FULL += 1
101 NB_FEATURES_OUT += 1
102
103 assert(aPartDoc.size("Folders") == 2), "Wrong number of folders: {}".format(aPartDoc.size("Folders"))
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 toFolder = FeatureList()
108 toFolder.append(aPoint2)
109
110 # move point to the folder
111 aSession.startOperation()
112 aFolder = aPartDoc.findFolderAbove(toFolder)
113 assert(aFolder is not None)
114 isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
115 aSession.finishOperation()
116 assert(isAdded)
117
118 NB_FEATURES_OUT -= 1
119
120 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
121 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
122
123 #=========================================================================
124 # Test 3. Add several points into a folder
125 #=========================================================================
126 aPoint4 = newPoint(aPartDoc, 0., 10., 0.)
127
128 NB_FEATURES_FULL += 1
129 NB_FEATURES_OUT += 1
130
131 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
132 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
133
134 # index without respect to foldering
135 assert(aPartDoc.index(aPoint4) == 5), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4))
136 # index according to folders
137 assert(aPartDoc.index(aPoint4, True) == 3), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
138
139 # add a folder
140 aSession.startOperation()
141 aFolder2 = aPartDoc.addFolder(aPoint3)
142 aSession.finishOperation()
143
144 NB_FEATURES_FULL += 1
145 NB_FEATURES_OUT += 1
146
147 assert(aPartDoc.size("Folders") == 3), "Wrong number of folders: {}".format(aPartDoc.size("Folders"))
148 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
149 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
150
151 # index without respect to foldering
152 assert(aPartDoc.index(aFolder2) == 4), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder2))
153 assert(aPartDoc.index(aPoint4) == 6), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4))
154 # index according to folders
155 assert(aPartDoc.index(aFolder2, True) == 2), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder2, True))
156 assert(aPartDoc.index(aPoint4, True) == 4), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
157
158 toFolder = FeatureList()
159 toFolder.append(aPoint3)
160 toFolder.append(aPoint4)
161
162 # move point to the folder
163 aSession.startOperation()
164 aFolder = aPartDoc.findFolderAbove(toFolder)
165 assert(aFolder is not None)
166 isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
167 aSession.finishOperation()
168 assert(isAdded)
169
170 NB_FEATURES_OUT -= 2
171
172 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
173 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
174
175 # index without respect to foldering
176 assert(aPartDoc.index(aFolder2) == 4), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder2))
177 assert(aPartDoc.index(aPoint3) == 5), "Wrong index of the point: {}".format(aPartDoc.index(aPoint3))
178 assert(aPartDoc.index(aPoint4) == 6), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4))
179 # index according to folders
180 assert(aPartDoc.index(aFolder2, True) == 2), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder2, True))
181 assert(aPartDoc.index(aPoint3, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint3, True))
182 assert(aPartDoc.index(aPoint4, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
183
184
185 from salome.shaper import model
186 assert(model.checkPythonDump())