Salome HOME
Merge branch 'Dev_GroupsRevision'
[modules/shaper.git] / src / ModelAPI / Test / TestFolder_Remove.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-24"
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 # Prepare some features and folders
46 #=========================================================================
47 aSession.startOperation()
48 aPart = aSession.moduleDocument().addFeature("Part")
49 aSession.finishOperation()
50
51 # add points
52 aPartDoc = aSession.activeDocument()
53 aPoint1 = newPoint(aPartDoc,  0.,  0., 0.)
54 aPoint2 = newPoint(aPartDoc, 10.,  0., 0.)
55 aPoint3 = newPoint(aPartDoc, 10., 10., 0.)
56 aPoint4 = newPoint(aPartDoc,  0., 10., 0.)
57
58 # add folders
59 aSession.startOperation()
60 aFolder1 = aPartDoc.addFolder(aPoint2)
61 aSession.finishOperation()
62 aSession.startOperation()
63 aFolder2 = aPartDoc.addFolder(aPoint2)
64 aSession.finishOperation()
65 aSession.startOperation()
66 aFolder3 = aPartDoc.addFolder(aPoint3)
67 aSession.finishOperation()
68
69 # place points into folders
70 toFolder = FeatureList()
71 toFolder.append(aPoint1)
72
73 aSession.startOperation()
74 aFolder = aPartDoc.findFolderBelow(toFolder)
75 assert(aFolder is not None)
76 isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
77 aSession.finishOperation()
78 assert(isAdded)
79
80 toFolder = FeatureList()
81 toFolder.append(aPoint3)
82 toFolder.append(aPoint4)
83
84 aSession.startOperation()
85 aFolder = aPartDoc.findFolderAbove(toFolder)
86 assert(aFolder is not None)
87 isAdded = aPartDoc.moveToFolder(toFolder, aFolder)
88 aSession.finishOperation()
89 assert(isAdded)
90
91 NB_FEATURES_FULL = 7
92 NB_FEATURES_OUT  = 4
93
94 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
95 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
96
97 assert(aPartDoc.index(aFolder1, True) == 0), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder1, True))
98 assert(aPartDoc.index(aFolder2, True) == 1), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder2, True))
99 assert(aPartDoc.index(aFolder3, True) == 3), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
100
101 assert(aPartDoc.index(aPoint1, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint1, True))
102 assert(aPartDoc.index(aPoint2, True) ==  2), "Wrong index of the point: {}".format(aPartDoc.index(aPoint2, True))
103 assert(aPartDoc.index(aPoint3, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint3, True))
104 assert(aPartDoc.index(aPoint4, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
105
106
107 #=========================================================================
108 # Test 1. Remove empty folder
109 #=========================================================================
110 aSession.startOperation()
111 aPartDoc.removeFolder(aFolder2)
112 aSession.finishOperation()
113
114 NB_FEATURES_FULL -= 1
115 NB_FEATURES_OUT  -= 1
116
117 assert(aPartDoc.size("Features") == NB_FEATURES_FULL), "Wrong number of features: {}, expected: {}".format(aPartDoc.size("Features"), NB_FEATURES_FULL)
118 assert(aPartDoc.size("Features", True) == NB_FEATURES_OUT), "Wrong number of features outside a folder: {}, expected: {}".format(aPartDoc.size("Features", True), NB_FEATURES_OUT)
119
120 assert(aPartDoc.index(aFolder1, True) == 0), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder1, True))
121 assert(aPartDoc.index(aFolder2, True) == -1), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder2, True))
122 assert(aPartDoc.index(aFolder3, True) == 2), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
123
124 assert(aPartDoc.index(aPoint1, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint1, True))
125 assert(aPartDoc.index(aPoint2, True) ==  1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint2, True))
126 assert(aPartDoc.index(aPoint3, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint3, True))
127 assert(aPartDoc.index(aPoint4, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
128
129
130 #=========================================================================
131 # Test 2. Remove non-empty folder
132 #=========================================================================
133 aSession.startOperation()
134 aPartDoc.removeFolder(aFolder3)
135 aSession.finishOperation()
136
137 NB_FEATURES_FULL -= 1
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 assert(aPartDoc.index(aFolder1, True) == 0), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder1, True))
144 assert(aPartDoc.index(aFolder2, True) == -1), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder2, True))
145 assert(aPartDoc.index(aFolder3, True) == -1), "Wrong index of the folder: {}".format(aPartDoc.index(aFolder3, True))
146
147 assert(aPartDoc.index(aPoint1, True) == -1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint1, True))
148 assert(aPartDoc.index(aPoint2, True) ==  1), "Wrong index of the point: {}".format(aPartDoc.index(aPoint2, True))
149 assert(aPartDoc.index(aPoint3, True) ==  2), "Wrong index of the point: {}".format(aPartDoc.index(aPoint3, True))
150 assert(aPartDoc.index(aPoint4, True) ==  3), "Wrong index of the point: {}".format(aPartDoc.index(aPoint4, True))
151
152
153 from salome.shaper import model
154 assert(model.checkPythonDump())