Salome HOME
Copyright update 2022
[modules/geom.git] / src / GEOM_SWIG / GEOM_TestField.py
1 # Copyright (C) 2013-2022  CEA/DEN, EDF R&D, OPEN CASCADE
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 # Python API for field on geometry manipulations:
21
22 # field          = geompy.CreateField(shape, name, type, dimension, componentNames)
23 # geompy.RemoveField(field)
24 # shape          = field.getShape()
25 # name           = field.getName()
26 # field.setName(name)
27 # typ            = field.getType()
28 # dim            = field.getDimension()
29 # componentNames = field.getComponents()
30 # n              = geompy.CountFields(shape)
31 # fields         = geompy.GetFields(shape)
32 # field          = geompy.GetField(shape, name)
33 #
34 # field.addStep(step, stamp, values)
35 # field.removeStep(step)
36 # n              = field.countSteps()
37 # steps          = field.getSteps() # donne la liste des numeros de pas du champ
38 # stamp          = field.getStamp(step)
39 # field.setStamp(step, stamp)
40 # values         = field.getValues(step)
41 # field.setValues(step, values)
42
43 MustFail = -1
44 geompy = None
45
46 def CheckFieldCreation(shape, name, ftype, dimension, componentNames, nbFiOrMustFail=-1):
47     # WARNING: assure name uniqueness to check geompy.GetField( shape, name )
48     try:
49         field = geompy.CreateField(shape, name, ftype, dimension, componentNames)
50     except Exception as e:
51         if nbFiOrMustFail == MustFail:
52             print("Ok, expected exception caught: %s"%e)
53             return
54         raise e
55     if nbFiOrMustFail == MustFail:
56         raise RuntimeError("Expected exception NOT thrown")
57     assert field.getShape()
58     assert field.getShape().IsSame( shape )
59     assert field.getName() == name
60     if isinstance( ftype, int ):
61         assert field.getType() == ftype
62     else:
63         assert field.getTypeEnum() == ftype
64     assert field.getDimension() == dimension
65     assert field.getComponents() == componentNames
66     assert geompy.CountFields( shape ) == nbFiOrMustFail
67     assert geompy.GetField( shape, name )
68     assert geompy.GetField( shape, name ).IsSame( field )
69
70 def CheckStepManips(field, step, stamp, values, nbStepsOrMustFail, toRemove=False):
71     try:
72         stp = field.addStep(step, stamp, values)
73     except Exception as e:
74         if nbStepsOrMustFail == MustFail:
75             print("Ok, expected exception caught: %s"%e)
76             return
77         raise e
78     if nbStepsOrMustFail == MustFail:
79         raise RuntimeError("Expected exception NOT thrown")
80     assert field.countSteps() == nbStepsOrMustFail
81     assert len( field.getSteps() ) == nbStepsOrMustFail
82     assert step in field.getSteps()
83     assert field.getStamp(step) == stamp
84     field.setStamp(step, stamp*2)
85     assert field.getStamp(step) == stamp*2
86     assert field.getValues(step) == values
87     values[-1] = values[0]
88     field.setValues(step, values)
89     assert field.getValues(step) == values
90     if toRemove:
91         field.removeStep(step)
92         assert field.countSteps() == nbStepsOrMustFail-1
93         assert len( field.getSteps() ) == nbStepsOrMustFail-1
94         assert step not in field.getSteps()
95         pass
96     return
97
98 def TestField (geomBuilder, math):
99
100     global geompy
101     geompy = geomBuilder
102
103     oldAutoPublish = geompy.myMaxNbSubShapesAllowed
104     geompy.addToStudyAuto()
105
106     shape = geompy.MakeBoxDXDYDZ(1,1,1)
107
108     # Field operations
109
110     import GEOM
111     CheckFieldCreation(shape,"fBool1",GEOM.FDT_Bool, 0, ['b1'], 1)
112     CheckFieldCreation(shape,"fInt1", GEOM.FDT_Int,  1, ['i1','i2'], 2)
113     CheckFieldCreation(shape,"fDbl1", 2,  3, ['d3'], 3)
114     CheckFieldCreation(shape,"fStr1", 3,  -1, ['s4'], 4)
115
116     # assert exception in case of invalid parameters
117     CheckFieldCreation(None,"bad",GEOM.FDT_Bool, 0, ['b1'], MustFail)   # no shape
118     CheckFieldCreation(shape,"",GEOM.FDT_Bool, 0, ['b1'], MustFail)     # no name
119     CheckFieldCreation(shape,"bad",-1, ['b1'], MustFail)                # too low data type
120     CheckFieldCreation(shape,"bad",4, ['b1'], MustFail)                 # too high data type
121     CheckFieldCreation(shape,"bad",GEOM.FDT_Bool, -2, ['b1'], MustFail) # too low dim
122     CheckFieldCreation(shape,"bad",GEOM.FDT_Bool, 4, ['b1'], MustFail)  # too high dim
123     CheckFieldCreation(shape,"bad",GEOM.FDT_Bool, 0, [], MustFail)      # no components
124
125     # Field step operations
126
127
128     toRemove = True
129
130     # string field on the whole shape
131     sfield = geompy.CreateField(shape, "strWhole", GEOM.FDT_String, -1, ["day","year"])
132     CheckStepManips( sfield, -1, -10, ["25 Sep","2013"], 1 )
133     CheckStepManips( sfield,  2,  10, ["26 Sep","2014"], 2, toRemove )
134     CheckStepManips( sfield, 10,1000, ["27 Sep","2015"], 2 )
135     # bool field on 8 vertices
136     bfield = geompy.CreateField(shape, "boolV", GEOM.FDT_Bool, 0, ["ok"])
137     CheckStepManips( bfield, 1, -1, [0,1]*4, 1, toRemove )
138     CheckStepManips( bfield, 2, -2, [1,0]*4, 1 )
139     # int field on 6 faces
140     ifield = geompy.CreateField(shape, "intF", GEOM.FDT_Int, 2, ["id","domain"])
141     CheckStepManips( ifield, -1, -10, list(range(12)),  1 )
142     CheckStepManips( ifield, -2, -20, list(range(6))*2, 2 )
143     # double field on a solid
144     dfield = geompy.CreateField(shape, "dblS", GEOM.FDT_Double, 3, ["a","b","c"])
145     CheckStepManips( dfield, -1, -10, [-1.1, 2.3, 4000], 1 )
146     CheckStepManips( dfield, -2, -20, list(range(3)), 2 )
147
148     # assert exception in case of invalid parameters
149     CheckStepManips( sfield, -1, -10, ["25 Sep","2013"], MustFail ) # step already exists
150     CheckStepManips( sfield, 20, -10, ["25 Sep"], MustFail ) # wrong data size
151     CheckStepManips( sfield, 30, -10, [1, 2], MustFail ) # wrong data type
152
153     geompy.addToStudyAuto(oldAutoPublish)
154
155     # Check Python dump
156     import tempfile, os
157     dumpFile = tempfile.NamedTemporaryFile().name
158     pyFile = dumpFile+".py"
159     # dump the study
160     import salome
161     assert( salome.myStudy.DumpStudy(os.path.dirname(dumpFile), os.path.basename(dumpFile), 1, 0))
162     exec(compile(open( pyFile ).read(), pyFile, 'exec'))
163     os.remove( pyFile )
164
165     print("Field manipulations work OK")