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